12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Service;
- use App\Model\Scrapp;
- /**
- * 报废原因
- * @package App\Models
- */
- class ScrappService extends Service
- {
- public function scrappEdit($data){
- list($status,$msg) = $this->scrappRule($data,false);
- if(!$status) return [$status,$msg];
- $update = $msg['data'][0];
- Scrapp::where('id',$data['id'])->update($update);
- return [true,'保存成功!'];
- }
- public function scrappAdd($data){
- list($status,$msg) = $this->scrappRule($data);
- if(!$status) return [$status,$msg];
- Scrapp::insert($msg['data']);
- return [true,'保存成功!'];
- }
- public function scrappDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- Scrapp::whereIn('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- return [true,'删除成功'];
- }
- public function scrappList($data){
- $model = Scrapp::where('del_time',0)
- ->select('*');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- $list = $this->limit($model,'',$data);
- return [200,$list];
- }
- public function scrappRule($data,$is_add = true){
- if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
- $title = array_column($data['data'],'title');
- $title = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $title);
- $title_count = array_count_values($title);
- foreach ($title as $value){
- if(empty($value)) return [false,'名称不能为空!'];
- if($title_count[$value] > 1) return [false,'名称不能重复'];
- }
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['upd_time'] = time();
- if($is_add){
- $bool = Scrapp::whereRaw("title = '{$value['title']}'")
- ->where('del_time',0)
- ->exists();
- $data['data'][$key]['crt_time'] = time();
- }else{
- if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
- $bool = Scrapp::whereRaw("title = '{$value['title']}'")
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if($bool) return [false,'名称不能重复'];
- }
- return [true,$data];
- }
- }
|