BasicTypeService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Depart;
  5. /**
  6. * 基础分类设置相关
  7. */
  8. class BasicTypeService extends Service
  9. {
  10. /**
  11. * 基础类型编辑
  12. * @param $data
  13. * @return array
  14. */
  15. public function basicTypeEdit($data, $user){
  16. list($status,$msg) = $this->basicTypeRule($data,$user,false);
  17. if(!$status) return [$status,$msg];
  18. $update = $msg['data'][0];
  19. BasicType::where('id',$data['id'])->update($update);
  20. return [true,''];
  21. }
  22. /**
  23. * 基础类型新增
  24. * @param $data
  25. * @return array
  26. */
  27. public function basicTypeAdd($data,$user){
  28. list($status,$msg) = $this->basicTypeRule($data, $user);
  29. if(!$status) return [$status,$msg];
  30. BasicType::insert($msg['data']);
  31. return [true,''];
  32. }
  33. /**
  34. * 基础类型删除
  35. * @param $data
  36. * @return array
  37. */
  38. public function basicTypeDel($data){
  39. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  40. BasicType::whereIn('id',$data['id'])->update([
  41. 'del_time'=>time()
  42. ]);
  43. return [true,''];
  44. }
  45. /**
  46. * 基础类型列表
  47. * @param $data
  48. * @return array
  49. */
  50. public function basicTypeList($data, $user){
  51. $model = BasicType::TopClear($user,$data);
  52. $model = $model->where('del_time',0)
  53. ->select('title','id','type','code')
  54. ->orderby('id', 'asc');
  55. if(! empty($data['type'])) $model->where('type',$data['type']);
  56. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  57. $list = $this->limit($model,'',$data);
  58. $list = $this->fillData($list);
  59. return [true, $list];
  60. }
  61. /**
  62. * 基础类型参数规则
  63. * @param $data
  64. * @param $is_check
  65. * @return array
  66. */
  67. public function basicTypeRule($data,$user, $is_check = true){
  68. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  69. //所属部门 以及 顶级部门
  70. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  71. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  72. $type = array_column($data['data'],'type');
  73. $type = array_map(function($val) {
  74. return $val !== null ? $val : 0;
  75. }, $type);
  76. foreach ($type as $value){
  77. if(empty($value)) return [false,'类型不能为空!'];
  78. if(! isset(BasicType::$type[$value])) return [false,'类型不存在!'];
  79. }
  80. $map = [];
  81. foreach ($data['data'] as $value){
  82. if(! isset($map[$value['title']])){
  83. $map[$value['title']][] = $value['type'];
  84. }else{
  85. if(! in_array($value['type'],$map[$value['title']])){
  86. $map[$value['title']][] = $value['type'];
  87. }
  88. }
  89. }
  90. $title = array_column($data['data'],'title');
  91. $title = array_map(function($val) {
  92. return $val !== null ? $val : 0;
  93. }, $title);
  94. $title_count = array_count_values($title);
  95. foreach ($title as $value){
  96. if(empty($value)) return [false,'名称不能为空!'];
  97. if($title_count[$value] > 1 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];
  98. }
  99. foreach ($data['data'] as $key => $value){
  100. $data['data'][$key]['type'] = $value['type'];
  101. $data['data'][$key]['upd_time'] = time();
  102. if($is_check){
  103. $bool = BasicType::where('title',$value['title'])
  104. ->where('top_depart_id',$data['top_depart_id'])
  105. ->where('type',$value['type'])
  106. ->where('del_time',0)
  107. ->exists();
  108. $data['data'][$key]['crt_time'] = time();
  109. $data['data'][$key]['crt_id'] = $user['id'];
  110. $data['data'][$key]['depart_id'] = $data['depart_id'];
  111. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  112. }else{
  113. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  114. $top_depart_id = BasicType::where('id',$data['id'])->value('top_depart_id');
  115. $bool = BasicType::where('title',$value['title'])
  116. ->where('top_depart_id',$top_depart_id)
  117. ->where('type',$value['type'])
  118. ->where('id','<>',$data['id'])
  119. ->where('del_time',0)
  120. ->exists();
  121. }
  122. if($bool) return [false,'名称不能重复'];
  123. }
  124. return [true, $data];
  125. }
  126. /**
  127. * 拼接数据
  128. * @param $data
  129. * @return array
  130. */
  131. public function fillData($data){
  132. if(empty($data['data'])) return $data;
  133. foreach ($data['data'] as $key => $value){
  134. $data['data'][$key]['type_name'] = BasicType::$type[$value['type']] ?? '';
  135. }
  136. return $data;
  137. }
  138. public function maked(){
  139. $list = BasicType::where('del_time',0)
  140. ->select('title','type','top_depart_id')
  141. ->get()->toArray();
  142. $basic_type2 = [];
  143. $basic_other_type = [];
  144. foreach ($list as $value){
  145. if($value['top_depart_id'] == 2){
  146. if(! in_array($value['type'], [28,23,24,22,8])){
  147. $basic_type2[] = $value['title'] . '|' . $value['type'];
  148. }
  149. }else{
  150. $basic_other_type[$value['top_depart_id']][] = $value['title'] . '|' . $value['type'];
  151. }
  152. }dd($basic_type2);
  153. $depart = Depart::where('del_time',0)
  154. ->where('id','<>',2)
  155. ->where('parent_id',0)
  156. // ->whereIn('id',[139,49])
  157. ->select('id')
  158. ->orderBy('id','asc')
  159. ->get()->toArray();
  160. $depart = array_column($depart,'id');
  161. $insert = [];
  162. $time = time();
  163. foreach ($depart as $value){
  164. if(! empty($basic_other_type[$value])){
  165. foreach ($basic_type2 as $val){
  166. if(! in_array($val,$basic_other_type[$value])) {
  167. $tmp = explode('|',$val);
  168. $insert[] = [
  169. 'title' => $tmp[0],
  170. 'type' => $tmp[1],
  171. 'top_depart_id' => $value,
  172. 'crt_time' => $time,
  173. 'crt_id' => 1
  174. ];
  175. }
  176. }
  177. }else{
  178. foreach ($basic_type2 as $val){
  179. $tmp = explode('|',$val);
  180. $insert[] = [
  181. 'title' => $tmp[0],
  182. 'type' => $tmp[1],
  183. 'top_depart_id' => $value,
  184. 'crt_time' => $time,
  185. 'crt_id' => 1
  186. ];
  187. }
  188. }
  189. }
  190. if(! empty($insert)) BasicType::insert($insert);
  191. dd('ok');
  192. }
  193. }