From 5d03eeecea96a310b339f7b8015c69169ffbf9f3 Mon Sep 17 00:00:00 2001 From: 18631081161 <2088094923@qq.com> Date: Thu, 29 Jan 2026 15:50:30 +0800 Subject: [PATCH] +1 --- .../src/XiangYi.Application/Services/InteractService.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/src/XiangYi.Application/Services/InteractService.cs b/server/src/XiangYi.Application/Services/InteractService.cs index 1979f4c..2e07c41 100644 --- a/server/src/XiangYi.Application/Services/InteractService.cs +++ b/server/src/XiangYi.Application/Services/InteractService.cs @@ -856,6 +856,7 @@ public class InteractService : IInteractService // 统计各类互动的新增数量 // 看过我:统计首次查看时间(CreateTime)在上次已读之后的新用户数量 // 这样同一用户多次查看只计算一次(首次查看时) + // 注意:使用 > 而不是 >= 来避免边界情况,但需要在标记已读时加1秒的缓冲 var viewedMeCount = await _viewRepository.CountAsync(v => v.TargetUserId == userId && v.CreateTime > user.LastViewedMeReadTime); @@ -882,7 +883,8 @@ public class InteractService : IInteractService throw new BusinessException(ErrorCodes.UserNotFound, "用户不存在"); } - var now = DateTime.Now; + // 加1秒缓冲,避免因时间精度问题导致刚标记已读的记录仍被统计为未读 + var now = DateTime.Now.AddSeconds(1); switch (type.ToLower()) { @@ -899,10 +901,10 @@ public class InteractService : IInteractService throw new BusinessException(ErrorCodes.InvalidParameter, "无效的互动类型"); } - user.UpdateTime = now; + user.UpdateTime = DateTime.Now; await _userRepository.UpdateAsync(user); - _logger.LogInformation("标记互动已读: UserId={UserId}, Type={Type}", userId, type); + _logger.LogInformation("标记互动已读: UserId={UserId}, Type={Type}, ReadTime={ReadTime}", userId, type, now); } #endregion