From d3ae45a360164cd2db1cb03db4ca009ca202ae11 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 3 Sep 2025 14:34:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=A4=E6=8E=A8=E6=B6=88=E6=81=AF=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/job/WorkbenchGroupPushJob.php | 132 ++++++++++-------- 1 file changed, 73 insertions(+), 59 deletions(-) diff --git a/Server/application/job/WorkbenchGroupPushJob.php b/Server/application/job/WorkbenchGroupPushJob.php index 2af080fa8..4cffc80df 100644 --- a/Server/application/job/WorkbenchGroupPushJob.php +++ b/Server/application/job/WorkbenchGroupPushJob.php @@ -58,7 +58,7 @@ class WorkbenchGroupPushJob { try { // 获取所有工作台 - $workbenches = Workbench::where(['status' => 1, 'type' => 3, 'isDel' => 0,'id' => 178])->order('id desc')->select(); + $workbenches = Workbench::where(['status' => 1, 'type' => 3, 'isDel' => 0])->order('id desc')->select(); foreach ($workbenches as $workbench) { // 获取工作台配置 $config = WorkbenchGroupPush::where('workbenchId', $workbench->id)->find(); @@ -87,7 +87,7 @@ class WorkbenchGroupPushJob } - // 发微信个人消息 + // 发微信消息 public function sendMsgToGroup($workbench, $config, $msgConf) { // 消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包(gif、其他表情包) 49:小程序/其他:图文、文件) @@ -117,6 +117,7 @@ class WorkbenchGroupPushJob } // 建立WebSocket $wsController = new WebSocketController(['userName' => $username, 'password' => $password, 'accountId' => $toAccountId]); + foreach ($msgConf as $content) { $sendData = []; $sqlData = []; @@ -293,69 +294,82 @@ class WorkbenchGroupPushJob return false; } - if ($config['pushType'] == 1) { - $limit = 10; - } else { - $limit = 1; - } + $limit = ($config['pushType'] == 1) ? 10 : 1; + $order = ($config['pushOrder'] == 1) ? 'ci.sendTime desc, ci.id asc' : 'ci.sendTime desc, ci.id desc'; + // 基础查询构建器 + $baseQuery = function() use ($workbench, $contentids) { + return Db::name('content_library')->alias('cl') + ->join('content_item ci', 'ci.libraryId = cl.id') + ->where(['cl.isDel' => 0, 'ci.isDel' => 0]) + ->where('ci.sendTime <= ' . (time() + 60)) + ->whereIn('cl.id', $contentids) + ->field('ci.id,ci.libraryId,ci.contentType,ci.title,ci.content,ci.resUrls,ci.urls,ci.comment,ci.sendTime'); + }; - //推送顺序 - if ($config['pushOrder'] == 1) { - $order = 'ci.sendTime desc, ci.id asc'; - } else { - $order = 'ci.sendTime desc, ci.id desc'; - } - - // 基础查询 - $query = Db::name('content_library')->alias('cl') - ->join('content_item ci', 'ci.libraryId = cl.id') + // 获取未发送的内容 + $unsentContent = $baseQuery() ->join('workbench_group_push_item wgpi', 'wgpi.contentId = ci.id and wgpi.workbenchId = ' . $workbench->id, 'left') - ->where(['cl.isDel' => 0, 'ci.isDel' => 0,'wgpi.isLoop' => 0]) + ->where('wgpi.id', 'null') + ->order($order) + ->limit($limit) + ->select(); + + if (!empty($unsentContent)) { + return $unsentContent; + } + + // 如果不允许循环发送,直接返回空 + if ($config['isLoop'] != 1) { + return []; + } + + // 循环发送逻辑:检查是否需要标记循环完成 + $this->checkAndResetLoop($workbench->id, $contentids); + + // 获取下一个要发送的内容(从内容库中查询,排除isLoop为0的数据) + $isPushIds = Db::name('workbench_group_push_item') + ->where(['workbenchId' => $workbench->id,'isLoop' => 0]) + ->column('contentId'); + $nextContent = $baseQuery() + ->whereNotIn('ci.id', $isPushIds) + ->group('ci.id') + ->order('ci.id asc') + ->limit($limit) + ->select(); + return $nextContent; + } + + /** + * 检查循环状态 + * @param int $workbenchId + * @param array $contentids + */ + private function checkAndResetLoop($workbenchId, $contentids) + { + // 统计总内容数 + $totalCount = Db::name('content_library')->alias('cl') + ->join('content_item ci', 'ci.libraryId = cl.id') + ->where(['cl.isDel' => 0, 'ci.isDel' => 0]) ->where('ci.sendTime <= ' . (time() + 60)) ->whereIn('cl.id', $contentids) - ->field([ - 'ci.id', - 'ci.libraryId', - 'ci.contentType', - 'ci.title', - 'ci.content', - 'ci.resUrls', - 'ci.urls', - 'ci.comment', - 'ci.sendTime' - ]); - // 复制 query - $query2 = clone $query; - $query3 = clone $query; - // 根据accountType处理不同的发送逻辑 - if ($config['isLoop'] == 1) { - // 可以循环发送 - // 1. 优先获取未发送的内容 - $unsentContent = $query->where('wgpi.id', 'null') - ->order($order) - ->limit(0, $limit) - ->select(); + ->count(); - if (!empty($unsentContent)) { - return $unsentContent; - } - $lastSendData = Db::name('workbench_group_push_item')->where('workbenchId', $workbench->id)->order('id desc')->find(); - $fastSendData = Db::name('workbench_group_push_item')->where('workbenchId', $workbench->id)->order('id asc')->find(); + // 统计已发送内容数(排除isLoop为0的数据) + $sentCount = Db::name('workbench_group_push_item') + ->alias('wgpi') + ->join('content_item ci', 'ci.id = wgpi.contentId') + ->join('content_library cl', 'cl.id = ci.libraryId') + ->where('wgpi.workbenchId', $workbenchId) + ->where('wgpi.isLoop', 0) + ->whereIn('cl.id', $contentids) + ->count('DISTINCT wgpi.contentId'); - $sentContent = $query2->where('wgpi.contentId', '<', $lastSendData['contentId'])->order('wgpi.id ASC')->group('wgpi.contentId')->limit(0, $limit)->select(); - - if (empty($sentContent)) { - $sentContent = $query3->where('wgpi.contentId', '=', $fastSendData['contentId'])->order('wgpi.id ASC')->group('wgpi.contentId')->limit(0, $limit)->select(); - } - return $sentContent; - } else { - // 不能循环发送,只获取未发送的内容 - $list = $query->where('wgpi.id', 'null') - ->order($order) - ->limit(0, $limit) - ->select(); - return $list; + // 记录循环状态 + if ($sentCount >= $totalCount) { + Db::name('workbench_group_push_item') + ->where(['workbenchId' => $workbenchId, 'isLoop' => 0]) + ->update(['isLoop' => 1]); } } @@ -408,4 +422,4 @@ class WorkbenchGroupPushJob return false; } -} \ No newline at end of file +} \ No newline at end of file