CrazyIter_Bin vor 3 Jahren
Ursprung
Commit
be7a70ee28

+ 211 - 26
TEAMModelAPI/Controllers/School/CourseController.cs

@@ -41,6 +41,8 @@ namespace TEAMModelAPI.Controllers
         private readonly IConfiguration _configuration;
         private readonly IConfiguration _configuration;
         private readonly CoreAPIHttpService _coreAPIHttpService;
         private readonly CoreAPIHttpService _coreAPIHttpService;
         private readonly AzureServiceBusFactory _serviceBus;
         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)
         public CourseController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus)
         {
         {
             _azureCosmos = azureCosmos;
             _azureCosmos = azureCosmos;
@@ -68,9 +70,8 @@ namespace TEAMModelAPI.Controllers
             var period = data.period.Find(x => x.id.Equals($"{_periodId}"));
             var period = data.period.Find(x => x.id.Equals($"{_periodId}"));
             if (period != null)
             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 });
+              
+                return Ok(new { period.subjects, period.timetable, period.grades, period.majors , weekDays });
             }
             }
             else
             else
             {
             {
@@ -128,7 +129,7 @@ namespace TEAMModelAPI.Controllers
                                 course.desc = string.IsNullOrWhiteSpace(courseDto.desc) ? course.desc : courseDto.desc;
                                 course.desc = string.IsNullOrWhiteSpace(courseDto.desc) ? course.desc : courseDto.desc;
                                 course.scope = "school";
                                 course.scope = "school";
                                 course.no = string.IsNullOrWhiteSpace(courseDto.no) ? course.no : courseDto.no;
                                 course.no = string.IsNullOrWhiteSpace(courseDto.no) ? course.no : courseDto.no;
-                                await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(course, new PartitionKey(course.code));
+                                await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(course,course.id, new PartitionKey(course.code));
                             }
                             }
                             else {
                             else {
                                 course = new Course
                                 course = new Course
@@ -164,50 +165,234 @@ namespace TEAMModelAPI.Controllers
             }
             }
         }
         }
 
 
+        public class IptCourse { 
+            public string courseId { get; set; }
+            public List<Schedule> schedules { get; set; }
+        
+        }
+
+        public class ScheduleDto : Schedule {
+            public ScheduleDto(Schedule schedule,string courseId) { 
+                this.room= schedule.room;
+                this.courseId = courseId;
+                this.time = schedule.time;
+                this.stulist = schedule.stulist;
+                this.classId =schedule.classId;
+                this.teacherId = schedule.teacherId;
+            }
+            public string courseId { get; set; }
+        }
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
         [HttpPost("upsert-course-schedule")]
         [HttpPost("upsert-course-schedule")]
         [ApiToken(Auth = "1302", Name = "更新课程的排课信息", RW = "W", Limit = false)]
         [ApiToken(Auth = "1302", Name = "更新课程的排课信息", RW = "W", Limit = false)]
         public async Task<IActionResult> UpsertCourseSchedule(JsonElement json)
         public async Task<IActionResult> UpsertCourseSchedule(JsonElement json)
         {
         {
             var (id, school) = HttpContext.GetApiTokenInfo();
             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 = "排课参数错误" }); }
+            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});
+            }
             School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
             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)
