KnowledgeService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using HTEXLib.COMM.Helpers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.Extension;
  8. namespace TEAMModelOS.SDK.Models.Service
  9. {
  10. public sealed class KnowledgeService
  11. {
  12. public static List<PointNode> TreeToList(List<PointTree> trees, List<PointNode> nodes/*, List<Block> blocks,HashSet<string> points*/)
  13. {
  14. List<PointNode> list = new List<PointNode>();
  15. trees.ForEach(x => {
  16. List<string> cids = new List<string>();
  17. if (x.children.IsNotEmpty())
  18. {
  19. x.children.ForEach(y => { cids.Add(y.id); /*points.Add(y.name); */y.pid=x.id; });
  20. }
  21. var node = new PointNode
  22. {
  23. name = x.name,
  24. id = x.id,
  25. pid = x.pid,
  26. cids= cids,
  27. used =x.used,
  28. link = x.link,
  29. };
  30. list.Add(node);
  31. //blocks.Add(new Block { name= x.name,points=x.nodes.Select(x=>x.name).ToList() });
  32. //points.Add(x.name);
  33. });
  34. nodes.AddRange(list);
  35. foreach (PointTree tree in trees)
  36. {
  37. if (null != tree.children && tree.children.Count > 0)
  38. {
  39. TreeToList(tree.children, nodes/*, blocks, points*/);
  40. }
  41. }
  42. return nodes;
  43. }
  44. public static List<PointTree> ListToTree(List<PointNode> noes)
  45. {
  46. List<PointTree> list = noes.ToJsonString().ToObject<List<PointTree>>();
  47. var res = from r in list group r by r.id into g select g;
  48. Dictionary<string, PointTree> blockDict = new Dictionary<string, PointTree>();
  49. foreach (var s in res)
  50. {
  51. blockDict.TryAdd(s.First().id, s.First());
  52. }
  53. return GetChild(list, blockDict);
  54. }
  55. private static List<PointTree> GetChild(List<PointTree> list, Dictionary<string, PointTree> dict)
  56. {
  57. // list = list.OrderBy(m => m.Order).ToList();
  58. List<PointTree> trees = new List<PointTree>();
  59. foreach (PointTree node in list)
  60. {
  61. bool flag = dict.TryGetValue(node.pid, out PointTree syllabus);
  62. if (flag && syllabus != null)
  63. {
  64. syllabus.children.Add(node);
  65. }
  66. else
  67. {
  68. trees.Add(node);
  69. }
  70. }
  71. return trees;
  72. }
  73. static public List<string> GetChildIds(PointTree node)
  74. {
  75. List<string> childIds = new List<string>();
  76. if (node.children != null)
  77. {
  78. foreach (PointTree child in node.children)
  79. {
  80. childIds.Add(child.id);
  81. var cids = GetChildIds(child);
  82. childIds.AddRange(cids);
  83. child.cids=cids.Count>100 ? child.children.Select(x => x.id).ToList() : cids;
  84. }
  85. }
  86. return childIds;
  87. }
  88. }
  89. }