MomentsCustomerListJobs.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. use App\Models\OpenWork\Statistics\Moment\ExternalUser;
  14. use App\Jobs\OpenWork\Statistics\MomentsVisibleCustomerJobs;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * SCRM 客户朋友圈发表时选择的可见范围
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-08-08
  21. */
  22. class MomentsCustomerListJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $message_data;
  26. protected $Records;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(array $message_data)
  33. {
  34. $this->message_data = $message_data;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. try {
  44. $companyId = $this->message_data['company_id'];
  45. (new DbService())->getConnectionNameByCompanyId($companyId);
  46. // 创建任务记录
  47. $this->Records = Records::create([
  48. 'job_id' => $companyId . '_MomentsCustomerListJobs',
  49. 'name' => static::class,
  50. 'payload' => json_encode($this->message_data),
  51. 'status' => 'processing',
  52. 'started_at' => now()
  53. ]);
  54. $corpid = $this->message_data['corpid'];
  55. $momentId = $this->message_data['moment_id'];
  56. $cursor = $this->message_data['cursor'];
  57. $limit = $this->message_data['limit'];
  58. $userId = $this->message_data['userid'];
  59. $work = OpenWork::getWork($corpid);
  60. $result = $work->external_contact_moment->getCustomers($momentId, $userId, $cursor, $limit);
  61. // 如果获取失败
  62. if (!$result) {
  63. Log::info('job_error', '客户朋友圈发表时选择的可见范围任务失败',[$this->message_data]);
  64. }
  65. if (isset($result['errcode']) && $result['errcode'] > 0) {
  66. $error_message = OpenWork::getErrmsg($result['errcode']);
  67. Log::info('job_error', '客户朋友圈发表时选择的可见范围任务失败', ['data' => $this->message_data, 'error' => $error_message]);
  68. }
  69. $external_user_list = ExternalUser::where(['corpid' => $corpid, 'moment_id' => $momentId])->select(DB::raw("CONCAT(userid, '_', external_userid) as userid_external_userid"))->pluck('userid_external_userid')->toarray();
  70. $customer_list = $result['customer_list'];
  71. $insert_data = [];
  72. if (!empty($customer_list)) {
  73. foreach ($customer_list as $task) {
  74. $userid_external_userid = $userId.'_'.$task['external_userid'];
  75. if(!empty($external_user_list) && in_array($userid_external_userid,$external_user_list)){
  76. continue ;
  77. }
  78. $insert_data[] = [
  79. 'corpid' => $corpid,
  80. 'moment_id' => $momentId,
  81. 'userid' => $userId,
  82. 'external_userid' => $task['external_userid'],
  83. 'insert_time' => time(),
  84. ];
  85. }
  86. ExternalUser::insert($insert_data);
  87. }
  88. //删除任务记录
  89. $this->Records->delete();
  90. //如果存在下一页
  91. if (isset($result['next_cursor']) && $result['next_cursor'] != '') {
  92. $next_cursor = $result['next_cursor'];
  93. $this->message_data['cursor'] = $next_cursor;
  94. $this->message_data['limit'] = $limit;
  95. MomentsCustomerListJobs::dispatch($this->message_data);
  96. // MomentsCustomerListJobs::dispatchSync($this->message_data);
  97. }else{
  98. //执行完毕查询成功送达的客户
  99. $VisibleCustomer_message= ['company_id' => $companyId,'corpid' => $corpid,'moment_id' => $momentId,'userid' => $userId,'cursor' => '','limit' => 3000];
  100. MomentsVisibleCustomerJobs::dispatch($VisibleCustomer_message);
  101. // MomentsVisibleCustomerJobs::dispatchSync($VisibleCustomer_message);
  102. }
  103. } catch (\Exception $e) {
  104. // 失败处理...
  105. if ($this->Records) {
  106. $this->Records->delete();
  107. }
  108. Log::info('job_error', '客户朋友圈发表时选择的可见范围任务失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  109. }
  110. }
  111. public function failed(\Throwable $exception)
  112. {
  113. Log::info('job_error', '客户朋友圈发表时选择的可见范围任务彻底失败', ['data' => $this->message_data, 'error' => $exception]);
  114. // 失败处理...
  115. if ($this->Records) {
  116. $this->Records->delete();
  117. }
  118. }
  119. }