package com.template.common.utils;

import com.template.model.pojo.SmartAuthorGroup;
import com.template.model.weixin.AuthorListGroup;

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

public class TreeRecordsUtil {

    public static List<AuthorListGroup> queryCommentTreeRecords(Integer pid, List<SmartAuthorGroup> lists) {
        List<AuthorListGroup> newTrees = new ArrayList<>();

        List<SmartAuthorGroup> datas = lists.stream().filter(e -> e.getParentId().equals(pid)).collect(Collectors.toList());

        for (SmartAuthorGroup data : datas) {
            AuthorListGroup item = AuthorListGroup.builder()
                    .id(data.getId())
                    .parentId(data.getParentId())
                    .name(data.getName())
                    .userId(data.getUserId())
                    .applyId(data.getApplyId())
                    .updateTime(data.getUpdateTime())
                    .createUser(data.getCreateUser())
                    .updateUser(data.getUpdateUser())
                    .deleted(data.getDeleted())
                    .build();
            List<AuthorListGroup> news = queryCommentTreeRecords(item.getId(), lists);
            if (news == null || news.size() == 0) {
                newTrees.add(item);
                continue;
            } else {
                item.setAuthorListGroups(news);
                newTrees.add(item);
            }
        }

        return newTrees;
    }
}
