forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionHelper.cs
More file actions
219 lines (191 loc) · 6.83 KB
/
RecursionHelper.cs
File metadata and controls
219 lines (191 loc) · 6.83 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
namespace Blog.Core.Common.Helper
{
/// <summary>
/// 泛型递归求树形结构
/// </summary>
public static class RecursionHelper
{
public static void LoopToAppendChildren(List<PermissionTree> all, PermissionTree curItem, long pid, bool needbtn)
{
var subItems = all.Where(ee => ee.Pid == curItem.value).ToList();
var btnItems = subItems.Where(ss => ss.isbtn == true).ToList();
if (subItems.Count > 0)
{
curItem.btns = new List<PermissionTree>();
curItem.btns.AddRange(btnItems);
}
else
{
curItem.btns = null;
}
if (!needbtn)
{
subItems = subItems.Where(ss => ss.isbtn == false).ToList();
}
if (subItems.Count > 0)
{
curItem.children = new List<PermissionTree>();
curItem.children.AddRange(subItems);
}
else
{
curItem.children = null;
}
if (curItem.isbtn)
{
//curItem.label += "按钮";
}
foreach (var subItem in subItems)
{
if (subItem.value == pid && pid > 0)
{
//subItem.disabled = true;//禁用当前节点
}
LoopToAppendChildren(all, subItem, pid, needbtn);
}
}
public static void LoopToAppendChildren(List<DepartmentTree> all, DepartmentTree curItem, long pid)
{
var subItems = all.Where(ee => ee.Pid == curItem.value).ToList();
if (subItems.Count > 0)
{
curItem.children = new List<DepartmentTree>();
curItem.children.AddRange(subItems);
}
else
{
curItem.children = null;
}
foreach (var subItem in subItems)
{
if (subItem.value == pid && pid > 0)
{
//subItem.disabled = true;//禁用当前节点
}
LoopToAppendChildren(all, subItem, pid);
}
}
public static void LoopNaviBarAppendChildren(List<NavigationBar> all, NavigationBar curItem)
{
var subItems = all.Where(ee => ee.pid == curItem.id).ToList();
if (subItems.Count > 0)
{
curItem.children = new List<NavigationBar>();
curItem.children.AddRange(subItems);
}
else
{
curItem.children = null;
}
foreach (var subItem in subItems)
{
LoopNaviBarAppendChildren(all, subItem);
}
}
public static void LoopToAppendChildrenT<T>(List<T> all, T curItem, string parentIdName = "Pid", string idName = "value", string childrenName = "children")
{
var subItems = all.Where(ee => ee.GetType().GetProperty(parentIdName).GetValue(ee, null).ToString() == curItem.GetType().GetProperty(idName).GetValue(curItem, null).ToString()).ToList();
if (subItems.Count > 0) curItem.GetType().GetField(childrenName).SetValue(curItem, subItems);
foreach (var subItem in subItems)
{
LoopToAppendChildrenT(all, subItem);
}
}
/// <summary>
/// 将父子级数据结构转换为普通list
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static List<T> TreeToList<T>(List<T> list, Action<T, T, List<T>> action = null)
{
List<T> results = new List<T>();
foreach (var item in list)
{
results.Add(item);
OperationChildData(results, item, action);
}
return results;
}
/// <summary>
/// 递归子级数据
/// </summary>
/// <param name="allList">树形列表数据</param>
/// <param name="item">Item</param>
public static void OperationChildData<T>(List<T> allList, T item, Action<T, T, List<T>> action)
{
dynamic dynItem = item;
if (dynItem.Children == null) return;
if (dynItem.Children.Count <= 0) return;
allList.AddRange(dynItem.Children);
foreach (var subItem in dynItem.Children)
{
action?.Invoke(item, subItem, allList);
OperationChildData(allList, subItem, action);
}
}
}
public class PermissionTree
{
public long value { get; set; }
public long Pid { get; set; }
public string label { get; set; }
public int order { get; set; }
public bool isbtn { get; set; }
public bool disabled { get; set; }
public List<PermissionTree> children { get; set; }
public List<PermissionTree> btns { get; set; }
}
public class DepartmentTree
{
public long value { get; set; }
public long Pid { get; set; }
public string label { get; set; }
public int order { get; set; }
public bool disabled { get; set; }
public List<DepartmentTree> children { get; set; }
}
public class NavigationBar
{
public long id { get; set; }
public long pid { get; set; }
public int order { get; set; }
public string name { get; set; }
public bool IsHide { get; set; } = false;
public bool IsButton { get; set; } = false;
public string path { get; set; }
public string Func { get; set; }
public string iconCls { get; set; }
public NavigationBarMeta meta { get; set; }
public List<NavigationBar> children { get; set; }
}
public class NavigationBarMeta
{
public string title { get; set; }
public bool requireAuth { get; set; } = true;
public bool NoTabPage { get; set; } = false;
public bool keepAlive { get; set; } = false;
}
public class NavigationBarPro
{
public long id { get; set; }
public long parentId { get; set; }
public int order { get; set; }
public string name { get; set; }
public bool IsHide { get; set; } = false;
public bool IsButton { get; set; } = false;
public string path { get; set; }
public string component { get; set; }
public string Func { get; set; }
public string iconCls { get; set; }
public NavigationBarMetaPro meta { get; set; }
}
public class NavigationBarMetaPro
{
public string title { get; set; }
public string icon { get; set; }
public bool show { get; set; } = false;
}
}