逻辑优化

This commit is contained in:
18631081161 2026-01-28 18:52:35 +08:00
parent c7da013d71
commit d46a07ffc1
3 changed files with 32 additions and 3 deletions

View File

@ -228,12 +228,14 @@
try {
const res = await getInteractCounts()
console.log('[Message] 互动统计API返回:', res)
if (res?.code === 0 && res.data) {
interactCounts.value = {
viewedMe: res.data.viewedMeCount || 0,
favoritedMe: res.data.favoritedMeCount || 0,
unlockedMe: res.data.unlockedMeCount || 0
}
console.log('[Message] 互动统计数据:', interactCounts.value)
}
} catch (error) {
console.error('加载互动统计失败:', error)

View File

@ -0,0 +1,24 @@
-- 清理 UserView 表中的重复记录
-- 保留每对用户UserId + TargetUserId的最新一条记录
-- 首先查看有多少重复记录
SELECT UserId, TargetUserId, COUNT(*) as cnt
FROM UserView
GROUP BY UserId, TargetUserId
HAVING COUNT(*) > 1;
-- 删除重复记录,保留 Id 最大的那条(即最新的)
DELETE FROM UserView
WHERE Id NOT IN (
SELECT MaxId FROM (
SELECT MAX(Id) as MaxId
FROM UserView
GROUP BY UserId, TargetUserId
) AS KeepRecords
);
-- 验证清理结果
SELECT UserId, TargetUserId, COUNT(*) as cnt
FROM UserView
GROUP BY UserId, TargetUserId
HAVING COUNT(*) > 1;

View File

@ -65,15 +65,16 @@ public class InteractService : IInteractService
var today = DateTime.Today;
// 查找今日是否已有浏览记录
// 查找是否已有该用户对目标用户的浏览记录(不按日期区分,每对用户只保留一条记录)
var existingViews = await _viewRepository.GetListAsync(v =>
v.UserId == userId && v.TargetUserId == targetUserId && v.ViewDate == today);
v.UserId == userId && v.TargetUserId == targetUserId);
var existingView = existingViews.FirstOrDefault();
if (existingView != null)
{
// 更新浏览次数和时间
existingView.ViewCount++;
existingView.ViewDate = today; // 更新为最新日期
existingView.LastViewTime = DateTime.Now;
existingView.UpdateTime = DateTime.Now;
await _viewRepository.UpdateAsync(existingView);
@ -794,8 +795,10 @@ public class InteractService : IInteractService
var lastUnlockedMeTime = user.LastUnlockedMeReadTime ?? user.CreateTime;
// 统计各类互动的新增数量
// 看过我统计首次查看时间CreateTime在上次已读之后的新用户数量
// 这样同一用户多次查看只计算一次(首次查看时)
var viewedMeCount = await _viewRepository.CountAsync(v =>
v.TargetUserId == userId && v.LastViewTime > lastViewedMeTime);
v.TargetUserId == userId && v.CreateTime > lastViewedMeTime);
var favoritedMeCount = await _favoriteRepository.CountAsync(f =>
f.TargetUserId == userId && f.CreateTime > lastFavoritedMeTime);