CourseController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 = "W", 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. school = school,
  106. desc = courseDto.desc,
  107. scope = "school",
  108. no = courseDto.no,
  109. };
  110. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
  111. }
  112. else
  113. {
  114. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{course.id}", new PartitionKey($"Course-{school}"));
  115. if (response.Status == 200)
  116. {
  117. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  118. course = jsonDocument.RootElement.ToObject<Course>();
  119. course.pk = "Course";
  120. course.name = string.IsNullOrWhiteSpace(courseDto.name) ? course.name : courseDto.name;
  121. course.subject = new SubjectSimple { id = subject.id, name = subject.name };
  122. course.period = new PeriodSimple { id = period.id, name = period.name };
  123. course.school = school;
  124. course.desc = string.IsNullOrWhiteSpace(courseDto.desc) ? course.desc : courseDto.desc;
  125. course.scope = "school";
  126. course.no = string.IsNullOrWhiteSpace(courseDto.no) ? course.no : courseDto.no;
  127. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
  128. }
  129. else {
  130. course = new Course
  131. {
  132. pk = "Course",
  133. id = Guid.NewGuid().ToString(),
  134. code = $"Course-{school}",
  135. name = courseDto.name,
  136. subject = new SubjectSimple { id = subject.id, name = subject.name },
  137. period = new PeriodSimple { id = period.id, name = period.name },
  138. school = school,
  139. desc = courseDto.desc,
  140. scope = "school",
  141. no = courseDto.no,
  142. };
  143. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
  144. }
  145. }
  146. return Ok(new { period.subjects, period.timetable, course = course });
  147. }
  148. else {
  149. return Ok(new { error =4, msg = "科目不存在!" });
  150. }
  151. }
  152. else
  153. {
  154. return Ok(new { error = 2, msg = "学段不存在!" });
  155. }
  156. }
  157. else
  158. {
  159. return Ok(new { error = 3, msg = courseDto.Valid() });
  160. }
  161. }
  162. [ProducesDefaultResponseType]
  163. [HttpPost("upsert-course-schedule")]
  164. [ApiToken(Auth = "1302", Name = "更新课程的排课信息", RW = "W", Limit = false)]
  165. public async Task<IActionResult> UpsertCourseSchedule(JsonElement json)
  166. {
  167. var (id, school) = HttpContext.GetApiTokenInfo();
  168. if (!json.TryGetProperty("courseId", out JsonElement _courseId)) { return Ok(new { error = 1, msg = "课程参数错误" }); }
  169. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  170. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_courseId}", new PartitionKey($"Course-{school}"));
  171. return Ok();
  172. }
  173. [ProducesDefaultResponseType]
  174. [HttpPost("get-course-list")]
  175. [ApiToken(Auth = "1303", Name = "获取课程列表信息", RW = "R", Limit = false)]
  176. public async Task<IActionResult> GetCourseList(JsonElement json)
  177. {
  178. var client = _azureCosmos.GetCosmosClient();
  179. var (id, school) = HttpContext.GetApiTokenInfo();
  180. json.TryGetProperty("periodId", out JsonElement periodId);
  181. json.TryGetProperty("subjectId", out JsonElement subjectId);
  182. StringBuilder sql = new StringBuilder($"SELECT c.id,c.name,c.subject,c.period,c.scope,c.no,c.school FROM c where 1=1 ");
  183. if (!string.IsNullOrWhiteSpace($"{periodId}"))
  184. {
  185. sql.Append($" and c.period.id='{periodId}'");
  186. }
  187. if (!string.IsNullOrWhiteSpace($"{subjectId}"))
  188. {
  189. sql.Append($" and c.subject.id='{subjectId}'");
  190. }
  191. List<dynamic> courses = new List<dynamic>();
  192. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  193. GetItemQueryIterator<dynamic>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  194. {
  195. courses.Add(item);
  196. }
  197. return Ok(new { courses });
  198. }
  199. [ProducesDefaultResponseType]
  200. [HttpPost("get-course-info")]
  201. [ApiToken(Auth = "1304", Name = "课程详细信息", RW = "R", Limit = false)]
  202. public async Task<IActionResult> GetCourseInfo(JsonElement json)
  203. {
  204. var (id, school) = HttpContext.GetApiTokenInfo();
  205. json.TryGetProperty("courseId", out JsonElement courseId);
  206. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
  207. .ReadItemStreamAsync($"{courseId}", new PartitionKey($"Course-{school}"));
  208. if (response.Status == 200)
  209. {
  210. JsonDocument document = JsonDocument.Parse(response.Content);
  211. Course course = document.RootElement.Deserialize<Course>();
  212. return Ok(new { course.name, course.id, course.subject, course.period, course.scope, course.school, course.no, course.desc, course.schedule });
  213. }
  214. else
  215. {
  216. return Ok(new { error = 1, msg = "课程不存在!" });
  217. }
  218. }
  219. }
  220. }