ScrappService.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Scrapp;
  4. /**
  5. * 报废原因
  6. * @package App\Models
  7. */
  8. class ScrappService extends Service
  9. {
  10. public function scrappEdit($data){
  11. list($status,$msg) = $this->scrappRule($data,false);
  12. if(!$status) return [$status,$msg];
  13. $update = $msg['data'][0];
  14. Scrapp::where('id',$data['id'])->update($update);
  15. return [true,'保存成功!'];
  16. }
  17. public function scrappAdd($data){
  18. list($status,$msg) = $this->scrappRule($data);
  19. if(!$status) return [$status,$msg];
  20. Scrapp::insert($msg['data']);
  21. return [true,'保存成功!'];
  22. }
  23. public function scrappDel($data){
  24. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  25. Scrapp::whereIn('id',$data['id'])->update([
  26. 'del_time' => time()
  27. ]);
  28. return [true,'删除成功'];
  29. }
  30. public function scrappList($data){
  31. $model = Scrapp::where('del_time',0)
  32. ->select('*');
  33. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  34. $list = $this->limit($model,'',$data);
  35. return [200,$list];
  36. }
  37. public function scrappRule($data,$is_add = true){
  38. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  39. $title = array_column($data['data'],'title');
  40. $title = array_map(function($val) {
  41. return $val !== null ? $val : 0;
  42. }, $title);
  43. $title_count = array_count_values($title);
  44. foreach ($title as $value){
  45. if(empty($value)) return [false,'名称不能为空!'];
  46. if($title_count[$value] > 1) return [false,'名称不能重复'];
  47. }
  48. foreach ($data['data'] as $key => $value){
  49. $data['data'][$key]['upd_time'] = time();
  50. if($is_add){
  51. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  52. ->where('del_time',0)
  53. ->exists();
  54. $data['data'][$key]['crt_time'] = time();
  55. }else{
  56. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  57. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  58. ->where('id','<>',$data['id'])
  59. ->where('del_time',0)
  60. ->exists();
  61. }
  62. if($bool) return [false,'名称不能重复'];
  63. }
  64. return [true,$data];
  65. }
  66. }