followUpRecordRule($data,$user,false); // if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new FollowUpRecord(); $model = $model->where('id',$data['id'])->first(); $model->data_id = $data['data_id'] ?? 0; $model->data_title = $data['data_title'] ?? ''; $model->type = $data['type'] ?? ''; $model->basic_type_id = $data['basic_type_id'] ; $model->visit_time = $data['visit_time']; $model->content = $data['content']; $model->is_remind = $data['is_remind'] ?? 0; $model->result = $data['result'] ?? ''; $model->save(); $time = time(); FollowUpRecordFile::where('del_time',0) ->where('follow_up_record_id',$data['id']) ->update(['del_time' => $time]); if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'follow_up_record_id' => $data['id'], 'file' => $value['url'], 'name' => $value['name'], 'type' => FollowUpRecordFile::type_one, 'crt_time' => $time, ]; } FollowUpRecordFile::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function followUpRecordAdd($data,$user){ list($status,$msg) = $this->followUpRecordRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new FollowUpRecord(); $model->data_id = $data['data_id'] ?? 0; $model->data_title = $data['data_title'] ?? ''; $model->type = $data['type'] ?? ''; $model->basic_type_id = $data['basic_type_id'] ; $model->visit_time = $data['visit_time']; $model->content = $data['content']; $model->is_remind = $data['is_remind'] ?? 0; $model->crt_id = $user['id']; $model->result = $data['result'] ?? ''; $model->customer_contact = $data['customer_contact'] ?? ''; $model->save(); $time = time(); $new = []; if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'follow_up_record_id' => $model->id, 'file' => $value['url'], 'name' => $value['name'], 'type' => FollowUpRecordFile::type_one, 'crt_time' => $time, ]; if(! empty($value['url'])) $new[] = $value['url']; } FollowUpRecordFile::insert($insert); } if ($data['type'] == FollowUpRecord::type_two){ if(! empty($data['customer_id'])){ // 存在客户id 就是编辑 Customer::where('id', $data['customer_id'])->update(['title' => $data['customer_title']]); if(! empty($data['customer_contact_id'])){ CustomerInfo::where('id', $data['customer_contact_id']) ->update(['contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact']]); }else{ CustomerInfo::insert([ 'customer_id' => $data['customer_id'], 'contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact'], 'type' => CustomerInfo::type_one, 'crt_time' => $time, ]); } }else{ $model = new Customer(); $model->title = $data['customer_title']; $model->model_type = $data['model_type']; $model->crt_id = $user['id']; $model->depart_id = $data['depart_id'] ?? 0; $model->top_depart_id = $data['top_depart_id'] ?? 0; $model->save(); CustomerInfo::insert([ 'customer_id' => $model->id, 'contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact'], 'type' => CustomerInfo::type_one, 'crt_time' => $time, ]); $data['customer_id'] = $model->id; } SalesOrder::where('id', $data['data_id']) ->update(['customer_id' => $data['customer_id'], 'customer_contact' => $data['customer_contact']]); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } // file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8); return [true, ['file' => ['new' => $new]]]; } public function followUpRecordDel($data){return [true, '']; if($this->isEmpty($data,'id')) return [false,'ID必须!']; try { DB::beginTransaction(); FollowUpRecord::where('id',$data['id'])->update([ 'del_time'=>time() ]); FollowUpRecordFile::where('del_time',0) ->where('follow_up_record_id', $data['id']) ->update(['del_time' => time()]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'删除成功']; } public function followUpRecordList($data,$user){ $model = FollowUpRecord::where('del_time',0) ->select('data_id','data_title','basic_type_id','visit_time','id','content','is_remind','crt_time','crt_id','type','result') ->orderBy('id','desc'); if(! empty($data['data_id'])) $model->where('data_id',$data['data_id']); if(! empty($data['basic_type_id'])) $model->where('basic_type_id', $data['basic_type_id']); if(! empty($data['crt_id'])) $model->where('crt_id',$data['crt_id']); if(! empty($data['type'])) $model->where('type',$data['type']); $list = $this->limit($model,'',$data); $list = $this->organizationData($list); return [true, $list]; } public function organizationData($data) { if (empty($data['data'])) return $data; $basic_type = BasicType::whereIn('id',array_unique(array_column($data['data'],'basic_type_id'))) ->pluck('title','id') ->toArray(); $follow_up_record_id = FollowUpRecordFile::where('del_time',0) ->where('type',FollowUpRecordFile::type_one) ->whereIn('follow_up_record_id',array_column($data['data'],'id')) ->where('file','<>','') ->select('follow_up_record_id') ->get()->toArray(); $follow_up_record_id = array_unique(array_column($follow_up_record_id,'follow_up_record_id')); foreach ($data['data'] as $key => $value){ $has_image = 0; if(in_array($value['id'], $follow_up_record_id)) $has_image = 1; $data['data'][$key]['has_image'] = $has_image; $data['data'][$key]['basic_type_name'] = $basic_type[$value['basic_type_id']] ?? ''; $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']): ''; } return $data; } public function followUpRecordRule(&$data,$user, $is_add = true){ if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空']; if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在']; if($data['type'] == FollowUpRecord::type_one){ $data['data_title'] = Customer::where('id',$data['data_id'])->value('title'); }elseif ($data['type'] == FollowUpRecord::type_two){ $data['data_title'] = SalesOrder::where('id',$data['data_id'])->value('order_number'); //所属部门 以及 顶级部门 if(empty($data['depart_id'])) { $data['depart_id'] = $this->getDepart($user); $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0; } if(empty($data['customer_title'])) return [false, '客户名称不能为空']; if(empty($data['contact_type_id'])) return [false, '客户联系类型不能为空']; if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空']; $customer_id = $data['customer_id'] ?? 0; if(empty($customer_id)){ if(empty($data['model_type'])) return [false,'客户模板类型不能为空']; if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误']; } $bool = Customer::where('del_time',0) ->when(! empty($customer_id), function ($query) use ($customer_id) { return $query->where('id', '<>',$customer_id); }) ->where('top_depart_id',$data['top_depart_id']) ->where('title', $data['customer_title']) ->exists(); if($bool) return [false,'客户名称在该门店下已存在']; $boolean = CustomerInfo::from('customer_info as a') ->join('customer as b','b.id','a.customer_id') ->when(! empty($customer_id), function ($query) use ($customer_id) { return $query->where('b.id', '<>',$customer_id); }) ->where('a.del_time',0) ->where('b.del_time',0) ->where('b.top_depart_id',$data['top_depart_id']) ->where('a.contact_info', $data['customer_contact']) ->exists(); if($boolean) return [false,'客户联系内容已存在']; } if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空']; if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空']; if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空']; if(! $is_add){ if($this->isEmpty($data,'id')) return [false,'ID不能为空!']; } $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']); return [true,'']; } public function followUpRecordDetail($data){ if($this->isEmpty($data,'id')) return [false,'ID必须!']; $record = []; $sales_info = FollowUpRecordFile::where('del_time',0) ->where('follow_up_record_id',$data['id']) ->get()->toArray(); $fileUploadService = new FileUploadService(); foreach ($sales_info as $value){ if ($value['type'] == FollowUpRecordFile::type_one){ $record[] = [ 'url' => $value['file'], 'name' => $value['name'], 'show_url' => $fileUploadService->getFileShow($value['file']), ]; } } return [true, $record]; } public function getLastVisitData($time, $type = FollowUpRecord::type_one){ $visit_time = $this->changeDateToDateMin($time); $follow_up_record = FollowUpRecord::where('del_time',0) ->where('visit_time','>=',$visit_time) ->where('type',$type) ->select('data_id') ->get()->toArray(); return array_unique(array_column($follow_up_record,'data_id')); } public function getVisitDataOfTime($data_id = [], $type = 0){ if(empty($data_id) || empty($type)) return []; $record = FollowUpRecord::where('del_time',0) ->where('type',$type) ->whereIn('data_id',$data_id) ->select('data_id',DB::raw('max(visit_time) as visit_time')) ->groupBy('data_id') ->pluck('visit_time','data_id')->toArray(); $record_array = []; if(! empty($record)){ $now = time(); foreach ($record as $key => $value){ $record_array[$key] = $this->showTimeAgo($value, $now); } } return $record_array; } public function followUpRecordBatchAdd($data,$user){ list($status,$msg) = $this->followUpRecordBatchRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $time = time(); $insert = []; foreach ($data['data_title'] as $value){ $insert[] = [ 'data_id' => $value['id'], 'data_title' => $value['order_number'], 'type' => $data['type'], 'basic_type_id' => $data['basic_type_id'], 'visit_time' => $data['visit_time'], 'content' => $data['content'], 'is_remind' => $data['is_remind'] ?? 0, 'crt_id' => $user['id'], 'crt_time' => $time, 'customer_contact' => $data['customer_contact'], ]; } FollowUpRecord::insert($insert); //订单合同id $data_id = array_column($data['data_title'],'id'); //获取上一次插入订单的所有id $last_insert_id = FollowUpRecord::whereIn('data_id', $data_id) ->where('crt_time', $time) ->where('crt_id', $user['id']) ->select('id') ->get()->toArray(); $new = []; if(! empty($data['file'])){ $insert = []; foreach ($last_insert_id as $key => $f_id){ foreach ($data['file'] as $value){ //生成oss文件数据 $tmp['origin'] = $value['url'] ?? ""; $tmp['img_list'] = []; $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $value['url']); $img = explode('.', $img); $copy = $img[0] . 'C' . $key . '.' . $img[1]; $copy = FileUploadService::string . FileUploadService::string2 . $copy; $tmp['img_list'][] = $copy; if(isset($new[$tmp['origin']])){ $new[$tmp['origin']]['img_list'][] = $copy; }else{ $new[$tmp['origin']] = $tmp; } //生成数据库记录数据 $insert[] = [ 'follow_up_record_id' => $f_id['id'], 'file' => $copy, 'type' => FollowUpRecordFile::type_one, 'name' => $value['name'], 'crt_time' => $time, ]; } } FollowUpRecordFile::insert($insert); } if ($data['type'] == FollowUpRecord::type_two){ if(! empty($data['customer_id'])){ // 存在客户id 就是编辑 Customer::where('id', $data['customer_id'])->update(['title' => $data['customer_title']]); if(! empty($data['customer_contact_id'])){ CustomerInfo::where('id', $data['customer_contact_id']) ->update(['contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact']]); }else{ CustomerInfo::insert([ 'customer_id' => $data['customer_id'], 'contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact'], 'type' => CustomerInfo::type_one, 'crt_time' => $time, ]); } }else{ $model = new Customer(); $model->title = $data['customer_title']; $model->model_type = $data['model_type']; $model->crt_id = $user['id']; $model->depart_id = $data['depart_id'] ?? 0; $model->top_depart_id = $data['top_depart_id'] ?? 0; $model->save(); CustomerInfo::insert([ 'customer_id' => $model->id, 'contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact'], 'type' => CustomerInfo::type_one, 'crt_time' => $time, ]); $data['customer_id'] = $model->id; } SalesOrder::whereIn('id', $data_id) ->update(['customer_id' => $data['customer_id'], 'customer_contact' => $data['customer_contact']]); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()]; } file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8); return [true, ['is_batch' => true, 'file' => ['new' => array_values($new)]]]; } public function followUpRecordBatchRule(&$data,$user){ if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空']; if(empty($data['type']) || $data['type'] != FollowUpRecord::type_two) return [false,'跟进类型不能为空或跟进类型非订单合同']; $data_id = explode(',', $data['data_id']); $data['data_title'] = SalesOrder::whereIn('id', $data_id) ->select('id','customer_id','order_number') ->get()->toArray(); $customerIds = array_unique(array_column($data['data_title'], 'customer_id')); $allEqual = count($customerIds) === 1; if(! $allEqual) 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(empty($data['customer_title'])) return [false, '客户名称不能为空']; if(empty($data['contact_type_id'])) return [false, '客户联系类型不能为空']; if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空']; $customer_id = $data['customer_id'] ?? 0; if(empty($customer_id)){ if(empty($data['model_type'])) return [false,'客户模板类型不能为空']; if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误']; } $bool = Customer::where('del_time',0) ->when(! empty($customer_id), function ($query) use ($customer_id) { return $query->where('id', '<>',$customer_id); }) ->where('top_depart_id',$data['top_depart_id']) ->where('title', $data['customer_title']) ->exists(); if($bool) return [false,'客户名称在该门店下已存在']; $boolean = CustomerInfo::from('customer_info as a') ->join('customer as b','b.id','a.customer_id') ->when(! empty($customer_id), function ($query) use ($customer_id) { return $query->where('b.id', '<>',$customer_id); }) ->where('a.del_time',0) ->where('b.del_time',0) ->where('b.top_depart_id',$data['top_depart_id']) ->where('a.contact_info', $data['customer_contact']) ->exists(); if($boolean) return [false,'客户联系方式已存在']; if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空']; if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空']; if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空']; $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']); return [true,'']; } }