+            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> schedules = new List<ScheduleDto>() ;
+            iptcourses.ToList().ForEach(x => {
+                x.schedules.ForEach(z => {
+                    schedules.Add(new ScheduleDto(z, x.courseId));
+                });
+            });
+            if (schedules.IsNotEmpty())
             {
             {
-                //检查排课时段相同的情况
-                Dictionary<string, List<Schedule>> dict = new Dictionary<string, List<Schedule>>();
+                //排查没用选用名单的排课
+                var noListSchedules = schedules.Where(x => string.IsNullOrWhiteSpace(x.stulist) && string.IsNullOrWhiteSpace(x.stulist));
+                //排查没有任课教师的排课,使用Valid 时已经验证
+                //var noTeacherSchedules = schedules.Where(x => string.IsNullOrWhiteSpace(x.teacherId));
+                //检查教师是否在同一天且上课时间段冲突的排课
+                Dictionary<string, List<Schedule>> teacher_check = new Dictionary<string, List<Schedule>>();
                 schedules.ForEach(item => {
                 schedules.ForEach(item => {
                     item.time.ForEach(x => {
                     item.time.ForEach(x => {
-                    string key = $"{item.teacherId}-{x.id}-{x.week}";
-                        if (dict.ContainsKey(key))
+                        
+                        string key = $"{item.teacherId}-{x.id}-{x.week}";//教师id-时间段id-星期几
+                        if (teacher_check.ContainsKey(key))
                         {
                         {
-                            dict[key].Add(item);
+                            teacher_check[key].Add(item);
                         }
                         }
                         else {
                         else {
-                            dict[key] = new List<Schedule> { item };
+                            teacher_check[key] = new List<Schedule> { item };
                         }
                         }
                     });
                     });
                 });
                 });
-
+                //最终能进入下一个条件匹配的排课
+                HashSet<Schedule> saveSchedules = new HashSet<Schedule>();
+                //检查排课冲突的大集合,包含教师,行政班,教学班,冲突的
+             
+                HashSet<Schedule> repeatSchedules = new HashSet<Schedule>();
+                //检查教师有安排冲突的
+                HashSet<Schedule> repeatTeahcerSchedules = new HashSet<Schedule>();
+                teacher_check.Values.ToList().ForEach(x => {
+                    if (x.Count > 1)
+                    {
+                        x.ForEach(y => {
+                            repeatTeahcerSchedules.Add(y);
+                            repeatSchedules.Add(y);
+                        });
+                    }
+                    else {
+                        saveSchedules.Add(x.First());
+                    }
+                });
                 //教学班
                 //教学班
                 var stulist_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.stulist));
                 var stulist_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.stulist));
+                Dictionary<string, List<Schedule>> stulist_check = new Dictionary<string, List<Schedule>>();
+                stulist_schedules.ToList().ForEach(item => {
+                    item.time.ForEach(x => {
+                        string key = $"{item.stulist}-{x.id}-{x.week}";//教师id-时间段id-星期几
+                        if (stulist_check.ContainsKey(key))
+                        {
+                            stulist_check[key].Add(item);
+                        }
+                        else
+                        {
+                            stulist_check[key] = new List<Schedule> { item };
+                        }
+                    });
+                });
+                //检查教学班和行政班有冲突的
+                HashSet<Schedule> repeatListSchedules = new HashSet<Schedule>();
+                stulist_check.Values.ToList().ForEach(x => {
+                    if (x.Count > 1)
+                    {
+                        x.ForEach(y => {
+                            repeatSchedules.Add(y);
+                            repeatListSchedules.Add(y);
+                        });
+
+                    }
+                    else
+                    {   
+                        var schedule = x.First();
+                        if (!repeatSchedules.Contains(schedule))
+                        {
+                            saveSchedules.Add(schedule);
+                        }
+                        else {
+                            repeatSchedules.Add(schedule);
+                        }
+                    }
+                });
                 //行政班
                 //行政班
                 var classId_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId));
                 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));
