SyllabusService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.Bibliography;
  3. using DocumentFormat.OpenXml.Wordprocessing;
  4. using HTEXLib.COMM.Helpers;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models;
  13. using TEAMModelOS.SDK.Models.Cosmos.Common;
  14. namespace TEAMModelOS.SDK.Services
  15. {
  16. public static class SyllabusService
  17. {
  18. /// <summary>
  19. /// 根据id查询列表串联id pid的新的关系列表
  20. /// </summary>
  21. /// <param name="nodes"></param>
  22. /// <param name="pid"></param>
  23. /// <param name="newNodes"></param>
  24. /// <returns></returns>
  25. public async static Task<HashSet<Tnode>> ImportSyllabus(List<List<string>> nodes,string volumeId,string scope ,string code , AzureCosmosFactory azureCosmos) {
  26. List<Syllabus> syllabuses= new List<Syllabus>();
  27. string tbname = scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? Constant.School : Constant.Teacher;
  28. await foreach (var item in azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c where c.volumeId='{volumeId}'",
  29. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") })) {
  30. syllabuses.Add(item);
  31. }
  32. foreach (var points in nodes) {
  33. int level = 0;
  34. Syllabus chapter = null;
  35. foreach (var point in points) {
  36. string pt = Regex.Replace(point, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
  37. if (level == 0)
  38. {
  39. var sylbs = syllabuses.SelectMany(z => z.children).Where(c => c.pid.Equals(volumeId) && c.title.Equals(Regex.Replace(point, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ")));
  40. if (sylbs.Any())
  41. {
  42. chapter = syllabuses.Find(z=>z.id.Equals(sylbs.First().id));
  43. if (chapter == null) { }
  44. }
  45. }
  46. else {
  47. }
  48. }
  49. }
  50. return null;
  51. }
  52. /// <summary>
  53. /// 根据id查询列表串联id pid的新的关系列表
  54. /// </summary>
  55. /// <param name="nodes"></param>
  56. /// <param name="pid"></param>
  57. /// <param name="newNodes"></param>
  58. /// <returns></returns>
  59. public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
  60. bool flag = false;
  61. string spid = "";
  62. foreach (var node in nodes) {
  63. if (node.pid == pid) {
  64. newNodes.Add(node);
  65. spid = node.id;
  66. GetNewNode(nodes, spid, newNodes);
  67. flag = true;
  68. }
  69. }
  70. return newNodes;
  71. //if (flag)
  72. //{
  73. // return GetNewNode(nodes, spid, newNodes);
  74. //}
  75. //else {
  76. // return newNodes;
  77. //}
  78. }
  79. public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes,long now)
  80. {
  81. int index = 0;
  82. foreach (SyllabusTree tr in trees)
  83. {
  84. tr.order = index;
  85. index++;
  86. }
  87. trees = trees.OrderBy(x => x.order).ToList();
  88. List<Tnode> list = new List<Tnode>();
  89. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  90. trees.ForEach(x=> {
  91. List<string> cids = new List<string>();
  92. if (x.children.IsNotEmpty()) {
  93. x.children.ForEach(y => cids.Add(y.id));
  94. }
  95. var node = new Tnode
  96. {
  97. title = x.title,
  98. id = x.id,
  99. pid = x.pid,
  100. order = x.order,
  101. rnodes = x.rnodes,
  102. cids= cids,
  103. creatorId=x.creatorId,
  104. creatorName=x.creatorName,
  105. updateTime= now
  106. };
  107. list.Add(node);
  108. });
  109. nodes.AddRange(list);
  110. foreach (SyllabusTree tree in trees)
  111. {
  112. if (null != tree.children && tree.children.Count > 0)
  113. {
  114. TreeToList(tree.children, nodes,now);
  115. }
  116. }
  117. return nodes;
  118. }
  119. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  120. {
  121. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  122. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  123. var res = from r in list group r by r.id into g select g;
  124. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  125. foreach (var s in res)
  126. {
  127. blockDict.TryAdd(s.First().id, s.First());
  128. }
  129. return GetChild(list, blockDict);
  130. }
  131. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  132. {
  133. // list = list.OrderBy(m => m.Order).ToList();
  134. List<SyllabusTree> trees = new List<SyllabusTree>();
  135. trees = trees.OrderBy(x => x.order).ToList();
  136. foreach (SyllabusTree node in list)
  137. {
  138. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  139. if (flag && syllabus != null)
  140. {
  141. syllabus.children.Add(node);
  142. }
  143. else
  144. {
  145. trees.Add(node);
  146. }
  147. }
  148. return trees;
  149. }
  150. #region 开放平台使用
  151. public static List<OSyllabusTree> OListToTree(List<OTnode> noes)
  152. {
  153. List<OSyllabusTree> list = noes.ToJsonString().ToObject<List<OSyllabusTree>>();
  154. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  155. var res = from r in list group r by r.id into g select g;
  156. Dictionary<string, OSyllabusTree> blockDict = new Dictionary<string, OSyllabusTree>();
  157. foreach (var s in res)
  158. {
  159. blockDict.TryAdd(s.First().id, s.First());
  160. }
  161. return GetChildO(list, blockDict);
  162. }
  163. private static List<OSyllabusTree> GetChildO(List<OSyllabusTree> list, Dictionary<string, OSyllabusTree> dict)
  164. {
  165. // list = list.OrderBy(m => m.Order).ToList();
  166. List<OSyllabusTree> trees = new List<OSyllabusTree>();
  167. trees = trees.OrderBy(x => x.order).ToList();
  168. foreach (OSyllabusTree node in list)
  169. {
  170. bool flag = dict.TryGetValue(node.pid, out OSyllabusTree syllabus);
  171. if (flag && syllabus != null)
  172. {
  173. syllabus.children.Add(node);
  174. }
  175. else
  176. {
  177. trees.Add(node);
  178. }
  179. }
  180. return trees;
  181. }
  182. #endregion
  183. }
  184. }