BasicTypeService.php 7.7 KB

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