feat(comment): 新增回复目标用户ID与昵称字段
This commit is contained in:
@@ -32,6 +32,12 @@ public class CommentVO {
|
|||||||
@Schema(description = "父评论ID")
|
@Schema(description = "父评论ID")
|
||||||
private Long parentId;
|
private Long parentId;
|
||||||
|
|
||||||
|
@Schema(description = "回复给的用户ID(仅回复评论有值)")
|
||||||
|
private Long replyToUserId;
|
||||||
|
|
||||||
|
@Schema(description = "回复给的用户昵称(仅回复评论有值)")
|
||||||
|
private String replyToUserName;
|
||||||
|
|
||||||
@Schema(description = "根评论ID")
|
@Schema(description = "根评论ID")
|
||||||
private Long rootId;
|
private Long rootId;
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import com.yolo.keyborad.service.UserService;
|
|||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -74,24 +75,10 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
|
|
||||||
// 转换为VO
|
// 转换为VO
|
||||||
Map<Long, KeyboardUser> finalUserMap = userMap;
|
Map<Long, KeyboardUser> finalUserMap = userMap;
|
||||||
|
Map<Long, KeyboardAiCompanionComment> commentById = entityPage.getRecords().stream()
|
||||||
|
.collect(Collectors.toMap(KeyboardAiCompanionComment::getId, c -> c));
|
||||||
return entityPage.convert(entity -> {
|
return entityPage.convert(entity -> {
|
||||||
CommentVO vo = new CommentVO();
|
return convertToVO(entity, finalUserMap, null, commentById);
|
||||||
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;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,13 +100,14 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
// 批量查询回复(每条一级评论取前3条回复)
|
// 批量查询回复(每条一级评论取前3条回复)
|
||||||
Map<Long, List<KeyboardAiCompanionComment>> repliesMap = Map.of();
|
Map<Long, List<KeyboardAiCompanionComment>> repliesMap = Map.of();
|
||||||
Map<Long, Long> replyCountMap = Map.of();
|
Map<Long, Long> replyCountMap = Map.of();
|
||||||
|
List<KeyboardAiCompanionComment> allReplies = List.of();
|
||||||
if (!topCommentIds.isEmpty()) {
|
if (!topCommentIds.isEmpty()) {
|
||||||
// 查询所有回复
|
// 查询所有回复
|
||||||
LambdaQueryWrapper<KeyboardAiCompanionComment> replyWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<KeyboardAiCompanionComment> replyWrapper = new LambdaQueryWrapper<>();
|
||||||
replyWrapper.in(KeyboardAiCompanionComment::getRootId, topCommentIds)
|
replyWrapper.in(KeyboardAiCompanionComment::getRootId, topCommentIds)
|
||||||
.eq(KeyboardAiCompanionComment::getStatus, 1)
|
.eq(KeyboardAiCompanionComment::getStatus, 1)
|
||||||
.orderByAsc(KeyboardAiCompanionComment::getCreatedAt);
|
.orderByAsc(KeyboardAiCompanionComment::getCreatedAt);
|
||||||
List<KeyboardAiCompanionComment> allReplies = this.list(replyWrapper);
|
allReplies = this.list(replyWrapper);
|
||||||
|
|
||||||
// 按rootId分组,每组取前3条
|
// 按rootId分组,每组取前3条
|
||||||
repliesMap = allReplies.stream()
|
repliesMap = allReplies.stream()
|
||||||
@@ -137,6 +125,11 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 用于“回复给谁”的映射:commentId -> comment
|
||||||
|
Map<Long, KeyboardAiCompanionComment> commentById = new HashMap<>();
|
||||||
|
entityPage.getRecords().forEach(c -> commentById.put(c.getId(), c));
|
||||||
|
allReplies.forEach(c -> commentById.put(c.getId(), c));
|
||||||
|
|
||||||
// 收集所有需要查询的用户ID(一级评论 + 回复)
|
// 收集所有需要查询的用户ID(一级评论 + 回复)
|
||||||
List<Long> userIds = new ArrayList<>(entityPage.getRecords().stream()
|
List<Long> userIds = new ArrayList<>(entityPage.getRecords().stream()
|
||||||
.map(KeyboardAiCompanionComment::getUserId)
|
.map(KeyboardAiCompanionComment::getUserId)
|
||||||
@@ -171,12 +164,12 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
Map<Long, Long> finalReplyCountMap = replyCountMap;
|
Map<Long, Long> finalReplyCountMap = replyCountMap;
|
||||||
|
|
||||||
return entityPage.convert(entity -> {
|
return entityPage.convert(entity -> {
|
||||||
CommentVO vo = convertToVO(entity, finalUserMap, likedCommentIds);
|
CommentVO vo = convertToVO(entity, finalUserMap, likedCommentIds, commentById);
|
||||||
|
|
||||||
// 填充回复列表
|
// 填充回复列表
|
||||||
List<KeyboardAiCompanionComment> replies = finalRepliesMap.getOrDefault(entity.getId(), List.of());
|
List<KeyboardAiCompanionComment> replies = finalRepliesMap.getOrDefault(entity.getId(), List.of());
|
||||||
List<CommentVO> replyVOs = replies.stream()
|
List<CommentVO> replyVOs = replies.stream()
|
||||||
.map(reply -> convertToVO(reply, finalUserMap, likedCommentIds))
|
.map(reply -> convertToVO(reply, finalUserMap, likedCommentIds, commentById))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
vo.setReplies(replyVOs);
|
vo.setReplies(replyVOs);
|
||||||
vo.setReplyCount(finalReplyCountMap.getOrDefault(entity.getId(), 0L).intValue());
|
vo.setReplyCount(finalReplyCountMap.getOrDefault(entity.getId(), 0L).intValue());
|
||||||
@@ -188,7 +181,12 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
/**
|
/**
|
||||||
* 将评论实体转换为VO
|
* 将评论实体转换为VO
|
||||||
*/
|
*/
|
||||||
private CommentVO convertToVO(KeyboardAiCompanionComment entity, Map<Long, KeyboardUser> userMap, Set<Long> likedCommentIds) {
|
private CommentVO convertToVO(
|
||||||
|
KeyboardAiCompanionComment entity,
|
||||||
|
Map<Long, KeyboardUser> userMap,
|
||||||
|
Set<Long> likedCommentIds,
|
||||||
|
Map<Long, KeyboardAiCompanionComment> commentById
|
||||||
|
) {
|
||||||
CommentVO vo = new CommentVO();
|
CommentVO vo = new CommentVO();
|
||||||
vo.setId(entity.getId());
|
vo.setId(entity.getId());
|
||||||
vo.setCompanionId(entity.getCompanionId());
|
vo.setCompanionId(entity.getCompanionId());
|
||||||
@@ -198,7 +196,7 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
vo.setContent(entity.getContent());
|
vo.setContent(entity.getContent());
|
||||||
vo.setLikeCount(entity.getLikeCount());
|
vo.setLikeCount(entity.getLikeCount());
|
||||||
vo.setCreatedAt(entity.getCreatedAt());
|
vo.setCreatedAt(entity.getCreatedAt());
|
||||||
vo.setLiked(likedCommentIds.contains(entity.getId()));
|
vo.setLiked(likedCommentIds != null && likedCommentIds.contains(entity.getId()));
|
||||||
|
|
||||||
// 填充用户信息
|
// 填充用户信息
|
||||||
KeyboardUser user = userMap.get(entity.getUserId());
|
KeyboardUser user = userMap.get(entity.getUserId());
|
||||||
@@ -206,6 +204,19 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|||||||
vo.setUserName(user.getNickName());
|
vo.setUserName(user.getNickName());
|
||||||
vo.setUserAvatar(user.getAvatarUrl());
|
vo.setUserAvatar(user.getAvatarUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 填充“回复给谁”(仅回复评论有值)
|
||||||
|
if (entity.getParentId() != null && commentById != null) {
|
||||||
|
KeyboardAiCompanionComment parent = commentById.get(entity.getParentId());
|
||||||
|
if (parent != null) {
|
||||||
|
vo.setReplyToUserId(parent.getUserId());
|
||||||
|
KeyboardUser replyToUser = userMap.get(parent.getUserId());
|
||||||
|
if (replyToUser != null) {
|
||||||
|
vo.setReplyToUserName(replyToUser.getNickName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return vo;
|
return vo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user