contacts_data = $contacts_data; $this->contact_service = app(ContactService::class); } public function getCorpId() { return $this->contacts_data['corpid'] ?? null; } /** * Execute the job. * * @return void */ public function handle() { try { $corpId = $this->contacts_data['corpid']; (new DbService())->getConnectionNameByCorpId($corpId); // 创建任务记录 $this->Records = Records::create([ 'job_id' => $this->contacts_data['corpid'] . '_WorkingMembersJobs', 'name' => static::class, 'payload' => json_encode($this->contacts_data), 'status' => 'processing', 'started_at' => now() ]); $ExternalAllocationRecordsModel = new ExternalAllocationRecordsModel(); //查询客户信息 $corpid = $this->contacts_data['corpid']; $externalUserId = $this->contacts_data['externalUserId']; $handoverUserId = $this->contacts_data['handoverUserId']; $takeoverUserId = $this->contacts_data['takeoverUserId']; $transferSuccessMessage = $this->contacts_data['transferSuccessMessage']; $execution_count = isset($this->contacts_data['execution_count']) ? $this->contacts_data['execution_count'] : 1; $operator_userid = $this->contacts_data['operator_userid']; $AllocationRecordsModel = new AllocationRecordsModel(); $work = OpenWork::getWork($corpid); $result = $work->external_contact->transferCustomer($externalUserId, $handoverUserId, $takeoverUserId, $transferSuccessMessage); // 如果获取结果失败 if (!$result) { Log::info('job_error', 'WorkingMembersJobs继承失败,网络错误', [$this->contacts_data]); }; // 错误提示 if ($result['errcode']) { if ($result['errcode'] == 45035 || $result['errcode'] == 45033) { //并发操作冲突|接口并发调用超过限制 if ($execution_count > 2) { $AllocationRecordsModel->where(['external_userid' => $externalUserId, 'corpid' => $corpid])->update(['status' => 3]); Log::info('job_error', 'WorkingMembersJobs继承失败,重试了3次仍然失败', ['data' => $this->contacts_data, 'error' => $result]); return true; } $message_data = $this->contacts_data; $message_data['execution_count'] = $execution_count + 1; $seconds = 5 * $message_data['execution_count']; //每次延长5秒 $this->dispatch($message_data)->delay($seconds); } else { $AllocationRecordsModel->where(['external_userid' => $externalUserId, 'corpid' => $corpid])->update(['status' => 3]); Log::info('job_error', 'WorkingMembersJobs继承失败,错误码:', ['data' => $this->contacts_data, 'error' => $result]); return true; } } $insert_data = []; foreach ($externalUserId as $key => $value) { $insert_data[] = [ 'corpid' => $corpid, 'external_userid' => $value, 'original_followup_person' => $handoverUserId, 'current_followup_person' => $takeoverUserId, 'operator_userid' => $operator_userid, //操作人用户ID 'inherit_type' => '1', //1=在职继承2=离职继承 'insert_time' => time(), 'status' => '1' ]; } //执行成功后,才写入数据库 $ExternalAllocationRecordsModel->insert($insert_data); //延时更新在职继承状态 $message_data = [ 'corpid' => $corpId, 'inherit_type' => '1', //1=在职继承 2=离职继承 'handover_userid' => $handoverUserId, 'takeover_userid' => $takeoverUserId, ]; UserInheritanceJobs::dispatch($message_data)->delay(86400); //延时1天执行 //删除任务记录 $this->Records->delete(); // 成功处理... } catch (\Exception $e) { // 失败处理... if ($this->Records) { $this->Records->delete(); } Log::info('job_error', 'WorkingMembersJobs任务队列执行失败', ['data' => $this->contacts_data, 'error' => $e->getMessage()]); } } public function failed(\Throwable $exception) { Log::info('job_error', 'WorkingMembersJobs任务队列执行彻底失败', ['data' => $this->contacts_data, 'error' => $exception]); // 失败处理... if ($this->Records) { $this->Records->delete(); } } }