SyllabusService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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,string grant_type, 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. if (!string.IsNullOrWhiteSpace(grant_type) && grant_type.Equals("upsert")) {
  143. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(chapter);
  144. }
  145. List<SyllabusTree> trees = SyllabusService.ListToTree(chapter.children);
  146. SyllabusTreeNode tree = new SyllabusTreeNode() { id = chapter.id, scope = chapter.scope, trees = trees, volumeId = chapter.volumeId, auth = chapter.auth, codeval = code };
  147. treeNodes.Add(tree);
  148. }
  149. if (volumeChange)
  150. {
  151. await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(volume, volume.id, new PartitionKey(volume.code));
  152. }
  153. List<SyllabusTreeNode> redt = new List<SyllabusTreeNode>();
  154. //对课纲树形结构排序
  155. if (volume.syllabusIds.IsNotEmpty())
  156. {
  157. volume.syllabusIds.ForEach(x =>
  158. {
  159. for (int index = 0; index < treeNodes.Count; index++)
  160. {
  161. if (treeNodes[index].id == x)
  162. {
  163. redt.Add(treeNodes[index]);
  164. treeNodes.RemoveAt(index);
  165. }
  166. }
  167. });
  168. redt.AddRange(treeNodes);
  169. return (resNodes, redt);
  170. }
  171. else {
  172. return (resNodes, treeNodes);
  173. }
  174. }
  175. /// <summary>
  176. /// 根据id查询列表串联id pid的新的关系列表
  177. /// </summary>
  178. /// <param name="nodes"></param>
  179. /// <param name="pid"></param>
  180. /// <param name="newNodes"></param>
  181. /// <returns></returns>
  182. public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
  183. bool flag = false;
  184. string spid = "";
  185. foreach (var node in nodes) {
  186. if (node.pid == pid) {
  187. newNodes.Add(node);
  188. spid = node.id;
  189. GetNewNode(nodes, spid, newNodes);
  190. flag = true;
  191. }
  192. }
  193. return newNodes;
  194. //if (flag)
  195. //{
  196. // return GetNewNode(nodes, spid, newNodes);
  197. //}
  198. //else {
  199. // return newNodes;
  200. //}
  201. }
  202. public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes,long now)
  203. {
  204. int index = 0;
  205. foreach (SyllabusTree tr in trees)
  206. {
  207. tr.order = index;
  208. index++;
  209. }
  210. trees = trees.OrderBy(x => x.order).ToList();
  211. List<Tnode> list = new List<Tnode>();
  212. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  213. trees.ForEach(x=> {
  214. List<string> cids = new List<string>();
  215. if (x.children.IsNotEmpty()) {
  216. x.children.ForEach(y => cids.Add(y.id));
  217. }
  218. var node = new Tnode
  219. {
  220. title = x.title,
  221. id = x.id,
  222. pid = x.pid,
  223. order = x.order,
  224. rnodes = x.rnodes,
  225. cids= cids,
  226. creatorId=x.creatorId,
  227. creatorName=x.creatorName,
  228. updateTime= now
  229. };
  230. list.Add(node);
  231. });
  232. nodes.AddRange(list);
  233. foreach (SyllabusTree tree in trees)
  234. {
  235. if (null != tree.children && tree.children.Count > 0)
  236. {
  237. TreeToList(tree.children, nodes,now);
  238. }
  239. }
  240. return nodes;
  241. }
  242. public static List<SyllabusTree> ListToTree(List<Tnode> noes)
  243. {
  244. List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
  245. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  246. var res = from r in list group r by r.id into g select g;
  247. Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
  248. foreach (var s in res)
  249. {
  250. blockDict.TryAdd(s.First().id, s.First());
  251. }
  252. return GetChild(list, blockDict);
  253. }
  254. private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
  255. {
  256. // list = list.OrderBy(m => m.Order).ToList();
  257. List<SyllabusTree> trees = new List<SyllabusTree>();
  258. trees = trees.OrderBy(x => x.order).ToList();
  259. foreach (SyllabusTree node in list)
  260. {
  261. bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
  262. if (flag && syllabus != null)
  263. {
  264. syllabus.children.Add(node);
  265. }
  266. else
  267. {
  268. trees.Add(node);
  269. }
  270. }
  271. return trees;
  272. }
  273. #region 开放平台使用
  274. public static List<OSyllabusTree> OListToTree(List<OTnode> noes)
  275. {
  276. List<OSyllabusTree> list = noes.ToJsonString().ToObject<List<OSyllabusTree>>();
  277. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  278. var res = from r in list group r by r.id into g select g;
  279. Dictionary<string, OSyllabusTree> blockDict = new Dictionary<string, OSyllabusTree>();
  280. foreach (var s in res)
  281. {
  282. blockDict.TryAdd(s.First().id, s.First());
  283. }
  284. return GetChildO(list, blockDict);
  285. }
  286. private static List<OSyllabusTree> GetChildO(List<OSyllabusTree> list, Dictionary<string, OSyllabusTree> dict)
  287. {
  288. // list = list.OrderBy(m => m.Order).ToList();
  289. List<OSyllabusTree> trees = new List<OSyllabusTree>();
  290. trees = trees.OrderBy(x => x.order).ToList();
  291. foreach (OSyllabusTree node in list)
  292. {
  293. bool flag = dict.TryGetValue(node.pid, out OSyllabusTree syllabus);
  294. if (flag && syllabus != null)
  295. {
  296. syllabus.children.Add(node);
  297. }
  298. else
  299. {
  300. trees.Add(node);
  301. }
  302. }
  303. return trees;
  304. }
  305. #endregion
  306. }
  307. }