cy 3 years ago
parent
commit
0afb3b2df3

+ 20 - 8
cooleshow-bbs/src/main/java/com/yonge/cooleshow/bbs/service/impl/BbsArticleServiceImpl.java

@@ -13,6 +13,7 @@ import com.yonge.cooleshow.bbs.enums.ArticleEnum;
 import com.yonge.cooleshow.bbs.enums.YesOrNoEnum;
 import com.yonge.cooleshow.bbs.vo.BbsReplyVo;
 import com.yonge.toolset.base.exception.BizException;
+import javafx.scene.Parent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -166,18 +167,29 @@ public class BbsArticleServiceImpl extends ServiceImpl<BbsArticleDao, BbsArticle
     public IPage<BbsReplyVo> articleReply(IPage<BbsReplyVo> page, BbsReplySearch query) {
         query.setStatus(ArticleEnum.PASS);
         List<BbsReplyVo> allReply = baseMapper.articleReply(null, query);
-        for (BbsReplyVo replyVo : allReply) {
+
+        List<BbsReplyVo> tree = allReply.stream().filter((parentReply) -> {
+            return parentReply.getParentId() == 0;
+        }).map((p) -> {
             List<BbsReplyVo> list = new ArrayList<>();
 
-            findChildren(replyVo, list);
-            replyVo.setChildren(list);
-        }
 
-        return page.setRecords(allReply);
+            List<BbsReplyVo> childrens = getChildren(p, allReply, list);
+            List<BbsReplyVo> collect = childrens.stream().sorted(Comparator.comparing(BbsReplyVo::getCreatedTime)).collect(Collectors.toList());
+            p.setChildren(collect);
+            return p;
+        }).collect(Collectors.toList());
+        return page.setRecords(tree);
     }
 
-
-    public void findChildren(BbsReplyVo replyVo, List<BbsReplyVo> list) {
-
+    private List<BbsReplyVo> getChildren(BbsReplyVo rootReply, List<BbsReplyVo> allReply, List<BbsReplyVo> list) {
+        allReply.stream().filter(categoryEntity -> {
+            return categoryEntity.getParentId().equals(rootReply.getId());
+        }).map(child -> {
+            list.add(child);
+            getChildren(child, allReply, list);
+            return child;
+        }).collect(Collectors.toList());
+        return list;
     }
 }