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) {
            String headPhoto = data.getHeadPhoto();
            String userName = data.getUserName();
//    商家没有头像和用户名
            if (headPhoto == null) {
                headPhoto = "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png";
            }
            if (userName == null) {
                userName = "商家";
            }
            CommentListVo item = CommentListVo.builder()
                    .id(data.getId())
                    .commentId(data.getCommentId())
                    .commentName(data.getCommentName())
                    .userName(userName)
                    .headPhoto(headPhoto)
                    //截取时间，这个时间会多一个0 yyyy/MM/dd
                    .dateTime(data.getDateTime().substring(0, 19))
                    .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;
    }
}
