MembersSendResultsInGroupsJobs.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Jobs\OpenWork\Statistics;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use App\Servers\DB\DbService;
  9. use App\Models\OpenWork\job\Records;
  10. use App\Facades\Servers\Logs\Log;
  11. use App\Facades\Servers\Wechat\OpenWork;
  12. use App\Models\OpenWork\Msg\TemplateResult as TemplateResultModel;
  13. use App\Servers\OpenWork\External\ContactService;
  14. /**
  15. * SCRM 客户群发成员执行结果
  16. * @author 唐远望
  17. * @version 1.0
  18. * @date 2025-08-12
  19. */
  20. class MembersSendResultsInGroupsJobs implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. protected $message_data;
  24. protected $Records;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(array $message_data)
  31. {
  32. $this->message_data = $message_data;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. try {
  42. $companyId = $this->message_data['company_id'];
  43. // 切换数据库
  44. (new DbService())->getConnectionNameByCompanyId($companyId);
  45. // 创建任务记录
  46. $this->Records = Records::create([
  47. 'job_id' => $companyId . '_MembersSendResultsInGroupsJobs',
  48. 'name' => static::class,
  49. 'payload' => json_encode($this->message_data),
  50. 'status' => 'processing',
  51. 'started_at' => now()
  52. ]);
  53. $corpid = $this->message_data['corpid'];
  54. $limit = $this->message_data['limit'];
  55. $msgId = $this->message_data['msg_id'];
  56. $cursor = $this->message_data['cursor'];
  57. $userid = $this->message_data['userid'];
  58. $tplId = $this->message_data['tpl_id'];
  59. // 同步获取企业群发成员执行结果
  60. $work = OpenWork::getWork($corpid);
  61. $result = $work->external_contact_message->getGroupmsgSendResult($msgId, $userid, $limit, $cursor);
  62. // 如果获取失败
  63. if (!$result) Log::info('job_error', '获取企业群发成员执行结果任务失败',[$this->message_data]);
  64. // 如果有错误
  65. if ( !empty($result['errcode']) ) Log::info('job_error', '获取企业群发成员执行结果任务失败', ['data' => $this->message_data, 'error' => OpenWork::getErrmsg($result['errcode'])]);
  66. // 有列表查询
  67. if (!empty($result['send_list'])) {
  68. // 发送成员执行结果
  69. $sendList = $result['send_list'];
  70. // 接收者ID,//external_userid 外部联系人userid,群发消息到企业的客户群不返回该字段,chat_id 外部客户群id,群发消息到客户不返回该字段
  71. $receiveIds = isset($sendList[0]['external_userid']) ? array_column($sendList, 'external_userid') : array_column($sendList, 'chat_id');
  72. //获取本地群发成员任务列表
  73. $localMsgList = TemplateResultModel::where(['tpl_id'=>$tplId,'msgid'=>$msgId])->whereIn('receive_id',$receiveIds)->select(['id','status','receive_id'])->get()->toArray();
  74. // 换格式
  75. $localMsgList = $localMsgList ? array_column($localMsgList, null,'receive_id') : [];
  76. // 明文ID转换成明文ID服务,并获取本地群发成员任务列表,并更新
  77. $ContactService = new ContactService();
  78. // 要新增的数据
  79. $insertData = [];
  80. // 循环发送信息
  81. foreach ($sendList as $task) {
  82. //处理成员明文ID
  83. if (isset($task['userid'])) {
  84. $open_userid_info = $ContactService->get_external_contact_batchget_user_list_by_data($corpid, [$task['userid']]);
  85. $task['userid'] = !empty($open_userid_info) ? $open_userid_info[0] : $task['userid'];
  86. }
  87. //外部联系人userid or 外部客户群id
  88. $receiveId = isset($task['external_userid']) ? $task['external_userid'] : ($task['chat_id'] ? $task['chat_id'] : '');
  89. // 发送时间
  90. $sendTime = isset($task['send_time']) ? $task['send_time'] : 0;
  91. // 如果存在接受者的数据
  92. if( isset($localMsgList[$receiveId]) ) {
  93. // 判断状态是不是一致
  94. if( $localMsgList[$receiveId]['status'] != $task['status'] ){
  95. // 更新数据
  96. TemplateResultModel::where(['id'=>$localMsgList[$receiveId]['id']])->update(['status'=>$task['status'],'send_time'=>$sendTime]);
  97. }
  98. }else{
  99. $insertData[]= [
  100. 'tpl_id' => $tplId,
  101. 'msgid' => $msgId,
  102. 'userid' => $task['userid'],
  103. 'receive_id' => $receiveId,
  104. 'status' => $task['status'], // 0-未发送 1-已发送 2-因客户不是好友导致发送失败 3-因客户已经收到其他群发消息导致发送失败
  105. 'send_time' => $sendTime,
  106. 'insert_time' => time()
  107. ];
  108. }
  109. }
  110. // 统一写入
  111. TemplateResultModel::insert($insertData);
  112. }
  113. //删除任务记录
  114. $this->Records->delete();
  115. //如果存在下一页,继续执行
  116. if (isset($result['next_cursor']) && $result['next_cursor'] !='') {
  117. $next_cursor = $result['next_cursor'];
  118. $next_message_data = $this->message_data;
  119. $next_message_data['cursor'] = $next_cursor;
  120. // 下一队列
  121. MembersSendResultsInGroupsJobs::dispatch($next_message_data);
  122. }
  123. } catch (\Exception $e) {
  124. // 失败处理...
  125. if ($this->Records) {
  126. $this->Records->delete();
  127. }
  128. Log::info('job_error', '同步获取企业群发成员执行结果任务失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  129. }
  130. }
  131. public function failed(\Throwable $exception)
  132. {
  133. Log::info('job_error', '同步获取企业群发成员执行结果任务彻底失败', ['data' => $this->message_data, 'error' => $exception]);
  134. // 失败处理...
  135. if ($this->Records) {
  136. $this->Records->delete();
  137. }
  138. }
  139. }