123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- 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;
- using System.ComponentModel.DataAnnotations;
- 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;
- //1 2 3 4 5 6 7
- private List<string> weekDays = new List<string> { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
- 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)
- {
-
- return Ok(new { period.subjects, period.timetable, period.grades, period.majors , weekDays });
- }
- 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).ReplaceItemAsync(course,course.id, 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() });
- }
- }
- public class IptCourse {
- public string courseId { get; set; }
- public List<Schedule> schedules { get; set; }
-
- }
-
- [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("courses", out JsonElement _courses)) { return Ok(new { error = 1, msg = "课程参数错误!" }); }
- IEnumerable<IptCourse> iptcourses= _courses.ToObject<List<IptCourse>>();
- var result = iptcourses.Valid();
- if (!result.isVaild) {
- return Ok(new { error = 2, msg =result});
- }
-
- HashSet<string> courseIds= iptcourses.Select(x => x.courseId).ToHashSet();
- if (courseIds.Count < 1) { return Ok(new { error = 1, msg = "课程参数错误!" }); }
- //string sql = $"select value(c) from c where c.id in({string.Join(",",courseIds.Select(x=>$"'{x}'"))})";
- string sql = $"select value(c) from c ";//直接获取全校的课程
- List<Course> courses = new List<Course>();
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
- .GetItemQueryIterator<Course>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{school}") })) {
- courses.Add(item);
- }
- //不存在的课程
- var notInCourseIds= courseIds.Except(courses.Select(x => x.id));
- iptcourses=iptcourses.Where(x => !notInCourseIds.Contains(x.courseId));
- //排查 课程学段,课程排课作息,课程排课的星期几是否准确
- List<ScheduleDto> import_schedules = new List<ScheduleDto>() ;
- //保存没有选用名单的排课。
- List<Schedule> schedules_noList= new List<Schedule>() ;
- List<ScheduleDto> import_weeksConfuse = new List<ScheduleDto>();
- iptcourses.ToList().ForEach(x => {
- x.schedules.ForEach(z => {
- if (!string.IsNullOrWhiteSpace(z.classId) || !string.IsNullOrWhiteSpace(z.stulist))
- {
- string classId = null;
- //行政班不为空,教学班为空,则名单取行政班
- classId = !string.IsNullOrWhiteSpace(z.classId) && string.IsNullOrWhiteSpace(z.stulist) ? z.classId : classId;
- //行政班为空,教学班不为空,则名单取教学班
- classId = string.IsNullOrWhiteSpace(z.classId) && !string.IsNullOrWhiteSpace(z.stulist) ? z.stulist : classId;
- //行政班,教学班都不为空,且相同,则任取一个,取的是行政班
- classId = !string.IsNullOrWhiteSpace(z.classId) && !string.IsNullOrWhiteSpace(z.stulist) && z.classId.Equals(z.stulist) ? z.classId : classId;
- //行政班,教学班都不为空,且不同,则取null
- classId = !string.IsNullOrWhiteSpace(z.classId) && !string.IsNullOrWhiteSpace(z.stulist) && !z.classId.Equals(z.stulist) ? null : classId;
- if (!string.IsNullOrWhiteSpace(classId))
- {
- z.time.ForEach(t =>
- {
- ScheduleDto scheduleDto = new ScheduleDto
- {
- courseId = x.courseId,
- roomId = z.room,
- classId = z.classId,
- stulist = z.stulist,
- teacherId = z.teacherId,
- timeId = t.id,
- week = t.week,
- keyTeacher = $"{z.teacherId}_{t.week}_{t.id}",
- keyGroupId = $"{classId}_{t.week}_{t.id}",
- keyRoomIds = string.IsNullOrWhiteSpace(z.room) ? null : $"{z.room}_{t.week}_{t.id}"
- };
- //星期几自检 1 2 3 4 5 6 7
- if (weekDays.Contains(t.week))
- {
- import_schedules.Add(scheduleDto);
- }
- else {
- import_weeksConfuse.Add(scheduleDto);
- }
-
- });
- }
- else { schedules_noList.Add(z); }
- }
- else {
- schedules_noList.Add(z);
- }
- });
- });
- //导入的排课自检。
- //教师自检
- var check_teacher = import_schedules.GroupBy(x => x.keyTeacher).Select(g => new { key = g.Key, list = g.ToList() });
- IEnumerable<ScheduleDto> import_teacherConfuse = new List<ScheduleDto>();
- import_teacherConfuse = check_teacher.Where(x => x.list.Count > 1).SelectMany(x => x.list);
- import_schedules.RemoveAll(x => import_teacherConfuse.Contains(x));
- //名单自检
- var check_groupId = import_schedules.GroupBy(x => x.keyGroupId).Select(g => new { key = g.Key, list = g.ToList() });
- IEnumerable<ScheduleDto> import_groupIdConfuse = new List<ScheduleDto>();
- import_groupIdConfuse = check_groupId.Where(x => x.list.Count > 1).SelectMany(x => x.list);
- import_schedules.RemoveAll(x => import_groupIdConfuse.Contains(x));
- //物理教室自检
- var check_roomIds = import_schedules.Where(r=>!string.IsNullOrWhiteSpace(r.keyRoomIds)).GroupBy(x => x.keyRoomIds).Select(g => new { key = g.Key, list = g.ToList() });
- IEnumerable<ScheduleDto> import_roomIdsConfuse = new List<ScheduleDto>();
- import_roomIdsConfuse = check_roomIds.Where(x => x.list.Count > 1).SelectMany(x => x.list);
- import_schedules.RemoveAll(x => import_roomIdsConfuse.Contains(x));
- //打散数据库已经有的排课信息
- List<ScheduleDto> database_schedules = new List<ScheduleDto>();
- courses.ForEach(x => {
- x.schedule.ForEach(z => {
- if (!string.IsNullOrWhiteSpace(z.teacherId) &&(!string.IsNullOrWhiteSpace(z.classId) || !string.IsNullOrWhiteSpace(z.stulist)))
- {
- string classId = null;
- //行政班不为空,教学班为空,则名单取行政班
- classId = !string.IsNullOrWhiteSpace(z.classId) && string.IsNullOrWhiteSpace(z.stulist) ? z.classId : classId;
- //行政班为空,教学班不为空,则名单取教学班
- classId = string.IsNullOrWhiteSpace(z.classId) && !string.IsNullOrWhiteSpace(z.stulist) ? z.stulist : classId;
- //行政班,教学班都不为空,且相同,则任取一个,取的是行政班
- classId = !string.IsNullOrWhiteSpace(z.classId) && !string.IsNullOrWhiteSpace(z.stulist) && z.classId.Equals(z.stulist) ? z.classId : classId;
- //行政班,教学班都不为空,且不同,则取null
- classId = !string.IsNullOrWhiteSpace(z.classId) && !string.IsNullOrWhiteSpace(z.stulist) && !z.classId.Equals(z.stulist) ? null : classId;
- if (!string.IsNullOrWhiteSpace(classId))
- {
- z.time.ForEach(t =>
- {
- ScheduleDto scheduleDto = new ScheduleDto
- {
- courseId = x.id,
- roomId = z.room,
- classId = z.classId,
- stulist = z.stulist,
- teacherId = z.teacherId,
- timeId = t.id,
- week = t.week,
- keyTeacher = $"{z.teacherId}_{t.week}_{t.id}",
- keyGroupId = $"{classId}_{t.week}_{t.id}",
- keyRoomIds = string.IsNullOrWhiteSpace(z.room) ? null : $"{z.room}_{t.week}_{t.id}"
- };
- database_schedules.Add(scheduleDto);
- });
- }
- }
- });
- });
- List<ScheduleDto> database_teacherConfuse = new List<ScheduleDto>();
- List<ScheduleDto> database_groupIdConfuse = new List<ScheduleDto>();
- List<ScheduleDto> database_roomIdsConfuse = new List<ScheduleDto>();
- import_schedules.ForEach(x => {
- //检查教师的排课是否冲突
- if (database_schedules.FindAll(s => s.keyTeacher.Equals(x.keyTeacher)).IsNotEmpty())
- {
- database_teacherConfuse.Add(x);
- }
- //检查名单的排课是否冲突
- if (database_schedules.FindAll(s => s.keyGroupId.Equals(x.keyGroupId)).IsNotEmpty())
- {
- database_groupIdConfuse.Add(x);
- }
- //检查教室的排课是否冲突
- if (database_schedules.FindAll(s => s.keyRoomIds.Equals(x.keyRoomIds)).IsNotEmpty())
- {
- database_roomIdsConfuse.Add(x);
- }
- });
- //移除 教师,名单,教室冲突的排课
- import_schedules.RemoveAll(x => database_teacherConfuse.Contains(x));
- import_schedules.RemoveAll(x => database_groupIdConfuse.Contains(x));
- import_schedules.RemoveAll(x => database_roomIdsConfuse.Contains(x));
- //最终导入之前,必须检查,课程是否存在(notInCourseIds),教师是否存在,名单是否存在,并重新排列行政班,教学班,
- //排课时间段id是否正确,星期几是否正确(import_weeksConfuse),教室是否正确
- School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
- //检查教师存在的
- HashSet<string> teachers = import_schedules.Select(x => x.teacherId).ToHashSet();
- if (teachers.Count > 0) {
-
- }
- //检查教师存在的
- HashSet<string> roomIds = import_schedules.Select(x => x.roomId).ToHashSet();
- if (roomIds.Count > 0)
- {
- }
- //检查名单存在的
- List<string> groupIds = new List<string>();
- var classIds = import_schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId)).Select(x => x.classId).ToHashSet();
- if (classIds.Any()) {
- groupIds.AddRange(classIds);
- }
- var stulists = import_schedules.Where(x => !string.IsNullOrWhiteSpace(x.stulist)).Select(x => x.stulist).ToHashSet();
- if (stulists.Any())
- {
- groupIds.AddRange(stulists);
- }
- return Ok(new {
- import_check= new {
- import_groupIdConfuse,//名单冲突的排课
- import_roomIdsConfuse,//物理教室冲突的排课
- import_teacherConfuse,//教室冲突的排课
- import_weeksConfuse },//错误的星期几编码
- database_check= new { },
- });
- }
- [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 = "课程不存在!" });
- }
- }
- }
- }
|