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; private RedisTemplate redisTemplate;
@GetMapping("/findByVolid/{volid}") @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)); return Result.success(commentService.findByVolid(volid));
} }

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

@ -1,7 +1,10 @@
package com.luoo.comment.service.impl; package com.luoo.comment.service.impl;
import api.Result;
import client.vo.SimpleUser;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.luoo.comment.client.UserClient;
import com.luoo.comment.dao.CommentDao; import com.luoo.comment.dao.CommentDao;
import com.luoo.comment.enums.CommentStatusEnum; import com.luoo.comment.enums.CommentStatusEnum;
import com.luoo.comment.pojo.Comment; import com.luoo.comment.pojo.Comment;
@ -18,6 +21,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import util.IdWorker; import util.IdWorker;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -28,6 +32,9 @@ import java.util.stream.Collectors;
@Transactional @Transactional
public class CommentServiceImpl implements CommentService { public class CommentServiceImpl implements CommentService {
@Autowired
private UserClient userClient;
@Autowired @Autowired
private CommentDao commentDao; private CommentDao commentDao;
@ -100,9 +107,15 @@ public class CommentServiceImpl implements CommentService {
query.with(pageable); query.with(pageable);
List<Comment> comments = mongoTemplate.find(query, Comment.class); 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<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())); 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()); // boolean isChange = targetComments.removeAll(idCommentMap.keySet());
for (CommentVo commentVo : commentVos) { 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())) { if (commentVo.getLevel() > 2 && ObjectUtil.isNotEmpty(commentVo.getTargetId())) {
CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId()); CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId());
if (targetCommentVo != null) { if (targetCommentVo != null) {
@ -130,7 +151,28 @@ public class CommentServiceImpl implements CommentService {
mongoTemplate.updateFirst(query,update,"spit"); mongoTemplate.updateFirst(query,update,"spit");
} }
public List<Comment> findByVolid(String volid) { public List<CommentVo> findByVolid(String volid) {
return commentDao.findByArticleId(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