package com.dcit.authority.dto.node;

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

/**
 * 构建树形结构
 * @author zhangzhizhong
 * 2020年4月24日
 */
public class BuildTreeNode {
	
	public static List<DeptTreeNode> buildTree(List<DeptTreeNode> treeNodeList){
		if(treeNodeList == null || treeNodeList.size() == 0){
            return null;
        }
        List<DeptTreeNode> topNodes = new ArrayList<DeptTreeNode>();
 
        for (DeptTreeNode children : treeNodeList) {
 
            String pid = children.getParentId();
            if (pid == null || "".equals(pid) || pid.equals("0")) {
                topNodes.add(children);
 
                continue;
            }
 
            for (DeptTreeNode parent : treeNodeList) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildList().add(children);
                    continue;
                }
            }
 
        }
 
        /*DeptTreeNode root = new DeptTreeNode();
        if (topNodes.size() == 1) {
            root = topNodes.get(0);
        } else {
        	root.setId("-1");
            root.setParentId("");
            root.setParentIds("");
            root.setChildList(topNodes);
            root.setFullName("顶级节点");
            root.setShortName("顶级节点");
        }*/
 
        return topNodes;
	}

}
