package com.steerinfo.ems.Utils;

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

public class TreeUtils <T extends TreePO>{


    public List<T> getTreeData(List<T> treeNodes){
        List<T> trees = new ArrayList<>();
        for (T treeNode : treeNodes) {
            if ("".equals(treeNode.getParentId()) || treeNode.getParentId() ==null) {
                trees.add(findAddressChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    /** 
    * 递归查找地址子节点 
    * @param treeNodes 
    * @return 
    */
    public T findAddressChildren(T treeNode,List<T> treeNodes) {
        for (T it : treeNodes) {
            if(treeNode.getCode().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<T>());
                }
                treeNode.getChildren().add(findAddressChildren(it,treeNodes));
            }
        }
        return treeNode;
    }
}
