123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using System.Text.Json;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Extension;
- using Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Options;
- using System.IO;
- using System.Dynamic;
- using System.Net.Http;
- using System.Net;
- using Newtonsoft.Json;
- using System.Linq;
- using StackExchange.Redis;
- using static TEAMModelOS.SDK.Models.Teacher;
- using Microsoft.Extensions.Configuration;
- using TEAMModelOS.Filter;
- using Microsoft.AspNetCore.Authorization;
- using HTEXLib.COMM.Helpers;
- using TEAMModelOS.SDK.Models.Service;
- namespace TEAMModelAPI.Controllers
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [ApiController]
- [Route("school")]
- public class CourseController : ControllerBase
- {
- public AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly IConfiguration _configuration;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- private readonly AzureServiceBusFactory _serviceBus;
- public CourseController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _azureRedis = azureRedis;
- _dingDing = dingDing;
- _option = option?.Value;
- _configuration = configuration;
- _coreAPIHttpService = coreAPIHttpService;
- _serviceBus = serviceBus;
- }
- /// <summary>
- /// 获取指定学段作息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-period-timetable")]
- [ApiToken(Auth = "1301", Name = "试卷和评测的条件信息", RW = "R", Limit = false)]
- public async Task<IActionResult> GetPaperExamCondition(JsonElement json)
- {
- json.TryGetProperty("periodId", out JsonElement _periodId);
- var (id, school) = HttpContext.GetApiTokenInfo();
- School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
- var period = data.period.Find(x => x.id.Equals($"{_periodId}"));
- if (period != null)
- {
- //1 2 3 4 5 6 7
- List<string> weekDays = new List<string> { "MON","TUE","WED","THU","FRI","SAT","SUN" };
- return Ok(new { period.subjects, period.timetable, period.grades, period.majors });
- }
- else
- {
- return Ok(new { error = 1, msg = "学段不存在!" });
- }
- }
- [ProducesDefaultResponseType]
- [HttpPost("upsert-course-info")]
- [ApiToken(Auth = "1302", Name = "课程详细信息", RW = "W", Limit = false)]
- public async Task<IActionResult> UpsertCourseInfo(JsonElement json)
- {
- var (id, school) = HttpContext.GetApiTokenInfo();
- if (!json.TryGetProperty("course", out JsonElement _course)) { return Ok(new { error = 1, msg = "课程对象不存在" }); }
- var courseDto = _course.ToObject<CourseDto>();
- Course course = null;
- if (courseDto != null && courseDto.Valid().isVaild) {
- School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
- var period = data.period.Find(x => x.id.Equals($"{courseDto.periodId}"));
- if (period != null)
- {
- var subject = period.subjects.Find(x => x.id.Equals($"{courseDto.subjectId}"));
- if (subject != null)
- {
- if (string.IsNullOrWhiteSpace(courseDto?.id))
- {
- course = new Course
- {
- pk = "Course",
- id = Guid.NewGuid().ToString(),
- code = $"Course-{school}",
- name = courseDto.name,
- subject = new SubjectSimple { id = subject.id, name = subject.name },
- period = new PeriodSimple { id = period.id, name = period.name },
- school = school,
- desc = courseDto.desc,
- scope = "school",
- no = courseDto.no,
- };
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
- }
- else
- {
- Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{course.id}", new PartitionKey($"Course-{school}"));
- if (response.Status == 200)
- {
- JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
- course = jsonDocument.RootElement.ToObject<Course>();
- course.pk = "Course";
- course.name = string.IsNullOrWhiteSpace(courseDto.name) ? course.name : courseDto.name;
- course.subject = new SubjectSimple { id = subject.id, name = subject.name };
- course.period = new PeriodSimple { id = period.id, name = period.name };
- course.school = school;
- course.desc = string.IsNullOrWhiteSpace(courseDto.desc) ? course.desc : courseDto.desc;
- course.scope = "school";
- course.no = string.IsNullOrWhiteSpace(courseDto.no) ? course.no : courseDto.no;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
- }
- else {
- course = new Course
- {
- pk = "Course",
- id = Guid.NewGuid().ToString(),
- code = $"Course-{school}",
- name = courseDto.name,
- subject = new SubjectSimple { id = subject.id, name = subject.name },
- period = new PeriodSimple { id = period.id, name = period.name },
- school = school,
- desc = courseDto.desc,
- scope = "school",
- no = courseDto.no,
- };
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
- }
- }
- return Ok(new { period.subjects, period.timetable, course = course });
- }
- else {
- return Ok(new { error =4, msg = "科目不存在!" });
- }
- }
- else
- {
- return Ok(new { error = 2, msg = "学段不存在!" });
- }
- }
- else
- {
- return Ok(new { error = 3, msg = courseDto.Valid() });
- }
- }
- [ProducesDefaultResponseType]
- [HttpPost("upsert-course-schedule")]
- [ApiToken(Auth = "1302", Name = "更新课程的排课信息", RW = "W", Limit = false)]
- public async Task<IActionResult> UpsertCourseSchedule(JsonElement json)
- {
- var (id, school) = HttpContext.GetApiTokenInfo();
- if (!json.TryGetProperty("courseId", out JsonElement _courseId)) { return Ok(new { error = 1, msg = "课程参数错误" }); }
- if (!json.TryGetProperty("schedules", out JsonElement _schedules) || !_schedules.ValueKind.Equals(JsonValueKind.Array)) { return Ok(new { error = 1, msg = "排课参数错误" }); }
- School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
- Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_courseId}", new PartitionKey($"Course-{school}"));
- List<Schedule> schedules= _schedules.ToObject<List<Schedule>>();
- if (schedules.IsNotEmpty() && schedules.Valid().isVaild)
- {
- //检查排课时段相同的情况
- Dictionary<string, List<Schedule>> dict = new Dictionary<string, List<Schedule>>();
- schedules.ForEach(item => {
- item.time.ForEach(x => {
- string key = $"{item.teacherId}-{x.id}-{x.week}";
- if (dict.ContainsKey(key))
- {
- dict[key].Add(item);
- }
- else {
- dict[key] = new List<Schedule> { item };
- }
- });
- });
- //教学班
- var stulist_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.stulist));
- //行政班
- var classId_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId));
- //处理教室和教学班名单,教师,同时存在且相同,重复的课堂
- var stulist_schedules_hasRoom = stulist_schedules.Where(x => !string.IsNullOrWhiteSpace(x.room));
- //处理教学班名单,教师,同时存在且相同,重复的课堂
- var stulist_schedules_noRoom = stulist_schedules.Where(x => !string.IsNullOrWhiteSpace(x.room));
- //处理教室和行政班名单,教师,同时存在且相同,重复的课堂
- var classId_schedules_hasRoom = stulist_schedules_hasRoom.Where(x => !string.IsNullOrWhiteSpace(x.room));
- //处理行政班名单,教师,同时存在且相同,重复的课堂
- var classId_schedules_noRoom = stulist_schedules_hasRoom.Where(x => !string.IsNullOrWhiteSpace(x.room));
-
- return Ok();
- }
- else {
- return Ok(new { error = 1, msg = "排课参数错误" });
-
- }
-
- }
- [ProducesDefaultResponseType]
- [HttpPost("get-course-list")]
- [ApiToken(Auth = "1303", Name = "获取课程列表信息", RW = "R", Limit = false)]
- public async Task<IActionResult> GetCourseList(JsonElement json)
- {
- var client = _azureCosmos.GetCosmosClient();
- var (id, school) = HttpContext.GetApiTokenInfo();
- json.TryGetProperty("periodId", out JsonElement periodId);
- json.TryGetProperty("subjectId", out JsonElement subjectId);
- StringBuilder sql = new StringBuilder($"SELECT c.id,c.name,c.subject,c.period,c.scope,c.no,c.school FROM c where 1=1 ");
- if (!string.IsNullOrWhiteSpace($"{periodId}"))
- {
- sql.Append($" and c.period.id='{periodId}'");
- }
- if (!string.IsNullOrWhiteSpace($"{subjectId}"))
- {
- sql.Append($" and c.subject.id='{subjectId}'");
- }
- List<dynamic> courses = new List<dynamic>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
- GetItemQueryIterator<dynamic>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
- {
- courses.Add(item);
- }
- return Ok(new { courses });
- }
- [ProducesDefaultResponseType]
- [HttpPost("get-course-info")]
- [ApiToken(Auth = "1304", Name = "课程详细信息", RW = "R", Limit = false)]
- public async Task<IActionResult> GetCourseInfo(JsonElement json)
- {
- var (id, school) = HttpContext.GetApiTokenInfo();
- json.TryGetProperty("courseId", out JsonElement courseId);
- Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
- .ReadItemStreamAsync($"{courseId}", new PartitionKey($"Course-{school}"));
- if (response.Status == 200)
- {
- JsonDocument document = JsonDocument.Parse(response.Content);
- Course course = document.RootElement.Deserialize<Course>();
- return Ok(new { course.name, course.id, course.subject, course.period, course.scope, course.school, course.no, course.desc, course.schedule });
- }
- else
- {
- return Ok(new { error = 1, msg = "课程不存在!" });
- }
- }
- }
- }
|