package com.keyware.shandan.bianmu.utils;

import com.keyware.shandan.bianmu.entity.DataLabelsVo;
import com.keyware.shandan.common.entity.TreeVo;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GuoXin
 * @date 2023-01-03
 */
public class DataLabelsTreeUtil {

    public static List<TreeVo> parseTreeNode(Collection<DataLabelsVo> labels, String checked) {
        return labels.stream().map(label -> TreeVo.builder()
                .basicData(label)
                .title(label.getTitle())
                .id(label.getId().toString())
                .parentId(label.getParentId().toString())
                .last(label.getIsLabel())
                .checked(checked)
                .iconClass(label.getIsLabel() ? "layui-icon layui-icon-note" : "")
                .build()).collect(Collectors.toList());
    }

    public static List<TreeVo> buildTree(Collection<DataLabelsVo> labels, Serializable parentId){
        List<TreeVo> allNodes = parseTreeNode(labels, "false");
        return allNodes.stream().filter(node -> node.getParentId().equals(parentId.toString())).peek(node->{
            node.setChildren(buildTree(labels, node.getId()));
        }).collect(Collectors.toList());
    }
}
