CrazyIter_Bin 3 년 전
부모
커밋
fe4956d61c

+ 155 - 185
TEAMModelAPI/Controllers/School/CourseController.cs

@@ -25,6 +25,8 @@ 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)]
@@ -171,17 +173,7 @@ namespace TEAMModelAPI.Controllers
         
         }
 
-        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]
         [HttpPost("upsert-course-schedule")]
         [ApiToken(Auth = "1302", Name = "更新课程的排课信息", RW = "W", Limit = false)]
@@ -194,7 +186,7 @@ namespace TEAMModelAPI.Controllers
             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"));
+         
             HashSet<string> courseIds= iptcourses.Select(x => x.courseId).ToHashSet();
             if (courseIds.Count < 1) { return Ok(new { error = 1, msg = "课程参数错误!" }); }
 
@@ -206,198 +198,176 @@ namespace TEAMModelAPI.Controllers
                 courses.Add(item);
             }
             //不存在的课程
-            var notinCourseIds= courseIds.Except(courses.Select(x => x.id));
-            iptcourses=iptcourses.Where(x => !notinCourseIds.Contains(x.courseId));
+            var notInCourseIds= courseIds.Except(courses.Select(x => x.id));
+            iptcourses=iptcourses.Where(x => !notInCourseIds.Contains(x.courseId));
 
             //排查 课程学段,课程排课作息,课程排课的星期几是否准确
-            List<ScheduleDto> schedules = new List<ScheduleDto>() ;
+            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 => {
-                    schedules.Add(new ScheduleDto(z, x.courseId));
-                });
-            });
-            if (schedules.IsNotEmpty())
-            {
-                //排查没用选用名单的排课
-                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 => {
-                    item.time.ForEach(x => {
-                        
-                        string key = $"{item.teacherId}-{x.id}-{x.week}";//教师id-时间段id-星期几
-                        if (teacher_check.ContainsKey(key))
-                        {
-                            teacher_check[key].Add(item);
-                        }
-                        else {
-                            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));
-                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));
-                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)
+                    if (!string.IsNullOrWhiteSpace(z.classId) || !string.IsNullOrWhiteSpace(z.stulist))
                     {
-                        x.ForEach(y => {
-                            repeatSchedules.Add(y);
-                            repeatListSchedules.Add(y);
-                        });
-
-                    }
-                    else
-                    {
-                        var schedule = x.First();
-                        if (!repeatSchedules.Contains(schedule))
-                        {
-                            saveSchedules.Add(schedule);
-                        }
-                        else
+                        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))
                         {
-                            repeatSchedules.Add(schedule);
+                            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));
 
-                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
+            //打散数据库已经有的排课信息
+            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)))
                     {
-                        var schedule = x.First();
-                        if (!repeatSchedules.Contains(schedule))
+                        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))
                         {
-                            saveSchedules.Add(schedule);
-                        }
-                        else
-                        {
-                            repeatSchedules.Add(schedule);
+                            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);
+                            });
                         }
                     }
                 });
-                //移除可能重复的(导入数据自检不能通过的)。剩下的是可以保存的,但是需要再次对数据库已经有的排课信息进行去重。
-                schedules.RemoveAll(x => repeatSchedules.Contains(x));
-
-
-                schedules.ToList().ForEach(schedule => {
-                    
-                });
-                return Ok(new
+            });
+            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()) 
                 {
-                    importCheckError = new
-                    {
-                        notinCourseIds,//课程不存在的排课
-                        noListSchedules,//没有教学名单的排课,包含行政班,教学班
-                        repeatTeahcerSchedules,//教师在同一课时有冲突的排课
-                        repeatListSchedules,//行政班或教学班在同一课时段有冲突的排课}
-                    },
-                    databaseCheckError=new { 
-                    }
-                });
+                    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);
             }
-            else { 
-                return Ok(new { error = 1, msg = "排课参数错误" }); 
-            
+            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]

+ 6 - 6
TEAMModelOS.SDK/DI/CoreAPI/CoreAPIHttpService.cs

