BasicTypeService.php 5.0 KB

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