fix: 旧帖子的last_reply_at也要及时更新(仅一次)

This commit is contained in:
Tim
2025-09-17 14:14:55 +08:00
parent 9306e35b84
commit f67e220894
2 changed files with 12 additions and 5 deletions

View File

@@ -83,7 +83,10 @@ public class PostMapper {
dto.setParticipants(participants.stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
LocalDateTime last = post.getLastReplyAt();
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
if (last == null) {
commentService.updatePostCommentStats(post);
}
dto.setLastReplyAt(post.getLastReplyAt());
dto.setReward(0);
dto.setSubscribed(false);
dto.setType(post.getType());

View File

@@ -346,12 +346,16 @@ public class CommentService {
/**
* Update post comment statistics (comment count and last reply time)
*/
private void updatePostCommentStats(Post post) {
public void updatePostCommentStats(Post post) {
long commentCount = commentRepository.countByPostId(post.getId());
LocalDateTime lastReplyAt = commentRepository.findLastCommentTime(post);
post.setCommentCount(commentCount);
post.setLastReplyAt(lastReplyAt);
LocalDateTime lastReplyAt = commentRepository.findLastCommentTime(post);
if (lastReplyAt == null) {
post.setLastReplyAt(post.getCreatedAt());
} else {
post.setLastReplyAt(lastReplyAt);
}
postRepository.save(post);
log.debug("Updated post {} stats: commentCount={}, lastReplyAt={}",