using System.Collections.Generic; using System.Linq; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos; namespace TEAMModelOS.SDK.Services { public class AbilityService { public static List TreeToList(List trees, List nodes, long now) { int index = 0; foreach (AbilityTaskTree tr in trees) { tr.order = index; index++; } trees = trees.OrderBy(x => x.order).ToList(); List list = new List(); //var list = trees.ToJsonString().ToObject>(); trees.ForEach(x => { List cids = new List(); if (x.children.IsNotEmpty()) { x.children.ForEach(y => cids.Add(y.id)); } var node = new Tnode { title = x.title, id = x.id, pid = x.pid, order = x.order, rnodes = x.rnodes, cids = cids, creatorId = x.creatorId, creatorName = x.creatorName, updateTime = now }; list.Add(node); }); nodes.AddRange(list); foreach (AbilityTaskTree tree in trees) { if (null != tree.children && tree.children.Count > 0) { TreeToList(tree.children, nodes, now); } } return nodes; } public static List ListToTree(List noes) { List list = noes.ToJsonString().ToObject>(); //var lookup = list.ToDictionary(n => n.RowKey, n => n); var res = from r in list group r by r.id into g select g; Dictionary blockDict = new Dictionary(); foreach (var s in res) { blockDict.TryAdd(s.First().id, s.First()); } return GetChild(list, blockDict); } private static List GetChild(List list, Dictionary dict) { // list = list.OrderBy(m => m.Order).ToList(); List trees = new List(); trees = trees.OrderBy(x => x.order).ToList(); foreach (AbilityTaskTree node in list) { bool flag = dict.TryGetValue(node.pid, out AbilityTaskTree ability); if (flag && ability != null) { ability.children.Add(node); } else { trees.Add(node); } } return trees; } } }