CourseController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.Models;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.SDK.DI;
  9. using System.Text.Json;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.Extension;
  12. using Azure.Cosmos;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Options;
  15. using System.IO;
  16. using System.Dynamic;
  17. using System.Net.Http;
  18. using System.Net;
  19. using Newtonsoft.Json;
  20. using System.Linq;
  21. using StackExchange.Redis;
  22. using static TEAMModelOS.SDK.Models.Teacher;
  23. using Microsoft.Extensions.Configuration;
  24. using TEAMModelOS.Filter;
  25. using Microsoft.AspNetCore.Authorization;
  26. using HTEXLib.COMM.Helpers;
  27. using TEAMModelOS.SDK.Models.Service;
  28. namespace TEAMModelAPI.Controllers
  29. {
  30. [ProducesResponseType(StatusCodes.Status200OK)]
  31. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  32. [ApiController]
  33. [Route("school")]
  34. public class CourseController : ControllerBase
  35. {
  36. public AzureCosmosFactory _azureCosmos;
  37. private readonly AzureStorageFactory _azureStorage;
  38. private readonly AzureRedisFactory _azureRedis;
  39. private readonly DingDing _dingDing;
  40. private readonly Option _option;
  41. private readonly IConfiguration _configuration;
  42. private readonly CoreAPIHttpService _coreAPIHttpService;
  43. private readonly AzureServiceBusFactory _serviceBus;
  44. public CourseController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus)
  45. {
  46. _azureCosmos = azureCosmos;
  47. _azureStorage = azureStorage;
  48. _azureRedis = azureRedis;
  49. _dingDing = dingDing;
  50. _option = option?.Value;
  51. _configuration = configuration;
  52. _coreAPIHttpService = coreAPIHttpService;
  53. _serviceBus = serviceBus;
  54. }
  55. /// <summary>
  56. /// 获取指定学段作息
  57. /// </summary>
  58. /// <param name="request"></param>
  59. /// <returns></returns>
  60. [ProducesDefaultResponseType]
  61. [HttpPost("get-period-timetable")]
  62. [ApiToken(Auth = "1301", Name = "试卷和评测的条件信息", RW = "R", Limit = false)]
  63. public async Task<IActionResult> GetPaperExamCondition(JsonElement json)
  64. {
  65. json.TryGetProperty("periodId", out JsonElement _periodId);
  66. var (id, school) = HttpContext.GetApiTokenInfo();
  67. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  68. var period = data.period.Find(x => x.id.Equals($"{_periodId}"));
  69. if (period != null)
  70. {
  71. return Ok(new { period.subjects, period.timetable, period.grades, period.majors });
  72. }
  73. else
  74. {
  75. return Ok(new { error = 1, msg = "学段不存在!" });
  76. }
  77. }
  78. [ProducesDefaultResponseType]
  79. [HttpPost("upsert-course-info")]
  80. [ApiToken(Auth = "1302", Name = "课程详细信息", RW = "R", Limit = false)]
  81. public async Task<IActionResult> UpsertCourseInfo(JsonElement json)
  82. {
  83. var (id, school) = HttpContext.GetApiTokenInfo();
  84. if (!json.TryGetProperty("course", out JsonElement _course)) { return Ok(new { error = 1, msg = "课程对象不存在" }); }
  85. var courseDto = _course.ToObject<CourseDto>();
  86. Course course = null;
  87. if (courseDto != null && courseDto.Valid().isVaild) {
  88. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  89. var period = data.period.Find(x => x.id.Equals($"{courseDto.periodId}"));
  90. if (period != null)
  91. {
  92. var subject = period.subjects.Find(x => x.id.Equals($"{courseDto.subjectId}"));
  93. if (subject != null)
  94. {
  95. if (string.IsNullOrWhiteSpace(courseDto?.id))
  96. {
  97. course = new Course
  98. {
  99. pk = "Course",
  100. id = Guid.NewGuid().ToString(),
  101. code = $"Course-{school}",
  102. name = courseDto.name,
  103. subject = new SubjectSimple { id = subject.id, name = subject.name },
  104. period= new PeriodSimple { id = period.id, name = period.name },
  105. };
  106. }
  107. else
  108. {
  109. }
  110. return Ok(new { period.subjects, period.timetable, period.grades, period.majors });
  111. }
  112. else {
  113. return Ok(new { error = 2, msg = "科目不存在!" });
  114. }
  115. }
  116. else
  117. {
  118. return Ok(new { error = 2, msg = "学段不存在!" });
  119. }
  120. }
  121. else
  122. {
  123. return Ok(new { error = 3, msg = courseDto.Valid() });
  124. }
  125. }
  126. [ProducesDefaultResponseType]
  127. [HttpPost("get-course-list")]
  128. [ApiToken(Auth = "1303", Name = "获取课程列表信息", RW = "R", Limit = false)]
  129. public async Task<IActionResult> GetCourseList(JsonElement json)
  130. {
  131. var client = _azureCosmos.GetCosmosClient();
  132. var (id, school) = HttpContext.GetApiTokenInfo();
  133. json.TryGetProperty("periodId", out JsonElement periodId);
  134. json.TryGetProperty("subjectId", out JsonElement subjectId);
  135. StringBuilder sql = new StringBuilder($"SELECT c.id,c.name,c.subject,c.period,c.scope,c.no,c.school FROM c where 1=1 ");
  136. if (!string.IsNullOrWhiteSpace($"{periodId}"))
  137. {
  138. sql.Append($" and c.period.id='{periodId}'");
  139. }
  140. if (!string.IsNullOrWhiteSpace($"{subjectId}"))
  141. {
  142. sql.Append($" and c.subject.id='{subjectId}'");
  143. }
  144. List<dynamic> courses = new List<dynamic>();
  145. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  146. GetItemQueryIterator<dynamic>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  147. {
  148. courses.Add(item);
  149. }
  150. return Ok(new { courses });
  151. }
  152. [ProducesDefaultResponseType]
  153. [HttpPost("get-course-info")]
  154. [ApiToken(Auth = "1304", Name = "课程详细信息", RW = "R", Limit = false)]
  155. public async Task<IActionResult> GetCourseInfo(JsonElement json)
  156. {
  157. var (id, school) = HttpContext.GetApiTokenInfo();
  158. json.TryGetProperty("courseId", out JsonElement courseId);
  159. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
  160. .ReadItemStreamAsync($"{courseId}", new PartitionKey($"Course-{school}"));
  161. if (response.Status == 200)
  162. {
  163. JsonDocument document = JsonDocument.Parse(response.Content);
  164. Course course = document.RootElement.Deserialize<Course>();
  165. return Ok(new { course.name, course.id, course.subject, course.period, course.scope, course.school, course.no, course.desc, course.schedule });
  166. }
  167. else
  168. {
  169. return Ok(new { error = 1, msg = "课程不存在!" });
  170. }
  171. }
  172. }
  173. }