+                Dictionary<string, List<Schedule>> classId_check = new Dictionary<string, List<Schedule>>();
+                classId_schedules.ToList().ForEach(item => {
+                    item.time.ForEach(x => {
+                        string key = $"{item.classId}-{x.id}-{x.week}";//教师id-时间段id-星期几
+                        if (classId_check.ContainsKey(key))
+                        {
+                            classId_check[key].Add(item);
+                        }
+                        else
+                        {
+                            classId_check[key] = new List<Schedule> { item };
+                        }
+                    });
+                });
+                classId_check.Values.ToList().ForEach(x => {
+                    if (x.Count > 1)
+                    {
+                        x.ForEach(y => {
+                            repeatSchedules.Add(y);
+                            repeatListSchedules.Add(y);
+                        });
 
 
-                //处理教室和行政班名单,教师,同时存在且相同,重复的课堂
-                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));
+                    }
+                    else
+                    {
+                        var schedule = x.First();
+                        if (!repeatSchedules.Contains(schedule))
+                        {
+                            saveSchedules.Add(schedule);
+                        }
+                        else
+                        {
+                            repeatSchedules.Add(schedule);
+                        }
+                    }
+                });
+                //教室资源占用情况冲突。
 
 
-             
-                return Ok();
+                var room_schedules = schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId));
+                Dictionary<string, List<Schedule>> room_check = new Dictionary<string, List<Schedule>>();
+                room_schedules.ToList().ForEach(item => {
+                    item.time.ForEach(x => {
+                        string key = $"{item.classId}-{x.id}-{x.week}";//教师id-时间段id-星期几
+                        if (room_check.ContainsKey(key))
+                        {
+                            room_check[key].Add(item);
+                        }
+                        else
+                        {
+                            room_check[key] = new List<Schedule> { item };
+                        }
+                    });
+                });
+                //检查教室有冲突的
+                HashSet<Schedule> repeatRoomSchedules = new HashSet<Schedule>();
+                room_check.Values.ToList().ForEach(x => {
+                    if (x.Count > 1)
+                    {
+                        x.ForEach(y => {
+                            repeatSchedules.Add(y);
+                            repeatRoomSchedules.Add(y);
+                        });
+
+                    }
+                    else
+                    {
+                        var schedule = x.First();
+                        if (!repeatSchedules.Contains(schedule))
+                        {
+                            saveSchedules.Add(schedule);
+                        }
+                        else
+                        {
+                            repeatSchedules.Add(schedule);
+                        }
+                    }
+                });
+                schedules.RemoveAll(x => repeatListSchedules.Contains(x));
+                schedules.RemoveAll(x => repeatRoomSchedules.Contains(x));
+                schedules.RemoveAll(x => repeatTeahcerSchedules.Contains(x));
+                ////处理教室和教学班名单,教师,同时存在且相同,重复的课堂
+                //var stulist_schedules_hasRoom = saveSchedules.Where(x =>  !string.IsNullOrWhiteSpace(x.stulist) &&  !string.IsNullOrWhiteSpace(x.room));
+                ////处理教学班名单,教师,同时存在且相同,重复的课堂
+                //var stulist_schedules_noRoom = stulist_schedules.Where(x => !string.IsNullOrWhiteSpace(x.stulist) &&  string.IsNullOrWhiteSpace(x.room));
+
+                ////处理教室和行政班名单,教师,同时存在且相同,重复的课堂
+                //var classId_schedules_hasRoom = classId_schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId) && !string.IsNullOrWhiteSpace(x.room));
+                ////处理行政班名单,教师,同时存在且相同,重复的课堂
+                //var classId_schedules_noRoom = classId_schedules.Where(x => !string.IsNullOrWhiteSpace(x.classId)  &&  string.IsNullOrWhiteSpace(x.room));
+                return Ok(new { 
+                    notinCourseIds ,//课程不存在的排课
+                    noListSchedules,//没有教学名单的排课,包含行政班,教学班
+                    repeatTeahcerSchedules,//教师在同一课时有冲突的排课
+                    repeatListSchedules,//行政班或教学班在同一课时段有冲突的排课
+
+                });
             }
             }
             else { 
             else { 
                 return Ok(new { error = 1, msg = "排课参数错误" }); 
                 return Ok(new { error = 1, msg = "排课参数错误" }); 

+ 2 - 0
TEAMModelOS.SDK/Models/Cosmos/School/Course.cs

@@ -60,6 +60,8 @@ namespace TEAMModelOS.SDK.Models
         /// </summary>
         /// </summary>
         [Required(ErrorMessage = "school 必须设置")]
         [Required(ErrorMessage = "school 必须设置")]
         public string school { get; set; }
         public string school { get; set; }
+        //哪一学年的课程
+        public int  year { get; set; }
 
 
     }
     }
 
 

+ 46 - 3
TEAMModelOS/Controllers/Common/HomeworkController.cs

@@ -24,6 +24,7 @@ using System.IO;
 using System.Linq;
 using System.Linq;
 using HTEXLib.COMM.Helpers;
 using HTEXLib.COMM.Helpers;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Authorization;
