WorkingMembersJobs.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Jobs\OpenWork\External;
  3. use App\Servers\DB\DbService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Servers\OpenWork\External\ContactService;
  10. use App\Facades\Servers\Logs\Log;
  11. use App\Models\OpenWork\job\Records;
  12. use App\Facades\Servers\Wechat\OpenWork;
  13. use App\Models\OpenWork\External\AllocationRecords as AllocationRecordsModel;
  14. use App\Models\OpenWork\External\AllocationRecords as ExternalAllocationRecordsModel;
  15. use App\Jobs\OpenWork\External\UserInheritanceJobs;
  16. /**
  17. * 在职成员客户继承队列
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-07-01
  21. */
  22. class WorkingMembersJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $contacts_data;
  26. protected ContactService $contact_service;
  27. protected $Records;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct(array $contacts_data)
  34. {
  35. //
  36. $this->contacts_data = $contacts_data;
  37. $this->contact_service = app(ContactService::class);
  38. }
  39. public function getCorpId()
  40. {
  41. return $this->contacts_data['corpid'] ?? null;
  42. }
  43. /**
  44. * Execute the job.
  45. *
  46. * @return void
  47. */
  48. public function handle()
  49. {
  50. try {
  51. $corpId = $this->contacts_data['corpid'];
  52. (new DbService())->getConnectionNameByCorpId($corpId);
  53. // 创建任务记录
  54. $this->Records = Records::create([
  55. 'job_id' => $this->contacts_data['corpid'] . '_WorkingMembersJobs',
  56. 'name' => static::class,
  57. 'payload' => json_encode($this->contacts_data),
  58. 'status' => 'processing',
  59. 'started_at' => now()
  60. ]);
  61. $ExternalAllocationRecordsModel = new ExternalAllocationRecordsModel();
  62. //查询客户信息
  63. $corpid = $this->contacts_data['corpid'];
  64. $externalUserId = $this->contacts_data['externalUserId'];
  65. $handoverUserId = $this->contacts_data['handoverUserId'];
  66. $takeoverUserId = $this->contacts_data['takeoverUserId'];
  67. $transferSuccessMessage = $this->contacts_data['transferSuccessMessage'];
  68. $execution_count = isset($this->contacts_data['execution_count']) ? $this->contacts_data['execution_count'] : 1;
  69. $operator_userid = $this->contacts_data['operator_userid'];
  70. $AllocationRecordsModel = new AllocationRecordsModel();
  71. $work = OpenWork::getWork($corpid);
  72. $result = $work->external_contact->transferCustomer($externalUserId, $handoverUserId, $takeoverUserId, $transferSuccessMessage);
  73. // 如果获取结果失败
  74. if (!$result) {
  75. Log::info('job_error', 'WorkingMembersJobs继承失败,网络错误', [$this->contacts_data]);
  76. };
  77. // 错误提示
  78. if ($result['errcode']) {
  79. if ($result['errcode'] == 45035 || $result['errcode'] == 45033) { //并发操作冲突|接口并发调用超过限制
  80. if ($execution_count > 2) {
  81. $AllocationRecordsModel->where(['external_userid' => $externalUserId, 'corpid' => $corpid])->update(['status' => 3]);
  82. Log::info('job_error', 'WorkingMembersJobs继承失败,重试了3次仍然失败', ['data' => $this->contacts_data, 'error' => $result]);
  83. return true;
  84. }
  85. $message_data = $this->contacts_data;
  86. $message_data['execution_count'] = $execution_count + 1;
  87. $seconds = 5 * $message_data['execution_count']; //每次延长5秒
  88. $this->dispatch($message_data)->delay($seconds);
  89. } else {
  90. $AllocationRecordsModel->where(['external_userid' => $externalUserId, 'corpid' => $corpid])->update(['status' => 3]);
  91. Log::info('job_error', 'WorkingMembersJobs继承失败,错误码:', ['data' => $this->contacts_data, 'error' => $result]);
  92. return true;
  93. }
  94. }
  95. $insert_data = [];
  96. foreach ($externalUserId as $key => $value) {
  97. $insert_data[] = [
  98. 'corpid' => $corpid,
  99. 'external_userid' => $value,
  100. 'original_followup_person' => $handoverUserId,
  101. 'current_followup_person' => $takeoverUserId,
  102. 'operator_userid' => $operator_userid, //操作人用户ID
  103. 'inherit_type' => '1', //1=在职继承2=离职继承
  104. 'insert_time' => time(),
  105. 'status' => '1'
  106. ];
  107. }
  108. //执行成功后,才写入数据库
  109. $ExternalAllocationRecordsModel->insert($insert_data);
  110. //延时更新在职继承状态
  111. $message_data = [
  112. 'corpid' => $corpId,
  113. 'inherit_type' => '1', //1=在职继承 2=离职继承
  114. 'handover_userid' => $handoverUserId,
  115. 'takeover_userid' => $takeoverUserId,
  116. ];
  117. UserInheritanceJobs::dispatch($message_data)->delay(86400); //延时1天执行
  118. //删除任务记录
  119. $this->Records->delete();
  120. // 成功处理...
  121. } catch (\Exception $e) {
  122. // 失败处理...
  123. if ($this->Records) {
  124. $this->Records->delete();
  125. }
  126. Log::info('job_error', 'WorkingMembersJobs任务队列执行失败', ['data' => $this->contacts_data, 'error' => $e->getMessage()]);
  127. }
  128. }
  129. public function failed(\Throwable $exception)
  130. {
  131. Log::info('job_error', 'WorkingMembersJobs任务队列执行彻底失败', ['data' => $this->contacts_data, 'error' => $exception]);
  132. // 失败处理...
  133. if ($this->Records) {
  134. $this->Records->delete();
  135. }
  136. }
  137. }