CourseController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. //1 2 3 4 5 6 7
  72. List<string> weekDays = new List<string> { "MON","TUE","WED","THU","FRI","SAT","SUN" };
  73. return Ok(new { period.subjects, period.timetable, period.grades, period.majors });
  74. }
  75. else
  76. {
  77. return Ok(new { error = 1, msg = "学段不存在!" });
  78. }
  79. }
  80. [ProducesDefaultResponseType]
  81. [HttpPost("upsert-course-info")]
  82. [ApiToken(Auth = "1302", Name = "课程详细信息", RW = "W", Limit = false)]
  83. public async Task<IActionResult> UpsertCourseInfo(JsonElement json)
  84. {
  85. var (id, school) = HttpContext.GetApiTokenInfo();
  86. if (!json.TryGetProperty("course", out JsonElement _course)) { return Ok(new { error = 1, msg = "课程对象不存在" }); }
  87. var courseDto = _course.ToObject<CourseDto>();
  88. Course course = null;
  89. if (courseDto != null && courseDto.Valid().isVaild) {
  90. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  91. var period = data.period.Find(x => x.id.Equals($"{courseDto.periodId}"));
  92. if (period != null)
  93. {
  94. var subject = period.subjects.Find(x => x.id.Equals($"{courseDto.subjectId}"));
  95. if (subject != null)
  96. {
  97. if (string.IsNullOrWhiteSpace(courseDto?.id))
  98. {
  99. course = new Course
  100. {
  101. pk = "Course",
  102. id = Guid.NewGuid().ToString(),
  103. code = $"Course-{school}",
  104. name = courseDto.name,
  105. subject = new SubjectSimple { id = subject.id, name = subject.name },
  106. period = new PeriodSimple { id = period.id, name = period.name },
  107. school = school,
  108. desc = courseDto.desc,
  109. scope = "school",
  110. no = courseDto.no,
  111. };
  112. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
  113. }
  114. else
  115. {
  116. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{course.id}", new PartitionKey($"Course-{school}"));
  117. if (response.Status == 200)
  118. {
  119. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  120. course = jsonDocument.RootElement.ToObject<Course>();
  121. course.pk = "Course";
  122. course.name = string.IsNullOrWhiteSpace(courseDto.name) ? course.name : courseDto.name;
  123. course.subject = new SubjectSimple { id = subject.id, name = subject.name };
  124. course.period = new PeriodSimple { id = period.id, name = period.name };
  125. course.school = school;
  126. course.desc = string.IsNullOrWhiteSpace(courseDto.desc) ? course.desc : courseDto.desc;
  127. course.scope = "school";
  128. course.no = string.IsNullOrWhiteSpace(courseDto.no) ? course.no : courseDto.no;
  129. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
  130. }
  131. else {
  132. course = new Course
  133. {
  134. pk = "Course",
  135. id = Guid.NewGuid().ToString(),
  136. code = $"Course-{school}",
  137. name = courseDto.name,
  138. subject = new SubjectSimple { id = subject.id, name = subject.name },
  139. period = new PeriodSimple { id = period.id, name = period.name },
  140. school = school,
  141. desc = courseDto.desc,
  142. scope = "school",
  143. no = courseDto.no,
  144. };
  145. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
  146. }
  147. }
  148. return Ok(new { period.subjects, period.timetable, course = course });
  149. }
  150. else {
  151. return Ok(new { error =4, msg = "科目不存在!" });
  152. }
  153. }
  154. else
  155. {
  156. return Ok(new { error = 2, msg = "学段不存在!" });
  157. }
  158. }
  159. else
  160. {
  161. return Ok(new { error = 3, msg = courseDto.Valid() });
  162. }
  163. }
  164. [ProducesDefaultResponseType]
  165. [HttpPost("upsert-course-schedule")]
  166. [ApiToken(Auth = "1302", Name = "更新课程的排课信息", RW = "W", Limit = false)]
  167. public async Task<IActionResult> UpsertCourseSchedule(JsonElement json)
  168. {
  169. var (id, school) = HttpContext.GetApiTokenInfo();
  170. if (!json.TryGetProperty("courseId", out JsonElement _courseId)) { return Ok(new { error = 1, msg = "课程参数错误" }); }
  171. if (!json.TryGetProperty("schedules", out JsonElement _schedules) || !_schedules.ValueKind.Equals(JsonValueKind.Array)) { return Ok(new { error = 1, msg = "排课参数错误" }); }
  172. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  173. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_courseId}", new PartitionKey($"Course-{school}"));
  174. List<Schedule> schedules= _schedules.ToObject<List<Schedule>>();
  175. if (schedules.IsNotEmpty() && schedules.Valid().isVaild)
  176. {
  177. //检查排课时段相同的情况
  178. Dictionary<string, List<Schedule>> dict = new Dictionary<string, List<Schedule>>();
  179. schedules.ForEach(item => {
  180. item.time.ForEach(x => {
  181. string key = $"{item.teacherId}-{x.id}-{x.week}";
  182. if (dict.ContainsKey(key))
  183. {
  184. dict[key].Add(item);
  185. }
  186. else {
  187. dict[key] = new List<Schedule> { item };
  188. }
  189. });
  190. });
  191. //教学班
  192. var stulist_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.stulist));
  193. //行政班
  194. var classId_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId));
  195. //处理教室和教学班名单,教师,同时存在且相同,重复的课堂
  196. var stulist_schedules_hasRoom = stulist_schedules.Where(x => !string.IsNullOrWhiteSpace(x.room));
  197. //处理教学班名单,教师,同时存在且相同,重复的课堂
  198. var stulist_schedules_noRoom = stulist_schedules.Where(x => !string.IsNullOrWhiteSpace(x.room));
  199. //处理教室和行政班名单,教师,同时存在且相同,重复的课堂
  200. var classId_schedules_hasRoom = stulist_schedules_hasRoom.Where(x => !string.IsNullOrWhiteSpace(x.room));
  201. //处理行政班名单,教师,同时存在且相同,重复的课堂
  202. var classId_schedules_noRoom = stulist_schedules_hasRoom.Where(x => !string.IsNullOrWhiteSpace(x.room));
  203. return Ok();
  204. }
  205. else {
  206. return Ok(new { error = 1, msg = "排课参数错误" });
  207. }
  208. }
  209. [ProducesDefaultResponseType]
  210. [HttpPost("get-course-list")]
  211. [ApiToken(Auth = "1303", Name = "获取课程列表信息", RW = "R", Limit = false)]
  212. public async Task<IActionResult> GetCourseList(JsonElement json)
  213. {
  214. var client = _azureCosmos.GetCosmosClient();
  215. var (id, school) = HttpContext.GetApiTokenInfo();
  216. json.TryGetProperty("periodId", out JsonElement periodId);
  217. json.TryGetProperty("subjectId", out JsonElement subjectId);
  218. StringBuilder sql = new StringBuilder($"SELECT c.id,c.name,c.subject,c.period,c.scope,c.no,c.school FROM c where 1=1 ");
  219. if (!string.IsNullOrWhiteSpace($"{periodId}"))
  220. {
  221. sql.Append($" and c.period.id='{periodId}'");
  222. }
  223. if (!string.IsNullOrWhiteSpace($"{subjectId}"))
  224. {
  225. sql.Append($" and c.subject.id='{subjectId}'");
  226. }
  227. List<dynamic> courses = new List<dynamic>();
  228. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  229. GetItemQueryIterator<dynamic>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  230. {
  231. courses.Add(item);
  232. }
  233. return Ok(new { courses });
  234. }
  235. [ProducesDefaultResponseType]
  236. [HttpPost("get-course-info")]
  237. [ApiToken(Auth = "1304", Name = "课程详细信息", RW = "R", Limit = false)]
  238. public async Task<IActionResult> GetCourseInfo(JsonElement json)
  239. {
  240. var (id, school) = HttpContext.GetApiTokenInfo();
  241. json.TryGetProperty("courseId", out JsonElement courseId);
  242. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
  243. .ReadItemStreamAsync($"{courseId}", new PartitionKey($"Course-{school}"));
  244. if (response.Status == 200)
  245. {
  246. JsonDocument document = JsonDocument.Parse(response.Content);
  247. Course course = document.RootElement.Deserialize<Course>();
  248. return Ok(new { course.name, course.id, course.subject, course.period, course.scope, course.school, course.no, course.desc, course.schedule });
  249. }
  250. else
  251. {
  252. return Ok(new { error = 1, msg = "课程不存在!" });
  253. }
  254. }
  255. }
  256. }