package com.engine.htsc.payment.util;

import com.engine.htsc.payment.pojo.TreeNode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 树型数据工具类
 */
public class TreeUtil {


	public static List<TreeNode> tree2list(List<TreeNode> list, TreeNode node) {
		List<TreeNode> result = new ArrayList<>();
		for (TreeNode pNode : list) {
			if (node != null) {
				pNode.setPid(node.getId());
				pNode.setpCode(node.getProjectCode());
				pNode.setpName(node.getName());
				pNode.setKey(String.join("#", pNode.getId(), pNode.getType()));
			}
			List<TreeNode> childrenNode = pNode.getChildren();
			if (CollectionUtils.isEmpty(childrenNode)) {
				pNode.setKey(String.join("#", pNode.getId(), pNode.getType()));
				pNode.setChildren(null);
				result.add(pNode);
			} else {
				result.addAll(tree2list(childrenNode, pNode));
			}
		}
		return result;
	}
}
