BasicTypeService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\BasicTypeAllUse;
  5. use App\Model\Depart;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * 基础分类设置相关
  9. */
  10. class BasicTypeService extends Service
  11. {
  12. /**
  13. * 基础类型编辑
  14. * @param $data
  15. * @return array
  16. */
  17. public function basicTypeEdit($data, $user){
  18. list($status,$msg) = $this->basicTypeRule($data,$user,false);
  19. if(!$status) return [$status,$msg];
  20. $update = $msg['data'][0];
  21. BasicType::where('id',$data['id'])->update($update);
  22. return [true,''];
  23. }
  24. /**
  25. * 基础类型新增
  26. * @param $data
  27. * @return array
  28. */
  29. public function basicTypeAdd($data,$user){
  30. list($status,$msg) = $this->basicTypeRule($data, $user);
  31. if(!$status) return [$status,$msg];
  32. BasicType::insert($msg['data']);
  33. return [true,''];
  34. }
  35. /**
  36. * 基础类型删除
  37. * @param $data
  38. * @return array
  39. */
  40. public function basicTypeDel($data){
  41. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  42. BasicType::whereIn('id',$data['id'])->update([
  43. 'del_time'=>time()
  44. ]);
  45. return [true,''];
  46. }
  47. /**
  48. * 基础类型列表
  49. * @param $data
  50. * @return array
  51. */
  52. public function basicTypeList($data, $user){
  53. $model = BasicType::TopClear($user,$data);
  54. $model = $model->where('del_time',0)
  55. ->select('title','id','type','code','top_depart_id')
  56. ->orderby('id', 'asc');
  57. if(! empty($data['type'])) $model->where('type',$data['type']);
  58. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  59. $list = $this->limit($model,'',$data);
  60. $list = $this->fillData($list);
  61. return [true, $list];
  62. }
  63. public function basicTypeCustomerList($data, $user){
  64. $data['top_depart_id'] = $user['head']['id'];
  65. $model = BasicType::TopClear($user,$data);
  66. $model = $model->where('del_time',0)
  67. ->select('title','id','type','code','top_depart_id')
  68. ->orderby('id', 'asc');
  69. if(! empty($data['type'])) $model->where('type',$data['type']);
  70. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  71. $list = $this->limit($model,'',$data);
  72. $list = $this->fillData($list);
  73. return [true, $list];
  74. }
  75. /**
  76. * 基础类型参数规则
  77. * @param $data
  78. * @param $is_check
  79. * @return array
  80. */
  81. public function basicTypeRule($data,$user, $is_check = true){
  82. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  83. //所属部门 以及 顶级部门
  84. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  85. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  86. $type = array_column($data['data'],'type');
  87. $type = array_map(function($val) {
  88. return $val !== null ? $val : 0;
  89. }, $type);
  90. foreach ($type as $value){
  91. if(empty($value)) return [false,'类型不能为空!'];
  92. if(! isset(BasicType::$type[$value])) return [false,'类型不存在!'];
  93. }
  94. $map = [];
  95. foreach ($data['data'] as $value){
  96. if(! isset($map[$value['title']])){
  97. $map[$value['title']][] = $value['type'];
  98. }else{
  99. if(! in_array($value['type'],$map[$value['title']])){
  100. $map[$value['title']][] = $value['type'];
  101. }
  102. }
  103. }
  104. $title = array_column($data['data'],'title');
  105. $title = array_map(function($val) {
  106. return $val !== null ? $val : 0;
  107. }, $title);
  108. $title_count = array_count_values($title);
  109. foreach ($title as $value){
  110. if(empty($value)) return [false,'名称不能为空!'];
  111. if($title_count[$value] > 1 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];
  112. }
  113. foreach ($data['data'] as $key => $value){
  114. $data['data'][$key]['type'] = $value['type'];
  115. $data['data'][$key]['upd_time'] = time();
  116. if($is_check){
  117. $bool = BasicType::where('title',$value['title'])
  118. ->where('top_depart_id',$data['top_depart_id'])
  119. ->where('type',$value['type'])
  120. ->where('del_time',0)
  121. ->exists();
  122. $data['data'][$key]['crt_time'] = time();
  123. $data['data'][$key]['crt_id'] = $user['id'];
  124. $data['data'][$key]['depart_id'] = $data['depart_id'];
  125. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  126. }else{
  127. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  128. $top_depart_id = BasicType::where('id',$data['id'])->value('top_depart_id');
  129. $bool = BasicType::where('title',$value['title'])
  130. ->where('top_depart_id',$top_depart_id)
  131. ->where('type',$value['type'])
  132. ->where('id','<>',$data['id'])
  133. ->where('del_time',0)
  134. ->exists();
  135. }
  136. if($bool) return [false,'名称不能重复'];
  137. }
  138. return [true, $data];
  139. }
  140. /**
  141. * 拼接数据
  142. * @param $data
  143. * @return array
  144. */
  145. public function fillData($data){
  146. if(empty($data['data'])) return $data;
  147. foreach ($data['data'] as $key => $value){
  148. $data['data'][$key]['type_name'] = BasicType::$type[$value['type']] ?? '';
  149. }
  150. return $data;
  151. }
  152. public function basicTypeSearch($data){
  153. $basic = BasicType::where('title', 'LIKE', '%'.$data.'%')
  154. ->select('id')
  155. ->get()->toArray();
  156. return array_column($basic,'id');
  157. }
  158. public function basicTypeSearchId($data){
  159. $title = "";
  160. $basic = BasicType::where('id', $data)
  161. ->select('title')
  162. ->first();
  163. if(! empty($basic)) $title = $basic->title;
  164. $basic = BasicType::where('title', $title)
  165. ->select('id')
  166. ->get()->toArray();
  167. return array_column($basic,'id');
  168. }
  169. //获取当前
  170. public function getMyBasicList($user, $type){
  171. if(! is_array($type)) $type = [$type];
  172. $depart_id = $this->getDepart($user);
  173. $top_depart_id = $user['depart_map'][$depart_id] ?? 0;
  174. return BasicType::where('del_time',0)
  175. ->whereIn('type',$type)
  176. ->where('top_depart_id',$top_depart_id)
  177. ->get()->toArray();
  178. }
  179. public function maked(){return;
  180. $list = BasicType::where('del_time',0)
  181. ->select('title','type','top_depart_id')
  182. ->get()->toArray();
  183. $basic_type2 = [];
  184. $basic_other_type = [];
  185. $delete = [8,12,13,14,16,25,26,21,28,22,23,10];
  186. foreach ($list as $value){
  187. if(in_array($value['type'], $delete)) continue;
  188. if($value['top_depart_id'] == 2){
  189. $basic_type2[] = $value['title'] . '|' . $value['type'];
  190. }else{
  191. $basic_other_type[$value['top_depart_id']][] = $value['title'] . '|' . $value['type'];
  192. }
  193. }
  194. $depart = Depart::where('del_time',0)
  195. ->where('id','<>',2)
  196. ->where('parent_id',0)
  197. // ->whereIn('id',[139,49])
  198. ->select('id')
  199. ->orderBy('id','asc')
  200. ->get()->toArray();
  201. $depart = array_column($depart,'id');
  202. $insert = [];
  203. $time = time();
  204. foreach ($depart as $value){
  205. foreach ($basic_type2 as $val){
  206. if(! empty($basic_other_type[$value])) {
  207. if(in_array($val,$basic_other_type[$value])) continue;
  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. }else{
  217. $tmp = explode('|',$val);
  218. $insert[] = [
  219. 'title' => $tmp[0],
  220. 'type' => $tmp[1],
  221. 'top_depart_id' => $value,
  222. 'crt_time' => $time,
  223. 'crt_id' => 1
  224. ];
  225. }
  226. }
  227. }
  228. if(! empty($insert)) BasicType::insert($insert);
  229. dd('ok');
  230. }
  231. public function basicTypeAllUseEdit($data, $user){
  232. list($status,$msg) = $this->basicTypeAllUseRule($data,$user,false);
  233. if(!$status) return [$status,$msg];
  234. $update = $msg['data'][0];
  235. BasicTypeAllUse::where('id',$data['id'])->update($update);
  236. return [true,''];
  237. }
  238. public function basicTypeAllUseAdd($data,$user){
  239. list($status,$msg) = $this->basicTypeRule($data, $user);
  240. if(!$status) return [$status,$msg];
  241. BasicTypeAllUse::insert($msg['data']);
  242. return [true,''];
  243. }
  244. public function basicTypeAllUseDel($data){
  245. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  246. BasicTypeAllUse::whereIn('id',$data['id'])->update([
  247. 'del_time'=>time()
  248. ]);
  249. return [true,''];
  250. }
  251. public function basicTypeAllUseList($data, $user){
  252. $model = BasicTypeAllUse::where('del_time',0)
  253. ->select('title','id','type')
  254. ->orderby('id', 'asc');
  255. if(! empty($data['type'])) $model->where('type',$data['type']);
  256. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  257. $list = $this->limit($model,'',$data);
  258. $list = $this->fillAllUseData($list);
  259. return [true, $list];
  260. }
  261. public function fillAllUseData($data){
  262. if(empty($data['data'])) return $data;
  263. foreach ($data['data'] as $key => $value){
  264. $data['data'][$key]['type_name'] = BasicTypeAllUse::$type[$value['type']] ?? '';
  265. }
  266. return $data;
  267. }
  268. public function basicTypeAllUseRule($data,$user, $is_check = true){
  269. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  270. $type = array_column($data['data'],'type');
  271. $type = array_map(function($val) {
  272. return $val !== null ? $val : 0;
  273. }, $type);
  274. foreach ($type as $value){
  275. if(empty($value)) return [false,'类型不能为空!'];
  276. if(! isset(BasicTypeAllUse::$type[$value])) return [false,'类型不存在!'];
  277. }
  278. $map = [];
  279. foreach ($data['data'] as $value){
  280. if(! isset($map[$value['title']])){
  281. $map[$value['title']][] = $value['type'];
  282. }else{
  283. if(! in_array($value['type'],$map[$value['title']])){
  284. $map[$value['title']][] = $value['type'];
  285. }
  286. }
  287. }
  288. $title = array_column($data['data'],'title');
  289. $title = array_map(function($val) {
  290. return $val !== null ? $val : 0;
  291. }, $title);
  292. $title_count = array_count_values($title);
  293. foreach ($title as $value){
  294. if(empty($value)) return [false,'名称不能为空!'];
  295. if($title_count[$value] > 1 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];
  296. }
  297. foreach ($data['data'] as $key => $value){
  298. $data['data'][$key]['type'] = $value['type'];
  299. $data['data'][$key]['upd_time'] = time();
  300. if($is_check){
  301. $bool = BasicTypeAllUse::where('title',$value['title'])
  302. ->where('type',$value['type'])
  303. ->where('del_time',0)
  304. ->exists();
  305. $data['data'][$key]['crt_time'] = time();
  306. $data['data'][$key]['crt_id'] = $user['id'];
  307. $data['data'][$key]['depart_id'] = $data['depart_id'];
  308. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  309. }else{
  310. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  311. $bool = BasicTypeAllUse::where('title',$value['title'])
  312. ->where('type',$value['type'])
  313. ->where('id','<>',$data['id'])
  314. ->where('del_time',0)
  315. ->exists();
  316. }
  317. if($bool) return [false,'名称不能重复'];
  318. }
  319. return [true, $data];
  320. }
  321. }