where('type',BasicType::type_10) ->select('id','title') ->get()->toArray(); \App\Model\CustomerFromThreePlatForm::where("del_time",0) ->where("customerName","<>","") ->orderBy('id') ->select('id','crt_time','city','actionType','acquireTime','clueType','product','wechat','clueId','transformType','clueSource','clueSourceId','customerName','platform','ipLocationCity','phone','isRepeatClue','flowType','fromUserId') ->chunkById(20, function ($data) use($basic){ // 开启事务 DB::transaction(function () use ($data,$basic) { $dataArray = $data->toArray(); $nowStamp = time(); list($main, $main_detail, $fail) = $this->processingData($dataArray, $nowStamp,$basic); $c_third_platform_id = array_column($main,'c_third_platform_id'); if (! empty($main) ) { Customer::insert($main); $map = Customer::whereIn('c_third_platform_id',$c_third_platform_id) ->pluck('id','c_third_platform_id') ->toArray(); $insert_detail = []; foreach ($main_detail as $c_third_platform_id => $value){ if(isset($map[$c_third_platform_id])){ foreach ($value as $v){ $v['customer_id'] = $map[$c_third_platform_id]; $insert_detail[] = $v; } } } if(! empty($insert_detail)) CustomerInfo::insert($insert_detail); echo '更新完成' . PHP_EOL; } if(! empty($fail)) { foreach ($fail as $update){ $customer_id = $map[$c_third_platform_id] ?? 0; \App\Model\CustomerFromThreePlatForm::where('id', $update['id']) ->update(['del_time' => $update['result'] , 'customer_id' => $customer_id]); } } }); }); echo '任务结束--------end---------------'; } private function processingData2($data,$time, $basic,$top_depart_id){ $return = $return1 = $return2 = []; $search = $info_phone = $info_wechat = []; foreach ($data as $value){ //同样的客户信息只保留最后一次的id if(! empty($value['phone'])) { $info_phone[$value['phone']] = $value['id']; $search[] = $value['phone']; } if(! empty($value['wechat'])) { $info_wechat[$value['wechat']] = $value['id']; $search[] = $value['wechat']; } } $contact = 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',$top_depart_id) ->whereIn('a.contact_info', $search) ->pluck('a.contact_info') ->toArray(); //result 1 联系方式数据库中已存在失败 2 联系方式重复传递失败 3 foreach ($data as $value){ if(! empty($value['phone'])){ if(in_array($value['phone'], $contact)){ $return2[] = [ 'id' => $value['id'], 'result' => 1 ]; }else{ $tmp_id = $info_phone[$value['phone']] ?? 0; if($tmp_id != $value['id']){ $return2[] = [ 'id' => $value['id'], 'result' => 2 ]; } } } if(! empty($value['wechat']) && in_array($value['wechat'], $contact)){ $return2[] = [ 'id' => $value['id'], 'result' => 1 ]; continue; } } } private function processingData($data, $time,$basic) { $return = []; // 成功数据 $return_detail = []; // 成功数据 $fail = []; // 失败数据(含 result 标记) $basic_map = array_column($basic,'title','id'); $info_phone = []; $info_wechat = []; $search = []; // 第一步:同批次去重(保留最后一次 ID) foreach ($data as $value) { if (!empty($value['phone'])) { $info_phone[$value['phone']] = $value['id']; $search[] = $value['phone']; } if (!empty($value['wechat'])) { $info_wechat[$value['wechat']] = $value['id']; $search[] = $value['wechat']; } } // 第二步:数据库中已存在的联系方式 $contact = 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', 2) ->whereIn('a.contact_info', $search) ->pluck('a.contact_info') ->toArray(); $contact = array_flip($contact); // 方便 in_array 变成 isset 检查 // 第三步:逐条判断 foreach ($data as $value) { $id = $value['id']; $isFail = false; // 检查手机号 if (!empty($value['phone'])) { if (isset($contact[$value['phone']])) { // 数据库已存在 $fail[] = ['id' => $id, 'result' => 1]; $isFail = true; } elseif (($info_phone[$value['phone']] ?? 0) !== $id) { // 同批重复 $fail[] = ['id' => $id, 'result' => 2]; $isFail = true; } } // 检查微信号(如果之前手机号已 fail,这里也可以继续检查,因为两者可能不同) if (!$isFail && !empty($value['wechat'])) { if (isset($contact[$value['wechat']])) { $fail[] = ['id' => $id, 'result' => 1]; $isFail = true; } elseif (($info_wechat[$value['wechat']] ?? 0) !== $id) { $fail[] = ['id' => $id, 'result' => 2]; $isFail = true; } } // 成功数据 if (! $isFail) { $fail[] = ['id' => $id, 'result' => 3]; $car_type = 0; if(! empty($value['product'])){ $t = explode('/', $value['product']); foreach ($t as $t_v){ if($car_type) continue; if(isset($basic_map[$t_v])) $car_type = $basic_map[$t_v]; } } $customer_from = 0; if($value['platform'] == 1){ $customer_from = 17190;//抖音DY }elseif($value['platform'] == 2){ $customer_from = 17202;//快手KS }elseif ($value['platform'] == 5){ $customer_from = 17195;//公众号GZH } $enter_time = $time; if(! empty($value['acquireTime'])) $enter_time = bcdiv($value['acquireTime'],1000); $return[] = [ 'c_third_platform_id' => $id, 'title' => $value['customerName'], 'model_type' => Customer::Model_type_one, 'progress_stage' => 9315, //A后台私信 'car_type' => $car_type, 'customer_from' => $customer_from, 'address2' => ! empty($value['city']) ? $value['city'] : $value['ipLocationCity'], 'depart_id' => 2, 'top_depart_id' => 2, 'crt_id' => 1, 'crt_time' => $time, 'upd_time' => $time, 'enter_time' => $enter_time, ]; if(! empty($value['phone'])){ $return_detail[$id][] = [ 'customer_id' => 0, 'contact_type' => 133, 'contact_info' => $value['phone'], 'crt_time' => $time, 'type' => CustomerInfo::type_one ]; } if(! empty($value['wechat'])){ $return_detail[$id][] = [ 'customer_id' => 0, 'contact_type' => 135, 'contact_info' => $value['wechat'], 'crt_time' => $time, 'type' => CustomerInfo::type_one ]; } } } return [$return, $return_detail, $fail]; } }