SyllabusService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using HTEXLib.COMM.Helpers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.SDK.Extension;
  7. using TEAMModelOS.SDK.Models;
  8. using TEAMModelOS.SDK.Models.Cosmos.Common;
  9. namespace TEAMModelOS.Services.Common
  10. {
  11. public static class SyllabusService
  12. {
  13. /// <summary>
  14. /// 根据id查询列表串联id pid的新的关系列表
  15. /// </summary>
  16. /// <param name="nodes"></param>
  17. /// <param name="pid"></param>
  18. /// <param name="newNodes"></param>
  19. /// <returns></returns>
  20. public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
  21. bool flag = false;
  22. string spid = "";
  23. foreach (var node in nodes) {
  24. if (node.pid == pid) {
  25. newNodes.Add(node);
  26. spid = node.id;
  27. GetNewNode(nodes, spid, newNodes);
  28. flag = true;
  29. }
  30. }
  31. return newNodes;
  32. //if (flag)
  33. //{
  34. // return GetNewNode(nodes, spid, newNodes);
  35. //}
  36. //else {
  37. // return newNodes;
  38. //}
  39. }
  40. public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes)
  41. {
  42. int index = 0;
  43. foreach (SyllabusTree tr in trees)
  44. {
  45. tr.order = index;
  46. index++;
  47. }
  48. trees = trees.OrderBy(x => x.order).ToList();
  49. List<Tnode> list = new List<Tnode>();
  50. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  51. trees.ForEach(x=> {
  52. List<string> cids = new List<string>();
  53. if (x.children.IsNotEmpty()) {
  54. x.children.ForEach(y => cids.Add(y.id));
  55. }
  56. var node = new Tnode
  57. {
  58. type = x.type,
  59. title = x.title,
  60. id = x.id,
  61. pid = x.pid,
  62. order = x.order,
  63. rnodes = x.rnodes,
  64. cids= cids
  65. };
  66. list.Add(node);
  67. });
  68. nodes.AddRange(list);
  69. foreach (SyllabusTree tree in trees)
  70. {
  71. if (null != tree.children && tree.children.Count > 0)
  72. {
  73. TreeToList(tree.children, nodes);
  74. }
  75. }
  76. return nodes;
  77. }
  78. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  79. {
  80. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  81. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  82. var res = from r in list group r by r.id into g select g;
  83. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  84. foreach (var s in res)
  85. {
  86. blockDict.TryAdd(s.First().id, s.First());
  87. }
  88. return GetChild(list, blockDict);
  89. }
  90. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  91. {
  92. // list = list.OrderBy(m => m.Order).ToList();
  93. List<SyllabusTree> trees = new List<SyllabusTree>();
  94. trees = trees.OrderBy(x => x.order).ToList();
  95. foreach (SyllabusTree node in list)
  96. {
  97. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  98. if (flag && syllabus != null)
  99. {
  100. syllabus.children.Add(node);
  101. }
  102. else
  103. {
  104. trees.Add(node);
  105. }
  106. }
  107. return trees;
  108. }
  109. }
  110. }