+using TEAMModelOS.SDK.Models.Service;
 
 
 namespace TEAMModelOS.Controllers.Learn
 namespace TEAMModelOS.Controllers.Learn
 {
 {
@@ -49,8 +50,9 @@ namespace TEAMModelOS.Controllers.Learn
         private readonly AzureStorageFactory _azureStorage;
         private readonly AzureStorageFactory _azureStorage;
         private readonly IHttpClientFactory _clientFactory;
         private readonly IHttpClientFactory _clientFactory;
         private readonly CoreAPIHttpService _coreAPIHttpService;
         private readonly CoreAPIHttpService _coreAPIHttpService;
+         private readonly NotificationService _notificationService;
         public IConfiguration _configuration { get; set; }
         public IConfiguration _configuration { get; set; }
-        public HomeworkController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
+        public HomeworkController(CoreAPIHttpService coreAPIHttpService, NotificationService notificationService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
             AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory, IConfiguration configuration)
             AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory, IConfiguration configuration)
         {
         {
             _snowflakeId = snowflakeId;
             _snowflakeId = snowflakeId;
@@ -62,7 +64,8 @@ namespace TEAMModelOS.Controllers.Learn
             _azureStorage = azureStorage;
             _azureStorage = azureStorage;
             _clientFactory = clientFactory;
             _clientFactory = clientFactory;
             _configuration = configuration;
             _configuration = configuration;
-            _coreAPIHttpService = coreAPIHttpService;
+            _coreAPIHttpService = coreAPIHttpService; 
+            _notificationService = notificationService;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -677,8 +680,28 @@ namespace TEAMModelOS.Controllers.Learn
                                         {
                                         {
                                            
                                            
                                         }
                                         }
+                                        
                                         await StatisticsService.SendServiceBus( ($"{standard}", new List<string> { $"{one.id}" }, $"{school}", new List<string>() { StatisticsService.OfflineRecord },0)  , _configuration, _serviceBus, client);
                                         await StatisticsService.SendServiceBus( ($"{standard}", new List<string> { $"{one.id}" }, $"{school}", new List<string>() { StatisticsService.OfflineRecord },0)  , _configuration, _serviceBus, client);
-                                    }
+                                        string bizcode = "submitanswer";
+                                        if (string.IsNullOrWhiteSpace(homework.creatorId)) {
+                                            Notification notification = new Notification
+                                            {
+                                                hubName = "hita",
+                                                type = "msg",
+                                                from = $"ies5:{_option.Location}:private",
+                                                to = new List<string> { homework.creatorId },
+                                                label = $"{bizcode}_homework",
+                                                body = new { location = _option.Location, biz = bizcode, tmdid = one.id, tmdname = one.name, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
+                                                expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
+                                            };
+                                            var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
+                                            var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
+                                            var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
+                                            var location = _option.Location;
+                                            var code = await _notificationService.SendNotification(clientID, clientSecret, location, url, notification);
+                                        }
+                                        
+                                    } 
                                 }
                                 }
                             }
                             }
 
 
@@ -693,6 +716,26 @@ namespace TEAMModelOS.Controllers.Learn
                                     await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<HomeworkRecord>(record, $"{_id}", new PartitionKey(partitionKey));
                                     await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<HomeworkRecord>(record, $"{_id}", new PartitionKey(partitionKey));
                                     taskStatus = 1;
                                     taskStatus = 1;
                                     msgid = 1;
                                     msgid = 1;
+
+                                    string bizcode = "submitanswer";
+                                    if (string.IsNullOrWhiteSpace(homework.creatorId))
+                                    {
+                                        Notification notification = new Notification
+                                        {
+                                            hubName = "hita",
+                                            type = "msg",
+                                            from = $"ies5:{_option.Location}:private",
+                                            to = new List<string> { homework.creatorId },
+                                            label = $"{bizcode}_homework",
+                                            body = new { location = _option.Location, biz = bizcode, tmdid = userid, tmdname =name , status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
+                                            expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
+                                        };
+                                        var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
+                                        var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
+                                        var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
+                                        var location = _option.Location;
+                                        var code = await _notificationService.SendNotification(clientID, clientSecret, location, url, notification);
+                                    }
                                 }
                                 }
                                 catch (CosmosException ex)
                                 catch (CosmosException ex)
                                 {
                                 {