123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace App\Service;
- use App\Model\SeeRange;
- use App\Model\Supplier;
- use App\Model\Employee;
- use Illuminate\Support\Facades\DB;
- class SupplierService extends Service
- {
- /**
- * 供应商编辑
- * @param $data
- * @param $user
- * @return array
- */
- public function customerEdit($data,$user){
- list($status,$msg) = $this->customerRule($data,$user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = Supplier::where('id',$data['id'])->first();
- $model->title = $data['title'];
- $model->code = $data['code'];
- $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
- $model->address2 = $data['address2'] ?? '';
- $model->mark = $data['mark'] ?? '';
- $model->is_main = $data['is_main'] ?? 0;
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 供应商新增
- * @param $data
- * @param $user
- * @return array
- */
- public function customerAdd($data,$user){
- list($status,$msg) = $this->customerRule($data,$user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Supplier();
- $model->title = $data['title'];
- $model->code = $data['code'];
- $model->mobile = $data['mobile'] ?? "";
- $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
- $model->address2 = $data['address2'] ?? '';
- $model->crt_id = $user['id'];
- $model->mark = $data['mark'] ?? '';
- $model->depart_id = $data['depart_id'] ?? 0;
- $model->top_depart_id = $data['top_depart_id'] ?? 0;
- $model->is_main = $data['is_main'] ?? 0;
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 供应商删除
- * @param $data
- * @return array
- */
- public function customerDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- Supplier::where('id',$data['id'])->update([
- 'del_time'=> time()
- ]);
- (new RangeService())->RangeDelete($data['id'],SeeRange::type_nine);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 供应商详情
- * @param $data
- * @return array
- */
- public function customerDetail($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = Supplier::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'供应商不存在或已被删除'];
- $customer = $customer->toArray();
- if(! empty($customer['address1'])) {
- $tmp = json_decode($customer['address1'],true);
- $customer['address1'] = $tmp;
- $tmp = implode(' ',$tmp);
- $tmp .= ' ' . $customer['address2'];
- $address = $tmp;
- }else{
- $address = $customer['address2'];
- }
- $customer['address'] = $address;
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
- //可见范围
- $return = (new RangeService())->RangeDetail($customer['id'],SeeRange::type_nine);
- $customer['depart'] = $return[0] ?? [];
- $customer['employee'] = $return[1] ?? [];
- return [true, $customer];
- }
- /**
- * 供应商列表
- * @param $data
- * @param $user
- * @return array
- */
- public function customerList($data,$user){
- $model = Supplier::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark','code')
- ->orderby('id', 'asc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
- if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model->where('crt_time','>=',$return[0]);
- $model->where('crt_time','<=',$return[1]);
- }
- if(! empty($data['get_all'])) $model->orWhere('is_main',Supplier::is_main);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- /**
- * 供应商参数规则
- * @param $data
- * @param $is_add
- * @return array
- */
- public function customerRule(&$data, $user, $is_add = true){
- if(empty($data['title'])) return [false,'供应商名称不能为空'];
- if(empty($data['code'])) return [false,'供应商编码不能为空'];
- //所属部门 以及 顶级部门
- if(empty($data['depart_id'])) {
- $data['depart_id'] = $this->getDepart($user);
- $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
- }
- if($is_add){
- $bool = Supplier::where('del_time',0)
- ->where('title',$data['title'])
- ->where('top_depart_id',$data['top_depart_id'])
- ->exists();
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = Supplier::where('del_time',0)
- ->where('id','<>',$data['id'])
- ->where('top_depart_id',$data['top_depart_id'])
- ->where('title',$data['title'])
- ->exists();
- }
- if($bool) return [false,'供应商名称不能重复'];
- return [true, ''];
- }
- /**
- * 拼接数据
- * @param $data
- * @return array
- */
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
- ->pluck('emp_name','id')
- ->toArray();
- foreach ($data['data'] as $key => $value){
- if(! empty($value['address1'])) {
- $tmp = json_decode($value['address1'],true);
- $tmp = implode(' ',$tmp);
- $tmp .= ' ' . $value['address2'];
- $address = $tmp;
- }else{
- $address = $value['address2'];
- }
- $data['data'][$key]['address'] = $address;
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- }
- return $data;
- }
- }
|