SyllabusService.cs 14 KB

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