BasicTypeService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 basicTypeSearch($data){
  152. $basic = BasicType::where('title', 'LIKE', '%'.$data.'%')
  153. ->select('id')
  154. ->get()->toArray();
  155. return array_column($basic,'id');
  156. }
  157. //获取当前
  158. public function getMyBasicList($user, $type){
  159. if(! is_array($type)) $type = [$type];
  160. $depart_id = $this->getDepart($user);
  161. $top_depart_id = $user['depart_map'][$depart_id] ?? 0;
  162. return BasicType::where('del_time',0)
  163. ->whereIn('type',$type)
  164. ->where('top_depart_id',$top_depart_id)
  165. ->get()->toArray();
  166. }
  167. public function maked(){
  168. $list = BasicType::where('del_time',0)
  169. ->select('title','type','top_depart_id')
  170. ->get()->toArray();
  171. $basic_type2 = [];
  172. $basic_other_type = [];
  173. foreach ($list as $value){
  174. if($value['top_depart_id'] == 2){
  175. if(! in_array($value['type'], [28,23,24,22,8])){
  176. $basic_type2[] = $value['title'] . '|' . $value['type'];
  177. }
  178. }else{
  179. $basic_other_type[$value['top_depart_id']][] = $value['title'] . '|' . $value['type'];
  180. }
  181. }dd($basic_type2);
  182. $depart = Depart::where('del_time',0)
  183. ->where('id','<>',2)
  184. ->where('parent_id',0)
  185. // ->whereIn('id',[139,49])
  186. ->select('id')
  187. ->orderBy('id','asc')
  188. ->get()->toArray();
  189. $depart = array_column($depart,'id');
  190. $insert = [];
  191. $time = time();
  192. foreach ($depart as $value){
  193. if(! empty($basic_other_type[$value])){
  194. foreach ($basic_type2 as $val){
  195. if(! in_array($val,$basic_other_type[$value])) {
  196. $tmp = explode('|',$val);
  197. $insert[] = [
  198. 'title' => $tmp[0],
  199. 'type' => $tmp[1],
  200. 'top_depart_id' => $value,
  201. 'crt_time' => $time,
  202. 'crt_id' => 1
  203. ];
  204. }
  205. }
  206. }else{
  207. foreach ($basic_type2 as $val){
  208. $tmp = explode('|',$val);
  209. $insert[] = [
  210. 'title' => $tmp[0],
  211. 'type' => $tmp[1],
  212. 'top_depart_id' => $value,
  213. 'crt_time' => $time,
  214. 'crt_id' => 1
  215. ];
  216. }
  217. }
  218. }
  219. if(! empty($insert)) BasicType::insert($insert);
  220. dd('ok');
  221. }
  222. }