123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Console\Repair;
- use App\Log;
- use App\Models\ChatGroupMassMsg;
- use App\Models\ChatGroupMassMsgRecord;
- use App\Models\Es\ChatGroupMassMsgLogEs;
- use App\Models\Es\ChatGroupMember;
- use App\RedisModel;
- use App\Service\MassMsgCreateService;
- use App\Support\EmailQueue;
- use Illuminate\Console\Command;
- class ChatGroupMassMsgSendResultRepair extends Command
- {
- protected $signature = 'ChatGroupMassMsgSendResultRepair';
- protected $description = '获取企业客户群群发成员执行结果数据修复';
- protected $limit = 500;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->info(date('m-d H:i:s') . ' 开始整理');
- try {
- $this->getMsgIdList();
- } catch (\Exception $exception) {
- $this->error(json_encode([
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'msg' => $exception->getMessage(),
- ]));
- }
- $this->info(date('Y-m-d H:i:s') . ' 整理结束');
- }
- public function getMsgIdList() {
- $recordList = ChatGroupMassMsgRecord::query()->where('msg_id', 'msgWj4VMwAAheKRK7WKlwXpQUTG0HpWjw')
- ->get();
- foreach($recordList as $record) {
- $jsonArr = [
- 'rule_id' => $record->rule_id,
- 'msg_id' => $record->msg_id,
- 'corpid' => $record->corpid,
- 'sender' => $record->sender,
- 'exec_time' => time()
- ];
- $this->info(json_encode($jsonArr));
- $this->getResult($jsonArr, 0);
- }
- }
- private function getResult($msgRecordInfo, $isRetry=1)
- {
- try {
- $ruleId = $msgRecordInfo['rule_id'];
- $msgId = $msgRecordInfo['msg_id'];
- $corpid = $msgRecordInfo['corpid'];
- $sender = $msgRecordInfo['sender'];
- // 获取规则信息
- $massMsgInfo = ChatGroupMassMsg::query()->find($ruleId);
- if (empty($massMsgInfo) || empty($massMsgInfo->send_time)) {
- $this->error('客户群群发消息规则信息不存在');
- return false;
- }
- // 获取规则记录
- $massMsgRecordInfo = ChatGroupMassMsgRecord::getRecordInfo($ruleId, $corpid, $msgId);
- if(empty($massMsgRecordInfo) || (isset($massMsgRecordInfo->status) && 3 == $massMsgRecordInfo->status)) {
- $this->error('群发消息已经取消');
- return false;
- }
- # 获取群发执行结果
- $cursor = '';
- $flag = true;
- $page = 1;
- while($flag){
- $page++;
- $responseData = MassMsgCreateService::getMassMsgSendResult($corpid, $msgId, $sender, $this->limit, $cursor);
- if($responseData === false) { // 企业群发成员执行结果获取失败,回收
- // Log::logError($this->signature, array_merge(['err_msg' => '客户群群发信息执行结果数据回收失败'], $msgRecordInfo), 'script_error');
- // RedisModel::lPush(ChatGroupMassMsg::CHAT_GROUP_MASS_MSG_FINISH_RDS, json_encode($msgRecordInfo));
- $this->error('客户群群发信息执行结果数据回收失败, 数据重新塞入队列');
- $flag = false;
- continue;
- }
- if(isset($responseData['errcode']) && $responseData['errcode'] == 41093) {
- $this->info('群发任务已取消');
- # 群发任务已取消
- continue;
- }
- $sendData = $responseData['send_list'] ?? [];
- if (empty($sendData)) {
- // Log::logInfo($this->signature, ['err_msg' => '获取客户群群发列表为空', 'param' => $responseData, 'res' => $responseData], 'script_error');
- $this->error('获取客户群群发列表为空');
- $flag = false;
- continue;
- }
- # 提取群聊id
- $chatIdList = array_column($sendData, 'chat_id');
- # 批量查询群聊人数
- $membersCountArr = ChatGroupMember::groupMemberCount($corpid, $chatIdList);
- if(empty($membersCountArr)) {
- $this->error('查询群人数结果为空');
- }
- # 保存数据
- // ChatGroupMassMsgLog::saveLog($ruleId, $msgId, $sendData, $corpid, $membersCountArr);
- ChatGroupMassMsgLogEs::saveLog($ruleId, $msgId, $sendData, $corpid, $massMsgInfo, $membersCountArr);
- # 翻页查询外部联系人所有的关联员工
- if(isset($responseData['next_cursor']) && !empty($responseData['next_cursor'])) {
- $cursor = $responseData['next_cursor'];
- } else {
- $flag = false;
- }
- }
- if($isRetry) {
- # 延迟获取,避免腾讯返回数据不完整
- // RedisModel::lPush(ChatGroupMassMsg::CHAT_GROUP_MASS_MSG_SEND_DELAY_RDS, json_encode([
- // 'rule_id' => $ruleId,
- // 'msg_id' => $msgId,
- // 'corpid' => $corpid,
- // 'sender' => $sender,
- // 'exec_time' => time() + 1800
- // ]));
- $this->info('数据重新塞入队列');
- }
- } catch (\Exception $e) {
- // RedisModel::lPush(ChatGroupMassMsg::CHAT_GROUP_MASS_MSG_FINISH_RDS, json_encode($msgRecordInfo));
- $this->error(json_encode([
- 'message' => '数据重新塞入队列',
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- ]));
- return false;
- }
- return true;
- }
- }
|