TreeUtil.cs
6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using NCC.Common.Extension;
using NCC.Dependency;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NCC.Common.Util
{
/// <summary>
/// 树结构帮助类
/// </summary>
[SuppressSniffer]
public static class TreeUtil
{
/// <summary>
/// 建造树结构
/// </summary>
/// <param name="allNodes">所有的节点</param>
/// <param name="parentId">节点</param>
/// <returns></returns>
public static List<T> ToTree<T>(this List<T> allNodes, string parentId = "0") where T : TreeModel, new()
{
List<T> resData = new List<T>();
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;
}
/// <summary>
/// 建造树结构
/// </summary>
/// <param name="allNodes">所有的节点</param>
/// <param name="parentId">节点</param>
/// <returns></returns>
public static List<T> ToTreeFilterParent<T>(this List<T> allNodes, string parentId = "0") where T : TreeModel, new()
{
List<T> resData = new List<T>();
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 私有成员
/// <summary>
/// 获取所有子节点
/// </summary>
/// <typeparam name="T">树模型(TreeModel或继承它的模型)</typeparam>
/// <param name="nodes">所有节点列表</param>
/// <param name="parentNode">父节点Id</param>
/// <returns></returns>
private static List<object> _GetChildren<T>(List<T> nodes, T parentNode) where T : TreeModel, new()
{
Type type = typeof(T);
var properties = type.GetProperties().ToList();
List<object> resData = new List<object>();
var children = nodes.Where(x => x.parentId == parentNode.id).ToList();
children.ForEach(aChildren =>
{
T newNode = new T();
resData.Add(newNode);
//赋值属性
foreach (var aProperty in properties.Where(x => x.CanWrite))
{
var value = aProperty.GetValue(aChildren, null);
aProperty.SetValue(newNode, value);
}
newNode.hasChildren = HaveChildren(nodes, aChildren.id);
if (newNode.hasChildren)
{
newNode.children = _GetChildren(nodes, newNode);
}
else
{
newNode.isLeaf = !newNode.hasChildren;
newNode.children = null;
}
});
return resData;
}
/// <summary>
/// 判断当前节点是否有子节点
/// </summary>
/// <typeparam name="T">树模型</typeparam>
/// <param name="nodes">所有节点</param>
/// <param name="nodeId">当前节点Id</param>
/// <returns></returns>
private static bool HaveChildren<T>(List<T> nodes, string nodeId) where T : TreeModel, new()
{
return nodes.Exists(x => x.parentId == nodeId);
}
///// <summary>
///// 递归重新解析成list
///// </summary>
///// <param name="allNodes"></param>
///// <param name="parentId"></param>
///// <returns></returns>
//public static List<TreeModel> DeTree(this List<TreeModel> allNodes, string parentId = "0")
//{
// List<TreeModel> list = new List<TreeModel>();
// foreach (var item in allNodes.Where(o => o.parentId == parentId))
// {
// list.Add(item);
// if (item.hasChildren)
// {
// var childrens = DeTree(allNodes, item.id);
// if (childrens != null) list.AddRange(childrens);
// item.hasChildren = false;
// item.children = new List<object>();
// }
// }
// return list;
//}
#endregion
}
/// <summary>
/// 树模型基类
/// </summary>
public class TreeModel
{
/// <summary>
/// 获取节点id
/// </summary>
/// <returns></returns>
public string id { get; set; }
/// <summary>
/// 获取节点父id
/// </summary>
/// <returns></returns>
//[JsonIgnore]
public string parentId { get; set; }
/// <summary>
/// 是否有子级
/// </summary>
public bool hasChildren { get; set; }
/// <summary>
/// 设置Children
/// </summary>
/// <param name="children"></param>
public List<object> children { get; set; } = new List<object>();
/// <summary>
/// 子节点数量
/// </summary>
public int num { get; set; }
/// <summary>
/// 是否为子节点
/// </summary>
public bool isLeaf { get; set; } = false;
}
}