using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.Entities; using EMIS.ViewModel.ExaminationManage; using EMIS.ViewModel; using EMIS.CommonLogic.ExaminationManage; using Bowin.Common.Utility; using EMIS.ViewModel.EducationSchedule; using System.IO; using EMIS.Utility; using Bowin.Common.DataTime; using AutoMapper; using EMIS.ViewModel.UniversityManage.ClassroomManage; namespace EMIS.ExtensionLogic.ServiceLogic.ExaminationManage { public class ExaminationPlanServices : EMIS.CommonLogic.ExaminationManage.ExaminationPlanServices { protected override bool AutoSchedule(List notCompleteExaminationPlanList, List examinationRoomList, List classroomViewList, TimeSegmentView examinationDate, List examinationTeacherList, List scheduledStudentList, List schedulingList, List excessiveUseList) { var needScheduleCount = notCompleteExaminationPlanList.Count; var scheduledCount = 0; var needModifyRecordList = new List(); foreach (var examinationRoom in examinationRoomList) { //如果考场在当前时间有排课,跳过 var schedule = schedulingList.FirstOrDefault(x => x.ClassroomID == examinationRoom.ClassroomID && x.Date == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime && x.EndTime >= examinationDate.StartTime); if (schedule != null) { continue; } var excessiveUse = excessiveUseList.FirstOrDefault(x => x.ClassroomID == examinationRoom.ClassroomID && x.Date == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime && x.EndTime >= examinationDate.StartTime); if (excessiveUse != null) { continue; } //按照排考人数从多到少排序,第一行拿出未排过的考试计划,然后从第二个计划开始,优先拿出排过考的考试计划, //这样做的目的是保证考试计划能排到最少的考场去,不至于学生太过分散 var orderedExaminationPlanViewList = notCompleteExaminationPlanList .Where(x => (x.MissionStudentCount - x.StudentCount) > 0) .OrderBy(x => x.MissionStudentCount - x.StudentCount).Take(1).ToList(); orderedExaminationPlanViewList = orderedExaminationPlanViewList .Concat(notCompleteExaminationPlanList.Where(x => !orderedExaminationPlanViewList.Select(w => w.ExaminationPlan.ExaminationPlanID) .Contains(x.ExaminationPlan.ExaminationPlanID) && (x.MissionStudentCount - x.StudentCount) > 0) .OrderBy(x => x.StudentCount > 0 ? 0 : 1) .ThenByDescending(x => x.MissionStudentCount - x.StudentCount)).ToList(); #region 获取目前有空的监考员,并随机抽出两个 //先找出排课没有冲突的监考员 var noScheduleTeacherList = (from allteacher in examinationTeacherList from scheduling in schedulingList.Where(x => x.TeacherList.Any(w => w.UserID == allteacher.UserID) && x.Date == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime && x.EndTime >= examinationDate.StartTime).DefaultIfEmpty() where scheduling == null select allteacher).ToList(); var teacherList = this.GetAvailableExaminationTeacher(noScheduleTeacherList, examinationDate.ExaminationDate, examinationDate.StartTime, examinationDate.EndTime, scheduledStudentList); if (teacherList.Count < 2) { return false; } #endregion //这个循环需要把考室尽量塞满…… foreach (var orderedExaminationPlanView in orderedExaminationPlanViewList) { //获取要排考的学生 var needScheduleStudentList = this.GetNeedScheduleStudentList(orderedExaminationPlanView, scheduledStudentList); //首先判断是否指定了考试时间,但并不是当前时间,这些自动排考无法处理 if (orderedExaminationPlanView.ExaminationPlan.ExaminationDate.HasValue && !(orderedExaminationPlanView.ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate && orderedExaminationPlanView.ExaminationPlan.StartTime == examinationDate.StartTime && orderedExaminationPlanView.ExaminationPlan.EndTime == examinationDate.EndTime)) { continue; } //然后判断冲突 if (IsConflict(orderedExaminationPlanView, needScheduleStudentList.Select(x => x.UserID).ToList(), examinationDate, examinationRoom.CF_Classroom, scheduledStudentList)) { continue; } var roomRemainSeat = examinationRoom.CF_Classroom.Examinationseating - scheduledStudentList .Where(x => x.ExaminationDate == examinationDate.ExaminationDate && x.ClassroomID == examinationRoom.ClassroomID && x.StartTime <= examinationDate.EndTime && x.EndTime >= examinationDate.StartTime).Count(); if (roomRemainSeat < needScheduleStudentList.Count) { continue; } var actualScheduleStudentList = needScheduleStudentList.ToList(); //记录住这些数据需要修改,后面所有考场和考试任务都检查完后,再统一修改 var result = new AutoScheduleModifyView { ExaminationPlan = orderedExaminationPlanView.ExaminationPlan, ExaminationDate = examinationDate.ExaminationDate, StartTime = examinationDate.StartTime, EndTime = examinationDate.EndTime, ExaminationRoom = examinationRoom, ScheduleStudentList = needScheduleStudentList }; var lastExaminationRoomLayout = scheduledStudentList.Where(x => x.ClassroomID == examinationRoom.ClassroomID).FirstOrDefault(); if (lastExaminationRoomLayout != null) { result.CF_Staff = new HashSet( examinationTeacherList.Where(x => lastExaminationRoomLayout.TeacherIDList.Contains(x.UserID ?? Guid.NewGuid())).Select(x => x.CF_Staff) ); } else { result.CF_Staff = new HashSet(teacherList); } needModifyRecordList.Add(result); //更新已排完的计划,这样后续的判断才会没问题 scheduledStudentList.AddRange(needScheduleStudentList.Select(x => new StudentClassroomView { UserID = x.UserID, ExaminationDate = examinationDate.ExaminationDate, StartTime = examinationDate.StartTime, EndTime = examinationDate.EndTime, ClassroomID = examinationRoom.ClassroomID, ExaminationStyleID = orderedExaminationPlanView.ExaminationPlan.ExaminationStyleID, TeacherIDList = result.CF_Staff.Select(w => w.UserID).ToList(), ExaminationPlanID = orderedExaminationPlanView.ExaminationPlan.ExaminationPlanID })); orderedExaminationPlanView.StudentCount += needScheduleStudentList.Count(); scheduledCount++; } } //如果排不完,返回失败,让外部决定如何处理 if (scheduledCount < needScheduleCount) { return false; } foreach (var needModifyRecord in needModifyRecordList) { #region 判断成功,开始更新数据 needModifyRecord.ExaminationPlan.ExaminationDate = examinationDate.ExaminationDate; needModifyRecord.ExaminationPlan.StartTime = examinationDate.StartTime; needModifyRecord.ExaminationPlan.EndTime = examinationDate.EndTime; var examinationRoomLayout = new EX_ExaminationRoomLayout { ExaminationRoomLayoutID = Guid.NewGuid(), ExaminationPlanID = needModifyRecord.ExaminationPlan.ExaminationPlanID, EX_ExaminationPlan = needModifyRecord.ExaminationPlan, ClassroomID = needModifyRecord.ExaminationRoom.ClassroomID, RowSpacing = 0, ColumnSpacing = 0, StudentOrderType = (int)CF_StudentOrderType.StudentNo, CF_Staff = needModifyRecord.CF_Staff }; this.SetNewStatus(examinationRoomLayout); //这块需要挪到上面检测的地方,在上面如何判断这个考场能容纳多少学生? var examinationRoomStudentViewList = needModifyRecord.ScheduleStudentList.Select(x => new ExaminationRoomStudentView { ExaminationRoomStudentID = Guid.NewGuid(), ExaminationRoomLayoutID = examinationRoomLayout.ExaminationRoomLayoutID, UserID = x.UserID, LoginID = x.Sys_User.LoginID, Remark = "" }).ToList(); var classroomView = classroomViewList.FirstOrDefault(x => x.ClassroomID == needModifyRecord.ExaminationRoom.ClassroomID); var examinationRoomStudentList = this.SetRoomStudentSeats(examinationRoomStudentViewList, classroomView, 0, 0, CF_StudentOrderType.StudentNo); examinationRoomLayout.EX_ExaminationRoomStudent = new HashSet(examinationRoomStudentList); needModifyRecord.ExaminationPlan.EX_ExaminationRoomLayout.Add(examinationRoomLayout); needModifyRecord.ExaminationRoom.CF_Classroom.EX_ExaminationRoomLayout.Add(examinationRoomLayout); #endregion } return true; } public override string AdultImport(string filePath, out string returnMessage) { StringBuilder errorMsg = new StringBuilder(); // 错误信息 List errList = new List(); //错误数据行 List successList = new List(); //导入的数据行 DataTimeHelper dth = new DataTimeHelper(); #region 解析文件,存放到一个List集合里 Dictionary cellheader = new Dictionary { { "CoursematerialName", "考试科目" }, { "ClassName", "考试班级名称" }, { "MissionStudentCount", "学生数" }, { "StudentCount", "排考人数" }, { "ExamsCategoryName", "考试性质" }, { "ExaminationModeName", "考试方式" }, { "ExaminationWeekNumString", "考试周次" }, { "ExaminationDateString", "考试日期" }, { "StartTimeString", "考试时间起"},{ "EndTimeString", "考试时间止" }, { "ClassroomName", "考室"},{ "TeacherNames", "监考老师" } }; List importRowlist = NpoiExcelHelper.ExcelToEntityList(cellheader, filePath, out errorMsg, out errList); File.Delete(filePath); cellheader.Add("ErrorMessage", "错误信息");//还原字典项 #endregion #region 清空格 for (int i = 0; i < importRowlist.Count(); i++) { if (importRowlist[i].CoursematerialName != null) importRowlist[i].CoursematerialName = importRowlist[i].CoursematerialName.Trim(); if (importRowlist[i].ClassName != null) importRowlist[i].ClassName = importRowlist[i].ClassName.Trim(); if (importRowlist[i].MissionStudentCount != null) importRowlist[i].MissionStudentCount = importRowlist[i].MissionStudentCount.Trim(); if (importRowlist[i].StudentCount != null) importRowlist[i].StudentCount = importRowlist[i].StudentCount.Trim(); if (importRowlist[i].ExamsCategoryName != null) importRowlist[i].ExamsCategoryName = importRowlist[i].ExamsCategoryName.Trim(); if (importRowlist[i].ExaminationModeName != null) importRowlist[i].ExaminationModeName = importRowlist[i].ExaminationModeName.Trim(); if (importRowlist[i].ExaminationWeekNumString != null) importRowlist[i].ExaminationWeekNumString = importRowlist[i].ExaminationWeekNumString.Trim(); if (importRowlist[i].ExaminationDateString != null) importRowlist[i].ExaminationDateString = importRowlist[i].ExaminationDateString.Trim(); if (importRowlist[i].StartTimeString != null) importRowlist[i].StartTimeString = importRowlist[i].StartTimeString.Trim(); if (importRowlist[i].EndTimeString != null) importRowlist[i].EndTimeString = importRowlist[i].EndTimeString.Trim(); if (importRowlist[i].ClassroomName != null) importRowlist[i].ClassroomName = importRowlist[i].ClassroomName.Trim(); if (importRowlist[i].TeacherNames != null) importRowlist[i].TeacherNames = importRowlist[i].TeacherNames.Trim(); } #endregion #region 检查文件中的内容 #region 读取用于校验的数据,便于后续处理 var coursematerialNameList = importRowlist.Select(x => x.CoursematerialName).ToList(); var classNameList = importRowlist.Select(x => x.ClassName).ToList(); //默认排当前学年学期的考试计划 var curSchoolyear = SchoolYearServices.Value.GetCurrentSchoolYear(); var classroomNameList = importRowlist.Select(x => x.ClassroomName).ToList(); var teacherNameList = importRowlist.Where(x => !string.IsNullOrEmpty(x.TeacherNames)) .SelectMany(x => x.TeacherNames.Split('/').ToList()).Distinct().ToList(); var coursematerialList = ExaminationPlanDAL.CoursematerialRepository.GetList(x => coursematerialNameList.Contains(x.CourseName)).ToList(); var examinationPlanList = ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => classNameList.Contains(x.ClassName) && x.SchoolyearID == curSchoolyear.SchoolYearID, (x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)), (x => x.CF_Student.Select(w => w.Sys_User))).ToList(); var examsCategoryList = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_ExamsCategory); var examinationModeList = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_ExaminationMode); var classroomList = ExaminationRoomSettingRepository .GetList(x => classroomNameList.Contains(x.CF_Classroom.Name)) .Select(x => x.CF_Classroom).ToList(); var classroomViewList = ClassroomDAL.GetClassroomViewQueryable(x => classroomNameList.Contains(x.Name)).ToList(); var teacherList = ExaminationPlanDAL.StaffDAL.staffRepository.GetList(x => teacherNameList.Contains(x.Sys_User.Name), (x => x.Sys_User)).ToList(); #endregion foreach (var importRow in importRowlist) { string errorMessage = ""; var errorItem = Mapper.DynamicMap(importRow); #region 检查基本录入格式 var curCoursematerialList = coursematerialList.Where(x => x.CourseName == (importRow.CoursematerialName ?? "").Trim()).ToList(); var examsCategory = examsCategoryList.FirstOrDefault(x => x.Name == (importRow.ExamsCategoryName ?? "").Trim()); var examinationMode = examinationModeList.FirstOrDefault(x => x.Name == (importRow.ExaminationModeName ?? "").Trim()); var classroom = classroomList.FirstOrDefault(x => x.Name == (importRow.ClassroomName ?? "").Trim()); var curTeacherNameList = (importRow.TeacherNames ?? "").Trim().Split('/').Where(x => !string.IsNullOrEmpty(x)).ToList(); var teacher = teacherList.Where(x => curTeacherNameList.Contains(x.Sys_User.Name)).ToList(); if (curCoursematerialList.Count == 0) { errorMessage += "没有对应的课程名称,请检查输入。"; } if (examsCategory == null) { errorMessage += "考试性质错误,请检查输入。"; } if (examinationMode == null) { errorMessage += "考试方式错误,请检查输入。"; } if (importRow.StudentCount == null) { importRow.StudentCount = importRow.MissionStudentCount; } if (string.IsNullOrEmpty(importRow.ClassroomName)) { errorMessage += "请填写考室。"; } else if (classroom == null) { errorMessage += "考室信息错误,并未将" + importRow.ClassroomName + "安排为考室,请检查输入。"; } var errorTeacherList = curTeacherNameList.Where(x => !teacher.Select(w => w.Sys_User.Name).Contains(x)).ToList(); if (errorTeacherList.Count > 0) { errorMessage += "监考老师" + string.Join("/", errorTeacherList) + "名字输入错误,请检查输入。"; } else if (teacher.Count == 0) { errorMessage += "请填写监考老师信息。"; } var coursematerialIDList = curCoursematerialList.Select(x => (Guid?)x.CoursematerialID).ToList(); var examinationPlan = examinationPlanList.FirstOrDefault(x => x.ClassName == importRow.ClassName && coursematerialIDList.Contains(x.CoursematerialID)); if (examsCategory != null && examinationMode != null) { examinationPlan = examinationPlanList.FirstOrDefault(x => x.ClassName == importRow.ClassName && coursematerialIDList.Contains(x.CoursematerialID) && x.ExamsCategoryID == examsCategory.Value && x.ExaminationModeID == examinationMode.Value); } if (examinationPlan == null) { examinationPlan = examinationPlanList.FirstOrDefault(x => x.ClassName == importRow.ClassName && coursematerialIDList.Contains(x.CoursematerialID)); if (examinationPlan == null) { errorMessage += "并没有该班级名称和课程名称的考试安排,请检查输入。"; } } else if (examinationPlan.RecordStatus == (int)CF_ApprovalStatus.Submitted) { errorMessage += "已存在相同的已提交的考试计划"; } if (classroom != null && examinationPlan != null && classroom.Examinationseating < examinationPlan.CF_Student.Count) { errorMessage += "所选考室可容纳的座位数不足,无法安排学生进行考试。"; } DateTime examinationDate = default(DateTime); if (string.IsNullOrEmpty(importRow.ExaminationDateString)) { errorMessage += "请填写考试日期。"; } else { try { DateTime date; if (DateTime.TryParse(importRow.ExaminationDateString.Trim().ToString(), out date)) { examinationDate = Convert.ToDateTime(importRow.ExaminationDateString.Trim().ToString()); } else { var dateTime = DateTime.FromOADate(double.Parse(importRow.ExaminationDateString.Trim().ToString())); examinationDate = dateTime; } } catch { errorMessage += "考试日期必须为年-月-日的日期格式。"; } } TimeSpan startTime = default(TimeSpan); if (string.IsNullOrEmpty(importRow.StartTimeString)) { errorMessage += "请填写考试时间起。"; } else { try { DateTime date; if (DateTime.TryParse(importRow.StartTimeString.Trim().ToString(), out date)) { startTime = Convert.ToDateTime(importRow.StartTimeString.Trim().ToString()).TimeOfDay; } else { var dateTime = DateTime.FromOADate(double.Parse(importRow.StartTimeString.Trim().ToString())); startTime = dateTime.TimeOfDay; } } catch { errorMessage += "考试时间起必须为时:分的时间格式。"; } } TimeSpan endTime = default(TimeSpan); if (string.IsNullOrEmpty(importRow.EndTimeString)) { errorMessage += "请填写考试时间止。"; } else { try { DateTime date; if (DateTime.TryParse(importRow.EndTimeString.Trim().ToString(), out date)) { endTime = Convert.ToDateTime(importRow.EndTimeString.Trim().ToString()).TimeOfDay; } else { var dateTime = DateTime.FromOADate(double.Parse(importRow.EndTimeString.Trim().ToString())); endTime = dateTime.TimeOfDay; } } catch { errorMessage += "考试时间止必须为时:分的时间格式。"; } } if (startTime >= endTime) { errorMessage += "结束时间需大于开始时间。"; } #endregion if (errorMessage != "") { errorItem.ErrorMessage = errorMessage; if (endTime != default(TimeSpan)) { errorItem.EndTimeString = " " + endTime; } if (startTime != default(TimeSpan)) { errorItem.StartTimeString = " " + startTime; } if (examinationDate != default(DateTime)) { errorItem.ExaminationDateString = " " + examinationDate; } errList.Add(errorItem); continue; } else { importRow.CoursematerialID = examinationPlan.CoursematerialID; importRow.ExaminationPlan = examinationPlan; importRow.ExamsCategoryID = examsCategory.Value; importRow.ExaminationModeID = examinationMode.Value; importRow.ExaminationStyleID = examinationPlan.ExaminationStyleID; importRow.ExaminationDate = examinationDate; importRow.StartTime = startTime; importRow.EndTime = endTime; importRow.ClassroomID = classroom.ClassroomID; importRow.Examinationseating = classroom.Examinationseating; importRow.TeacherList = teacher; successList.Add(importRow); } } var checkList = successList; #endregion #region 准备排课数据用于校验 var scheduleDateTimeList = successList.GroupBy(x => x.ExaminationPlan.SchoolyearID) .Select(x => new { SchoolyearID = x.Key, DataTimeList = x.GroupBy(w => new { StartTime = w.ExaminationDate.Value.Add(w.StartTime.Value), EndTime = w.ExaminationDate.Value.Add(w.EndTime.Value) }) .Select(w => new StartEndTimeView { StartTime = w.Key.StartTime, EndTime = w.Key.EndTime }).ToList() }).ToList(); List schedulingList = new List(); List excessiveUseList = new List(); scheduleDateTimeList.ForEach(x => { schedulingList.AddRange(this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewForExaminationTeacher(x.SchoolyearID.Value, x.DataTimeList)); excessiveUseList.AddRange(this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( x.SchoolyearID.Value, x.DataTimeList)); }); #endregion #region 检查导入文件内部数据的冲突 #region 检查考室冲突 //var classroomConflictList = (from source in successList // from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID // && x.ExaminationDate == source.ExaminationDate // && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime // && x.ClassroomID == source.ClassroomID) // select new { source, dest }).ToList(); //classroomConflictList.ForEach(x => //{ // var errorItem = x.source; // errorItem.ErrorMessage = "考场安排和其他行存在时间冲突,请检查。"; // errList.Add(errorItem); // successList.Remove(x.source); //}); #endregion #region 检查导入文件导入同一考室,并且时间重复的,人数是否超过可排人次 var classroomstudentCount = (from source in successList from studentcount in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.ExaminationDate == source.ExaminationDate && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.ClassroomID == source.ClassroomID) group studentcount by studentcount.ExaminationPlan.ExaminationPlanID into gstucount join succ in successList on gstucount.Key equals succ.ExaminationPlan.ExaminationPlanID select new { //source, succ }).ToList(); classroomstudentCount.ForEach(x => { var count = int.Parse(x.succ.MissionStudentCount); var isrepeatList = checkList.Any(y => y.ExaminationPlan.ExaminationPlanID != x.succ.ExaminationPlan.ExaminationPlanID && y.ExaminationDate == x.succ.ExaminationDate && y.StartTime <= x.succ.EndTime && y.EndTime >= x.succ.StartTime && y.ClassroomID == x.succ.ClassroomID); if (isrepeatList) { var repeatList = checkList.Where(y => y.ExaminationPlan.ExaminationPlanID != x.succ.ExaminationPlan.ExaminationPlanID && y.ExaminationDate == x.succ.ExaminationDate && y.StartTime <= x.succ.EndTime && y.EndTime >= x.succ.StartTime && y.ClassroomID == x.succ.ClassroomID).ToList(); foreach (var repeat in repeatList) { count = count + int.Parse(repeat.MissionStudentCount); } } if (count > classroomList.FirstOrDefault(w => w.ClassroomID == x.succ.ClassroomID).Examinationseating) { var errorItem = x.succ; errorItem.ErrorMessage = "考场座位数不足,请检查。"; errList.Add(errorItem); successList.Remove(x.succ); } }); #endregion #region 检查教师冲突 var teacherConflictList = (from source in successList from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.ExaminationDate == source.ExaminationDate && x.ClassroomID != source.ClassroomID && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.TeacherList.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() > 0).Take(1) select source).ToList(); teacherConflictList.ForEach(x => { var errorItem = x; errorItem.ErrorMessage = "监考老师和其他行存在时间冲突,请检查。"; errList.Add(errorItem); successList.Remove(x); }); var teacherClassroomConflictList = (from source in successList from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.ExaminationDate == source.ExaminationDate && x.ClassroomID == source.ClassroomID && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.TeacherList.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() != x.TeacherList.Count).Take(1) select source).ToList(); teacherClassroomConflictList.ForEach(x => { var errorItem = x; errorItem.ErrorMessage = "同一考场的监考老师必须相同,请检查考室或监考老师是否存在冲突。"; errList.Add(errorItem); successList.Remove(x); }); #endregion #region 检查学生冲突 var studentConflictList = (from source in successList from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.ExaminationDate == source.ExaminationDate && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.ExaminationPlan.CF_Student.Select(w => w.UserID).Intersect(source.ExaminationPlan.CF_Student.Select(w => w.UserID)).Count() > 0) select new { source, dest }).ToList(); studentConflictList.ForEach(x => { var errorItem = x.source; errorItem.ErrorMessage = "考试学生和其他行存在时间冲突,请检查。"; errList.Add(errorItem); successList.Remove(x.source); }); #endregion #endregion #region 检查和其他考场安排之间的冲突 var examinationDateList = successList.Select(x => x.ExaminationDate).Distinct().ToList(); var sameDateExaminationPlanQuery = ExaminationPlanDAL.ExaminationRoomLayoutRepository .GetList(x => examinationDateList.Contains(x.EX_ExaminationPlan.ExaminationDate), (x => x.EX_ExaminationPlan), (x => x.EX_ExaminationPlan.CF_Student), (x => x.CF_Staff), (x => x.EX_ExaminationRoomStudent)); var sameDateExaminationPlanList = sameDateExaminationPlanQuery.ToList(); #region 检查考室冲突 //var classroomOuterConflictList = (from source in successList // from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID // && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate // && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime // && x.ClassroomID == source.ClassroomID) // select new { source, dest }).ToList(); //classroomOuterConflictList.ForEach(x => //{ // var errorItem1 = x.source; // errorItem1.ErrorMessage = "考场安排和已有的考试安排存在时间冲突,请检查。"; // errList.Add(errorItem1); // successList.Remove(x.source); //}); #endregion #region 检查教师冲突 var teacherOuterConflictList = (from source in successList from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime && x.ClassroomID != source.ClassroomID && x.CF_Staff.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() > 0).Take(1) select new { source, dest }).ToList(); teacherOuterConflictList.ForEach(x => { var errorItem1 = x.source; errorItem1.ErrorMessage = "监考老师和已有的考试安排存在时间冲突,请检查。"; errList.Add(errorItem1); successList.Remove(x.source); }); var teacherOuterClassroomConflictList = (from source in successList from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate && x.ClassroomID == source.ClassroomID && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime && x.CF_Staff.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() != x.CF_Staff.Count).Take(1) select source).ToList(); teacherOuterClassroomConflictList.ForEach(x => { var errorItem = x; errorItem.ErrorMessage = "监考老师和已有的考试安排存在冲突,同一考场的监考老师必须相同,请检查。"; errList.Add(errorItem); successList.Remove(x); }); #endregion #region 检查学生冲突 var studentOuterConflictList = (from source in successList from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime && x.EX_ExaminationPlan.CF_Student.Select(w => w.UserID).Intersect(source.ExaminationPlan.CF_Student.Select(w => w.UserID)).Count() > 0) select new { source, dest }).ToList(); studentOuterConflictList.ForEach(x => { var errorItem = x.source; errorItem.ErrorMessage = "考试学生和已有的考试安排存在时间冲突,请检查。"; errList.Add(errorItem); successList.Remove(x.source); }); #endregion #endregion #region 检查教室占用冲突 var classroomExcessUseConflictList = (from source in successList from dest in excessiveUseList.Where(x => x.Date == source.ExaminationDate && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.ClassroomID == source.ClassroomID).Take(1) select new { source, dest }).ToList(); classroomExcessUseConflictList.ForEach(x => { var errorItem2 = x.source; errorItem2.ErrorMessage = "考场已被占用,请检查。"; errList.Add(errorItem2); successList.Remove(x.source); }); #endregion #region 检查和课表的冲突 #region 检查考室冲突 var classroomScheduleConflictList = (from source in successList from dest in schedulingList.Where(x => x.Date == source.ExaminationDate && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.ClassroomID == source.ClassroomID).Take(1) select new { source, dest }).ToList(); classroomScheduleConflictList.ForEach(x => { var errorItem2 = x.source; errorItem2.ErrorMessage = "考场安排和已有的排课存在时间冲突,请检查。"; errList.Add(errorItem2); successList.Remove(x.source); }); #endregion #region 检查教师冲突 var teacherScheduleConflictList = (from source in successList from dest in schedulingList.Where(x => x.Date == source.ExaminationDate && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.TeacherList.Select(w => w.UserID) .Intersect(source.TeacherList.Select(w => (Guid?)w.UserID)).Count() > 0).Take(1) select new { source, dest }).ToList(); teacherScheduleConflictList.ForEach(x => { var errorItem1 = x.source; errorItem1.ErrorMessage = "监考老师和已有的排课存在时间冲突,请检查。"; errList.Add(errorItem1); successList.Remove(x.source); }); #endregion #region 检查学生冲突 var studentScheduleConflictList = (from source in successList from dest in schedulingList.Where(x => x.Date == source.ExaminationDate && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime && x.StudentList.Select(w => w.UserID).Intersect(source.ExaminationPlan.CF_Student.Select(w => w.UserID)).Count() > 0).Take(1) select new { source, dest }).ToList(); studentScheduleConflictList.ForEach(x => { var errorItem = x.source; errorItem.ErrorMessage = "考试学生和已有的排课存在时间冲突,请检查。"; errList.Add(errorItem); successList.Remove(x.source); }); #endregion #endregion #region 添加数据 var scheduledStudentList = new List(); var deletedExaminationPlanIDList = new List(); foreach (var succesResult in successList) { succesResult.ExaminationPlan.ExaminationDate = succesResult.ExaminationDate; succesResult.ExaminationPlan.StartTime = succesResult.StartTime; succesResult.ExaminationPlan.EndTime = succesResult.EndTime; succesResult.ExaminationStyleID = succesResult.ExaminationStyleID; //succesResult.ExaminationPlan.EX_ExaminationRoomLayout.ForEach(x => this.UnitOfWork.Remove(x)); //succesResult.ExaminationPlan.EX_ExaminationRoomLayout = new HashSet(); EX_ExaminationRoomLayout newLayout = new EX_ExaminationRoomLayout() { ExaminationRoomLayoutID = Guid.NewGuid(), ExaminationPlanID = succesResult.ExaminationPlan.ExaminationPlanID, ClassroomID = succesResult.ClassroomID, RowSpacing = 0, ColumnSpacing = 0, StudentOrderType = (int)CF_StudentOrderType.StudentNo, CF_Staff = new HashSet(succesResult.TeacherList) }; this.SetNewStatus(newLayout); var roomRemainSeat = succesResult.Examinationseating - sameDateExaminationPlanList .Where(x => x.EX_ExaminationPlan != null && x.EX_ExaminationPlan.ExaminationPlanID != succesResult.ExaminationPlan.ExaminationPlanID && x.EX_ExaminationPlan.ExaminationDate == succesResult.ExaminationDate && x.ClassroomID == succesResult.ClassroomID && x.EX_ExaminationPlan.StartTime <= succesResult.EndTime && x.EX_ExaminationPlan.EndTime >= succesResult.StartTime).Sum(x => x.EX_ExaminationRoomStudent.Count) - scheduledStudentList.Where(x => x.ClassroomID == succesResult.ClassroomID && x.ExaminationDate == succesResult.ExaminationDate && x.StartTime <= succesResult.EndTime && x.EndTime >= succesResult.StartTime) .Count(); if (roomRemainSeat <= succesResult.ExaminationPlan.CF_Student.Count) { var errorItem = succesResult; errorItem.ErrorMessage = "考场剩余座位不足,无法安排考试,请检查考场的使用情况。"; errList.Add(errorItem); continue; } var studentList = (from s in succesResult.ExaminationPlan.CF_Student join ss in scheduledStudentList.Where(x => x.ExaminationDate == succesResult.ExaminationDate && x.StartTime <= succesResult.EndTime && x.EndTime >= succesResult.StartTime) on s.UserID equals ss.UserID into dss from ess in dss.DefaultIfEmpty() where ess == null select s).ToList(); var examinationRoomStudentViewList = studentList.Take(roomRemainSeat ?? 0) .Select(x => new ExaminationRoomStudentView { ExaminationRoomStudentID = Guid.NewGuid(), ExaminationRoomLayoutID = newLayout.ExaminationRoomLayoutID, UserID = x.UserID, LoginID = x.Sys_User.LoginID, Remark = "" }).ToList(); var classroomView = classroomViewList.FirstOrDefault(x => x.ClassroomID == succesResult.ClassroomID); var examinationRoomStudentList = this.SetRoomStudentSeats(examinationRoomStudentViewList, classroomView, 0, 0, CF_StudentOrderType.StudentNo); if (!deletedExaminationPlanIDList.Contains(succesResult.ExaminationPlan.ExaminationPlanID)) { UnitOfWork.Remove(x => x.EX_ExaminationRoomLayout.ExaminationPlanID == succesResult.ExaminationPlan.ExaminationPlanID); UnitOfWork.Remove(x => x.ExaminationPlanID == succesResult.ExaminationPlan.ExaminationPlanID); deletedExaminationPlanIDList.Add(succesResult.ExaminationPlan.ExaminationPlanID); } newLayout.EX_ExaminationRoomStudent = new HashSet(examinationRoomStudentList); scheduledStudentList.AddRange(newLayout.EX_ExaminationRoomStudent.Select(x => new StudentClassroomView { ExaminationDate = succesResult.ExaminationDate, StartTime = succesResult.StartTime, EndTime = succesResult.EndTime, ClassroomID = newLayout.ClassroomID, UserID = x.UserID })); this.UnitOfWork.Add(newLayout); succesResult.ExaminationPlan.EX_ExaminationRoomLayout.Add(newLayout); } #endregion this.UnitOfWork.Commit(); returnMessage = string.Format("提示:成功导入{0}条记录,失败{1}条,详情请点击错误数据下载查看。", (importRowlist.Count - errList.Count), errList.Count); if (errList.Count > 0) { return NpoiExcelHelper.EntityListToExcel2003(cellheader, errList, "导入问题列表", ""); } else { return ""; } } /// /// 松山逻辑,提交时需要判断和课表或教室占用的冲突,判断课表主要是因为在排考的同时可能因为调课操作导致课表变动 /// /// public override void Submit(IList examinationPlanIDList) { var examinationPlanList = examinationPlanRepository.GetList(x => examinationPlanIDList.Contains(x.ExaminationPlanID), (x => x.EX_ExaminationRoomLayout)).ToList(); var examinationPlanLayoutList = ExaminationPlanDAL.ExaminationRoomLayoutRepository .GetList(x => examinationPlanIDList.Contains(x.ExaminationPlanID), (x => x.CF_Staff.Select(w => w.Sys_User)), (x => x.EX_ExaminationRoomStudent.Select(w => w.CF_Student.Sys_User)), (x => x.CF_Classroom)); var submitedPlan = examinationPlanList.Where(x => x.RecordStatus != (int)CF_ApprovalStatus.NotSubmitted).ToList(); if (submitedPlan.Count > 0) { string submitedClassNames = string.Join(",", submitedPlan.Select(x => x.ClassName)) + "并不在未提交状态,请选择正确的记录进行提交。"; throw new Exception(submitedClassNames); } var noLayoutPlanList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID into depl from eepl in depl.DefaultIfEmpty() where eepl == null select ep).ToList(); if (noLayoutPlanList.Count > 0) { string noLayoutPlanClassNames = string.Join(",", noLayoutPlanList.Select(x => x.ClassName)) + "没有设置对应的考场,请先设置再进行提交。"; throw new Exception(noLayoutPlanClassNames); } var noTeacherPlanList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID from s in epl.CF_Staff.DefaultIfEmpty() where s == null select new { Plan = ep, Classroom = epl.CF_Classroom }).ToList(); if (noTeacherPlanList.Count > 0) { string noTeacherPlanClassNames = string.Join(";", noTeacherPlanList.Select(x => x.Plan.ClassName + ",考场" + x.Classroom.Name)) + "没有设置监考老师,请先设置再进行提交。"; throw new Exception(noTeacherPlanClassNames); } var noStudentPlanList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID from s in epl.EX_ExaminationRoomStudent.DefaultIfEmpty() where s == null select new { Plan = ep, Classroom = epl.CF_Classroom }).ToList(); if (noStudentPlanList.Count > 0) { string noStudentPlanClassNames = string.Join(";", noStudentPlanList.Select(x => x.Plan.ClassName + ",考场" + x.Classroom.Name)) + "没有设置学生,请先设置再进行提交。"; throw new Exception(noStudentPlanClassNames); } var scheduleDateTimeList = examinationPlanList.GroupBy(x => x.SchoolyearID) .Select(x => new { SchoolyearID = x.Key, DataTimeList = x.GroupBy(w => new { StartTime = w.ExaminationDate.Value.Add(w.StartTime.Value), EndTime = w.ExaminationDate.Value.Add(w.EndTime.Value) }) .Select(w => new StartEndTimeView { StartTime = w.Key.StartTime, EndTime = w.Key.EndTime }).ToList() }).ToList(); List schedulingList = new List(); List excessiveUseList = new List(); scheduleDateTimeList.ForEach(x => { schedulingList.AddRange(this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewForExaminationTeacher(x.SchoolyearID.Value, x.DataTimeList)); excessiveUseList.AddRange(this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( x.SchoolyearID.Value, x.DataTimeList)); }); #region 检查教室占用冲突 var classroomExcessUseConflictList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID from dest in excessiveUseList.Where(x => x.Date == ep.ExaminationDate && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime && epl.ClassroomID == x.ClassroomID).Take(1) group epl by new { ep.ExaminationPlanID, ep.ClassName } into g select g.Key.ClassName + "的教室" + string.Join("、", g.Select(w => w.CF_Classroom.Name))).ToList(); if (classroomExcessUseConflictList.Count > 0) { throw new Exception(string.Join(";", classroomExcessUseConflictList) + "被占用,无法提交。"); } #endregion #region 检查和课表的冲突 #region 检查考室冲突 var classroomScheduleConflictList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID from dest in schedulingList.Where(x => x.Date == ep.ExaminationDate && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime && x.ClassroomID == epl.ClassroomID).Take(1) group epl by new { ep.ExaminationPlanID, ep.ClassName } into g select g.Key.ClassName + "的教室" + string.Join("、", g.Select(w => w.CF_Classroom.Name))).ToList(); if (classroomScheduleConflictList.Count > 0) { throw new Exception(string.Join(";", classroomScheduleConflictList) + "和已有的排课存在时间冲突,请检查。"); } #endregion #region 检查教师冲突 var teacherScheduleConflictList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID from s in epl.CF_Staff from dest in schedulingList.Where(x => x.Date == ep.ExaminationDate && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime && x.TeacherList.Select(w => w.UserID).Contains(s.UserID)).Take(1) group s by new { ep.ExaminationPlanID, ep.ClassName } into g select g.Key.ClassName + "的监考老师" + string.Join("、", g.Select(w => w.Sys_User.Name))).ToList(); if (teacherScheduleConflictList.Count > 0) { throw new Exception(string.Join("、", teacherScheduleConflictList) + "和已有的排课存在时间冲突,请检查。"); } #endregion #region 检查学生冲突 var studentScheduleConflictList = (from ep in examinationPlanList join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID from s in epl.EX_ExaminationRoomStudent from dest in schedulingList.Where(x => x.Date == ep.ExaminationDate && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime && x.StudentList.Select(w => (Guid?)w.UserID).Contains(s.UserID)).Take(1) group s by new { ep.ExaminationPlanID, ep.ClassName } into g select g.Key.ClassName + "的考试学生" + string.Join("、", g.Select(w => w.CF_Student.Sys_User.Name))).ToList(); if (studentScheduleConflictList.Count > 0) { throw new Exception(string.Join("、", studentScheduleConflictList) + "和已有的排课存在时间冲突,请检查。"); } #endregion #endregion if (examinationPlanIDList.Count > 0) { UnitOfWork.Update((x => new EX_ExaminationPlan { RecordStatus = (int)CF_ApprovalStatus.Submitted }), (x => examinationPlanIDList.Contains(x.ExaminationPlanID))); } } } }