BasicTypeService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. class BasicTypeService extends Service
  5. {
  6. public function basicTypeEdit($data){
  7. list($status,$msg) = $this->basicTypeRule($data,false);
  8. if(!$status) return [$status,$msg];
  9. $update = $msg['data'][0];
  10. BasicType::where('id',$data['id'])->update($update);
  11. return [true,''];
  12. }
  13. public function basicTypeAdd($data){
  14. list($status,$msg) = $this->basicTypeRule($data);
  15. if(!$status) return [$status,$msg];
  16. BasicType::insert($msg['data']);
  17. return [true,''];
  18. }
  19. public function basicTypeDel($data){
  20. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  21. BasicType::whereIn('id',$data['id'])->update([
  22. 'del_time'=>time()
  23. ]);
  24. return [true,''];
  25. }
  26. public function basicTypeList($data){
  27. $model = BasicType::where('del_time',0)
  28. ->select('title','id','type')
  29. ->orderby('id', 'asc');
  30. if(! empty($data['type'])) $model->where('type',$data['type']);
  31. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  32. $list = $this->limit($model,'',$data);
  33. $list = $this->fillData($list);
  34. return [true, $list];
  35. }
  36. public function basicTypeRule($data,$is_check = true){
  37. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  38. $type = array_column($data['data'],'type');
  39. $type = array_map(function($val) {
  40. return $val !== null ? $val : 0;
  41. }, $type);
  42. foreach ($type as $value){
  43. if(empty($value)) return [false,'类型不能为空!'];
  44. if(! isset(BasicType::$type[$value])) return [false,'类型不存在!'];
  45. }
  46. $map = [];
  47. foreach ($data['data'] as $value){
  48. if(! isset($map[$value['title']])){
  49. $map[$value['title']][] = $value['type'];
  50. }else{
  51. if(! in_array($value['type'],$map[$value['title']])){
  52. $map[$value['title']][] = $value['type'];
  53. }
  54. }
  55. }
  56. $title = array_column($data['data'],'title');
  57. $title = array_map(function($val) {
  58. return $val !== null ? $val : 0;
  59. }, $title);
  60. $title_count = array_count_values($title);
  61. foreach ($title as $value){
  62. if(empty($value)) return [false,'名称不能为空!'];
  63. if($title_count[$value] > 1 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];
  64. }
  65. foreach ($data['data'] as $key => $value){
  66. $data['data'][$key]['type'] = $value['type'];
  67. $data['data'][$key]['upd_time'] = time();
  68. if($is_check){
  69. $bool = BasicType::where('title',$value['title'])
  70. ->where('type',$value['type'])
  71. ->where('del_time',0)
  72. ->exists();
  73. $data['data'][$key]['crt_time'] = time();
  74. }else{
  75. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  76. $bool = BasicType::where('title',$value['title'])
  77. ->where('type',$value['type'])
  78. ->where('id','<>',$data['id'])
  79. ->where('del_time',0)
  80. ->exists();
  81. }
  82. if($bool) return [false,'名称不能重复'];
  83. }
  84. return [true, $data];
  85. }
  86. public function fillData($data){
  87. if(empty($data['data'])) return $data;
  88. foreach ($data['data'] as $key => $value){
  89. $data['data'][$key]['type_name'] = BasicType::$type[$value['type']] ?? '';
  90. }
  91. return $data;
  92. }
  93. }