TableHeadService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Service;
  3. use App\Model\TableSearchSetting;
  4. use App\Model\TableSetting;
  5. use Illuminate\Support\Facades\DB;
  6. class TableHeadService extends Service
  7. {
  8. public function tableheadAdd($data, $user){
  9. if(empty($data['table_head'])) return [false,'自定义表头不能为空'];
  10. if(empty($data['menu_id'])) return [false, 'menu_id不能为空'];
  11. $time = time();
  12. $insert = [];
  13. //表头
  14. foreach ($data['table_head'] as $value){
  15. if(empty($value['key'])) return [false, 'key不能为空'];
  16. if(empty($value['value'])) return [false, 'value不能为空'];
  17. if(empty($value['sort'])) return [false, 'sort不能为空'];
  18. if(! isset($value['is_show'])) return [false, 'is_show不能为空'];
  19. $insert[] = [
  20. 'key' => $value['key'],
  21. 'value' => $value['value'],
  22. 'sort' => $value['sort'],
  23. 'is_show' => $value['is_show'],
  24. 'is_click_detail' => $value['is_click_detail'] ?? 0,
  25. 'float' => $value['float'],
  26. 'width' => $value['width'] ?? 150,
  27. 'menu_id' => $data['menu_id'],
  28. 'crt_time' => $time,
  29. 'crt_id' => $user['id'],
  30. ];
  31. }
  32. DB::beginTransaction();
  33. try{
  34. TableSetting::where('del_time',0)
  35. ->where('crt_id',$user['id'])
  36. ->where('menu_id',$data['menu_id'])
  37. ->where('key', '<>', TableSetting::IS_FILTER)
  38. ->update(['del_time' => $time]);
  39. TableSetting::insert($insert);
  40. DB::commit();
  41. }catch (\Exception $exception){
  42. DB::rollBack();
  43. return [false, $exception->getMessage()];
  44. }
  45. return [true,''];
  46. }
  47. public function tableheadGet($data, $user){
  48. if(empty($data['menu_id'])) return [false,'menu_id不能为空!'];
  49. $header = TableSetting::where('del_time',0)
  50. ->where('menu_id',$data['menu_id'])
  51. ->where('crt_id',$user['id'])
  52. ->select('key','value','sort','is_show','is_click_detail','menu_id','float','width')
  53. ->get()->toArray();
  54. $header_map = array_column($header,null,'key');
  55. $header_default = config("header.{$data['menu_id']}") ?? [];
  56. foreach ($header_default as $key => $value){
  57. if(isset($header_map[$value['key']])) {
  58. //存在保存好的设置的表头 以下信息沿用
  59. $tmp = $header_map[$value['key']];
  60. $header_default[$key]['sort'] = $tmp['sort'];
  61. $header_default[$key]['is_show'] = $tmp['is_show'];
  62. $header_default[$key]['is_click_detail'] = $tmp['is_click_detail'];
  63. $header_default[$key]['float'] = $tmp['float'];
  64. $header_default[$key]['width'] = $tmp['width'];
  65. }else{
  66. $header_default[$key]['sort'] = $key + 1;
  67. $header_default[$key]['is_show'] = 1;
  68. $header_default[$key]['float'] = 0;
  69. $is_click_detail = 0;
  70. if($value['key'] == "order_number") {
  71. $is_click_detail = 1;
  72. }elseif ($value['key'] == "title"){
  73. $is_click_detail = 1;
  74. }
  75. //默认选中
  76. $header_default[$key]['is_click_detail'] = $is_click_detail;
  77. $header_default[$key]['width'] = 150;
  78. }
  79. }
  80. //时间排序
  81. usort($header_default, function($a, $b) {
  82. return $a['sort'] - $b['sort'];
  83. });
  84. return [true, $header_default];
  85. }
  86. public function filterAdd($data, $user){
  87. if(! isset($data['is_filter'])) return [false,'是否打开筛选不能为空'];
  88. if(empty($data['menu_id'])) return [false, 'menu_id不能为空'];
  89. $time = time();
  90. $insert = [];
  91. //是否打开筛选
  92. $insert[] = [
  93. 'key' => TableSetting::IS_FILTER,
  94. 'value' => "是否打开筛选",
  95. 'sort' => 0,
  96. 'is_show' => 0,
  97. 'is_click_detail' => $data['is_filter'] ?? 1,
  98. 'float' => 0,
  99. 'width' => 0,
  100. 'menu_id' => $data['menu_id'],
  101. 'crt_time' => $time,
  102. 'crt_id' => $user['id'],
  103. ];
  104. DB::beginTransaction();
  105. try{
  106. TableSetting::where('del_time',0)
  107. ->where('crt_id',$user['id'])
  108. ->where('menu_id',$data['menu_id'])
  109. ->where('key', TableSetting::IS_FILTER)
  110. ->update(['del_time' => $time]);
  111. TableSetting::insert($insert);
  112. DB::commit();
  113. }catch (\Exception $exception){
  114. DB::rollBack();
  115. return [false, $exception->getMessage()];
  116. }
  117. return [true,''];
  118. }
  119. public function filterGet($data, $user){
  120. if(empty($data['menu_id'])) return [false,'menu_id不能为空!'];
  121. $is_filter_model = TableSetting::where('del_time',0)
  122. ->where('menu_id',$data['menu_id'])
  123. ->where('crt_id',$user['id'])
  124. ->where('key', TableSetting::IS_FILTER)
  125. ->select('is_click_detail as is_filter')
  126. ->first();
  127. $is_filter = 1;
  128. if(! empty($is_filter_model)) $is_filter = $is_filter_model->is_filter;
  129. return [true, ['is_filter' => $is_filter]];
  130. }
  131. public function tableSearchAdd($data, $user){
  132. if(empty($data['table_search'])) return [false,'搜索项不能为空'];
  133. if(empty($data['menu_id'])) return [false, 'menu_id不能为空'];
  134. $time = time();
  135. $insert = [];
  136. //表头
  137. foreach ($data['table_search'] as $value){
  138. if(empty($value['key'])) return [false, 'key不能为空'];
  139. if(! isset($value['is_show'])) return [false, 'is_show不能为空'];
  140. $insert[] = [
  141. 'key' => $value['key'],
  142. 'is_show' => $value['is_show'],
  143. 'menu_id' => $data['menu_id'],
  144. 'crt_time' => $time,
  145. 'crt_id' => $user['id'],
  146. ];
  147. }
  148. DB::beginTransaction();
  149. try{
  150. TableSearchSetting::where('del_time',0)
  151. ->where('crt_id',$user['id'])
  152. ->where('menu_id',$data['menu_id'])
  153. ->update(['del_time' => $time]);
  154. TableSearchSetting::insert($insert);
  155. DB::commit();
  156. }catch (\Exception $exception){
  157. DB::rollBack();
  158. return [false, $exception->getMessage()];
  159. }
  160. return [true,''];
  161. }
  162. public function tableSearchGet($data, $user){
  163. if(empty($data['menu_id'])) return [false,'menu_id不能为空!'];
  164. $search = TableSearchSetting::where('del_time',0)
  165. ->where('menu_id',$data['menu_id'])
  166. ->where('crt_id',$user['id'])
  167. ->select('key','is_show','menu_id')
  168. ->get()->toArray();
  169. return [true, $search];
  170. }
  171. }