SyllabusService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.SDK.Services
  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,long now)
  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. title = x.title,
  59. id = x.id,
  60. pid = x.pid,
  61. order = x.order,
  62. rnodes = x.rnodes,
  63. cids= cids,
  64. creatorId=x.creatorId,
  65. creatorName=x.creatorName,
  66. updateTime= now
  67. };
  68. list.Add(node);
  69. });
  70. nodes.AddRange(list);
  71. foreach (SyllabusTree tree in trees)
  72. {
  73. if (null != tree.children && tree.children.Count > 0)
  74. {
  75. TreeToList(tree.children, nodes,now);
  76. }
  77. }
  78. return nodes;
  79. }
  80. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  81. {
  82. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  83. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  84. var res = from r in list group r by r.id into g select g;
  85. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  86. foreach (var s in res)
  87. {
  88. blockDict.TryAdd(s.First().id, s.First());
  89. }
  90. return GetChild(list, blockDict);
  91. }
  92. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  93. {
  94. // list = list.OrderBy(m => m.Order).ToList();
  95. List<SyllabusTree> trees = new List<SyllabusTree>();
  96. trees = trees.OrderBy(x => x.order).ToList();
  97. foreach (SyllabusTree node in list)
  98. {
  99. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  100. if (flag && syllabus != null)
  101. {
  102. syllabus.children.Add(node);
  103. }
  104. else
  105. {
  106. trees.Add(node);
  107. }
  108. }
  109. return trees;
  110. }
  111. }
  112. }