CustomerGroupSendingListJobs.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Jobs\OpenWork\Statistics;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  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\DB\DbService;
  10. use App\Models\OpenWork\job\Records;
  11. use App\Facades\Servers\Logs\Log;
  12. use App\Facades\Servers\Wechat\OpenWork;
  13. /**
  14. * SCRM 客户群发数据任务
  15. * @author 唐远望
  16. * @version 1.0
  17. * @date 2025-08-12
  18. */
  19. class CustomerGroupSendingListJobs implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $message_data;
  23. protected $Records;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(array $message_data)
  30. {
  31. $this->message_data = $message_data;
  32. }
  33. /**
  34. * Execute the job.
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. try {
  41. $companyId = $this->message_data['company_id'];
  42. (new DbService())->getConnectionNameByCompanyId($companyId);
  43. // 创建任务记录
  44. $this->Records = Records::create([
  45. 'job_id' => $companyId . '_MomentsCustomerInteractionJobs',
  46. 'name' => static::class,
  47. 'payload' => json_encode($this->message_data),
  48. 'status' => 'processing',
  49. 'started_at' => now()
  50. ]);
  51. $chatType = $this->message_data['chat_type'];
  52. $corpid = $this->message_data['corpid'];
  53. $userId = $this->message_data['userid'];
  54. $startTime = $this->message_data['start_time'];
  55. $endTime = $this->message_data['end_time'];
  56. $msgId = $this->message_data['msg_id'];
  57. $limit = $this->message_data['limit'];
  58. $cursor = $this->message_data['cursor'];
  59. // 同步获取群发记录列表
  60. $work = OpenWork::getWork($corpid);
  61. $result = $work->external_contact_message->getGroupmsgListV2($chatType, $startTime, $endTime, $msgId, $limit, $cursor, $userId);
  62. // 如果获取失败
  63. if (!$result) {
  64. Log::info('job_error', '获取客户群发数据失败', ['data' => $this->message_data]);
  65. }
  66. if (isset($result['errcode']) && $result['errcode'] > 0) {
  67. $error_message = OpenWork::getErrmsg($result['errcode']);
  68. Log::info('job_error', '获取客户群发数据失败', ['data' => $this->message_data, 'error' => $error_message]);
  69. }
  70. //如果存在下一页,继续执行
  71. if (isset($result['next_cursor'])) {
  72. $next_cursor = $result['next_cursor'];
  73. $next_message_data = $this->message_data;
  74. $next_message_data['cursor'] = $next_cursor;
  75. CustomerGroupSendingListJobs::dispatch($next_message_data);
  76. }
  77. //删除任务记录
  78. $this->Records->delete();
  79. } catch (\Exception $e) {
  80. // 失败处理...
  81. if ($this->Records) {
  82. $this->Records->delete();
  83. }
  84. Log::info('job_error', '同步客户群发数据失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  85. }
  86. }
  87. public function failed(\Throwable $exception)
  88. {
  89. Log::info('job_error', '同步客户群发数据彻底失败', ['data' => $this->message_data, 'error' => $exception]);
  90. // 失败处理...
  91. if ($this->Records) {
  92. $this->Records->delete();
  93. }
  94. }
  95. }