customerRule($data,$user, false); if(!$status) return [$status,$msg]; $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $data['title']; $params = $this->getDataFile($data); (new OperationLogService())->setOperationList($params,$user,2); try { DB::beginTransaction(); //车型 if(empty($data['car_type']) && ! empty($data['car_type_title'])){ $model_2 = new BasicType(); $model_2->title = $data['car_type_title']; $model_2->type = 10; $model_2->depart_id = $data['depart_id'] ?? 0; $model_2->top_depart_id = $data['top_depart_id'] ?? 0; $model_2->crt_id = $user['id']; $model_2->save(); $data['car_type'] = $model_2->id; } $model = Customer::where('id',$data['id'])->first(); $model->title = $data['title']; $model->code = $data['code'] ?? ""; $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 = ! empty($data['address1']) ? json_encode($data['address1']) : ''; $model->address2 = $data['address2'] ?? ''; $model->mark = $data['mark'] ?? ''; $model->importance = $data['importance'] ?? ''; $model->company = $data['company'] ?? ''; // $model->company_short_name = $data['company_short_name'] ?? ''; $model->state_type = $data['state_type'] ?? 0; $model->customer_state = $data['customer_state'] ?? 0; $model->save(); $time = time(); $old = CustomerInfo::where('del_time',0) ->where('customer_id',$data['id']) ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six]) ->select('file') ->get()->toArray(); $old = array_column($old,'file'); CustomerInfo::where('del_time',0) ->where('customer_id',$data['id']) ->whereNotIn('type',CustomerInfo::$no_edit) ->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, // 'data_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, 'data_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, 'data_id' => $value, 'type' => CustomerInfo::type_four, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } $new = []; if(! empty($data['img'])){ $insert = []; foreach ($data['img'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value['url'], 'type' => CustomerInfo::type_five, 'name' => $value['name'], 'crt_time' => $time, ]; if(in_array($value['url'], $old)) { foreach ($old as $o_k => $o_v){ if($o_v == $value['url']) unset($old[$o_k]); } }else{ $new[] = $value['url']; } } CustomerInfo::insert($insert); } if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value['url'], 'type' => CustomerInfo::type_six, 'name' => $value['name'], 'crt_time' => $time, ]; if(in_array($value['url'], $old)) { foreach ($old as $o_k => $o_v){ if($o_v == $value['url']) unset($old[$o_k]); } }else{ $new[] = $value['url']; } } CustomerInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, ['file' => ['new' => $new, 'old' => $old]]]; } /** * 客户新增 * @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(); //车型 if(empty($data['car_type']) && ! empty($data['car_type_title'])){ $model_2 = new BasicType(); $model_2->title = $data['car_type_title']; $model_2->type = 10; $model_2->depart_id = $data['depart_id'] ?? 0; $model_2->top_depart_id = $data['top_depart_id'] ?? 0; $model_2->crt_id = $user['id']; $model_2->save(); $data['car_type'] = $model_2->id; } $model = new Customer(); $model->title = $data['title']; $model->code = $data['code'] ?? ""; $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 = ! empty($data['address1']) ? json_encode($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->top_depart_id = $data['top_depart_id'] ?? 0; $model->state_type = $data['state_type'] ?? 0; $model->customer_state = $data['customer_state'] ?? 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, 'data_id' => $value, 'type' => CustomerInfo::type_two, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); Customer::where('id',$model->id)->update([ 'fp_top_depart_id' => $data['fp_top_depart_id'] ?? 0, 'fp_time' => $time, ]); } if(! empty($data['employee_two'])){ $insert = []; foreach ($data['employee_two'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'data_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, 'data_id' => $value, 'type' => CustomerInfo::type_four, 'crt_time' => $time, ]; } CustomerInfo::insert($insert); } $new = []; if(! empty($data['img'])){ $insert = []; foreach ($data['img'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value['url'], 'type' => CustomerInfo::type_five, 'name' => $value['name'], 'crt_time' => $time, ]; if(! empty($value['url'])) $new[] = $value['url']; } CustomerInfo::insert($insert); } if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'customer_id' => $model->id, 'file' => $value['url'], 'type' => CustomerInfo::type_six, 'name' => $value['name'], 'crt_time' => $time, ]; if(! empty($value['url'])) $new[] = $value['url']; } CustomerInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } $data['order_number'] = Customer::$order_number . "|" . $model->id . "|" . $data['title']; (new OperationLogService())->setOperationList($data,$user); return [true, ['file' => ['new' => $new]]]; } /** * 客户删除 * @param $data * @return array */ public function customerDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; if($data['id'] < 0) return [false, '系统数据,操作失败!']; try { DB::beginTransaction(); Customer::where('id',$data['id'])->update([ 'del_time'=> time() ]); $old = CustomerInfo::where('del_time',0) ->where('customer_id',$data['id']) ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six]) ->select('file') ->get()->toArray(); $old = array_column($old,'file'); CustomerInfo::where('del_time',0) ->where('customer_id',$data['id']) ->update(['del_time' => time()]); (new RangeService())->RangeDelete($data['id'],SeeRange::type_one); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, ['file' => ['old' => $old]]]; } /** * 客户详情 * @param $data * @return array */ public function customerDetail($data,$user){ 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(); //是否是总社的客户 $is_main = 0; if($customer['top_depart_id'] == $user['head']['id']) $is_main = 1; $customer['is_belong_to_main'] = $is_main; $customer['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $customer['title']; $customer['intention_product_title'] = Product::where('id',$customer['intention_product'])->value('title') ?? ""; $customer['product_category'] = []; if(! empty($customer['intention_product'])){ $pro = (new ProductService())->getProductDetail([$customer['intention_product']]); $product_category = $pro[$customer['intention_product']]['product_category'] ?? ''; $customer['product_category'] = $product_category ? json_decode($product_category,true) : []; } $address_map = config('address'); $address_str = []; if(! empty($customer['address1'])) { $tmp = json_decode($customer['address1'],true); $this->findLabelsByValue($address_map,$tmp,$address_str); $customer['address1'] = $tmp; $tmp = implode(' ',$address_str); $tmp .= ' ' . $customer['address2']; $address = $tmp; }else{ $address = $customer['address2']; } $customer['address'] = $address; $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = []; $array = [ $customer['customer_intention'], $customer['customer_from'], $customer['customer_type'], $customer['car_type'], $customer['progress_stage'], $customer['state_type'], $customer['customer_state'], ]; $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title'); $customer = [$customer]; 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]['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]['depart_title'] = $depart_title ?? ''; } $customer = $customer[0]; $customer_info = CustomerInfo::where('del_time',0) ->where('customer_id',$customer['id']) ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name') ->get()->toArray(); $emp_id = []; $emp_id[] = $customer['crt_id']; foreach ($customer_info as $value){ if(in_array($value['type'], CustomerInfo::$man)){ $emp_id[] = $value['data_id']; } } $emp_map = Employee::whereIn('id',array_unique($emp_id)) ->pluck('emp_name','id') ->toArray(); $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type'))) ->pluck('title','id') ->toArray(); $fileUploadService = new FileUploadService(); foreach ($customer_info as $value){ if($value['type'] == CustomerInfo::type_one){ $tmp = [ 'id' => $value['contact_type'], 'title' => $basic_map2[$value['contact_type']] ?? '', 'info' => $value['contact_info'] ]; $customer['customer_contact'][] = $tmp; }elseif ($value['type'] == CustomerInfo::type_two){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; $customer['employee_one'][] = $tmp; }elseif ($value['type'] == CustomerInfo::type_three){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; $customer['employee_two'][] = $tmp; }elseif ($value['type'] == CustomerInfo::type_four){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; $customer['employee_three'][] = $tmp; }elseif ($value['type'] == CustomerInfo::type_five){ $tmp = [ 'url' => $value['file'], 'name' => $value['name'], 'show_url' => $fileUploadService->getFileShow($value['file']), ]; $customer['img'][] = $tmp; }elseif ($value['type'] == CustomerInfo::type_six){ $tmp = [ 'url' => $value['file'], 'name' => $value['name'], 'show_url' => $fileUploadService->getFileShow($value['file']), ]; $customer['file'][] = $tmp; }elseif ($value['type'] == CustomerInfo::type_nine){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; $customer['old_employee_one'][] = $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 = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one); $customer['depart'] = $return[0] ?? []; $customer['employee'] = $return[1] ?? []; return [true, $customer]; } /** * 客户列表 * @param $data * @param $user * @return array */ public function customerList($data,$user){ $model = Customer::Clear($user,$data); $model = $model->where('del_time',0) ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id') ->orderby('id', 'desc'); if(! empty($data['id'])) $model->where('id', $data['id']); if(! empty($data['my_fz']) || ! empty($data['my_xt'])){ $id = $this->getCustomerId($data,$user); $model->whereIn('id',$id); } if(! empty($data['pond_state'])) { $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司 if(empty($search_depart_id)){ $top_depart_id = $user['depart_top'][0] ?? []; $top_depart_id = $top_depart_id['depart_id'] ?? 0; }else{ //查询 顶级公司 $top_depart_id = $search_depart_id; } // 进入公海池的客户 公司下所有人可见 $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id); } if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['time_type'])) { if($data['time_type'] == 1) { $start = strtotime('today'); $end = strtotime('tomorrow') - 1; }elseif ($data['time_type'] == 2){ $start = strtotime('this week',strtotime('today')); $end = strtotime('this week +6 days 23:59:59', strtotime('today')); } if(! empty($start) && ! empty($end)) { $model->where('crt_time','>=',$start); $model->where('crt_time','<=',$end); } } if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']); if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%'); if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%'); if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']); if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']); if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']); if(! empty($data['customer_intention_title'])){ $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]); $model->whereIn('customer_intention', $id); } if(! empty($data['customer_from_title'])){ $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]); $model->whereIn('customer_from', $id); } if(! empty($data['customer_type_title'])) { $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30]); $model->whereIn('customer_type', $id); } 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['crt_name'])){ $id = (new RangeService())->crtNameSearch($data); $model->whereIn('crt_id',$id); } if(! empty($data['fz'])){ $id = (new RangeService())->customerSearch($data); $model->whereIn('id',$id); } if(! empty($data['last_visit_time'])){ $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']); $model->whereIn('id',$id); } if(! empty($data['contact_info'])){ $customer_info = CustomerInfo::where('del_time',0) ->where("type",CustomerInfo::type_one) ->where('contact_info','LIKE', '%'.$data['contact_info'].'%') ->select('customer_id') ->get()->toArray(); $model->whereIn('id',array_column($customer_info,'customer_id')); } $list = $this->limit($model,'',$data); $list = $this->fillData($list,$data); return [true, $list]; } public function customerList2($data,$user){ $model = Customer::Clear($user,$data); $model = $model->where('del_time',0) ->where('fp_time','>',0) ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id') ->orderby('fp_time', 'desc'); if(! empty($data['my_fz']) || ! empty($data['my_xt'])){ $id = $this->getCustomerId($data,$user); $model->whereIn('id',$id); } if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['time_type'])) { if($data['time_type'] == 1) { $start = strtotime('today'); $end = strtotime('tomorrow') - 1; }elseif ($data['time_type'] == 2){ $start = strtotime('this week',strtotime('today')); $end = strtotime('this week +6 days 23:59:59', strtotime('today')); } if(! empty($start) && ! empty($end)) { $model->where('crt_time','>=',$start); $model->where('crt_time','<=',$end); } } if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']); if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%'); if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%'); if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']); if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']); if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']); 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['crt_name'])){ $id = (new RangeService())->crtNameSearch($data); $model->whereIn('crt_id',$id); } if(! empty($data['fz'])){ $id = (new RangeService())->customerSearch($data); $model->whereIn('id',$id); } if(! empty($data['last_visit_time'])){ $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']); $model->whereIn('id',$id); } if(! empty($data['contact_info'])){ $customer_info = CustomerInfo::where('del_time',0) ->where("type",CustomerInfo::type_one) ->where('contact_info','LIKE', '%'.$data['contact_info'].'%') ->select('customer_id') ->get()->toArray(); $model->whereIn('id',array_column($customer_info,'customer_id')); } if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){ $return = $this->changeDateToTimeStampAboutRange($data['fp_time']); $model->where('fp_time','>=',$return[0]); $model->where('fp_time','<=',$return[1]); } $list = $this->limit($model,'',$data); $list = $this->fillData($list,$data); return [true, $list]; } public function getCustomerId($data,$user){ $return = []; if(! empty($data['my_fz'])){ $info = CustomerInfo::where('del_time',0) ->where('data_id',$user['id']) ->where('type',CustomerInfo::type_two) ->select('customer_id') ->get()->toArray(); $return = array_unique(array_column($info,'customer_id')); } if(! empty($data['my_xt'])){ $info = CustomerInfo::where('del_time',0) ->where('data_id',$user['id']) ->where('type',CustomerInfo::type_three) ->select('customer_id') ->get()->toArray(); $return = array_unique(array_column($info,'customer_id')); } return $return; } /** * 客户参数规则 * @param $data * @param $is_add * @return array */ public function customerRule(&$data, $user, $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(empty($data['depart_id'])) { $data['depart_id'] = $this->getDepart($user); $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0; } 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(empty($data['car_type']) && ! empty($data['car_type_title'])){ $bool = BasicType::where('title',$data['car_type_title']) ->where('top_depart_id',$data['top_depart_id']) ->where('type',10) ->where('del_time',0) ->exists(); if($bool) return [false,'车型名称已存在']; } if($is_add){ $bool = Customer::where('del_time',0) ->where('top_depart_id',$data['top_depart_id']) ->where('title',$data['title']) // ->where('model_type',$data['model_type']) ->exists(); if(! empty($data['customer_contact'])) { $search = []; foreach ($data['customer_contact'] as $value){ $search[] = $value['info']; } $boolean = CustomerInfo::from('customer_info as a') ->join('customer as b','b.id','a.customer_id') ->where('a.del_time',0) ->where('b.del_time',0) ->where('b.top_depart_id',$data['top_depart_id']) ->whereIn('a.contact_info', $search) ->exists(); if($boolean) return [false,'客户联系内容已存在']; } }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Customer::where('del_time',0) ->where('id','<>',$data['id']) ->where('top_depart_id',$data['top_depart_id']) ->where('title',$data['title']) // ->where('model_type',$data['model_type']) ->exists(); if(! empty($data['customer_contact'])) { $search = []; foreach ($data['customer_contact'] as $value){ $search[] = $value['info']; } $boolean = CustomerInfo::from('customer_info as a') ->join('customer as b','b.id','a.customer_id') ->where('b.id','<>',$data['id']) ->where('a.del_time',0) ->where('b.del_time',0) ->where('b.top_depart_id',$data['top_depart_id']) ->whereIn('a.contact_info', $search) ->exists(); if($boolean) return [false,'客户联系内容已存在']; } } if($bool) return [false,'客户名称不能重复']; return [true, '']; } /** * 拼接数据 * @param $data * @return array */ public function fillData($data,$ergs){ if(empty($data['data'])) return $data; $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_intention'),array_column($data['data'],'customer_from'),array_column($data['data'],'customer_type'),array_column($data['data'],'car_type'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state'))); $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id'))) ->pluck('title','id') ->toArray(); $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id'))) ->pluck('emp_name','id') ->toArray(); $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product'))) ->pluck('title','id') ->toArray(); //跟进记录 $record_array = (new FollowUpRecordService())->getVisitDataOfTime(array_column($data['data'],'id'), FollowUpRecord::type_one); $customer_info = CustomerInfo::where('del_time',0) ->whereIn('customer_id',array_column($data['data'],'id')) ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three]) ->select('type','contact_type','contact_info','customer_id','data_id') ->get()->toArray(); //客户的联系方式 客户的负责人 协同人 $customer_info_map = $fz = $customer_info_map2 = $xt = []; $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id'))) ->pluck('emp_name','id') ->toArray(); $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray(); foreach ($customer_info as $value){ if($value['type'] == CustomerInfo::type_one){ $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? ""; $customer_info_map[$value['customer_id']][] = $value; if(! isset($customer_info_map2[$value['customer_id']])){ $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info']; }else{ $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info']; } }elseif($value['type'] == CustomerInfo::type_two){ $tmp = $emp_map[$value['data_id']] ?? ""; if(isset($fz[$value['customer_id']])){ $fz[$value['customer_id']] .= ',' . $tmp; }else{ $fz[$value['customer_id']] = $tmp; } }else{ $tmp = $emp_map[$value['data_id']] ?? ""; if(isset($xt[$value['customer_id']])){ $xt[$value['customer_id']] .= ',' . $tmp; }else{ $xt[$value['customer_id']] = $tmp; } } } $array2 = array_unique(array_merge_recursive(array_column($data['data'],'top_depart_id'),array_column($data['data'],'fp_top_depart_id'))); $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray(); $address_map = config('address'); foreach ($data['data'] as $key => $value){ $address_str = []; if(! empty($value['address1'])) { $tmp = json_decode($value['address1'],true); $this->findLabelsByValue($address_map,$tmp,$address_str); $tmp = implode(' ',$address_str); $tmp .= ' ' . $value['address2']; $address = $tmp; }else{ $address = $value['address2']; } $data['data'][$key]['address'] = $address; $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? ''; $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? ''; $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? ''; $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? ''; $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? ''; $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? ''; $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? ''; $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? ''; $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? ''; $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; $customer_tmp = $customer_info_map[$value['id']] ?? []; $data['data'][$key]['customer_detail'] = $customer_tmp; $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? ""; $data['data'][$key]['fz'] = $fz[$value['id']] ?? []; $record_tmp = $record_array[$value['id']] ?? ""; $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录"; $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? ""; $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title']; $data['data'][$key]['top_depart_title'] = $depart[$value['top_depart_id']] ?? ""; $data['data'][$key]['fp_top_depart_title'] = $depart[$value['fp_top_depart_id']] ?? ""; $data['data'][$key]['xt'] = $xt[$value['id']] ?? ""; } return $data; } //抢客户 public function customerGrabbing($data, $user){ $key = Customer::$limitKey . $data['customer_id']; list($status,$msg) = $this->customerGrabbingRule($data,$key); if(! $status) { //释放锁 $this->dellimitingSendRequestBackg($key); return [false,$msg]; } try { DB::beginTransaction(); $time = time(); $insert = []; //负责人获取 改为前负责人 $man = CustomerInfo::where('del_time',0) ->where('customer_id',$data['customer_id']) ->where('type', CustomerInfo::type_two) ->get()->toArray(); foreach ($man as $value){ $insert[] = [ 'customer_id' => $data['customer_id'], 'data_id' => $value['data_id'], 'type' => CustomerInfo::type_nine, 'crt_time' => $time ]; } //增加自己为负责人 $insert[] = [ 'customer_id' => $data['customer_id'], 'data_id' => $user['id'], 'type' => CustomerInfo::type_two, 'crt_time' => $time ]; //人员清空 CustomerInfo::where('del_time',0) ->where('customer_id',$data['customer_id']) ->whereIn('type', CustomerInfo::$man2) ->update(['del_time' => $time]); //写入最新人员 CustomerInfo::insert($insert); //更新公海池状态 Customer::where('id',$data['customer_id'])->update([ 'pond_state' => 0 ]); DB::commit(); }catch (\Exception $exception){ //释放锁 $this->dellimitingSendRequestBackg($key); DB::rollBack(); return [false,$exception->getMessage()]; } //释放锁 $this->dellimitingSendRequestBackg($key); return [true, '']; } public function customerGrabbingRule($data, $key){ if(empty($data['customer_id'])) return [false,'请选择客户']; //上锁 list($status,$msg) = $this->limitingSendRequestBackg($key); if(! $status) return [false,$msg]; $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first(); if(empty($customer)) return [false,'客户不存在或已被删除']; $customer = $customer->toArray(); if(empty($customer['pond_state'])) return [false,'客户已退出公海池']; return [true,'']; } public function searchBy($data,$user){ $model = BasicType::TopClear($user,$data); $result = $model->where('del_time',0) ->where('type', 2) ->where('title', 'LIKE', '%'.$data['customer_from'].'%') ->select('id') ->get()->toArray(); $model2 = Customer::Clear($user,$data); $customer = $model2->where('del_time',0) ->whereIn('customer_from', array_column($result,'id')) ->select('id') ->get()->toArray(); return array_column($customer,'id'); } }