BasicTypeService.php 5.2 KB

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