diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index c0e72cf..068b61f 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -28,7 +28,7 @@ public class CommentController extends BaseController{ private RedisTemplate redisTemplate; @GetMapping("/findByVolid/{volid}") - public Result> findByVolid(@PathVariable String volid){ + public Result> findByVolid(@PathVariable String volid){ return Result.success(commentService.findByVolid(volid)); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java index 7b371ad..d17d6e6 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java @@ -22,5 +22,5 @@ public interface CommentService { void thumbup(String commentId); - List findByVolid(String volid); + List findByVolid(String volid); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java index 233dbd2..cdbfc4f 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java @@ -1,7 +1,10 @@ package com.luoo.comment.service.impl; +import api.Result; +import client.vo.SimpleUser; import cn.hutool.core.util.ObjectUtil; +import com.luoo.comment.client.UserClient; import com.luoo.comment.dao.CommentDao; import com.luoo.comment.enums.CommentStatusEnum; import com.luoo.comment.pojo.Comment; @@ -18,6 +21,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -28,6 +32,9 @@ import java.util.stream.Collectors; @Transactional public class CommentServiceImpl implements CommentService { + @Autowired + private UserClient userClient; + @Autowired private CommentDao commentDao; @@ -100,9 +107,15 @@ public class CommentServiceImpl implements CommentService { query.with(pageable); List comments = mongoTemplate.find(query, Comment.class); + List userIds = comments.stream().map(Comment::getUserId).collect(Collectors.toList()); + + Result> userByIds = userClient.findUserByIds(userIds); + + Map userIdAndInfoMap = userByIds.getData().stream().collect(Collectors.toMap(SimpleUser::getUserId, Function.identity())); + List commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList()); - List targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList()); +// List targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList()); Map idCommentMap = commentVos.stream().collect(Collectors.toMap(CommentVo::getId, Function.identity())); @@ -111,6 +124,14 @@ public class CommentServiceImpl implements CommentService { // boolean isChange = targetComments.removeAll(idCommentMap.keySet()); for (CommentVo commentVo : commentVos) { + + // 处理昵称 + SimpleUser simpleUser = userIdAndInfoMap.get(commentVo.getUserId()); + + if (ObjectUtil.isNotEmpty(simpleUser)) { + commentVo.setNickName(simpleUser.getNickName()); + } + // 处理叠楼评论回复 if (commentVo.getLevel() > 2 && ObjectUtil.isNotEmpty(commentVo.getTargetId())) { CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId()); if (targetCommentVo != null) { @@ -130,7 +151,28 @@ public class CommentServiceImpl implements CommentService { mongoTemplate.updateFirst(query,update,"spit"); } - public List findByVolid(String volid) { - return commentDao.findByArticleId(volid); + public List findByVolid(String volid) { + List byArticleId = commentDao.findByArticleId(volid); + + if (ObjectUtil.isEmpty(byArticleId)) { + return new ArrayList<>(); + } + + List commentVos = byArticleId.stream().map(Comment::convertVo).collect(Collectors.toList()); + + List userIds = byArticleId.stream().map(Comment::getUserId).collect(Collectors.toList()); + Result> userByIds = userClient.findUserByIds(userIds); + + Map userIdAndInfoMap = userByIds.getData().stream().collect(Collectors.toMap(SimpleUser::getUserId, Function.identity())); + + for (CommentVo commentVo : commentVos) { + // 处理昵称 + SimpleUser simpleUser = userIdAndInfoMap.get(commentVo.getUserId()); + + if (ObjectUtil.isNotEmpty(simpleUser)) { + commentVo.setNickName(simpleUser.getNickName()); + } + } + return commentVos; } }