| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace App\Jobs\OpenWork\External;
- use App\Servers\DB\DbService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use App\Servers\OpenWork\External\ContactService;
- use App\Facades\Servers\Logs\Log;
- use App\Models\OpenWork\job\Records;
- use App\Facades\Servers\Wechat\OpenWork;
- use App\Models\OpenWork\External\AllocationRecords as AllocationRecordsModel;
- use App\Models\OpenWork\External\AllocationRecords as ExternalAllocationRecordsModel;
- use App\Jobs\OpenWork\External\UserInheritanceJobs;
- /**
- * 在职成员客户继承队列
- * @author 唐远望
- * @version 1.0
- * @date 2025-07-01
- */
- class WorkingMembersJobs implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $contacts_data;
- protected ContactService $contact_service;
- protected $Records;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct(array $contacts_data)
- {
- //
- $this->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();
- }
- }
- }
|