diff --git a/common/common-utils/src/main/java/cn/bunny/common/utils/CommentUtil.java b/common/common-utils/src/main/java/cn/bunny/common/utils/CommentUtil.java deleted file mode 100644 index b733f66..0000000 --- a/common/common-utils/src/main/java/cn/bunny/common/utils/CommentUtil.java +++ /dev/null @@ -1,51 +0,0 @@ -package cn.bunny.common.utils; - -import cn.bunny.vo.system.comment.CommentVo; - -import java.util.ArrayList; -import java.util.List; - -public class CommentUtil { - /** - * 构建树型结构 - * - * @param commentList 评论列表 - * @return 结构列表 - */ - public static List buildTree(List commentList) { - // 构建树形结构 - List tree = new ArrayList<>(); - // 遍历评论列表 - for (CommentVo comment : commentList) { - // 找到顶级评论(没有父评论) - if (comment.getPCommentId() == 0) { - // 递归构建子评论 - comment.setChildren(getChildren(comment.getId(), commentList)); - tree.add(comment); - } - } - return tree; - } - - /** - * 递归获取子评论 - * - * @param commentId 当前评论ID - * @param commentList 评论列表 - * @return 子评论列表 - */ - private static List getChildren(Long commentId, List commentList) { - List children = new ArrayList<>(); - - // 遍历评论列表 - for (CommentVo comment : commentList) { - // 找到当前评论的子评论 - if (Long.valueOf(comment.getPCommentId()).equals(commentId)) { - // 递归构建子评论的子评论 - comment.setChildren(getChildren(comment.getId(), commentList)); - children.add(comment); - } - } - return children; - } -} diff --git a/service/service-web/src/test/java/cn/bunny/service/web/service/impl/CommentServiceImplTest.java b/service/service-web/src/test/java/cn/bunny/service/web/service/impl/CommentServiceImplTest.java deleted file mode 100644 index acaf5d0..0000000 --- a/service/service-web/src/test/java/cn/bunny/service/web/service/impl/CommentServiceImplTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package cn.bunny.service.web.service.impl; - -import cn.bunny.common.utils.CommentUtil; -import cn.bunny.dto.common.CommentQueryDto; -import cn.bunny.entity.system.article.ForumComment; -import cn.bunny.vo.system.comment.CommentVo; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import org.junit.jupiter.api.Test; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.ArrayList; -import java.util.List; - -@SpringBootTest -class CommentServiceImplTest { - @Autowired - private CommentMapper commentMapper; - - /** - * 获取文章评论 - */ - @Test - public void loadComment() { - CommentQueryDto dto = new CommentQueryDto(); - dto.setArticleId("RtiXj832TFL4nhW"); - // 返回参数 - ArrayList list = new ArrayList<>(); - // 查询 - Page page = new Page<>(1, 6); - IPage commentList = commentMapper.loadComment(page, dto); - - // 准备返回值 - commentList.getRecords().forEach(forumComments -> { - CommentVo commentVo = new CommentVo(); - BeanUtils.copyProperties(forumComments, commentVo); - list.add(commentVo); - }); - - List buildTree = CommentUtil.buildTree(list); - System.out.println(buildTree); - } -} \ No newline at end of file