| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 | <?phpnamespace App\Service;use App\Model\Storehouse;use App\Model\StorehouseEmployee;use Illuminate\Support\Facades\DB;class StorehouseService extends Service{    public function edit($data){        list($status,$msg) = $this->storehouseRule($data,false);        if(!$status) return [$status,$msg];        $res = $data['data'][0];        try{            DB::beginTransaction();            Storehouse::where('id',$data['id'])->update([                'title' => $res['title']            ]);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,'保存成功!'];    }    public function add($data){        list($status,$msg) = $this->storehouseRule($data);        if(!$status) return [$status,$msg];        try{            DB::beginTransaction();            $insert = [];            $time = time();            foreach ($data['data'] as $value){                $insert = [                    'title' => $value['title'],                    'crt_time' => $time                ];            }            Storehouse::insert($insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,'保存成功!'];    }    public function del($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据'];        try {            DB::beginTransaction();            Storehouse::whereIn('id',$data['id'])->update([                'del_time'=>time()            ]);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,'删除成功'];    }    public function storehouseDetail($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据'];        $detail = Storehouse::where('id',$data['id'])->first()->toArray();        return [true,$detail];    }    public function storehouseList($data, $user){        $model = Storehouse::TopClear($user,$data);        $model = $model->where('del_time',0)            ->select('id','title','depart_id','top_depart_id')            ->orderBy('id','asc');        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        $list = $this->limit($model,'',$data);        $list = $this->fillData($list, $user);        return [true,$list];    }    public function fillData($data, $user){        if(empty($data['data'])) return $data;        //总社id        $top_depart_id = $user['head'] ?? [];        $top_depart_id = $top_depart_id['id'] ?? 0;        foreach ($data['data'] as $key => $value){            $is_top = 0;            if($value['depart_id'] == $top_depart_id) $is_top = 1;            $data['data'][$key]['is_top'] = $is_top;        }        return $data;    }    public function storehouseRule($data, $is_check = 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 $value){            if($is_check){                $bool = Storehouse::where('title',$value['title'])                    ->where('del_time',0)                    ->exists();            }else{                if($this->isEmpty($data,'id')) return [false,'id不能为空!'];                $bool = Storehouse::where('title',$value['title'])                    ->where('id','<>',$data['id'])                    ->where('del_time',0)                    ->exists();            }            if($bool) return [false,'仓库名称不能重复'];        }        return [true,''];    }}
 |