AreaService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Area;
  4. use App\Model\Screen;
  5. /**
  6. * 区域相关
  7. * @package App\Models
  8. */
  9. class AreaService extends Service
  10. {
  11. public function edit($data){
  12. list($status,$msg) = $this->areaRule($data,false);
  13. if(!$status) return [$status,$msg];
  14. $model = Area::where('id',$data['id'])->first();
  15. $model->title = $data['title'];
  16. $model->code = $data['code'];
  17. $model->type = $data['type'];
  18. $model->save();
  19. return [true,'保存成功!'];
  20. }
  21. public function add($data){
  22. list($status,$msg) = $this->areaRule($data);
  23. if(!$status) return [$status,$msg];
  24. $model = new Area();
  25. $model->title = $data['title'];
  26. $model->code = $data['code'];
  27. $model->type = $data['type'];
  28. $model->save();
  29. return [true,'保存成功!'];
  30. }
  31. public function del($data){
  32. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  33. $bool = Screen::where('del_time',0)->whereIn('area_id',$data['id'])->exists();
  34. if($bool) return [false,'区域已经绑定仓'];
  35. Area::whereIn('id',$data['id'])->update([
  36. 'del_time'=>time()
  37. ]);
  38. return [true,'删除成功'];
  39. }
  40. public function AreaList($data){
  41. $model = Area::where('del_time',0)
  42. ->select('title','id','code','type')
  43. ->orderby('id', 'desc');
  44. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  45. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  46. if(! empty($data['type'])) $model->where('type', $data['type']);
  47. $list = $this->limit($model,'',$data);
  48. return [true,$list];
  49. }
  50. public function areaRule($data, $is_check = true){
  51. if(empty($data['title'])) return [false,'区域名称不能为空!'];
  52. if(empty($data['code'])) return [false,'区域编码不能为空!'];
  53. if(empty($data['type'])) return [false,'区域类型不能为空'];
  54. if($is_check){
  55. $bool = Area::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  56. ->where('type',$data['type'])
  57. ->where('del_time',0)
  58. ->exists();
  59. }else{
  60. if(empty($data['id'])) return [false,'id不能为空!'];
  61. $bool = Area::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  62. ->where('type',$data['type'])
  63. ->where('id','<>',$data['id'])
  64. ->where('del_time',0)
  65. ->exists();
  66. }
  67. if($bool) return [false,'编码或区域名称不能重复'];
  68. return [true, ''];
  69. }
  70. }