SyllabusService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.Drawing;
  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, List<SyllabusTreeNode> tree)> 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. Volume volume = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<Volume>($"{volumeId}", new PartitionKey($"Volume-{code}"));
  37. HashSet<Syllabus> chapters = new HashSet<Syllabus>();
  38. long now= DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  39. List<List<IdCode> > resNodes = new List<List<IdCode>>();
  40. foreach (var points in nodes) {
  41. // points 作为 层级路径 ["汽车考照", "汽车法规","驾驶道德、交通安全常识及行车安全检查与维护"]
  42. int level = 0;
  43. // 根节点 作为章节进行当前 路径操作的主要对象
  44. Syllabus chapter = null;
  45. //用于记录 ["汽车考照", "汽车法规","驾驶道德、交通安全常识及行车安全检查与维护"] 标题的id 路径
  46. List<(string title, string id)> nodesVal = new List<(string title, string id)>();
  47. //上一次操作的 id 用于方便检索下一层级时作为pid
  48. string lastId =null;
  49. foreach (var point in points) {
  50. string express = "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]";
  51. //是否开启特殊符号的正则过滤
  52. string pt = ignoreCharacter? Regex.Replace(point, express, ""):point;
  53. if (level == 0)
  54. {
  55. var sylbs = syllabuses.SelectMany(z => z.children).Where(c => c.pid.Equals(volumeId) && pt.Equals(ignoreCharacter? Regex.Replace(c.title, express, ""): c.title));
  56. if (sylbs.Any())
  57. {
  58. //找到相关的章节
  59. chapter = syllabuses.Where(z=>z.id.Equals(sylbs.First().id)).FirstOrDefault();
  60. if (chapter == null )
  61. {
  62. foreach (var item in syllabuses)
  63. {
  64. var it = item.children.Find(z => z.id.Equals(sylbs.First().id));
  65. if (it != null)
  66. {
  67. item.id = sylbs.First().id;
  68. chapter = item;
  69. chapters.Add(chapter);
  70. lastId = item.id;
  71. nodesVal.Add((point, item.id));
  72. break;
  73. }
  74. }
  75. }
  76. else {
  77. chapters.Add(chapter);
  78. lastId = chapter.id;
  79. nodesVal.Add((point, sylbs.First().id));
  80. }
  81. }
  82. else {
  83. //未找到相关的章节
  84. string id = id = Guid.NewGuid().ToString();
  85. lastId= id;
  86. chapter = new Syllabus {
  87. id = id,
  88. volumeId = volumeId,
  89. code = $"Syllabus-{code}",
  90. pk = "Syllabus",
  91. ttl = -1,
  92. scope= scope,
  93. children= new List<Tnode> { new Tnode {id = id ,pid=volumeId, creatorId = creatorId,
  94. creatorName = creatorName,title=pt,updateTime= now} }
  95. };
  96. chapters.Add(chapter);
  97. syllabuses.Add(chapter);
  98. nodesVal.Add((point, id));
  99. }
  100. //下钻一层
  101. level += 1;
  102. }
  103. else {
  104. if (!string.IsNullOrWhiteSpace(lastId))
  105. {
  106. var child = chapter.children.Find(child => child.pid.Equals(lastId) && pt.Equals(ignoreCharacter ? Regex.Replace(child.title, express, "") : child.title));
  107. if (child == null)
  108. {
  109. string id = id = Guid.NewGuid().ToString();
  110. child = new Tnode {
  111. id = id,
  112. pid = lastId,
  113. creatorId = creatorId,
  114. creatorName = creatorName,
  115. title = pt,
  116. updateTime = now
  117. };
  118. chapter.children.Add(child);
  119. lastId = child.id;
  120. nodesVal.Add((point, child.id));
  121. }
  122. else {
  123. lastId= child.id;
  124. nodesVal.Add((point, child.id));
  125. }
  126. level += 1;
  127. }
  128. }
  129. }
  130. if (chapter != null) {
  131. resNodes.Add(nodesVal.Select(z => new IdCode { id = z.id, code = z.title }).ToList());
  132. }
  133. }
  134. bool volumeChange=false;
  135. List<SyllabusTreeNode> treeNodes = new List<SyllabusTreeNode>();
  136. foreach (var chapter in chapters)
  137. {
  138. if (!volume.syllabusIds.Contains(chapter.id)) {
  139. volume.syllabusIds.Add(chapter.id);
  140. volumeChange = true;
  141. }
  142. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(chapter);
  143. List<SyllabusTree> trees = SyllabusService.ListToTree(chapter.children);
  144. SyllabusTreeNode tree = new SyllabusTreeNode() { id = chapter.id, scope = chapter.scope, trees = trees, volumeId = chapter.volumeId, auth = chapter.auth, codeval = code };
  145. treeNodes.Add(tree);
  146. }
  147. if (volumeChange)
  148. {
  149. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(volume, volume.id, new PartitionKey(volume.code));
  150. }
  151. List<SyllabusTreeNode> redt = new List<SyllabusTreeNode>();
  152. //对课纲树形结构排序
  153. if (volume.syllabusIds.IsNotEmpty())
  154. {
  155. volume.syllabusIds.ForEach(x =>
  156. {
  157. for (int index = 0; index < treeNodes.Count; index++)
  158. {
  159. if (treeNodes[index].id == x)
  160. {
  161. redt.Add(treeNodes[index]);
  162. treeNodes.RemoveAt(index);
  163. }
  164. }
  165. });
  166. redt.AddRange(treeNodes);
  167. return (resNodes, redt);
  168. }
  169. else {
  170. return (resNodes, treeNodes);
  171. }
  172. }
  173. /// <summary>
  174. /// 根据id查询列表串联id pid的新的关系列表
  175. /// </summary>
  176. /// <param name="nodes"></param>
  177. /// <param name="pid"></param>
  178. /// <param name="newNodes"></param>
  179. /// <returns></returns>
  180. public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
  181. bool flag = false;
  182. string spid = "";
  183. foreach (var node in nodes) {
  184. if (node.pid == pid) {
  185. newNodes.Add(node);
  186. spid = node.id;
  187. GetNewNode(nodes, spid, newNodes);
  188. flag = true;
  189. }
  190. }
  191. return newNodes;
  192. //if (flag)
  193. //{
  194. // return GetNewNode(nodes, spid, newNodes);
  195. //}
  196. //else {
  197. // return newNodes;
  198. //}
  199. }
  200. public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes,long now)
  201. {
  202. int index = 0;
  203. foreach (SyllabusTree tr in trees)
  204. {
  205. tr.order = index;
  206. index++;
  207. }
  208. trees = trees.OrderBy(x => x.order).ToList();
  209. List<Tnode> list = new List<Tnode>();
  210. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  211. trees.ForEach(x=> {
  212. List<string> cids = new List<string>();
  213. if (x.children.IsNotEmpty()) {
  214. x.children.ForEach(y => cids.Add(y.id));
  215. }
  216. var node = new Tnode
  217. {
  218. title = x.title,
  219. id = x.id,
  220. pid = x.pid,
  221. order = x.order,
  222. rnodes = x.rnodes,
  223. cids= cids,
  224. creatorId=x.creatorId,
  225. creatorName=x.creatorName,
  226. updateTime= now
  227. };
  228. list.Add(node);
  229. });
  230. nodes.AddRange(list);
  231. foreach (SyllabusTree tree in trees)
  232. {
  233. if (null != tree.children && tree.children.Count > 0)
  234. {
  235. TreeToList(tree.children, nodes,now);
  236. }
  237. }
  238. return nodes;
  239. }
  240. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  241. {
  242. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  243. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  244. var res = from r in list group r by r.id into g select g;
  245. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  246. foreach (var s in res)
  247. {
  248. blockDict.TryAdd(s.First().id, s.First());
  249. }
  250. return GetChild(list, blockDict);
  251. }
  252. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  253. {
  254. // list = list.OrderBy(m => m.Order).ToList();
  255. List<SyllabusTree> trees = new List<SyllabusTree>();
  256. trees = trees.OrderBy(x => x.order).ToList();
  257. foreach (SyllabusTree node in list)
  258. {
  259. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  260. if (flag && syllabus != null)
  261. {
  262. syllabus.children.Add(node);
  263. }
  264. else
  265. {
  266. trees.Add(node);
  267. }
  268. }
  269. return trees;
  270. }
  271. #region 开放平台使用
  272. public static List<OSyllabusTree> OListToTree(List<OTnode> noes)
  273. {
  274. List<OSyllabusTree> list = noes.ToJsonString().ToObject<List<OSyllabusTree>>();
  275. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  276. var res = from r in list group r by r.id into g select g;
  277. Dictionary<string, OSyllabusTree> blockDict = new Dictionary<string, OSyllabusTree>();
  278. foreach (var s in res)
  279. {
  280. blockDict.TryAdd(s.First().id, s.First());
  281. }
  282. return GetChildO(list, blockDict);
  283. }
  284. private static List<OSyllabusTree> GetChildO(List<OSyllabusTree> list, Dictionary<string, OSyllabusTree> dict)
  285. {
  286. // list = list.OrderBy(m => m.Order).ToList();
  287. List<OSyllabusTree> trees = new List<OSyllabusTree>();
  288. trees = trees.OrderBy(x => x.order).ToList();
  289. foreach (OSyllabusTree node in list)
  290. {
  291. bool flag = dict.TryGetValue(node.pid, out OSyllabusTree syllabus);
  292. if (flag && syllabus != null)
  293. {
  294. syllabus.children.Add(node);
  295. }
  296. else
  297. {
  298. trees.Add(node);
  299. }
  300. }
  301. return trees;
  302. }
  303. #endregion
  304. }
  305. }