ResignationMembersJobs.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Jobs\OpenWork\External;
  3. use App\Servers\DB\DbService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use App\Servers\OpenWork\External\ContactService;
  11. use App\Facades\Servers\Logs\Log;
  12. use App\Models\OpenWork\job\Records;
  13. use App\Facades\Servers\Wechat\OpenWork;
  14. use App\Models\OpenWork\External\AllocationRecords as AllocationRecordsModel;
  15. use App\Models\OpenWork\External\AllocationRecords as ExternalAllocationRecordsModel;
  16. /**
  17. * 离职成员客户继承队列
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-07-01
  21. */
  22. class ResignationMembersJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. private $contacts_data;
  26. private ContactService $contact_service;
  27. private $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'] . '_ResignationMembersJobs',
  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. $operator_userid = $this->contacts_data['operator_userid'];
  68. $work = OpenWork::getWork($corpid);
  69. $result = $work->external_contact->resignedTransferCustomer($externalUserId, $handoverUserId, $takeoverUserId);
  70. if (!$result || $result['errcode']) {
  71. Log::info('job_error', 'ResignationMembersJobs离职继承失败', ['data' => $this->contacts_data, 'error' => $result]);
  72. }
  73. $insert_data = [];
  74. foreach ($externalUserId as $key => $value) {
  75. $insert_data[] = [
  76. 'corpid' => $corpId,
  77. 'external_userid' => $value,
  78. 'original_followup_person' => $handoverUserId,
  79. 'current_followup_person' => $takeoverUserId,
  80. 'operator_userid' => $operator_userid,
  81. 'inherit_type' => '2', //1=在职继承2=离职继承
  82. 'insert_time' => time(),
  83. 'status' => '1'
  84. ];
  85. }
  86. //执行成功后,才写入数据库
  87. $ExternalAllocationRecordsModel->insert($insert_data);
  88. //延时更新离职继承状态
  89. $message_data = [
  90. 'corpid' => $corpId,
  91. 'inherit_type' => '2', //1=在职继承 2=离职继承
  92. 'handover_userid' => $handoverUserId,
  93. 'takeover_userid' => $takeoverUserId,
  94. ];
  95. UserInheritanceJobs::dispatch($message_data)->delay(86400); //延时1天执行
  96. //删除任务记录
  97. $this->Records->delete();
  98. // 成功处理...
  99. } catch (\Exception $e) {
  100. // 失败处理...
  101. if ($this->Records) {
  102. $this->Records->delete();
  103. }
  104. Log::info('job_error', 'ResignationMembersJobs任务队列执行失败', ['data' => $this->contacts_data, 'error' => $e->getMessage()]);
  105. }
  106. }
  107. public function failed(\Throwable $exception)
  108. {
  109. Log::info('job_error', 'ResignationMembersJobs任务队列执行彻底失败', ['data' => $this->contacts_data, 'error' => $exception->getMessage()]);
  110. // 失败处理...
  111. if ($this->Records) {
  112. $this->Records->delete();
  113. }
  114. }
  115. }