using JNPF.Common.Extension;
using JNPF.DependencyInjection;
namespace JNPF.Common.Security;
///
/// 树结构帮助类.
///
[SuppressSniffer]
public static class TreeHelper
{
///
/// 建造树结构.
///
/// 所有的节点.
/// 节点.
///
public static List ToTree(this List allNodes, string parentId = "0")
where T : TreeModel, new()
{
List resData = new List();
// 查找出父类对象
List rootNodes = allNodes.FindAll(x => x.parentId == parentId || x.parentId.IsNullOrEmpty());
// 移除父类对象
allNodes.RemoveAll(x => x.parentId == parentId || x.parentId.IsNullOrEmpty());
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