代码提交

This commit is contained in:
wong
2025-09-04 10:49:22 +08:00
parent d3ae45a360
commit 3c68a603af
5 changed files with 661 additions and 162 deletions

View File

@@ -58,7 +58,7 @@ class WorkbenchGroupPushJob
{
try {
// 获取所有工作台
$workbenches = Workbench::where(['status' => 1, 'type' => 3, 'isDel' => 0])->order('id desc')->select();
$workbenches = Workbench::where(['status' => 1, 'type' => 3, 'isDel' => 0,'id' => 178])->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,7 +117,6 @@ class WorkbenchGroupPushJob
}
// 建立WebSocket
$wsController = new WebSocketController(['userName' => $username, 'password' => $password, 'accountId' => $toAccountId]);
foreach ($msgConf as $content) {
$sendData = [];
$sqlData = [];
@@ -294,82 +293,72 @@ class WorkbenchGroupPushJob
return false;
}
$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');
};
// 获取未发送的内容
$unsentContent = $baseQuery()
->join('workbench_group_push_item wgpi', 'wgpi.contentId = ci.id and wgpi.workbenchId = ' . $workbench->id, 'left')
->where('wgpi.id', 'null')
->order($order)
->limit($limit)
->select();
if (!empty($unsentContent)) {
return $unsentContent;
if ($config['pushType'] == 1) {
$limit = 10;
} else {
$limit = 1;
}
// 如果不允许循环发送,直接返回空
if ($config['isLoop'] != 1) {
return [];
//推送顺序
if ($config['pushOrder'] == 1) {
$order = 'ci.sendTime desc, ci.id asc';
} else {
$order = 'ci.sendTime desc, ci.id desc';
}
// 循环发送逻辑:检查是否需要标记循环完成
$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')
// 基础查询
$query = Db::name('content_library')->alias('cl')
->join('content_item ci', 'ci.libraryId = cl.id')
->join('workbench_group_push_item wgpi', 'wgpi.contentId = ci.id and wgpi.workbenchId = ' . $workbench->id, 'left')
->where(['cl.isDel' => 0, 'ci.isDel' => 0])
->where('ci.sendTime <= ' . (time() + 60))
->whereIn('cl.id', $contentids)
->count();
->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();
exit_data($unsentContent);
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 ($sentCount >= $totalCount) {
Db::name('workbench_group_push_item')
->where(['workbenchId' => $workbenchId, 'isLoop' => 0])
->update(['isLoop' => 1]);
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();
exit_data($list);
return $list;
}
}
@@ -422,4 +411,4 @@ class WorkbenchGroupPushJob
return false;
}
}
}