1. handle findByVolid interface

main
lyp 10 months ago
parent bebfd9d122
commit 446c3f4196

@ -28,7 +28,7 @@ public class CommentController extends BaseController{
private RedisTemplate redisTemplate;
@GetMapping("/findByVolid/{volid}")
public Result<List<Comment>> findByVolid(@PathVariable String volid){
public Result<List<CommentVo>> findByVolid(@PathVariable String volid){
return Result.success(commentService.findByVolid(volid));
}

@ -22,5 +22,5 @@ public interface CommentService {
void thumbup(String commentId);
List<Comment> findByVolid(String volid);
List<CommentVo> findByVolid(String volid);
}

@ -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<Comment> comments = mongoTemplate.find(query, Comment.class);
List<String> userIds = comments.stream().map(Comment::getUserId).collect(Collectors.toList());
Result<List<SimpleUser>> userByIds = userClient.findUserByIds(userIds);
Map<String, SimpleUser> userIdAndInfoMap = userByIds.getData().stream().collect(Collectors.toMap(SimpleUser::getUserId, Function.identity()));
List<CommentVo> commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList());
List<String> targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
// List<String> targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
Map<String, CommentVo> 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<Comment> findByVolid(String volid) {
return commentDao.findByArticleId(volid);
public List<CommentVo> findByVolid(String volid) {
List<Comment> byArticleId = commentDao.findByArticleId(volid);
if (ObjectUtil.isEmpty(byArticleId)) {
return new ArrayList<>();
}
List<CommentVo> commentVos = byArticleId.stream().map(Comment::convertVo).collect(Collectors.toList());
List<String> userIds = byArticleId.stream().map(Comment::getUserId).collect(Collectors.toList());
Result<List<SimpleUser>> userByIds = userClient.findUserByIds(userIds);
Map<String, SimpleUser> 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;
}
}

Loading…
Cancel
Save