SyllabusService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.ExtendedProperties;
  3. using DocumentFormat.OpenXml.Office2010.Excel;
  4. using DocumentFormat.OpenXml.Wordprocessing;
  5. using HTEXLib.COMM.Helpers;
  6. using Microsoft.Azure.Cosmos.Linq;
  7. using OpenXmlPowerTools;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Models;
  16. using TEAMModelOS.SDK.Models.Cosmos.Common;
  17. namespace TEAMModelOS.SDK.Services
  18. {
  19. public static class SyllabusService
  20. {
  21. /// <summary>
  22. /// 根据id查询列表串联id pid的新的关系列表
  23. /// </summary>
  24. /// <param name="nodes"></param>
  25. /// <param name="pid"></param>
  26. /// <param name="newNodes"></param>
  27. /// <returns></returns>
  28. 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) {
  29. HashSet<Syllabus> syllabuses= new HashSet<Syllabus>();
  30. string tbname = scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? Constant.School : Constant.Teacher;
  31. await foreach (var item in azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c where c.volumeId='{volumeId}'",
  32. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") })) {
  33. syllabuses.Add(item);
  34. }
  35. Volume volume = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<Volume>($"{volumeId}", new PartitionKey($"Volume-{code}"));
  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. bool volumeChange=false;
  134. foreach (var chapter in chapters)
  135. {
  136. if (!volume.syllabusIds.Contains(chapter.id)) {
  137. volume.syllabusIds.Add(chapter.id);
  138. volumeChange = true;
  139. }
  140. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(chapter);
  141. }
  142. if (volumeChange) {
  143. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(volume,volume.id,new PartitionKey(volume.code));
  144. }
  145. return (resNodes,chapters);
  146. }
  147. /// <summary>
  148. /// 根据id查询列表串联id pid的新的关系列表
  149. /// </summary>
  150. /// <param name="nodes"></param>
  151. /// <param name="pid"></param>
  152. /// <param name="newNodes"></param>
  153. /// <returns></returns>
  154. public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
  155. bool flag = false;
  156. string spid = "";
  157. foreach (var node in nodes) {
  158. if (node.pid == pid) {
  159. newNodes.Add(node);
  160. spid = node.id;
  161. GetNewNode(nodes, spid, newNodes);
  162. flag = true;
  163. }
  164. }
  165. return newNodes;
  166. //if (flag)
  167. //{
  168. // return GetNewNode(nodes, spid, newNodes);
  169. //}
  170. //else {
  171. // return newNodes;
  172. //}
  173. }
  174. public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes,long now)
  175. {
  176. int index = 0;
  177. foreach (SyllabusTree tr in trees)
  178. {
  179. tr.order = index;
  180. index++;
  181. }
  182. trees = trees.OrderBy(x => x.order).ToList();
  183. List<Tnode> list = new List<Tnode>();
  184. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  185. trees.ForEach(x=> {
  186. List<string> cids = new List<string>();
  187. if (x.children.IsNotEmpty()) {
  188. x.children.ForEach(y => cids.Add(y.id));
  189. }
  190. var node = new Tnode
  191. {
  192. title = x.title,
  193. id = x.id,
  194. pid = x.pid,
  195. order = x.order,
  196. rnodes = x.rnodes,
  197. cids= cids,
  198. creatorId=x.creatorId,
  199. creatorName=x.creatorName,
  200. updateTime= now
  201. };
  202. list.Add(node);
  203. });
  204. nodes.AddRange(list);
  205. foreach (SyllabusTree tree in trees)
  206. {
  207. if (null != tree.children && tree.children.Count > 0)
  208. {
  209. TreeToList(tree.children, nodes,now);
  210. }
  211. }
  212. return nodes;
  213. }
  214. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  215. {
  216. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  217. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  218. var res = from r in list group r by r.id into g select g;
  219. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  220. foreach (var s in res)
  221. {
  222. blockDict.TryAdd(s.First().id, s.First());
  223. }
  224. return GetChild(list, blockDict);
  225. }
  226. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  227. {
  228. // list = list.OrderBy(m => m.Order).ToList();
  229. List<SyllabusTree> trees = new List<SyllabusTree>();
  230. trees = trees.OrderBy(x => x.order).ToList();
  231. foreach (SyllabusTree node in list)
  232. {
  233. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  234. if (flag && syllabus != null)
  235. {
  236. syllabus.children.Add(node);
  237. }
  238. else
  239. {
  240. trees.Add(node);
  241. }
  242. }
  243. return trees;
  244. }
  245. #region 开放平台使用
  246. public static List<OSyllabusTree> OListToTree(List<OTnode> noes)
  247. {
  248. List<OSyllabusTree> list = noes.ToJsonString().ToObject<List<OSyllabusTree>>();
  249. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  250. var res = from r in list group r by r.id into g select g;
  251. Dictionary<string, OSyllabusTree> blockDict = new Dictionary<string, OSyllabusTree>();
  252. foreach (var s in res)
  253. {
  254. blockDict.TryAdd(s.First().id, s.First());
  255. }
  256. return GetChildO(list, blockDict);
  257. }
  258. private static List<OSyllabusTree> GetChildO(List<OSyllabusTree> list, Dictionary<string, OSyllabusTree> dict)
  259. {
  260. // list = list.OrderBy(m => m.Order).ToList();
  261. List<OSyllabusTree> trees = new List<OSyllabusTree>();
  262. trees = trees.OrderBy(x => x.order).ToList();
  263. foreach (OSyllabusTree node in list)
  264. {
  265. bool flag = dict.TryGetValue(node.pid, out OSyllabusTree syllabus);
  266. if (flag && syllabus != null)
  267. {
  268. syllabus.children.Add(node);
  269. }
  270. else
  271. {
  272. trees.Add(node);
  273. }
  274. }
  275. return trees;
  276. }
  277. #endregion
  278. }
  279. }