SyllabusService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.Bibliography;
  3. using DocumentFormat.OpenXml.ExtendedProperties;
  4. using DocumentFormat.OpenXml.Office2010.Excel;
  5. using DocumentFormat.OpenXml.Wordprocessing;
  6. using HTEXLib.COMM.Helpers;
  7. using Microsoft.Azure.Cosmos.Linq;
  8. using OpenXmlPowerTools;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models;
  17. using TEAMModelOS.SDK.Models.Cosmos.Common;
  18. namespace TEAMModelOS.SDK.Services
  19. {
  20. public static class SyllabusService
  21. {
  22. /// <summary>
  23. /// 根据id查询列表串联id pid的新的关系列表
  24. /// </summary>
  25. /// <param name="nodes"></param>
  26. /// <param name="pid"></param>
  27. /// <param name="newNodes"></param>
  28. /// <returns></returns>
  29. public async static Task<(List<List<IdCode>> idCodes, HashSet<Syllabus> syllabus)> ImportSyllabus(List<List<string>> nodes,string volumeId,string scope ,string code,string creatorId ,string creatorName, AzureCosmosFactory azureCosmos, bool ignoreCharacter = true) {
  30. HashSet<Syllabus> syllabuses= new HashSet<Syllabus>();
  31. string tbname = scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? Constant.School : Constant.Teacher;
  32. await foreach (var item in azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c where c.volumeId='{volumeId}'",
  33. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") })) {
  34. syllabuses.Add(item);
  35. }
  36. HashSet<Syllabus> chapters = new HashSet<Syllabus>();
  37. long now= DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  38. List<List<IdCode> > resNodes = new List<List<IdCode>>();
  39. foreach (var points in nodes) {
  40. // points 作为 层级路径 ["汽车考照", "汽车法规","驾驶道德、交通安全常识及行车安全检查与维护"]
  41. int level = 0;
  42. // 根节点 作为章节进行当前 路径操作的主要对象
  43. Syllabus chapter = null;
  44. //用于记录 ["汽车考照", "汽车法规","驾驶道德、交通安全常识及行车安全检查与维护"] 标题的id 路径
  45. List<(string title, string id)> nodesVal = new List<(string title, string id)>();
  46. //上一次操作的 id 用于方便检索下一层级时作为pid
  47. string lastId =null;
  48. foreach (var point in points) {
  49. string express = "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]";
  50. //是否开启特殊符号的正则过滤
  51. string pt = ignoreCharacter? Regex.Replace(point, express, ""):point;
  52. if (level == 0)
  53. {
  54. var sylbs = syllabuses.SelectMany(z => z.children).Where(c => c.pid.Equals(volumeId) && pt.Equals(ignoreCharacter? Regex.Replace(c.title, express, ""): c.title));
  55. if (sylbs.Any())
  56. {
  57. //找到相关的章节
  58. chapter = syllabuses.Where(z=>z.id.Equals(sylbs.First().id)).FirstOrDefault();
  59. if (chapter == null )
  60. {
  61. foreach (var item in syllabuses)
  62. {
  63. var it = item.children.Find(z => z.id.Equals(sylbs.First().id));
  64. if (it != null)
  65. {
  66. item.id = sylbs.First().id;
  67. chapter = item;
  68. chapters.Add(chapter);
  69. lastId = item.id;
  70. nodesVal.Add((point, item.id));
  71. break;
  72. }
  73. }
  74. }
  75. else {
  76. chapters.Add(chapter);
  77. lastId = chapter.id;
  78. nodesVal.Add((point, sylbs.First().id));
  79. }
  80. }
  81. else {
  82. //未找到相关的章节
  83. string id = id = Guid.NewGuid().ToString();
  84. lastId= id;
  85. chapter = new Syllabus {
  86. id = id,
  87. volumeId = volumeId,
  88. code = $"Syllabus-{code}",
  89. pk = "Syllabus",
  90. ttl = -1,
  91. scope= scope,
  92. children= new List<Tnode> { new Tnode {id = id ,pid=volumeId, creatorId = creatorId,
  93. creatorName = creatorName,title=pt,updateTime= now} }
  94. };
  95. chapters.Add(chapter);
  96. syllabuses.Add(chapter);
  97. nodesVal.Add((point, id));
  98. }
  99. //下钻一层
  100. level += 1;
  101. }
  102. else {
  103. if (!string.IsNullOrWhiteSpace(lastId))
  104. {
  105. var child = chapter.children.Find(child => child.pid.Equals(lastId) && pt.Equals(ignoreCharacter ? Regex.Replace(child.title, express, "") : child.title));
  106. if (child == null)
  107. {
  108. string id = id = Guid.NewGuid().ToString();
  109. child = new Tnode {
  110. id = id,
  111. pid = lastId,
  112. creatorId = creatorId,
  113. creatorName = creatorName,
  114. title = pt,
  115. updateTime = now
  116. };
  117. chapter.children.Add(child);
  118. lastId = child.id;
  119. nodesVal.Add((point, child.id));
  120. }
  121. else {
  122. lastId= child.id;
  123. nodesVal.Add((point, child.id));
  124. }
  125. level += 1;
  126. }
  127. }
  128. }
  129. if (chapter != null) {
  130. resNodes.Add(nodesVal.Select(z => new IdCode { id = z.id, code = z.title }).ToList());
  131. }
  132. }
  133. foreach(var chapter in chapters)
  134. {
  135. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(chapter);
  136. }
  137. return (resNodes,chapters);
  138. }
  139. /// <summary>
  140. /// 根据id查询列表串联id pid的新的关系列表
  141. /// </summary>
  142. /// <param name="nodes"></param>
  143. /// <param name="pid"></param>
  144. /// <param name="newNodes"></param>
  145. /// <returns></returns>
  146. public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
  147. bool flag = false;
  148. string spid = "";
  149. foreach (var node in nodes) {
  150. if (node.pid == pid) {
  151. newNodes.Add(node);
  152. spid = node.id;
  153. GetNewNode(nodes, spid, newNodes);
  154. flag = true;
  155. }
  156. }
  157. return newNodes;
  158. //if (flag)
  159. //{
  160. // return GetNewNode(nodes, spid, newNodes);
  161. //}
  162. //else {
  163. // return newNodes;
  164. //}
  165. }
  166. public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes,long now)
  167. {
  168. int index = 0;
  169. foreach (SyllabusTree tr in trees)
  170. {
  171. tr.order = index;
  172. index++;
  173. }
  174. trees = trees.OrderBy(x => x.order).ToList();
  175. List<Tnode> list = new List<Tnode>();
  176. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  177. trees.ForEach(x=> {
  178. List<string> cids = new List<string>();
  179. if (x.children.IsNotEmpty()) {
  180. x.children.ForEach(y => cids.Add(y.id));
  181. }
  182. var node = new Tnode
  183. {
  184. title = x.title,
  185. id = x.id,
  186. pid = x.pid,
  187. order = x.order,
  188. rnodes = x.rnodes,
  189. cids= cids,
  190. creatorId=x.creatorId,
  191. creatorName=x.creatorName,
  192. updateTime= now
  193. };
  194. list.Add(node);
  195. });
  196. nodes.AddRange(list);
  197. foreach (SyllabusTree tree in trees)
  198. {
  199. if (null != tree.children && tree.children.Count > 0)
  200. {
  201. TreeToList(tree.children, nodes,now);
  202. }
  203. }
  204. return nodes;
  205. }
  206. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  207. {
  208. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  209. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  210. var res = from r in list group r by r.id into g select g;
  211. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  212. foreach (var s in res)
  213. {
  214. blockDict.TryAdd(s.First().id, s.First());
  215. }
  216. return GetChild(list, blockDict);
  217. }
  218. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  219. {
  220. // list = list.OrderBy(m => m.Order).ToList();
  221. List<SyllabusTree> trees = new List<SyllabusTree>();
  222. trees = trees.OrderBy(x => x.order).ToList();
  223. foreach (SyllabusTree node in list)
  224. {
  225. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  226. if (flag && syllabus != null)
  227. {
  228. syllabus.children.Add(node);
  229. }
  230. else
  231. {
  232. trees.Add(node);
  233. }
  234. }
  235. return trees;
  236. }
  237. #region 开放平台使用
  238. public static List<OSyllabusTree> OListToTree(List<OTnode> noes)
  239. {
  240. List<OSyllabusTree> list = noes.ToJsonString().ToObject<List<OSyllabusTree>>();
  241. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  242. var res = from r in list group r by r.id into g select g;
  243. Dictionary<string, OSyllabusTree> blockDict = new Dictionary<string, OSyllabusTree>();
  244. foreach (var s in res)
  245. {
  246. blockDict.TryAdd(s.First().id, s.First());
  247. }
  248. return GetChildO(list, blockDict);
  249. }
  250. private static List<OSyllabusTree> GetChildO(List<OSyllabusTree> list, Dictionary<string, OSyllabusTree> dict)
  251. {
  252. // list = list.OrderBy(m => m.Order).ToList();
  253. List<OSyllabusTree> trees = new List<OSyllabusTree>();
  254. trees = trees.OrderBy(x => x.order).ToList();
  255. foreach (OSyllabusTree node in list)
  256. {
  257. bool flag = dict.TryGetValue(node.pid, out OSyllabusTree syllabus);
  258. if (flag && syllabus != null)
  259. {
  260. syllabus.children.Add(node);
  261. }
  262. else
  263. {
  264. trees.Add(node);
  265. }
  266. }
  267. return trees;
  268. }
  269. #endregion
  270. }
  271. }