1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| @Service("commentService") public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService { @Override public ResponseResult commentList(Integer type, Long articleId, Integer pageNum, Integer pageSize) { LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Comment::getType, String.valueOf(type)); if (SystemConstants.COMMENT_TYPE_ARTICLE.equals(String.valueOf(type))) { queryWrapper.eq(Comment::getArticleId, articleId); } queryWrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOT_ID); queryWrapper.eq(Comment::getStatus, SystemConstants.COMMENT_STATUS_NORMAL); queryWrapper.orderByDesc(Comment::getCreateTime);
Page<Comment> page = new Page<>(pageNum, pageSize); page(page, queryWrapper);
List<CommentVo> commentVoList = toCommentVoList(page.getRecords());
for (CommentVo commentVo : commentVoList) { List<CommentVo> children = getChildren(commentVo.getId()); commentVo.setChildren(children); }
return ResponseResult.okResult(new PageVo(commentVoList, page.getTotal())); }
private List<CommentVo> getChildren(Long rootId) { LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Comment::getRootId, rootId); queryWrapper.eq(Comment::getStatus, SystemConstants.COMMENT_STATUS_NORMAL); queryWrapper.orderByAsc(Comment::getCreateTime); List<Comment> comments = list(queryWrapper); return toCommentVoList(comments); }
private List<CommentVo> toCommentVoList(List<Comment> comments) { List<CommentVo> voList = BeanCopyUtils.copyBeanList(comments, CommentVo.class); for (int i = 0; i < voList.size(); i++) { CommentVo vo = voList.get(i); Comment original = comments.get(i); vo.setAvatar(original.getAvatar()); if (StringUtils.hasText(original.getEmail()) && original.getEmail().equalsIgnoreCase(SystemConstants.BLOGGER_EMAIL)) { vo.setUserType("1"); } else if (original.getUserId() != null) { User user = userMapper.selectById(original.getUserId()); if (user != null) { vo.setUserType(user.getUserType()); } } else { vo.setUserType("0"); } if (vo.getReplyToCommentId() != null && vo.getReplyToCommentId() > 0) { Comment replyTo = getById(vo.getReplyToCommentId()); if (replyTo != null) { vo.setReplyToCommentNickname(replyTo.getNickname()); if (StringUtils.hasText(replyTo.getEmail()) && replyTo.getEmail().equalsIgnoreCase(SystemConstants.BLOGGER_EMAIL)) { vo.setReplyToCommentUserType("1"); } else if (replyTo.getUserId() != null){ User replyToUser = userMapper.selectById(replyTo.getUserId()); if (replyToUser != null) { vo.setReplyToCommentUserType(replyToUser.getUserType()); } } else { vo.setReplyToCommentUserType("0"); } } } } return voList; } }
|