using System; using System.Collections.Generic; using System.Linq; namespace EntrustSettle.Common.Helper { /// /// 泛型递归求树形结构 /// public static class RecursionHelper { public static void LoopToAppendChildren(List 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(); 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(); 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 all, DepartmentTree curItem, long pid) { var subItems = all.Where(ee => ee.Pid == curItem.value).ToList(); if (subItems.Count > 0) { curItem.children = new List(); 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 all, NavigationBar curItem) { var subItems = all.Where(ee => ee.pid == curItem.id).ToList(); if (subItems.Count > 0) { curItem.children = new List(); curItem.children.AddRange(subItems); } else { curItem.children = null; } foreach (var subItem in subItems) { LoopNaviBarAppendChildren(all, subItem); } } public static void LoopToAppendChildrenT(List 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); } } /// /// 将父子级数据结构转换为普通list /// /// /// public static List TreeToList(List list, Action> action = null) { List results = new List(); foreach (var item in list) { results.Add(item); OperationChildData(results, item, action); } return results; } /// /// 递归子级数据 /// /// 树形列表数据 /// Item public static void OperationChildData(List allList, T item, Action> 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 children { get; set; } public List 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 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 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; } }