package com.emergency.avenue.admin.api.vo;

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

/**
 * <p>
 *
 * </P>
 *
 * @Author: wenpeng_qing
 * @Date: 2019-04-22 10:22
 * @Version: 1.0.0
 */
public class TreeBuilder {

	/**
	 * 使用递归方法建树
	 *
	 * @param treeNodes
	 * @return
	 */
	public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes) {
		List<TreeNode> trees = new ArrayList<TreeNode>();
		for (TreeNode treeNode : treeNodes) {
			if (treeNode.getParentId().equals("0")) {
				trees.add(findChildren(treeNode, treeNodes));
			}
		}
		trees = sorted(trees);
		return trees;
	}


    /**
     * 使用递归方法建树
     *
     * @param treeNodes
     * @return
     */
    public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes,String parentId) {
        List<TreeNode> trees = new ArrayList<TreeNode>();
        for (TreeNode treeNode : treeNodes) {
            if (treeNode.getParentId().equals(parentId)) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        trees = sorted(trees);
        return trees;
    }

	/**
	 * 递归查找子节点
	 *
	 * @param treeNodes
	 * @return
	 */
	public static TreeNode findChildren(TreeNode treeNode, List<TreeNode> treeNodes) {
		for (TreeNode it : treeNodes) {
			if (treeNode.getId().equals(it.getParentId())) {
				if (treeNode.getChildren() == null) {
					treeNode.setChildren(new ArrayList<TreeNode>());
				}
				treeNode.getChildren().add(findChildren(it, treeNodes));
			}
		}
		treeNode.setChildren(sorted(treeNode.getChildren()));
		return treeNode;
	}

	public static List<TreeNode> sorted(List<TreeNode> treeNode) {
		if (Objects.nonNull(treeNode) && treeNode.size() > 0) {
			treeNode = treeNode.stream().sorted((node1, node2) -> {
				if (node1.getSort() >= node2.getSort()) {
					return 1;
				} else {
					return -1;
				}
			}).collect(Collectors.toList());
			for (TreeNode it : treeNode) {
				if (Objects.nonNull(it.getChildren()) && it.getChildren().size() > 0) {
					it.setChildren(sorted(it.getChildren()));
				}
			}
		}
		return treeNode;
	}

	/**
	 * 使用递归方法建树
	 *
	 * @param treeNodes
	 * @return
	 */
	public static List<TreeNode> buildKnowByRecursive(List<TreeNode> treeNodes) {
		List<TreeNode> trees = new ArrayList<TreeNode>();
		for (TreeNode treeNode : treeNodes) {
			if (treeNode.getParentId().equals("0")) {
				trees.add(findKnowChildren(treeNode, treeNodes));
			}
		}
		trees = sorted(trees);
		return trees;
	}


	/**
	 * 递归查找子节点
	 *
	 * @param treeNodes
	 * @return
	 */
	public static TreeNode findKnowChildren(TreeNode treeNode, List<TreeNode> treeNodes) {
		for (TreeNode it : treeNodes) {
			if (treeNode.getDictId().equals(it.getParentId())) {
				if (treeNode.getChildren() == null) {
					treeNode.setChildren(new ArrayList<TreeNode>());
				}
				treeNode.getChildren().add(findKnowChildren(it, treeNodes));
			}
		}
		treeNode.setChildren(sorted(treeNode.getChildren()));
		return treeNode;
	}
}
