SyllabusController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using TEAMModelOS.SDK;
  5. using TEAMModelOS.SDK.DI;
  6. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  7. using System;
  8. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  9. using System.Linq;
  10. using TEAMModelOS.Models;
  11. using System.Text.Json;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Models;
  14. using Microsoft.AspNetCore.Http;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using Azure.Cosmos;
  17. using TEAMModelOS.Services.Common;
  18. namespace TEAMModelOS.Controllers
  19. {
  20. [ProducesResponseType(StatusCodes.Status200OK)]
  21. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  22. //[Authorize(Roles = "IES5")]
  23. [Route("common/syllabus")]
  24. [ApiController]
  25. //[Authorize]
  26. public class SyllabusController : ControllerBase
  27. {
  28. private readonly AzureCosmosFactory _azureCosmos;
  29. public SyllabusController(AzureCosmosFactory azureCosmos)
  30. {
  31. _azureCosmos = azureCosmos;
  32. }
  33. /*
  34. {
  35. "id": "0baf00db-0768-4b62-a8f7-280f6bcebf71",
  36. "scope": "school",
  37. "trees": [
  38. {
  39. "children": [
  40. {
  41. "children": [],
  42. "id": "AC4BA269-541B-4DFC-92A5-D773068A6439",
  43. "pid": "2dfcc62e-8eea-9881-dc79-384b2f0afbec",
  44. "order": 0,
  45. "rnodes": [
  46. {
  47. "type": "doc",
  48. "id": "a2bee388-5584-72cc-1d9a-d8a77d255364",
  49. "code": "hbcn",
  50. "scope": "private",
  51. "cntr": "1595321354",
  52. "link": [
  53. "https://teammodelstorage.blob.core.chinacloudapi.cn/1595321354/doc/2020智慧課堂與智慧教研.pptx"
  54. ],
  55. "title": "2020智慧課堂與智慧教研.pptx"
  56. }
  57. ],
  58. "cids": [],
  59. "creatorId": null,
  60. "updateTime": 0,
  61. "title": "1-1 新冠疫情小贴士"
  62. }
  63. ],
  64. "id": "2dfcc62e-8eea-9881-dc79-384b2f0afbec",
  65. "pid": "0baf00db-0768-4b62-a8f7-280f6bcebf71",
  66. "order": 0,
  67. "rnodes": [
  68. {
  69. "type": "doc",
  70. "id": "cf1b4d21-66e1-e6c7-c944-30a03e419fa6",
  71. "code": "hbcn",
  72. "scope": "school",
  73. "cntr": "hbcn",
  74. "link": [
  75. "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/syllabus/IES5试卷模板制作说明(终).pdf"
  76. ],
  77. "title": "IES5试卷模板制作说明(终).pdf"
  78. },
  79. {
  80. "type": "doc",
  81. "id": "f3e82595-7340-a5fe-1004-04538ca09b86",
  82. "code": "hbcn",
  83. "scope": "school",
  84. "cntr": "hbcn",
  85. "link": [
  86. "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/syllabus/111.pdf"
  87. ],
  88. "title": "111.pdf"
  89. },
  90. {
  91. "type": "doc",
  92. "id": "e1b31639-dad9-9efb-020b-159dd045f238",
  93. "code": "hbcn",
  94. "scope": "school",
  95. "cntr": "hbcn",
  96. "link": [
  97. "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/doc/6789.pdf"
  98. ],
  99. "title": "6789.pdf"
  100. },
  101. {
  102. "type": "doc",
  103. "id": "aaeb4b5c-0450-cb4d-a1ac-244f3d115c4a",
  104. "code": "hbcn",
  105. "scope": "private",
  106. "cntr": "1595321354",
  107. "link": [
  108. "https://teammodelstorage.blob.core.chinacloudapi.cn/1595321354/doc/422北京培新活动邀请函.pdf"
  109. ],
  110. "title": "422北京培新活动邀请函.pdf"
  111. },
  112. {
  113. "type": "doc",
  114. "id": "c5f5838c-5c2f-5e9b-a734-a473cd2cad2e",
  115. "code": "hbcn",
  116. "scope": "school",
  117. "cntr": "hbcn",
  118. "link": [
  119. "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/doc/6666.pdf"
  120. ],
  121. "title": "6666.pdf"
  122. },
  123. {
  124. "type": "doc",
  125. "id": "4a106c8e-6831-5f4b-4ec3-3c18f778cab2",
  126. "code": "hbcn",
  127. "scope": "school",
  128. "cntr": "hbcn",
  129. "link": [
  130. "https://teammodelstorage.blob.core.chinacloudapi.cn/hbcn/doc/6.pdf"
  131. ],
  132. "title": "6.pdf"
  133. }
  134. ],
  135. "cids": [
  136. "AC4BA269-541B-4DFC-92A5-D773068A6439"
  137. ],
  138. "creatorId": null,
  139. "updateTime": 0,
  140. "title": "第一单元 新冠疫情防控"
  141. },
  142. {
  143. "children": [
  144. {
  145. "children": [],
  146. "id": "FC5132E1-9723-4875-B3B5-D3DC98D194FA",
  147. "pid": "4f12752c-852f-6e90-a3df-1f1f710af23d",
  148. "order": 0,
  149. "rnodes": [],
  150. "cids": [],
  151. "creatorId": null,
  152. "updateTime": 0,
  153. "title": "2-1 全球醍摩豆智慧教育研究院"
  154. },
  155. {
  156. "children": [],
  157. "id": "3CAD52BE-67B6-4EDB-8EFB-9122960D816A",
  158. "pid": "4f12752c-852f-6e90-a3df-1f1f710af23d",
  159. "order": 1,
  160. "rnodes": [],
  161. "cids": [],
  162. "creatorId": null,
  163. "updateTime": 0,
  164. "title": "2-2 醍摩豆研究院课程体系"
  165. }
  166. ],
  167. "id": "4f12752c-852f-6e90-a3df-1f1f710af23d",
  168. "pid": "0baf00db-0768-4b62-a8f7-280f6bcebf71",
  169. "order": 1,
  170. "rnodes": [],
  171. "cids": [
  172. "FC5132E1-9723-4875-B3B5-D3DC98D194FA",
  173. "3CAD52BE-67B6-4EDB-8EFB-9122960D816A"
  174. ],
  175. "creatorId": null,
  176. "updateTime": 0,
  177. "title": "第二单元 醍摩豆手册学习"
  178. }
  179. ]
  180. }
  181. */
  182. /// <summary>
  183. /// 批量保存或更新课纲
  184. ///
  185. /// </summary>
  186. /// <param name="request"></param>
  187. /// <returns></returns>
  188. [ProducesDefaultResponseType]
  189. [HttpPost("upsert-tree")]
  190. public async Task<IActionResult> SaveOrUpdateAsTree(SyllabusTreeNode request)
  191. {
  192. List<List<SyllabusTree>> tts = new List<List<SyllabusTree>>();
  193. foreach (var tree in request.trees) {
  194. Syllabus syllabus = new Syllabus();
  195. List<Tnode> nodes = new List<Tnode>();
  196. SyllabusService.TreeToList(new List<SyllabusTree> { tree }, nodes);
  197. syllabus.children = nodes;
  198. syllabus.id = !string.IsNullOrEmpty(tree.id) ? tree.id : Guid.NewGuid().ToString();
  199. syllabus.code = $"Syllabus-{request.id}";
  200. syllabus.pk = "Syllabus";
  201. syllabus.ttl = -1;
  202. syllabus.volumeId = request.id;
  203. if (request.scope == "school")
  204. {
  205. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync<Syllabus>(syllabus, new Azure.Cosmos.PartitionKey($"Syllabus-{request.id}"));
  206. }
  207. else
  208. {
  209. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<Syllabus>(syllabus, new Azure.Cosmos.PartitionKey($"Syllabus-{request.id}"));
  210. }
  211. List<SyllabusTree> trees = SyllabusService.ListToTree(nodes);
  212. tts.Add(trees);
  213. }
  214. request.trees = new List<SyllabusTree>() ;
  215. tts.ForEach(x => request.trees.AddRange(x));
  216. return Ok(request);
  217. }
  218. /// <summary>
  219. /// 查找课纲
  220. /// </summary>
  221. /// <param name="request"></param>
  222. /// <returns></returns>
  223. [ProducesDefaultResponseType]
  224. [HttpPost("find-id")]
  225. public async Task<IActionResult> Find(JsonElement request)
  226. {
  227. var client = _azureCosmos.GetCosmosClient();
  228. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  229. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  230. SyllabusTreeNode tree = null;
  231. string vcode = null;
  232. List<List<SyllabusTree>> tts = new List<List<SyllabusTree>>();
  233. try {
  234. if (scope.ToString().Equals("school"))
  235. {
  236. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c ",
  237. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") }))
  238. {
  239. vcode = item.volumeId;
  240. List<SyllabusTree> trees = SyllabusService.ListToTree(item.children);
  241. tts.Add(trees);
  242. }
  243. tree = new SyllabusTreeNode() { id = vcode, scope = "school", trees= new List<SyllabusTree>() };
  244. tts.ForEach(x => tree.trees.AddRange(x));
  245. }
  246. else
  247. {
  248. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c ",
  249. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") }))
  250. {
  251. vcode = item.volumeId;
  252. List<SyllabusTree> trees = SyllabusService.ListToTree(item.children);
  253. tts.Add(trees);
  254. }
  255. tree = new SyllabusTreeNode() { id = vcode, scope = "school", trees = new List<SyllabusTree>() };
  256. tts.ForEach(x => tree.trees.AddRange(x));
  257. }
  258. return Ok(new { tree });
  259. } catch (Exception ex) {
  260. return Ok(new { tree});
  261. }
  262. }
  263. /// <summary>
  264. /// 删除章节
  265. /// </summary>
  266. /// <param name="request"></param>
  267. /// <returns></returns>
  268. [ProducesDefaultResponseType]
  269. [HttpPost("delete")]
  270. public async Task<IActionResult> Delete(JsonElement request)
  271. {
  272. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  273. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  274. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  275. var client = _azureCosmos.GetCosmosClient();
  276. if (scope.ToString().Equals("school"))
  277. {
  278. var response = await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Syllabus-{code}"));
  279. return Ok(new { code = response.Status });
  280. }
  281. else
  282. {
  283. var response = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Syllabus-{code}"));
  284. return Ok(new { code = response.Status });
  285. }
  286. }
  287. private async Task<List<Tnode>> FindByPid(Tnode data, List<Tnode> nodes, List<Tnode> nodedata)
  288. {
  289. foreach (Tnode syllabus in nodedata)
  290. {
  291. if (syllabus.pid == data.id)
  292. {
  293. nodes.Add(syllabus);
  294. await FindByPid(syllabus, nodes, nodedata);
  295. }
  296. }
  297. return nodes;
  298. }
  299. //private async Task<List<Syllabus>> SaveOrUpdateToNodes(List<Tnode> syllabusNodes)
  300. //{
  301. // List<Syllabus> syllabuses;
  302. // var data = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReadItemAsync<Syllabus>( syllabusNodes[0].code ,new Azure.Cosmos.PartitionKey(""));
  303. // if (data.IsEmpty())
  304. // {
  305. // var syllabus = new Syllabus { id = syllabusNodes[0].code, code = syllabusNodes[0].code, children = new List<SyllabusNode>() };
  306. // await _azureCosmos.SaveOrUpdate(syllabus);
  307. // data = new List<Syllabus>() { syllabus };
  308. // }
  309. // List<SyllabusNode> syllabusNodes1 = new List<SyllabusNode>();
  310. // if (data.IsNotEmpty())
  311. // {
  312. // syllabusNodes1.AddRange(data[0].children);
  313. // //replace
  314. // for (int i = 0; i < data[0].children.Count; i++)
  315. // {
  316. // for (int j = 0; j < syllabusNodes.Count; j++)
  317. // {
  318. // if (data[0].children[i].id == syllabusNodes[j].id && data[0].children[i].code == syllabusNodes[j].code)
  319. // {
  320. // syllabusNodes1.Remove(data[0].children[i]);
  321. // syllabusNodes1.Add(syllabusNodes[j]);
  322. // syllabusNodes.Remove(syllabusNodes[j]);
  323. // }
  324. // }
  325. // }
  326. // data[0].children = syllabusNodes1;
  327. // //新增
  328. // data[0].children.AddRange(syllabusNodes);
  329. // if (data[0].children.IsNotEmpty())
  330. // {
  331. // var len = data[0].children.Count;
  332. // for (int i = 0; i < len; i++)
  333. // {
  334. // if (data[0].children[i].items.IsNotEmpty())
  335. // {
  336. // data[0].children[i].item = true;
  337. // }
  338. // if (data[0].children[i].resources.IsNotEmpty())
  339. // {
  340. // data[0].children[i].resource = true;
  341. // }
  342. // }
  343. // }
  344. // syllabuses = await _azureCosmos.SaveOrUpdateAll<Syllabus>(data);
  345. // }
  346. // else
  347. // {
  348. // throw new BizException("保存失败", ResponseCode.FAILED);
  349. // }
  350. // return syllabuses;
  351. //}
  352. }
  353. }