BasicTypeService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $list = $this->limit($model,'',$data);
  57. $list = $this->fillData($list);
  58. return [true, $list];
  59. }
  60. /**
  61. * 基础类型参数规则
  62. * @param $data
  63. * @param $is_check
  64. * @return array
  65. */
  66. public function basicTypeRule($data,$user, $is_check = true){
  67. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  68. //所属部门 以及 顶级部门
  69. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  70. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  71. $type = array_column($data['data'],'type');
  72. $type = array_map(function($val) {
  73. return $val !== null ? $val : 0;
  74. }, $type);
  75. foreach ($type as $value){
  76. if(empty($value)) return [false,'类型不能为空!'];
  77. if(! isset(BasicType::$type[$value])) return [false,'类型不存在!'];
  78. }
  79. $map = [];
  80. foreach ($data['data'] as $value){
  81. if(! isset($map[$value['title']])){
  82. $map[$value['title']][] = $value['type'];
  83. }else{
  84. if(! in_array($value['type'],$map[$value['title']])){
  85. $map[$value['title']][] = $value['type'];
  86. }
  87. }
  88. }
  89. $title = array_column($data['data'],'title');
  90. $title = array_map(function($val) {
  91. return $val !== null ? $val : 0;
  92. }, $title);
  93. $title_count = array_count_values($title);
  94. foreach ($title as $value){
  95. if(empty($value)) return [false,'名称不能为空!'];
  96. if($title_count[$value] > 1 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];
  97. }
  98. foreach ($data['data'] as $key => $value){
  99. $data['data'][$key]['type'] = $value['type'];
  100. $data['data'][$key]['upd_time'] = time();
  101. if($is_check){
  102. $bool = BasicType::where('title',$value['title'])
  103. ->where('top_depart_id',$data['top_depart_id'])
  104. ->where('type',$value['type'])
  105. ->where('del_time',0)
  106. ->exists();
  107. $data['data'][$key]['crt_time'] = time();
  108. $data['data'][$key]['crt_id'] = $user['id'];
  109. $data['data'][$key]['depart_id'] = $data['depart_id'];
  110. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  111. }else{
  112. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  113. $top_depart_id = BasicType::where('id',$data['id'])->value('top_depart_id');
  114. $bool = BasicType::where('title',$value['title'])
  115. ->where('top_depart_id',$top_depart_id)
  116. ->where('type',$value['type'])
  117. ->where('id','<>',$data['id'])
  118. ->where('del_time',0)
  119. ->exists();
  120. }
  121. if($bool) return [false,'名称不能重复'];
  122. }
  123. return [true, $data];
  124. }
  125. /**
  126. * 拼接数据
  127. * @param $data
  128. * @return array
  129. */
  130. public function fillData($data){
  131. if(empty($data['data'])) return $data;
  132. foreach ($data['data'] as $key => $value){
  133. $data['data'][$key]['type_name'] = BasicType::$type[$value['type']] ?? '';
  134. }
  135. return $data;
  136. }
  137. }