@@ -233,12 +233,12 @@ namespace TEAMModelOS.SDK
     }
     public class TmdidImplicit
     {
-            public string id_token { get; set; }
-    public string access_token { get; set; }
-    public string expires_in { get; set; }
-    public string token_type { get; set; }
-}
-public class CoreUser
+        public string id_token { get; set; }
+        public string access_token { get; set; }
+        public string expires_in { get; set; }
+        public string token_type { get; set; }
+    }
+    public class CoreUser
     {
         public string id { get; set; }
         public string vid { get; set; }

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

@@ -82,11 +82,31 @@ namespace TEAMModelOS.SDK.Models
         /// </summary>
         public string stulist { get; set; }
         public List<TimeInfo> time { get; set; } = new List<TimeInfo>();
-       
+    }
+    public class ScheduleDto
+    {
+        public string courseId { get; set; }
+        /// <summary>
+        /// 教室
+        /// </summary>
+        public string roomId { get; set; }
+        /// <summary>
+        /// 班级名单id
+        /// </summary>
+        public string classId { get; set; }
+        public string teacherId { get; set; }
+        /// <summary>
+        /// 自定义名单
+        /// </summary>
+        public string stulist { get; set; }
 
+        public string timeId { get; set; }
+        public string week { get; set; }
+        public string keyTeacher { get; set; }//teacherId_week_timeId
+        public string keyGroupId { get; set; }//classId/stulist_week_timeId
+        public string keyRoomIds { get; set; }//room_week_timeId
     }
 
-    
     public class TimeInfo
     {
         public string id { get; set; }

+ 1 - 1
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -12,7 +12,6 @@
 
   <ItemGroup>
     <PackageReference Include="CHTCHSConv" Version="1.0.0" />
-    <PackageReference Include="HTEXLib" Version="5.2203.232" />
     <PackageReference Include="AspectCore.Extensions.Reflection" Version="2.2.0" />
     <PackageReference Include="Azure.Cosmos" Version="4.0.0-preview3" />
     <PackageReference Include="Azure.Identity" Version="1.5.0" />
@@ -22,6 +21,7 @@
     <PackageReference Include="Azure.Storage.Queues" Version="12.9.0" />
     <PackageReference Include="ClouDASLibx" Version="1.2.7" />
     <PackageReference Include="DocumentFormat.OpenXml" Version="2.15.0" />
+    <PackageReference Include="HTEXLib" Version="5.2203.232" />
     <PackageReference Include="HtmlAgilityPack" Version="1.11.42" />
     <PackageReference Include="Lib.AspNetCore.ServerSentEvents" Version="8.1.0" />
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.3" />

+ 25 - 4
TEAMModelOS/Controllers/XTest/FixDataController.cs

@@ -1435,6 +1435,7 @@ namespace TEAMModelOS.Controllers
                         schoolTeacher.subjectIds = schoolTeacher.subjectIds == null ? new List<string>() : schoolTeacher.subjectIds;
                         schoolTeacher.status = "join";
                         schoolTeacher.job = "管理员";
+                        schoolTeacher.pk = "Teacher";
                         await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(schoolTeacher, tmdid, new PartitionKey($"Teacher-{school.id}"));
                         schoolsTeachers.Add(schoolTeacher);
                     }
@@ -1614,10 +1615,30 @@ namespace TEAMModelOS.Controllers
         [HttpPost("set-sc-admin-by-tmdid")]
         public async Task<IActionResult> SetScAdminByTmdid(JsonElement request) {
 
-            if (!request.TryGetProperty("tmdids", out JsonElement _tmdids)) { return BadRequest(); }
-            List<string> tmdids = _tmdids.ToObject<List<string>>();
-            string sql = "";
-            return Ok();
+            if (!request.TryGetProperty("userkeys", out JsonElement _userkeys)) { return BadRequest(); }
+            List<string> userkeys = _userkeys.ToObject<List<string>>();
+           
+            var content = new StringContent(userkeys.Select(x => x).ToJsonString(), Encoding.UTF8, "application/json");
+            string json = await _coreAPIHttpService.GetUserInfos(content);
+            List<TmdInfo> tmdInfos = json.ToObject<List<TmdInfo>>();
+            string sql = $"select value(c) from c where c.pk='Teacher' and  c.id in ({string.Join(",", tmdInfos.Select(x => $"'{x.id}'"))})";
+            List<SchoolTeacher> schoolTeachers = new List<SchoolTeacher>();
+            await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIterator<SchoolTeacher>(queryText: sql))
+            {
+                schoolTeachers.Add(item);
+            }
+            var groups = schoolTeachers.GroupBy(x => x.id).Select(y => new { key = y.Key, list = y.ToList() });
+            var countMore1 = groups.Where(x => x.list.Count > 1).SelectMany(x => x.list);
+            var countEqual1 = groups.Where(x => x.list.Count == 1).SelectMany(x => x.list);
+            countEqual1.ToList().ForEach(x => {
+                if (!x.roles.Contains("admin")) {
+                    x.roles.Add("admin");
+                }
+            });
+            foreach (var item in countEqual1) {
+               await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
+            }
+            return Ok(new { countMore1 ,countEqual1});
         
         }
         /// <summary>