This commit is contained in:
wong
2026-01-12 14:38:29 +08:00
parent cd38e0eedd
commit 80ae205cb3

View File

@@ -41,6 +41,37 @@ class MessageController extends BaseController
->field('id')
->buildSql();
// 计算总数:群聊数量 + 好友数量
// 使用与 UNION 查询相同的逻辑来计算总数
$countQuery = "
SELECT COUNT(*) as total FROM (
(SELECT DISTINCT wc.id
FROM s2_wechat_chatroom wc
INNER JOIN s2_wechat_message m ON wc.id = m.wechatChatroomId AND m.type = 2
INNER JOIN (
SELECT wechatChatroomId, MAX(wechatTime) as maxTime, MAX(id) as maxId
FROM s2_wechat_message
WHERE type = 2
GROUP BY wechatChatroomId
) latest ON m.wechatChatroomId = latest.wechatChatroomId AND m.wechatTime = latest.maxTime AND m.id = latest.maxId
WHERE wc.accountId = {$accountId} AND wc.isDeleted = 0
)
UNION ALL
(SELECT DISTINCT m.wechatFriendId as id
FROM s2_wechat_message m
INNER JOIN (
SELECT wechatFriendId, MAX(wechatTime) as maxTime, MAX(id) as maxId
FROM s2_wechat_message
WHERE type = 1 AND wechatFriendId IN {$friendSubQuery}
GROUP BY wechatFriendId
) latest ON m.wechatFriendId = latest.wechatFriendId AND m.wechatTime = latest.maxTime AND m.id = latest.maxId
WHERE m.type = 1 AND m.wechatFriendId IN {$friendSubQuery}
)
) as combined
";
$totalResult = Db::query($countQuery);
$total = !empty($totalResult) ? (int)$totalResult[0]['total'] : 0;
// 优化后的查询使用MySQL兼容的查询方式
$unionQuery = "
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime,m.wechatAccountId, 2 as msgType, wc.nickname, wc.chatroomAvatar as avatar, wc.chatroomId, wc.isTop
@@ -166,7 +197,7 @@ class MessageController extends BaseController
}
unset($v);
return ResponseHelper::success($list);
return ResponseHelper::success(['list' => $list, 'total' => $total]);
}