KnowledgeService.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.nodes.IsNotEmpty())
  18. {
  19. x.nodes.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.nodes && tree.nodes.Count > 0)
  38. {
  39. TreeToList(tree.nodes, 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.nodes.Add(node);
  65. }
  66. else
  67. {
  68. trees.Add(node);
  69. }
  70. }
  71. return trees;
  72. }
  73. }
  74. }