customerRule($data,$user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Supplier::where('id',$data['id'])->first(); $model->title = $data['title']; $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(); $time = time(); SupplierInfo::where('del_time',0) ->where('supplier_id',$data['id']) ->update(['del_time' => $time]); if(! empty($data['employee'])){ $insert = []; foreach ($data['employee'] as $value){ $insert[] = [ 'supplier_id' => $model->id, 'data_id' => $value, 'type' => SupplierInfo::type_two, 'crt_time' => $time, ]; } SupplierInfo::insert($insert); } if(! empty($data['depart'])){ $insert = []; foreach ($data['depart'] as $value){ $insert[] = [ 'supplier_id' => $model->id, 'data_id' => $value, 'type' => SupplierInfo::type_one, 'crt_time' => $time, ]; } SupplierInfo::insert($insert); } 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->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(); $time = time(); if(! empty($data['employee'])){ $insert = []; foreach ($data['employee'] as $value){ $insert[] = [ 'supplier_id' => $model->id, 'data_id' => $value, 'type' => SupplierInfo::type_two, 'crt_time' => $time, ]; } SupplierInfo::insert($insert); } if(! empty($data['depart'])){ $insert = []; foreach ($data['depart'] as $value){ $insert[] = [ 'supplier_id' => $model->id, 'data_id' => $value, 'type' => SupplierInfo::type_one, 'crt_time' => $time, ]; } SupplierInfo::insert($insert); } 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() ]); SupplierInfo::where('del_time',0) ->where('supplier_id',$data['id']) ->update(['del_time' => time()]); 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(); $address = ''; if(! empty($customer['address1'])) { $tmp = json_decode($customer['address1'],true); $customer['address1'] = $tmp; $tmp = implode(' ',$tmp); $tmp .= ' ' . $customer['address2']; $address = $tmp; } $customer['address'] = $address; $customer_info = SupplierInfo::where('del_time',0) ->where('supplier_id',$customer['id']) ->get()->toArray(); $emp_id = []; $emp_id[] = $customer['crt_id']; $depart = []; foreach ($customer_info as $value){ if($value['type'] == SupplierInfo::type_two){ $emp_id[] = $value['data_id']; }else{ $depart[] = $value['data_id']; } } $emp_map = Employee::whereIn('id',array_unique($emp_id)) ->pluck('emp_name','id') ->toArray(); $depart_map = Depart::whereIn('id',array_unique($depart)) ->pluck('title','id') ->toArray(); $customer['depart'] = $customer['employee'] = []; foreach ($customer_info as $value){ if($value['type'] == SupplierInfo::type_one){ $tmp = [ 'id' => $value['data_id'], 'name' => $depart_map[$value['data_id']], ]; $customer['depart'][] = $tmp; }elseif ($value['type'] == SupplierInfo::type_two){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; $customer['employee'][] = $tmp; } } $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? ''; $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; return [true, $customer]; } /** * 供应商列表 * @param $data * @param $user * @return array */ public function customerList($data,$user){ $model = new Supplier(['userData' => $user,'search'=> $data]); $model = $model->where('del_time',0) ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark') ->orderby('id', 'asc'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); 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['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不能为空']; $top_depart_id = Supplier::where('id',$data['id'])->value('top_depart_id'); $bool = Supplier::where('del_time',0) ->where('id','<>',$data['id']) ->where('top_depart_id',$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){ $address = ''; if(! empty($value['address1'])) { $tmp = json_decode($value['address1'],true); $tmp = implode(' ',$tmp); $tmp .= ' ' . $value['address2']; $address = $tmp; } $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; } }