package com.nantian.cloud.jly.vo;

import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.List;

@UtilityClass
public class TreeVoUtil {

    public static <T extends TreeVo> List<T> findTree(List<T> treeNodes,String id){
        List<T> trees = new ArrayList<T>();
        for (T treeNode : treeNodes) {
            //if ("".equals(treeNode.getPvalue()) || null==treeNode.getPvalue() ) {
            if ("0".equals(treeNode.getPvalue()) ||
                    "".equals(treeNode.getPvalue()) ||
                    null==treeNode.getPvalue() ||
                id.equals(treeNode.getValue())) {
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    public static <T extends TreeVo> T findChildren(T treeNode,List<T> treeNodes) {
        for (T node : treeNodes) {
            if(treeNode.getValue().equals(node.getPvalue())) {
                treeNode.getChildren().add(findChildren(node,treeNodes));
            }
        }
        return treeNode;
    }
}
