企微短剧业务系统

ChatGroupMassMsgSendResultRepair.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Console\Repair;
  3. use App\Log;
  4. use App\Models\ChatGroupMassMsg;
  5. use App\Models\ChatGroupMassMsgRecord;
  6. use App\Models\Es\ChatGroupMassMsgLogEs;
  7. use App\Models\Es\ChatGroupMember;
  8. use App\RedisModel;
  9. use App\Service\MassMsgCreateService;
  10. use App\Support\EmailQueue;
  11. use Illuminate\Console\Command;
  12. class ChatGroupMassMsgSendResultRepair extends Command
  13. {
  14. protected $signature = 'ChatGroupMassMsgSendResultRepair';
  15. protected $description = '获取企业客户群群发成员执行结果数据修复';
  16. protected $limit = 500;
  17. public function handle()
  18. {
  19. \DB::connection()->disableQueryLog();
  20. $this->info(date('m-d H:i:s') . ' 开始整理');
  21. try {
  22. $this->getMsgIdList();
  23. } catch (\Exception $exception) {
  24. $this->error(json_encode([
  25. 'file' => $exception->getFile(),
  26. 'line' => $exception->getLine(),
  27. 'msg' => $exception->getMessage(),
  28. ]));
  29. }
  30. $this->info(date('Y-m-d H:i:s') . ' 整理结束');
  31. }
  32. public function getMsgIdList() {
  33. $recordList = ChatGroupMassMsgRecord::query()->where('msg_id', 'msgWj4VMwAAheKRK7WKlwXpQUTG0HpWjw')
  34. ->get();
  35. foreach($recordList as $record) {
  36. $jsonArr = [
  37. 'rule_id' => $record->rule_id,
  38. 'msg_id' => $record->msg_id,
  39. 'corpid' => $record->corpid,
  40. 'sender' => $record->sender,
  41. 'exec_time' => time()
  42. ];
  43. $this->info(json_encode($jsonArr));
  44. $this->getResult($jsonArr, 0);
  45. }
  46. }
  47. private function getResult($msgRecordInfo, $isRetry=1)
  48. {
  49. try {
  50. $ruleId = $msgRecordInfo['rule_id'];
  51. $msgId = $msgRecordInfo['msg_id'];
  52. $corpid = $msgRecordInfo['corpid'];
  53. $sender = $msgRecordInfo['sender'];
  54. // 获取规则信息
  55. $massMsgInfo = ChatGroupMassMsg::query()->find($ruleId);
  56. if (empty($massMsgInfo) || empty($massMsgInfo->send_time)) {
  57. $this->error('客户群群发消息规则信息不存在');
  58. return false;
  59. }
  60. // 获取规则记录
  61. $massMsgRecordInfo = ChatGroupMassMsgRecord::getRecordInfo($ruleId, $corpid, $msgId);
  62. if(empty($massMsgRecordInfo) || (isset($massMsgRecordInfo->status) && 3 == $massMsgRecordInfo->status)) {
  63. $this->error('群发消息已经取消');
  64. return false;
  65. }
  66. # 获取群发执行结果
  67. $cursor = '';
  68. $flag = true;
  69. $page = 1;
  70. while($flag){
  71. $page++;
  72. $responseData = MassMsgCreateService::getMassMsgSendResult($corpid, $msgId, $sender, $this->limit, $cursor);
  73. if($responseData === false) { // 企业群发成员执行结果获取失败,回收
  74. // Log::logError($this->signature, array_merge(['err_msg' => '客户群群发信息执行结果数据回收失败'], $msgRecordInfo), 'script_error');
  75. // RedisModel::lPush(ChatGroupMassMsg::CHAT_GROUP_MASS_MSG_FINISH_RDS, json_encode($msgRecordInfo));
  76. $this->error('客户群群发信息执行结果数据回收失败, 数据重新塞入队列');
  77. $flag = false;
  78. continue;
  79. }
  80. if(isset($responseData['errcode']) && $responseData['errcode'] == 41093) {
  81. $this->info('群发任务已取消');
  82. # 群发任务已取消
  83. continue;
  84. }
  85. $sendData = $responseData['send_list'] ?? [];
  86. if (empty($sendData)) {
  87. // Log::logInfo($this->signature, ['err_msg' => '获取客户群群发列表为空', 'param' => $responseData, 'res' => $responseData], 'script_error');
  88. $this->error('获取客户群群发列表为空');
  89. $flag = false;
  90. continue;
  91. }
  92. # 提取群聊id
  93. $chatIdList = array_column($sendData, 'chat_id');
  94. # 批量查询群聊人数
  95. $membersCountArr = ChatGroupMember::groupMemberCount($corpid, $chatIdList);
  96. if(empty($membersCountArr)) {
  97. $this->error('查询群人数结果为空');
  98. }
  99. # 保存数据
  100. // ChatGroupMassMsgLog::saveLog($ruleId, $msgId, $sendData, $corpid, $membersCountArr);
  101. ChatGroupMassMsgLogEs::saveLog($ruleId, $msgId, $sendData, $corpid, $massMsgInfo, $membersCountArr);
  102. # 翻页查询外部联系人所有的关联员工
  103. if(isset($responseData['next_cursor']) && !empty($responseData['next_cursor'])) {
  104. $cursor = $responseData['next_cursor'];
  105. } else {
  106. $flag = false;
  107. }
  108. }
  109. if($isRetry) {
  110. # 延迟获取,避免腾讯返回数据不完整
  111. // RedisModel::lPush(ChatGroupMassMsg::CHAT_GROUP_MASS_MSG_SEND_DELAY_RDS, json_encode([
  112. // 'rule_id' => $ruleId,
  113. // 'msg_id' => $msgId,
  114. // 'corpid' => $corpid,
  115. // 'sender' => $sender,
  116. // 'exec_time' => time() + 1800
  117. // ]));
  118. $this->info('数据重新塞入队列');
  119. }
  120. } catch (\Exception $e) {
  121. // RedisModel::lPush(ChatGroupMassMsg::CHAT_GROUP_MASS_MSG_FINISH_RDS, json_encode($msgRecordInfo));
  122. $this->error(json_encode([
  123. 'message' => '数据重新塞入队列',
  124. 'file' => $e->getFile(),
  125. 'line' => $e->getLine(),
  126. 'msg' => $e->getMessage(),
  127. ]));
  128. return false;
  129. }
  130. return true;
  131. }
  132. }