customerRule($data,false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Customer::where('id',$data['id'])->first(); $model->title = $data['title']; $model->model_type = $data['model_type']; $model->customer_intention = $data['customer_intention'] ?? 0; $model->customer_from = $data['customer_from'] ?? 0; $model->customer_type = $data['customer_type'] ?? 0; $model->car_type = $data['car_type'] ?? 0; $model->consulting_product = $data['consulting_product'] ?? ''; $model->intention_product = $data['intention_product'] ?? 0; $model->progress_stage = $data['progress_stage'] ?? 0; $model->address1 = $data['address1'] ?? ''; $model->address2 = $data['address2'] ?? ''; $model->crt_id = $user['id']; $model->mark = $data['mark'] ?? ''; $model->importance = $data['importance'] ?? ''; $model->company = $data['company'] ?? ''; $model->company_short_name = $data['company_short_name'] ?? ''; $model->depart_id = $data['depart_id'] ?? 0; $model->state_type = $data['state_type'] ?? 0; $model->customer_state = $data['customer_state'] ?? 0; $model->customer_grade = $data['customer_grade'] ?? 0; $model->save(); $time = time(); CustomerInfo::where('del_time',0) ->where('customer_id',$data['id']) ->update(['del_time' => $time]); if(! empty($data['customer_contact'])){ $insert = []; foreach ($data['customer_contact'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'contact_type' => $value['id'], 'contact_info' => $value['info'], 'type' => CustomerInfo::type_one, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['employee_one'])){ $insert = []; foreach ($data['employee_one'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'employee_id' => $value, 'type' => CustomerInfo::type_two, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['employee_two'])){ $insert = []; foreach ($data['employee_two'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'employee_id' => $value, 'type' => CustomerInfo::type_three, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['employee_three'])){ $insert = []; foreach ($data['employee_three'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'employee_id' => $value, 'type' => CustomerInfo::type_four, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['img'])){ $insert = []; foreach ($data['img'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value, 'type' => CustomerInfo::type_five, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value, 'type' => CustomerInfo::type_six, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function customerAdd($data,$user){ list($status,$msg) = $this->customerRule($data); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Customer(); $model->title = $data['title']; $model->model_type = $data['model_type']; $model->customer_intention = $data['customer_intention'] ?? 0; $model->customer_from = $data['customer_from'] ?? 0; $model->customer_type = $data['customer_type'] ?? 0; $model->car_type = $data['car_type'] ?? 0; $model->consulting_product = $data['consulting_product'] ?? ''; $model->intention_product = $data['intention_product'] ?? 0; $model->progress_stage = $data['progress_stage'] ?? 0; $model->address1 = $data['address1'] ?? ''; $model->address2 = $data['address2'] ?? ''; $model->crt_id = $user['id']; $model->mark = $data['mark'] ?? ''; $model->importance = $data['importance'] ?? ''; $model->company = $data['company'] ?? ''; $model->company_short_name = $data['company_short_name'] ?? ''; $model->depart_id = $data['depart_id'] ?? 0; $model->state_type = $data['state_type'] ?? 0; $model->customer_state = $data['customer_state'] ?? 0; $model->customer_grade = $data['customer_grade'] ?? 0; $model->save(); $time = time(); if(! empty($data['customer_contact'])){ $insert = []; foreach ($data['customer_contact'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'contact_type' => $value['id'], 'contact_info' => $value['info'], 'type' => CustomerInfo::type_one, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['employee_one'])){ $insert = []; foreach ($data['employee_one'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'employee_id' => $value, 'type' => CustomerInfo::type_two, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['employee_two'])){ $insert = []; foreach ($data['employee_two'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'employee_id' => $value, 'type' => CustomerInfo::type_three, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['employee_three'])){ $insert = []; foreach ($data['employee_three'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'employee_id' => $value, 'type' => CustomerInfo::type_four, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['img'])){ $insert = []; foreach ($data['img'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value, 'type' => CustomerInfo::type_five, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value, 'type' => CustomerInfo::type_six, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function customerDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; Customer::whereIn('id',$data['id'])->update([ 'del_time'=> time() ]); CustomerInfo::where('del_time',0) ->where('customer_id',$data['id']) ->update(['del_time' => time()]); return [true,'']; } public function customerDetail($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = Customer::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'客户不存在或已被删除']; $customer = $customer->toArray(); $array = [ $customer['customer_intention'], $customer['customer_from'], $customer['customer_type'], $customer['car_type'], $customer['intention_product'], $customer['progress_stage'], $customer['state_type'], $customer['customer_state'], $customer['customer_grade'], ]; $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); foreach ($customer as $key => $value){ $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? ''; $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? ''; $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? ''; $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? ''; $customer[$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? ''; $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? ''; $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? ''; $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? ''; $customer[$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? ''; } return [true, $customer]; } public function customerList($data,$user){ $model = Customer::where('del_time',0) ->select('title','id','type') ->orderby('id', 'asc') ->where('type',$data['type']); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true, $list]; } public function customerRule($data, $is_add = true){ if(empty($data['model_type'])) return [false,'客户模板类型不能为空']; if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误']; if(empty($data['title'])) return [false,'客户名称错误']; if($data['model_type'] == Customer::Model_type_one){ if(empty($data['customer_from'])) return [false,'客户来源不能为空']; if(empty($data['customer_type'])) return [false,'客户类别不能为空']; if(empty($data['consulting_product'])) return [false,'咨询产品不能为空']; if(empty($data['progress_stage'])) return [false,'进展阶段不能为空']; if(empty($data['employee_one'])) return [false,'销售SA不能为空']; }else{ if(empty($data['car_type'])) return [false,'车型不能为空']; if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空']; } if($is_add){ $bool = Customer::where('del_time',0) ->where('title',$data['title']) ->where('model_type',$data['model_type']) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Customer::where('del_time',0) ->where('id','<>',$data['id']) ->where('title',$data['title']) ->where('model_type',$data['model_type']) ->exists(); } if($bool) return [false,'客户名称不能重复']; return [true, $data]; } public function fillData($data){ if(empty($data['data'])) return $data; return $data; } }