using HTEXLib.COMM.Helpers; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TEAMModelOS.SDK.Extension; namespace TEAMModelOS.SDK.Models.Service { public sealed class KnowledgeService { public static List TreeToList(List trees, List nodes/*, List blocks,HashSet points*/) { List list = new List(); trees.ForEach(x => { List cids = new List(); if (x.children.IsNotEmpty()) { x.children.ForEach(y => { cids.Add(y.id); /*points.Add(y.name); */y.pid=x.id; }); } var node = new PointNode { name = x.name, id = x.id, pid = x.pid, cids= cids, used =x.used, link = x.link, }; list.Add(node); //blocks.Add(new Block { name= x.name,points=x.nodes.Select(x=>x.name).ToList() }); //points.Add(x.name); }); nodes.AddRange(list); foreach (PointTree tree in trees) { if (null != tree.children && tree.children.Count > 0) { TreeToList(tree.children, nodes/*, blocks, points*/); } } return nodes; } public static List ListToTree(List noes) { List list = noes.ToJsonString().ToObject>(); 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(); foreach (PointTree node in list) { bool flag = dict.TryGetValue(node.pid, out PointTree syllabus); if (flag && syllabus != null) { syllabus.children.Add(node); } else { trees.Add(node); } } return trees; } static public List GetChildIds(PointTree node) { List childIds = new List(); if (node.children != null) { foreach (PointTree child in node.children) { childIds.Add(child.id); var cids = GetChildIds(child); childIds.AddRange(cids); child.cids=cids.Count>100 ? child.children.Select(x => x.id).ToList() : cids; } } return childIds; } } }