BasicTypeService.php 4.3 KB

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