package com.happy.Until;

import com.happy.vo.CommentListVo;
import com.happy.vo.CommentVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TreeRecordsUtil {
    /**
     * 根据父类ID获取树形菜单数据
     * @param parentID 父级ID
     * @param lists 数据集合
     * @return
     */
    public static List<CommentListVo> queryCommentTreeRecords(String parentID, List<CommentVo> lists) {
        List<CommentListVo> newTrees = new ArrayList<>();

        List<CommentVo> datas = lists.stream().filter(e -> e.getCommentId().equals(parentID)).collect(Collectors.toList());

        for (CommentVo data : datas) {
            CommentListVo item = CommentListVo.builder()
                    .id(data.getId())
                    .commentId(data.getCommentId())
                    .commentName(data.getCommentName())
                    .userName(data.getUserName())
                    .headPhoto(data.getHeadPhoto())
            // 将时间转化为 yyyy/MM/dd
                    .dateTime( DateUtil.parseDateToStr((Func.parseDate(data.getDateTime())), DateUtil.Time_Formatter_Day))
                    .content(data.getContent())
                    .build();
            List<CommentListVo> news = queryCommentTreeRecords(item.getId(), lists);
            if (news == null || news.size() == 0) {
                newTrees.add(item);
                continue;
            } else {
                item.setCommentVoList(news);
                newTrees.add(item);
            }
        }

        return newTrees;
    }
}
