From 190fb95bb610571d05827da0570f12549a5f23ef Mon Sep 17 00:00:00 2001 From: ziin Date: Wed, 4 Feb 2026 18:27:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(comment):=20=E6=96=B0=E5=A2=9E=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E7=9B=AE=E6=A0=87=E7=94=A8=E6=88=B7ID=E4=B8=8E?= =?UTF-8?q?=E6=98=B5=E7=A7=B0=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yolo/keyborad/model/vo/CommentVO.java | 6 ++ ...KeyboardAiCompanionCommentServiceImpl.java | 55 +++++++++++-------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/yolo/keyborad/model/vo/CommentVO.java b/src/main/java/com/yolo/keyborad/model/vo/CommentVO.java index c9a48d8..85bf8a0 100644 --- a/src/main/java/com/yolo/keyborad/model/vo/CommentVO.java +++ b/src/main/java/com/yolo/keyborad/model/vo/CommentVO.java @@ -32,6 +32,12 @@ public class CommentVO { @Schema(description = "父评论ID") private Long parentId; + @Schema(description = "回复给的用户ID(仅回复评论有值)") + private Long replyToUserId; + + @Schema(description = "回复给的用户昵称(仅回复评论有值)") + private String replyToUserName; + @Schema(description = "根评论ID") private Long rootId; diff --git a/src/main/java/com/yolo/keyborad/service/impl/KeyboardAiCompanionCommentServiceImpl.java b/src/main/java/com/yolo/keyborad/service/impl/KeyboardAiCompanionCommentServiceImpl.java index f2aed85..e50262f 100644 --- a/src/main/java/com/yolo/keyborad/service/impl/KeyboardAiCompanionCommentServiceImpl.java +++ b/src/main/java/com/yolo/keyborad/service/impl/KeyboardAiCompanionCommentServiceImpl.java @@ -14,6 +14,7 @@ import com.yolo.keyborad.service.UserService; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; +import java.util.HashMap; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -74,24 +75,10 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl finalUserMap = userMap; + Map commentById = entityPage.getRecords().stream() + .collect(Collectors.toMap(KeyboardAiCompanionComment::getId, c -> c)); return entityPage.convert(entity -> { - CommentVO vo = new CommentVO(); - vo.setId(entity.getId()); - vo.setCompanionId(entity.getCompanionId()); - vo.setUserId(entity.getUserId()); - vo.setParentId(entity.getParentId()); - vo.setRootId(entity.getRootId()); - vo.setContent(entity.getContent()); - vo.setLikeCount(entity.getLikeCount()); - vo.setCreatedAt(entity.getCreatedAt()); - - // 填充用户信息 - KeyboardUser user = finalUserMap.get(entity.getUserId()); - if (user != null) { - vo.setUserName(user.getNickName()); - vo.setUserAvatar(user.getAvatarUrl()); - } - return vo; + return convertToVO(entity, finalUserMap, null, commentById); }); } @@ -113,13 +100,14 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl> repliesMap = Map.of(); Map replyCountMap = Map.of(); + List allReplies = List.of(); if (!topCommentIds.isEmpty()) { // 查询所有回复 LambdaQueryWrapper replyWrapper = new LambdaQueryWrapper<>(); replyWrapper.in(KeyboardAiCompanionComment::getRootId, topCommentIds) .eq(KeyboardAiCompanionComment::getStatus, 1) .orderByAsc(KeyboardAiCompanionComment::getCreatedAt); - List allReplies = this.list(replyWrapper); + allReplies = this.list(replyWrapper); // 按rootId分组,每组取前3条 repliesMap = allReplies.stream() @@ -137,6 +125,11 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl comment + Map commentById = new HashMap<>(); + entityPage.getRecords().forEach(c -> commentById.put(c.getId(), c)); + allReplies.forEach(c -> commentById.put(c.getId(), c)); + // 收集所有需要查询的用户ID(一级评论 + 回复) List userIds = new ArrayList<>(entityPage.getRecords().stream() .map(KeyboardAiCompanionComment::getUserId) @@ -171,12 +164,12 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl finalReplyCountMap = replyCountMap; return entityPage.convert(entity -> { - CommentVO vo = convertToVO(entity, finalUserMap, likedCommentIds); + CommentVO vo = convertToVO(entity, finalUserMap, likedCommentIds, commentById); // 填充回复列表 List replies = finalRepliesMap.getOrDefault(entity.getId(), List.of()); List replyVOs = replies.stream() - .map(reply -> convertToVO(reply, finalUserMap, likedCommentIds)) + .map(reply -> convertToVO(reply, finalUserMap, likedCommentIds, commentById)) .collect(Collectors.toList()); vo.setReplies(replyVOs); vo.setReplyCount(finalReplyCountMap.getOrDefault(entity.getId(), 0L).intValue()); @@ -188,7 +181,12 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl userMap, Set likedCommentIds) { + private CommentVO convertToVO( + KeyboardAiCompanionComment entity, + Map userMap, + Set likedCommentIds, + Map commentById + ) { CommentVO vo = new CommentVO(); vo.setId(entity.getId()); vo.setCompanionId(entity.getCompanionId()); @@ -198,7 +196,7 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl