BasicTypeService.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ->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,'type')) return [false,'基础类型不能为空'];
  38. if(! isset(BasicType::$type[$data['type']])) return [false,'基础类型不存在'];
  39. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  40. $title = array_column($data['data'],'title');
  41. $title = array_map(function($val) {
  42. return $val !== null ? $val : 0;
  43. }, $title);
  44. $title_count = array_count_values($title);
  45. foreach ($title as $value){
  46. if(empty($value)) return [false,'名称不能为空!'];
  47. if($title_count[$value] > 1) return [false,'名称不能重复'];
  48. }
  49. foreach ($data['data'] as $key => $value){
  50. $data['data'][$key]['type'] = $data['type'];
  51. $data['data'][$key]['upd_time'] = time();
  52. if($is_check){
  53. $bool = BasicType::where('title',$value['title'])
  54. ->where('type',$data['type'])
  55. ->where('del_time',0)
  56. ->exists();
  57. $data['data'][$key]['crt_time'] = time();
  58. }else{
  59. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  60. $bool = BasicType::where('title',$value['title'])
  61. ->where('type',$data['type'])
  62. ->where('id','<>',$data['id'])
  63. ->where('del_time',0)
  64. ->exists();
  65. }
  66. if($bool) return [false,'名称不能重复'];
  67. }
  68. return [true, $data];
  69. }
  70. public function fillData($data){
  71. if(empty($data['data'])) return $data;
  72. return $data;
  73. }
  74. }