using NCC.Common.Extension;
using NCC.Dependency;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NCC.Common.Util
{
///
/// 树结构帮助类
///
[SuppressSniffer]
public static class TreeUtil
{
///
/// 建造树结构
///
/// 所有的节点
/// 节点
///
public static List ToTree(this List allNodes, string parentId = "0") where T : TreeModel, new()
{
List resData = new List();
var rootNodes = allNodes.Where(x => x.parentId == parentId || x.parentId.IsNullOrEmpty()).ToList();
resData = rootNodes;
resData.ForEach(aRootNode =>
{
aRootNode.hasChildren = HaveChildren(allNodes, aRootNode.id);
if (aRootNode.hasChildren)
{
aRootNode.children = _GetChildren(allNodes, aRootNode);
aRootNode.num = aRootNode.children.Count();
}
else
{
aRootNode.isLeaf = !aRootNode.hasChildren;
aRootNode.children = null;
}
});
return resData;
}
///
/// 建造树结构
///
/// 所有的节点
/// 节点
///
public static List ToTreeFilterParent(this List allNodes, string parentId = "0") where T : TreeModel, new()
{
List resData = new List();
var rootNodes = allNodes.Where(x => x.parentId == parentId).ToList();
resData = rootNodes;
resData.ForEach(aRootNode =>
{
aRootNode.hasChildren = HaveChildren(allNodes, aRootNode.id);
if (aRootNode.hasChildren)
{
aRootNode.children = _GetChildren(allNodes, aRootNode);
aRootNode.num = aRootNode.children.Count();
}
else
{
aRootNode.isLeaf = !aRootNode.hasChildren;
aRootNode.children = null;
}
});
return resData;
}
#region 私有成员
///
/// 获取所有子节点
///
/// 树模型(TreeModel或继承它的模型)
/// 所有节点列表
/// 父节点Id
///
private static List