using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Data.Entity; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using EMIS.DataLogic.ExaminationManage; using EMIS.Entities; using EMIS.ViewModel; using EMIS.ViewModel.ExaminationManage; using EMIS.Utility.FormValidate; using EMIS.DataLogic.UniversityManage.TeacherManage; using EMIS.ViewModel.Students; using EMIS.DataLogic.UniversityManage.ClassroomManage; using EMIS.DataLogic.Common.Students; using EMIS.DataLogic.ScoreManage; using EMIS.CommonLogic.CalendarManage; using EMIS.ViewModel.UniversityManage.ClassroomManage; using Bowin.Common.DataTime; using Bowin.Common.Utility; using EMIS.Utility; using EMIS.DataLogic.Repositories; using AutoMapper; using System.Text.RegularExpressions; using System.IO; using EMIS.DataLogic.UniversityManage.AdministrativeOrgan; using EMIS.CommonLogic.EducationSchedule; using EMIS.CommonLogic.SystemServices; using EMIS.ViewModel.EducationSchedule; using EMIS.ViewModel.CalendarManage; using System.Transactions; using EMIS.ViewModel.UniversityManage.TeacherManage; namespace EMIS.CommonLogic.ExaminationManage { public class ExaminationPlanServices : BaseWorkflowServices, IExaminationPlanServices { public UserRepository UserRepository { get; set; } public ExaminationRoomSettingRepository ExaminationRoomSettingRepository { get; set; } public EducationSchedulingRepository EducationSchedulingRepository { get; set; } public ExaminationPlanRepository examinationPlanRepository { get; set; } public ExaminationPlanDAL ExaminationPlanDAL { get; set; } public ExaminationTeacherDAL ExaminationTeacherDAL { get; set; } public FinalExaminationDAL FinalExaminationDAL { get; set; } public RoomSettingDAL RoomSettingDAL { get; set; } public TimeSegmentDAL TimeSegmentDAL { get; set; } public StaffDAL StaffDAL { get; set; } public StudentsDAL StudentsDAL { get; set; } public ClassroomDAL ClassroomDAL { get; set; } public CollegeDAL CollegeDAL { get; set; } public Lazy SchoolYearServices { get; set; } public Lazy ScheduleServices { get; set; } public Lazy ClassroomExcessiveUseServices { get; set; } public Bowin.Common.Linq.Entity.IGridResultSet GetExaminationPlanViewList( ViewModel.ConfiguretView examinationPlanView, Guid? schoolyearID, Guid? collegeID, int? gradeYearID, int? standardID, Guid? coursematerialID, int? examsCategoryID, int? examinationModeID, int? examinationStyleID, int? starWeek, int? endWeek, int? courseEndWeek, int? statusID, int pageIndex, int pageSize) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { exp = exp.And(x => x.CollegeID == collegeID); } if(gradeYearID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.GradeID == gradeYearID) .Select(s => s.CF_Classmajor.CF_Grademajor.GradeID).FirstOrDefault() == gradeYearID); } if(standardID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID) .Select(s => s.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID).FirstOrDefault() == standardID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.CoursematerialID == coursematerialID); } if (examsCategoryID.HasValue) { exp = exp.And(x => x.ExamsCategoryID == examsCategoryID); } if (examinationModeID.HasValue) { exp = exp.And(x => x.ExaminationModeID == examinationModeID); } if (examinationStyleID.HasValue) { exp = exp.And(x => x.ExaminationStyleID == examinationStyleID); } if (statusID.HasValue) { exp = exp.And(x => x.RecordStatus == statusID); } if (starWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum >= starWeek)); } if(endWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum <= endWeek)); } var q = ExaminationPlanDAL.GetExaminationPlanView(exp); if(courseEndWeek.HasValue) { q = q.Where(x => x.CourseEndWeekNum == courseEndWeek); } if (!string.IsNullOrEmpty(examinationPlanView.ConditionValue) && !string.IsNullOrEmpty(examinationPlanView.Attribute)) q = q.DynamicWhere(examinationPlanView.Attribute, examinationPlanView.Condition, examinationPlanView.ConditionValue); try { return this.GetQueryByDataRangeByCollege(q).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExamsCategoryID).ThenBy(x => x.CourseEndWeekNum).ThenBy(x => x.CoursematerialName).ThenBy(x => x.ClassName).ToGridResultSet(pageIndex, pageSize); } catch (Exception ex) { throw ex; } } public IGridResultSet GetGdssExaminationPlanViewList(ConfiguretView configuretView, Guid? schoolyearID, Guid? collegeID, Guid? classmajorID, int? gradeYearID, int? standardID, Guid? coursematerialID, int? examsCategoryID, int? examinationModeID, int? examinationStyleID, int? courseEndWeek, int? statusID, int pageIndex, int pageSize) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { //原先是用教学任务关联的由于补考记录没有对应的教学任务,所以改用专业课程关联, //学校那边说不会有多个教研室开同一门可的情况,即使有,那这些教研室同样可以查到,也没有问题 exp = exp.And(x => x.EM_Coursematerial.EM_SpecialtyCourse.Any(w => w.CF_Department.CollegeID == collegeID && w.IsEnable == true)); } if (gradeYearID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.GradeID == gradeYearID) .Select(s => s.CF_Classmajor.CF_Grademajor.GradeID).FirstOrDefault() == gradeYearID); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID) .Select(s => s.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID).FirstOrDefault() == standardID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.CoursematerialID == coursematerialID); } if (examsCategoryID.HasValue) { exp = exp.And(x => x.ExamsCategoryID == examsCategoryID); } if (examinationModeID.HasValue) { exp = exp.And(x => x.ExaminationModeID == examinationModeID); } if (examinationStyleID.HasValue) { exp = exp.And(x => x.ExaminationStyleID == examinationStyleID); } if (classmajorID.HasValue) { var classmajor = ExaminationPlanDAL.ClassmajorRepository.GetList(x => x.ClassmajorID == classmajorID).FirstOrDefault(); exp = exp.And(x => x.ClassName == classmajor.Name); } if (statusID.HasValue) { exp = exp.And(x => x.RecordStatus == statusID); } var q = ExaminationPlanDAL.GetExaminationPlanView(exp); if (courseEndWeek.HasValue) { q = q.Where(x => x.CourseEndWeekNum == courseEndWeek); } if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); try { var result = this.GetQueryByDataRangeByCollege(q).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExamsCategoryID).ThenBy(x => x.CourseEndWeekNum).ThenBy(x => x.CoursematerialName).ThenBy(x => x.ClassName).ToGridResultSet(pageIndex, pageSize); var ExaminationPlanID = result.rows.Select(x => x.ExaminationPlanID).ToList(); var ExaminationPlanTeacherList = ExaminationPlanDAL.GetExaminationRoomLayoutView(x => ExaminationPlanID.Contains((Guid)x.ExaminationPlanID)).ToList(); result.rows.ForEach(x => x.ExaminationRoomLayoutView = new HashSet(ExaminationPlanTeacherList.Where(y => y.ExaminationPlanID == x.ExaminationPlanID))); return result; } catch (Exception ex) { throw ex; } } public List GetExaminationPlanViewList(ViewModel.ConfiguretView examinationPlanView, Guid? schoolyearID, Guid? collegeID, int? gradeYearID, int? standardID, Guid? coursematerialID, int? examsCategoryID, int? examinationModeID, int? examinationStyleID, int? starWeek, int? endWeek, int? courseEndWeek, int? statusID, List selectIDlist) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { exp = exp.And(x => x.CollegeID == collegeID); } if (gradeYearID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.GradeID == gradeYearID) .Select(s => s.CF_Classmajor.CF_Grademajor.GradeID).FirstOrDefault() == gradeYearID); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID) .Select(s => s.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID).FirstOrDefault() == standardID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.CoursematerialID == coursematerialID); } if (examsCategoryID.HasValue) { exp = exp.And(x => x.ExamsCategoryID == examsCategoryID); } if (examinationModeID.HasValue) { exp = exp.And(x => x.ExaminationModeID == examinationModeID); } if (examinationStyleID.HasValue) { exp = exp.And(x => x.ExaminationStyleID == examinationStyleID); } if (starWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum >= starWeek)); } if (endWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum <= endWeek)); } if (statusID.HasValue) { exp = exp.And(x => x.RecordStatus == statusID); } if (selectIDlist != null && selectIDlist.Count > 0) { exp = exp.And(x => selectIDlist.Contains(x.ExaminationPlanID)); } var q = ExaminationPlanDAL.GetExaminationPlanView(exp); if (courseEndWeek.HasValue) { q = q.Where(x => x.CourseEndWeekNum == courseEndWeek); } if (!string.IsNullOrEmpty(examinationPlanView.ConditionValue) && !string.IsNullOrEmpty(examinationPlanView.Attribute)) q = q.DynamicWhere(examinationPlanView.Attribute, examinationPlanView.Condition, examinationPlanView.ConditionValue); return this.GetQueryByDataRangeByCollege(q).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExamsCategoryID).ThenBy(x => x.CourseEndWeekNum).ThenBy(x => x.CoursematerialName).ThenBy(x => x.ClassName).ToList(); } public List GetAdultExaminationPlanViewList(ViewModel.ConfiguretView examinationPlanView, Guid? schoolyearID, Guid? collegeID, int? gradeYearID, int? standardID, Guid? coursematerialID, int? examsCategoryID, int? examinationModeID, int? examinationStyleID, int? starWeek, int? endWeek, int? courseEndWeek, int? statusID, List selectIDlist) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { exp = exp.And(x => x.CollegeID == collegeID); } if (gradeYearID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.GradeID == gradeYearID) .Select(s => s.CF_Classmajor.CF_Grademajor.GradeID).FirstOrDefault() == gradeYearID); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID) .Select(s => s.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID).FirstOrDefault() == standardID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.CoursematerialID == coursematerialID); } if (examsCategoryID.HasValue) { exp = exp.And(x => x.ExamsCategoryID == examsCategoryID); } if (examinationModeID.HasValue) { exp = exp.And(x => x.ExaminationModeID == examinationModeID); } if (examinationStyleID.HasValue) { exp = exp.And(x => x.ExaminationStyleID == examinationStyleID); } if (starWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum >= starWeek)); } if (endWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum <= endWeek)); } if (statusID.HasValue) { exp = exp.And(x => x.RecordStatus == statusID); } if (selectIDlist != null && selectIDlist.Count > 0) { exp = exp.And(x => selectIDlist.Contains(x.ExaminationPlanID)); } var q = ExaminationPlanDAL.GetExaminationPlanViewForAdult(exp); if (courseEndWeek.HasValue) { q = q.Where(x => x.CourseEndWeekNum == courseEndWeek); } if (!string.IsNullOrEmpty(examinationPlanView.ConditionValue) && !string.IsNullOrEmpty(examinationPlanView.Attribute)) q = q.DynamicWhere(examinationPlanView.Attribute, examinationPlanView.Condition, examinationPlanView.ConditionValue); return this.GetQueryByDataRangeByCollege(q).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExamsCategoryID).ThenBy(x => x.CourseEndWeekNum).ThenBy(x => x.CoursematerialName).ThenBy(x => x.ClassName).ToList(); } public List GetGdssExaminationPlanViewList(ViewModel.ConfiguretView examinationPlanView, Guid? schoolyearID, Guid? collegeID, Guid? classmajorID, int? gradeYearID, int? standardID, Guid? coursematerialID, int? examsCategoryID, int? examinationModeID, int? examinationStyleID, int? courseEndWeek, int? statusID, List selectIDlist) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { //原先是用教学任务关联的由于补考记录没有对应的教学任务,所以改用专业课程关联, //学校那边说不会有多个教研室开同一门可的情况,即使有,那这些教研室同样可以查到,也没有问题 exp = exp.And(x => x.EM_Coursematerial.EM_SpecialtyCourse.Any(w => w.CF_Department.CollegeID == collegeID)); } if (gradeYearID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.GradeID == gradeYearID) .Select(s => s.CF_Classmajor.CF_Grademajor.GradeID).FirstOrDefault() == gradeYearID); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID) .Select(s => s.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID).FirstOrDefault() == standardID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.CoursematerialID == coursematerialID); } if (examsCategoryID.HasValue) { exp = exp.And(x => x.ExamsCategoryID == examsCategoryID); } if (examinationModeID.HasValue) { exp = exp.And(x => x.ExaminationModeID == examinationModeID); } if (examinationStyleID.HasValue) { exp = exp.And(x => x.ExaminationStyleID == examinationStyleID); } if(classmajorID.HasValue) { var classmajor = ExaminationPlanDAL.ClassmajorRepository.GetList(x => x.ClassmajorID == classmajorID).FirstOrDefault(); exp = exp.And(x => x.ClassName == classmajor.Name); } if (statusID.HasValue) { exp = exp.And(x => x.RecordStatus == statusID); } if (selectIDlist != null && selectIDlist.Count > 0) { exp = exp.And(x => selectIDlist.Contains(x.ExaminationPlanID)); } var q = ExaminationPlanDAL.GetExaminationPlanViewForAdult(exp); if (courseEndWeek.HasValue) { q = q.Where(x => x.CourseEndWeekNum == courseEndWeek); } if (!string.IsNullOrEmpty(examinationPlanView.ConditionValue) && !string.IsNullOrEmpty(examinationPlanView.Attribute)) q = q.DynamicWhere(examinationPlanView.Attribute, examinationPlanView.Condition, examinationPlanView.ConditionValue); return this.GetQueryByDataRangeByCollege(q).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExamsCategoryID).ThenBy(x => x.CourseEndWeekNum).ThenBy(x => x.CoursematerialName).ThenBy(x => x.ClassName).ToList(); } public IGridResultSet GetExaminationTeacherViewInfo(Guid? examinationRoomLayoutID) { return ExaminationPlanDAL.GetExaminationTeacherView(x => x.ExaminationRoomLayoutID == examinationRoomLayoutID, (x => true)).ToGridResultSet(); } public IGridResultSet GetExaminationStudentViewInfo(Guid? examinationRoomLayoutID) { var roomLayout = ExaminationPlanDAL.ExaminationRoomLayoutRepository.GetSingle(x => x.ExaminationRoomLayoutID == examinationRoomLayoutID); var q = ExaminationPlanDAL.GetExaminationRoomStudentView(x => x.ExaminationRoomLayoutID == examinationRoomLayoutID); if (roomLayout.StudentOrderType == (int)CF_StudentOrderType.StudentNo) { return q.OrderBy(x => x.LoginID).ToGridResultSet(); } else { return q.ToGridResultSet(); } } public ViewModel.ExaminationManage.ExaminationPlanView GetExaminationPlanViewInfo(Guid? examinationPlanID) { return ExaminationPlanDAL.GetExaminationPlanView(x => x.ExaminationPlanID == examinationPlanID).FirstOrDefault(); } public ExaminationRoomLayoutView GetExaminationRoomLayoutViewInfo(Guid? examinationRoomLayoutID) { return ExaminationPlanDAL.GetExaminationRoomLayoutView(x => x.ExaminationRoomLayoutID == examinationRoomLayoutID).FirstOrDefault(); } public virtual void GenerateExaminationPlan(Guid? schoolyearID, Guid? collegeID, int? gradeID, int? standardID) { var nowDate = DateTime.Now; var recordStatusID = (int)CF_ApprovalStatus.NotSubmitted; var user = CustomPrincipal.Current; Expression> exp = x => true; Expression> emcExp = x => true; //x.SchoolyearID == schoolyearID; if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { exp = exp.And(x => x.CollegeID == collegeID); } if (gradeID.HasValue && gradeID != -1) { emcExp = emcExp.And(x => x.CF_Classmajor.Any(y => y.CF_Grademajor.GradeID == gradeID)); } if (standardID.HasValue && standardID != -1) { emcExp = emcExp.And(x => x.CF_Classmajor.Any(y => y.CF_Grademajor.CF_Facultymajor.StandardID == standardID)); } var examinationPlanQuery = ExaminationPlanDAL.GetExaminationPlanByEducationMission(exp, emcExp); List examinationPlanViewList = this.GetQueryByDataRangeByCollege(examinationPlanQuery).ToList(); var dbExaminationPlanList = ExaminationPlanDAL.ExaminationPlanRepository.Entities; int sameCount = 0; List listExaminationPlan = new List(); foreach (var examinationPlan in examinationPlanViewList) { if (dbExaminationPlanList.Any(x => x.ClassName.Contains(examinationPlan.ClassName))) { sameCount = dbExaminationPlanList.Where(x => x.ClassName.Contains(examinationPlan.ClassName)).Count(); } EX_ExaminationPlan newexaminationPlan = new EX_ExaminationPlan(); newexaminationPlan.ExaminationPlanID = Guid.NewGuid(); newexaminationPlan.SchoolyearID = examinationPlan.SchoolyearID; newexaminationPlan.CollegeID = examinationPlan.CollegeID; newexaminationPlan.CoursematerialID = examinationPlan.CoursematerialID; newexaminationPlan.ExaminationModeID = examinationPlan.ExaminationModeID; newexaminationPlan.ExaminationStyleID = examinationPlan.ExaminationStyleID; newexaminationPlan.ExamsCategoryID = (int)CF_ExamsCategory.FinalExam; newexaminationPlan.EducationMissionID = examinationPlan.EducationMissionID; newexaminationPlan.ClassmajorID = examinationPlan.ClassmajorID; //if (sameCount > 0) //{ // examinationPlan.ClassName = examinationPlan.ClassName + sameCount.ToString(); //} newexaminationPlan.ClassName = examinationPlan.ClassName; newexaminationPlan.ExaminationDate = examinationPlan.ExaminationDate; newexaminationPlan.StartTime = examinationPlan.StartTime; newexaminationPlan.EndTime = examinationPlan.EndTime; newexaminationPlan.CF_Student = new HashSet(examinationPlan.Students); SetNewStatus(newexaminationPlan); newexaminationPlan.RecordStatus = recordStatusID; listExaminationPlan.Add(newexaminationPlan); } //var examinationPlanList = examinationPlanViewList.Select(x => new EX_ExaminationPlan //{ // ExaminationPlanID = Guid.NewGuid(), // SchoolyearID = x.SchoolyearID, // CollegeID = x.CollegeID, // CoursematerialID = x.CoursematerialID, // ExaminationModeID = x.ExaminationModeID, // ExaminationStyleID = x.ExaminationStyleID, // ExamsCategoryID = (int)CF_ExamsCategory.FinalExam, // EducationMissionID = x.EducationMissionID, // ClassmajorID = x.ClassmajorID, // ClassName = x.ClassName, // ExaminationDate = x.ExaminationDate, // StartTime = x.StartTime, // EndTime = x.EndTime, // CF_Student = new HashSet(x.Students), // RecordStatus = recordStatusID, // CreateUserID = user.UserID, // CreateTime = nowDate, // ModifyUserID = user.UserID, // ModifyTime = nowDate //}).ToList(); this.UnitOfWork.BulkInsert(listExaminationPlan); this.UnitOfWork.BulkInsert(listExaminationPlan, (x => x.CF_Student)); } public void GenerateResitPlan(Guid schoolyearID) { try { var statusList = this.GetStatusViewList(); var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curSchoolyear = SchoolYearServices.Value.GetCurrentSchoolYear(); List listExaminationPlan = new List(); var dbExaminationPlanList = ExaminationPlanDAL.ExaminationPlanRepository.Entities; var resitExaminationPlanList = this.GetQueryByDataRangeByCollege(ExaminationPlanDAL.GetExaminationPlanByResitStudent(schoolyearID, curSchoolyear.SchoolYearID)).ToList(); var resitExaminationPlanByFinalExaminationList = this.GetQueryByDataRangeByCollege(ExaminationPlanDAL.GetExaminationPlanByResitExamination(curSchoolyear.SchoolYearID)).ToList(); resitExaminationPlanList = resitExaminationPlanList.Concat(resitExaminationPlanByFinalExaminationList).ToList(); int sameCount = 0; foreach (var resitExaminationPlan in resitExaminationPlanList) { if (dbExaminationPlanList.Any(x => x.ClassName.Contains(resitExaminationPlan.ClassName))) { sameCount = dbExaminationPlanList.Where(x => x.ClassName.Contains(resitExaminationPlan.ClassName)).Count(); } EX_ExaminationPlan examinationPlan = new EX_ExaminationPlan(); examinationPlan.ExaminationPlanID = Guid.NewGuid(); examinationPlan.SchoolyearID = resitExaminationPlan.SchoolyearID; examinationPlan.CollegeID = resitExaminationPlan.CollegeID; examinationPlan.CoursematerialID = resitExaminationPlan.CoursematerialID; examinationPlan.ExaminationModeID = resitExaminationPlan.ExaminationModeID; examinationPlan.ExaminationStyleID = resitExaminationPlan.ExaminationStyleID; examinationPlan.ExamsCategoryID = (int)CF_ExamsCategory.Resit; //if (sameCount > 0) //{ // resitExaminationPlan.ClassName = resitExaminationPlan.ClassName + sameCount.ToString(); //} examinationPlan.ClassName = resitExaminationPlan.ClassName; examinationPlan.CF_Student = new HashSet(resitExaminationPlan.Students); SetNewStatus(examinationPlan); examinationPlan.RecordStatus = (int)CF_ApprovalStatus.NotSubmitted; listExaminationPlan.Add(examinationPlan); } UnitOfWork.BulkInsert(listExaminationPlan); this.UnitOfWork.BulkInsert(listExaminationPlan, (x => x.CF_Student)); } catch (Exception) { throw; } } public void GenerateGraduateExamPlan(int year, Guid? collegeID, int? standardID, decimal? learnSystem) { try { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curSchoolyear = SchoolYearServices.Value.GetCurrentSchoolYear(); List listExaminationPlan = new List(); var dbgraduateExamPlanList = ExaminationPlanDAL.ExaminationPlanRepository.Entities; var graduateExamPlanList = this.GetQueryByDataRangeByCollege(ExaminationPlanDAL.GetExaminationPlanByGraduateExamStudent(year,collegeID,standardID,learnSystem, curSchoolyear.SchoolYearID)).ToList(); var graduateExamPlanByFinalExaminationList = this.GetQueryByDataRangeByCollege(ExaminationPlanDAL.GetExaminationPlanByGraduateExamination(curSchoolyear.SchoolYearID)).ToList(); graduateExamPlanList = graduateExamPlanList.Concat(graduateExamPlanByFinalExaminationList).ToList(); int sameCount = 0; foreach (var graduateExamPlan in graduateExamPlanList) { if (dbgraduateExamPlanList.Any(x => x.ClassName.Contains(graduateExamPlan.ClassName))) { sameCount = dbgraduateExamPlanList.Where(x => x.ClassName.Contains(graduateExamPlan.ClassName)).Count(); } EX_ExaminationPlan examinationPlan = new EX_ExaminationPlan(); examinationPlan.ExaminationPlanID = Guid.NewGuid(); examinationPlan.SchoolyearID = graduateExamPlan.SchoolyearID; examinationPlan.CollegeID = graduateExamPlan.CollegeID; examinationPlan.CoursematerialID = graduateExamPlan.CoursematerialID; examinationPlan.ExaminationModeID = graduateExamPlan.ExaminationModeID; examinationPlan.ExaminationStyleID = graduateExamPlan.ExaminationStyleID; examinationPlan.ExamsCategoryID = (int)CF_ExamsCategory.GraduationExam; //if (sameCount > 0) //{ // graduateExamPlan.ClassName = graduateExamPlan.ClassName + sameCount.ToString(); //} examinationPlan.ClassName = graduateExamPlan.ClassName; examinationPlan.CF_Student = new HashSet(graduateExamPlan.Students); SetNewStatus(examinationPlan); examinationPlan.RecordStatus = (int)CF_ApprovalStatus.NotSubmitted; listExaminationPlan.Add(examinationPlan); } UnitOfWork.BulkInsert(listExaminationPlan); this.UnitOfWork.BulkInsert(listExaminationPlan, (x => x.CF_Student)); } catch (Exception) { throw; } } public void Save(ExaminationPlanView examinationPlanView) { var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var examinationPlanScheduleView = ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationPlanID == examinationPlanView.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).FirstOrDefault(); var scheduledPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationDate == examinationPlanScheduleView.ExaminationPlan.ExaminationDate && x.StartTime <= examinationPlanScheduleView.ExaminationPlan.EndTime && x.EndTime >= examinationPlanScheduleView.ExaminationPlan.StartTime && x.EX_ExaminationRoomLayout.Count() > 0 && x.ExaminationPlanID != examinationPlanScheduleView.ExaminationPlan.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).ToList(); var timeSegment = new TimeSegmentView { ExaminationDate = examinationPlanView.ExaminationDate.Value, StartTime = examinationPlanView.StartTime.Value, EndTime = examinationPlanView.EndTime.Value }; var errorList = new List(); foreach (var classroomLayout in examinationPlanScheduleView.ExaminationPlan.EX_ExaminationRoomLayout) { var studentIDList = classroomLayout.EX_ExaminationRoomStudent.Select(x => x.UserID.Value).ToList(); if (IsConflictForSetTime(examinationPlanScheduleView, studentIDList, timeSegment, classroomLayout.CF_Classroom, scheduledPlanViewList)) { errorList.Add("考场" + classroomLayout.CF_Classroom.Name + "时间安排存在冲突,请重新核对后再进行设置。"); } } if (errorList.Count > 0) { throw new Exception(string.Join(",", errorList)); } if (examinationPlanScheduleView != null) { examinationPlanScheduleView.ExaminationPlan.ExaminationDate = examinationPlanView.ExaminationDate; examinationPlanScheduleView.ExaminationPlan.StartTime = examinationPlanView.StartTime; examinationPlanScheduleView.ExaminationPlan.EndTime = examinationPlanView.EndTime; this.SetModifyStatus(examinationPlanScheduleView.ExaminationPlan); } else { throw new Exception("指定的考试计划不存在。"); } UnitOfWork.Commit(); } public virtual void Submit(IList examinationPlanIDList) { var examinationPlanList = ExaminationPlanDAL.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), (x => x.EX_ExaminationRoomStudent), (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); } if (examinationPlanIDList.Count > 0) { UnitOfWork.Update((x => new EX_ExaminationPlan { RecordStatus = (int)CF_ApprovalStatus.Submitted }), (x => examinationPlanIDList.Contains(x.ExaminationPlanID))); } } public void UnSubmit(IList examinationPlanIDList) { var examinationPlanList = ExaminationPlanDAL.ExaminationPlanRepository .GetList(x => examinationPlanIDList.Contains(x.ExaminationPlanID), (x => x.EX_ExaminationRoomLayout)).ToList(); var submitedPlan = examinationPlanList.Where(x => x.RecordStatus != (int)CF_ApprovalStatus.Submitted).ToList(); if (submitedPlan.Count > 0) { string submitedClassNames = string.Join(",", submitedPlan.Select(x => x.ClassName)) + "并不在已提交状态,请选择正确的记录进行撤回。"; throw new Exception(submitedClassNames); } if (examinationPlanIDList.Count > 0) { UnitOfWork.Update((x => new EX_ExaminationPlan { RecordStatus = (int)CF_ApprovalStatus.NotSubmitted }), (x => examinationPlanIDList.Contains(x.ExaminationPlanID))); } } public IGridResultSet GetExaminationRoomLayoutViewList(Guid examinationPlanID) { return this.ExaminationPlanDAL.GetExaminationRoomLayoutView(x => x.ExaminationPlanID == examinationPlanID).ToGridResultSet(); } protected List SetRoomStudentSeats(IList studentList, ClassroomView classroom, int rowSpacing, int columnSpacing, CF_StudentOrderType? orderType = null) { var result = new List(); if (classroom == null) throw new Exception("指定的教室资源不存在。"); int currentIndex = 0; var firstRowSeatCount = Math.Ceiling((double)classroom.ColumnCount / (double)(columnSpacing + 1)); var rowCount = Math.Ceiling((double)classroom.RowCout / (double)(rowSpacing + 1)); switch (orderType) { case CF_StudentOrderType.StudentNo: studentList = studentList.OrderBy(x => x.LoginID).ToList(); break; case CF_StudentOrderType.Random: studentList = studentList.OrderBy(x => Guid.NewGuid()).ToList(); break; } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < firstRowSeatCount; j++) { if (currentIndex >= studentList.Count) { break; } var roomStudent = new EX_ExaminationRoomStudent(); roomStudent.ExaminationRoomStudentID = Guid.NewGuid(); roomStudent.ExaminationRoomLayoutID = studentList[currentIndex].ExaminationRoomLayoutID; roomStudent.UserID = studentList[currentIndex].UserID; roomStudent.Row = i * (rowSpacing + 1) + 1; roomStudent.Column = j * (columnSpacing + 1) + 1; roomStudent.Remark = studentList[currentIndex].Remark; result.Add(roomStudent); currentIndex++; } } return result; } public void SaveClassroomLayout(ViewModel.ExaminationManage.ExaminationRoomLayoutView examinationRoomLayoutView, IList staffIDList, IList studentList) { var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var examinationRoomLayoutEntity = ExaminationPlanDAL.ExaminationRoomLayoutRepository .GetSingle(x => x.ExaminationRoomLayoutID == examinationRoomLayoutView.ExaminationRoomLayoutID, (x => x.CF_Staff), (x => x.EX_ExaminationRoomStudent)); var examinationPlanView = ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationPlanID == examinationRoomLayoutView.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).FirstOrDefault(); if (examinationPlanView == null) { throw new Exception("找不到对应的考试计划。"); } if (examinationRoomLayoutEntity == null && examinationPlanView.ExaminationPlan.EX_ExaminationRoomLayout.Any(x => x.ClassroomID == examinationRoomLayoutView.ClassroomID)) { throw new Exception("该考试计划已经设置该教室为考场!"); } var scheduledPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationDate == examinationPlanView.ExaminationPlan.ExaminationDate && x.StartTime <= examinationPlanView.ExaminationPlan.EndTime && x.EndTime >= examinationPlanView.ExaminationPlan.StartTime && x.EX_ExaminationRoomLayout.Count() > 0 && x.ExaminationPlanID != examinationPlanView.ExaminationPlan.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).ToList(); var timeSegment = new TimeSegmentView { ExaminationDate = examinationPlanView.ExaminationPlan.ExaminationDate.Value, StartTime = examinationPlanView.ExaminationPlan.StartTime.Value, EndTime = examinationPlanView.ExaminationPlan.EndTime.Value }; var classroomEntity = ExaminationPlanDAL.ClassroomRepository.GetSingle(x => x.ClassroomID == examinationRoomLayoutView.ClassroomID, (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationPlan))); if (IsConflictForSetTime(examinationPlanView, studentList.Select(x => x.UserID.Value).ToList(), timeSegment, classroomEntity, scheduledPlanViewList)) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,请重新核对后再进行设置。"); } if (examinationRoomLayoutEntity != null) { examinationRoomLayoutEntity.ExaminationPlanID = examinationRoomLayoutView.ExaminationPlanID; examinationRoomLayoutEntity.ClassroomID = examinationRoomLayoutView.ClassroomID; examinationRoomLayoutEntity.RowSpacing = examinationRoomLayoutView.RowSpacing; examinationRoomLayoutEntity.ColumnSpacing = examinationRoomLayoutView.ColumnSpacing; examinationRoomLayoutEntity.StudentOrderType = examinationRoomLayoutView.StudentOrderType; this.SetModifyStatus(examinationRoomLayoutEntity); } else { examinationRoomLayoutEntity = new EX_ExaminationRoomLayout(); examinationRoomLayoutEntity.ExaminationRoomLayoutID = Guid.NewGuid(); examinationRoomLayoutEntity.ExaminationPlanID = examinationRoomLayoutView.ExaminationPlanID; examinationRoomLayoutEntity.ClassroomID = examinationRoomLayoutView.ClassroomID; examinationRoomLayoutEntity.RowSpacing = examinationRoomLayoutView.RowSpacing; examinationRoomLayoutEntity.ColumnSpacing = examinationRoomLayoutView.ColumnSpacing; examinationRoomLayoutEntity.StudentOrderType = examinationRoomLayoutView.StudentOrderType; this.SetModifyStatus(examinationRoomLayoutEntity); this.UnitOfWork.Add(examinationRoomLayoutEntity); } var staffList = this.StaffDAL.staffRepository.GetList(x => staffIDList.Contains(x.UserID)).ToList(); examinationRoomLayoutEntity.CF_Staff = new HashSet(); examinationRoomLayoutEntity.CF_Staff = new HashSet(staffList); UnitOfWork.Remove(x => x.ExaminationRoomLayoutID == examinationRoomLayoutEntity.ExaminationRoomLayoutID); var classroomID = (examinationRoomLayoutEntity.ClassroomID ?? Guid.Empty); var classroom = ClassroomDAL.GetClassroomViewQueryable(x => x.ClassroomID == classroomID).FirstOrDefault(); var roomStudentList = this.SetRoomStudentSeats(studentList, classroom, examinationRoomLayoutEntity.RowSpacing ?? 0, examinationRoomLayoutEntity.ColumnSpacing ?? 0); roomStudentList.ForEach(x => { x.ExaminationRoomLayoutID = examinationRoomLayoutEntity.ExaminationRoomLayoutID; this.SetNewStatus(x); this.UnitOfWork.Add(x); }); UnitOfWork.Commit(); } /// /// 新增保存 /// /// /// /// public void SaveAddClassroomLayout(ExaminationRoomLayoutView examinationRoomLayoutView, IList staffIDList, IList studentList) { var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var examinationRoomLayoutEntity = ExaminationPlanDAL.ExaminationRoomLayoutRepository .GetSingle(x => x.ExaminationRoomLayoutID == examinationRoomLayoutView.ExaminationRoomLayoutID, (x => x.CF_Staff), (x => x.EX_ExaminationRoomStudent)); var examinationPlanView = ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationPlanID == examinationRoomLayoutView.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).FirstOrDefault(); if (examinationPlanView == null) { throw new Exception("找不到对应的考试计划。"); } var scheduledPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationDate == examinationPlanView.ExaminationPlan.ExaminationDate && x.StartTime <= examinationPlanView.ExaminationPlan.EndTime && x.EndTime >= examinationPlanView.ExaminationPlan.StartTime && x.EX_ExaminationRoomLayout.Count() > 0 && x.ExaminationPlanID != examinationPlanView.ExaminationPlan.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).ToList(); var timeSegment = new TimeSegmentView { ExaminationDate = examinationPlanView.ExaminationPlan.ExaminationDate.Value, StartTime = examinationPlanView.ExaminationPlan.StartTime.Value, EndTime = examinationPlanView.ExaminationPlan.EndTime.Value }; var classroomEntity = ExaminationPlanDAL.ClassroomRepository.GetSingle(x => x.ClassroomID == examinationRoomLayoutView.ClassroomID, (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationPlan))); if (IsConflictForSetTime(examinationPlanView, studentList.Select(x => x.UserID.Value).ToList(), timeSegment, classroomEntity, scheduledPlanViewList)) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,请重新核对后再进行设置。"); } if (examinationRoomLayoutEntity != null) { examinationRoomLayoutEntity.ExaminationPlanID = examinationRoomLayoutView.ExaminationPlanID; examinationRoomLayoutEntity.ClassroomID = examinationRoomLayoutView.ClassroomID; examinationRoomLayoutEntity.RowSpacing = examinationRoomLayoutView.RowSpacing; examinationRoomLayoutEntity.ColumnSpacing = examinationRoomLayoutView.ColumnSpacing; examinationRoomLayoutEntity.StudentOrderType = examinationRoomLayoutView.StudentOrderType; this.SetModifyStatus(examinationRoomLayoutEntity); } else { examinationRoomLayoutEntity = new EX_ExaminationRoomLayout(); examinationRoomLayoutEntity.ExaminationRoomLayoutID = Guid.NewGuid(); examinationRoomLayoutEntity.ExaminationPlanID = examinationRoomLayoutView.ExaminationPlanID; examinationRoomLayoutEntity.ClassroomID = examinationRoomLayoutView.ClassroomID; examinationRoomLayoutEntity.RowSpacing = examinationRoomLayoutView.RowSpacing; examinationRoomLayoutEntity.ColumnSpacing = examinationRoomLayoutView.ColumnSpacing; examinationRoomLayoutEntity.StudentOrderType = examinationRoomLayoutView.StudentOrderType; this.SetModifyStatus(examinationRoomLayoutEntity); this.UnitOfWork.Add(examinationRoomLayoutEntity); } var staffList = this.StaffDAL.staffRepository.GetList(x => staffIDList.Contains(x.UserID)).ToList(); examinationRoomLayoutEntity.CF_Staff = new HashSet(); examinationRoomLayoutEntity.CF_Staff = new HashSet(staffList); UnitOfWork.Remove(x => x.ExaminationRoomLayoutID == examinationRoomLayoutEntity.ExaminationRoomLayoutID); var classroomID = (examinationRoomLayoutEntity.ClassroomID ?? Guid.Empty); var classroom = ClassroomDAL.GetClassroomViewQueryable(x => x.ClassroomID == classroomID).FirstOrDefault(); var roomStudentList = this.SetRoomStudentSeats(studentList, classroom, examinationRoomLayoutEntity.RowSpacing ?? 0, examinationRoomLayoutEntity.ColumnSpacing ?? 0); roomStudentList.ForEach(x => { x.ExaminationRoomLayoutID = examinationRoomLayoutEntity.ExaminationRoomLayoutID; this.SetNewStatus(x); this.UnitOfWork.Add(x); }); UnitOfWork.Commit(); } public Bowin.Common.Linq.Entity.IGridResultSet GetAvailableStudentViewList(Guid examinationPlanID, Guid? collegeID, Guid? grademajorID, Guid? classmajorID, CF_StudentOrderType studentOrderType, int? pageIndex, int? pageSize) { var inschool = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x=>x.InSchoolStatusID).ToList(); Expression> studentExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && inschool.Contains(x.InSchoolStatusID)); Expression> classExp = (x => true); Expression> gradeExp = (x => true); IQueryable query; if (collegeID.HasValue || grademajorID.HasValue || classmajorID.HasValue) { if (collegeID.HasValue) { gradeExp = gradeExp.And(x => x.CF_Facultymajor.CollegeID == collegeID); } if (grademajorID.HasValue) { classExp = classExp.And(x => x.GrademajorID == grademajorID); } if (classmajorID.HasValue) { studentExp = studentExp.And(x => x.ClassmajorID == classmajorID); } query = StudentsDAL.GetStudentQueryable(studentExp, classExp, gradeExp); } else { query = ExaminationPlanDAL.GetAvailableStudentView(x => x.ExaminationPlanID == examinationPlanID, studentExp); } if (studentOrderType == CF_StudentOrderType.Random) { query = query.OrderByNewId(); } else if (studentOrderType == CF_StudentOrderType.StudentNo) { query = query.OrderBy(x => x.LoginID); } return query.ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetAddAvailableStudentViewList(Guid? collegeID, Guid? grademajorID, Guid? classmajorID, CF_StudentOrderType studentOrderType, int? pageIndex, int? pageSize) { var inschool = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); Expression> studentExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && inschool.Contains(x.InSchoolStatusID)); Expression> classExp = (x => true); Expression> gradeExp = (x => true); IQueryable query; if (collegeID.HasValue || grademajorID.HasValue || classmajorID.HasValue) { if (collegeID.HasValue) { gradeExp = gradeExp.And(x => x.CF_Facultymajor.CollegeID == collegeID); } if (grademajorID.HasValue) { classExp = classExp.And(x => x.GrademajorID == grademajorID); } if (classmajorID.HasValue) { studentExp = studentExp.And(x => x.ClassmajorID == classmajorID); } query = StudentsDAL.GetStudentQueryable(studentExp, classExp, gradeExp); } else { query = ExaminationPlanDAL.GetAvailableStudentView(x => true, studentExp); } if (studentOrderType == CF_StudentOrderType.Random) { query = query.OrderByNewId(); } else if (studentOrderType == CF_StudentOrderType.StudentNo) { query = query.OrderBy(x => x.LoginID); } return query.ToGridResultSet(pageIndex, pageSize); } public void DeleteExaminationClassroomLayout(IList examinationRoomLayoutIDs) { var examinationLayoutList = this.ExaminationPlanDAL.ExaminationRoomLayoutRepository .GetList(x => examinationRoomLayoutIDs.Contains(x.ExaminationRoomLayoutID), (x => x.EX_ExaminationRoomStudent), (x => x.CF_Staff)).ToList(); foreach (var examinationLayout in examinationLayoutList) { examinationLayout.CF_Staff = new HashSet(); examinationLayout.EX_ExaminationRoomStudent.ToList().ForEach(x => this.UnitOfWork.Remove(x)); this.UnitOfWork.Remove(examinationLayout); } this.UnitOfWork.Commit(); } public void Delete(IList examinationPlanIDs) { try { var nullableIDs = examinationPlanIDs.Select(x => (Guid?)x).ToList(); List ExaminationPlanList = examinationPlanRepository .GetList(x => examinationPlanIDs.Contains(x.ExaminationPlanID), (x => x.CF_Student), (x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff))).ToList(); if (ExaminationPlanList.Any(x => x.RecordStatus != (int)CF_ApprovalStatus.NotSubmitted)) { throw new Exception("要删除的信息只能是未提交的信息。"); } var layoutList = ExaminationPlanList.SelectMany(x => x.EX_ExaminationRoomLayout).ToList(); var layoutIDList = layoutList.Select(x => (Guid?)x.ExaminationRoomLayoutID).ToList(); using (var scope = new TransactionScope()) { UnitOfWork.Delete(ExaminationPlanList, (x => x.CF_Student)); UnitOfWork.Delete(layoutList, (x => x.CF_Staff)); UnitOfWork.Delete(x => layoutIDList.Contains(x.ExaminationRoomLayoutID)); UnitOfWork.Delete(x => layoutIDList.Contains(x.ExaminationRoomLayoutID)); UnitOfWork.Delete(x => examinationPlanIDs.Contains(x.ExaminationPlanID)); scope.Complete(); } } catch (Exception ex) { throw ex; } } public virtual void AutoGenerateExaminationRoomLayout(IList examinationPlanIDList) { DateTime today = DateTime.Today; var userCollegeIDList = this.GetQueryByDataRangeByCollege(CollegeDAL.GetCollegeViewQueryable()).Select(x => x.CollegeID).ToList(); var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var curSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); //获取可排时间 var examinationDateList = this.TimeSegmentDAL.GetTimeSegmentView(x => x.ExaminationDate > today) .OrderByDescending(x => x.IsForResit).ThenBy(x => x.ExaminationDate).ThenBy(x => x.StartTime).ToList(); //获取当前学年学期的考试计划(由于要检测冲突,所以不能只拿没排完的,也不要限制数据范围) var examinationPlanList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.SchoolyearID == curSchoolyear.SchoolYearID //&& (x.ExamsCategoryID == (int)CF_ExamsCategory.Resit || x.ExamsCategoryID == (int)CF_ExamsCategory.GraduationExam) ) .Include(x => x.EM_Coursematerial) .Include(x => x.CF_Student.Select(w => w.Sys_User)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).ToList(); //获取没排完的考试计划 var notCompleteExaminationPlanList = examinationPlanList.Where(x => x.StudentCount < x.MissionStudentCount && userCollegeIDList.Contains(x.ExaminationPlan.CollegeID.Value) && x.ExaminationPlan.RecordStatus == (int)EX_ExaminationPlanStatus.NotSubmitted && (examinationPlanIDList.Count == 0 || examinationPlanIDList.Contains(x.ExaminationPlan.ExaminationPlanID))).ToList(); if (examinationPlanIDList.Where(x => !notCompleteExaminationPlanList .Select(w => (Guid?)w.ExaminationPlan.ExaminationPlanID).Contains(x)).Count() > 0) { throw new Exception("当前选中的考试计划不包含当前学年学期需要排考的计划,请检查选择。"); } //获取已经指定时间的未排考试计划(三部分组成: //1、已经指定了时间的未排计划; //2、未指定时间但有其他同科目的未排计划已经指定了时间; //3、未指定时间但有其他同科目的已派计划已经指定了时间) var notCompleteExaminationPlanWithDateList = notCompleteExaminationPlanList.Where(x => x.ExaminationPlan.ExaminationDate.HasValue && x.ExaminationPlan.StartTime.HasValue && x.ExaminationPlan.EndTime.HasValue).ToList(); notCompleteExaminationPlanWithDateList = notCompleteExaminationPlanWithDateList.Concat( notCompleteExaminationPlanList.Where(x => !notCompleteExaminationPlanWithDateList.Select(w => w.ExaminationPlan.ExaminationPlanID).Contains(x.ExaminationPlan.ExaminationPlanID) && notCompleteExaminationPlanWithDateList.Select(w => w.ExaminationPlan.CoursematerialID).Contains(x.ExaminationPlan.CoursematerialID) ) ).Concat( from notComplete in notCompleteExaminationPlanList join complete in examinationPlanList.Where(x => x.ExaminationPlan.ExaminationDate.HasValue) on notComplete.ExaminationPlan.CoursematerialID equals complete.ExaminationPlan.CoursematerialID group notComplete by notComplete.ExaminationPlan.ExaminationPlanID into g select g.FirstOrDefault() ).ToList(); //获取排课 var scheduleDateTimeList = examinationDateList.Select(x => new StartEndTimeView { StartTime = x.ExaminationDate.Add(x.StartTime), EndTime = x.ExaminationDate.Add(x.EndTime) }) .Concat(notCompleteExaminationPlanWithDateList .Where(x => x.ExaminationPlan.ExaminationDate.HasValue && x.ExaminationPlan.StartTime.HasValue && x.ExaminationPlan.EndTime.HasValue) .GroupBy(x => new { StartTime = x.ExaminationPlan.ExaminationDate.Value.Add(x.ExaminationPlan.StartTime.Value), EndTime = x.ExaminationPlan.ExaminationDate.Value.Add(x.ExaminationPlan.EndTime.Value) }).Select(x => new StartEndTimeView { StartTime = x.Key.StartTime, EndTime = x.Key.EndTime })) .ToList(); var schedulingList = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var excessiveUseList = this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var conflictStudentPlanWithDateList = (from plan in notCompleteExaminationPlanWithDateList from student in plan.ExaminationPlan.CF_Student from scheduling in schedulingList.Where(x => x.Date == plan.ExaminationPlan.ExaminationDate && x.StartTime <= plan.ExaminationPlan.EndTime && x.EndTime >= plan.ExaminationPlan.StartTime) from schedulingStudent in scheduling.StudentList.Where(x => x.UserID == student.UserID) group plan by plan.ExaminationPlan.ExaminationPlanID into g select g.FirstOrDefault() ).ToList(); if (conflictStudentPlanWithDateList.Count > 0) { throw new Exception(string.Format("考试计划{0}由于指定了具体的考试时间,而学生需要上课无法安排,未免影响整体的考试安排,排考中止。", string.Join("、", conflictStudentPlanWithDateList.Select(x => x.ExaminationPlan.ClassName)))); } //获取未指定时间的未排考试计划 var notCompleteExaminationPlanWithOutDateList = notCompleteExaminationPlanList .Where(x => !notCompleteExaminationPlanWithDateList.Select(w => w.ExaminationPlan.CoursematerialID).Contains(x.ExaminationPlan.CoursematerialID) ).ToList(); //获取已排完的考试计划 var scheduledStudentList = (from plan in examinationPlanList.Where(x => x.StudentCount > 0) from layout in plan.ExaminationPlan.EX_ExaminationRoomLayout from student in layout.EX_ExaminationRoomStudent select new StudentClassroomView { ExaminationDate = plan.ExaminationPlan.ExaminationDate, StartTime = plan.ExaminationPlan.StartTime, EndTime = plan.ExaminationPlan.EndTime, ClassroomID = layout.ClassroomID, UserID = student.UserID, TeacherIDList = layout.CF_Staff.Select(x => x.UserID).ToList(), ExaminationStyleID = plan.ExaminationPlan.ExaminationStyleID, ExaminationPlanID = plan.ExaminationPlan.ExaminationPlanID }).ToList(); //获取足够的考室信息 var examinationRoomList = this.RoomSettingDAL.ExaminationRoomSettingRepository.GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE, (x => x.CF_Classroom.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)), (x => x.CF_Classroom.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationPlan)), (x => x.CF_Classroom.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent))).ToList(); var classroomIDList = examinationRoomList.Select(x => x.ClassroomID).ToList(); var classroomViewList = this.ClassroomDAL.GetClassroomViewQueryable(x => classroomIDList.Contains(x.ClassroomID)).ToList(); //获取足够的监考员信息 var examinationTeacherList = this.ExaminationTeacherDAL.ExaminationTeacherRepository.GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE, (x => x.CF_Staff)) .ToList(); var existsExaminationDateList = notCompleteExaminationPlanWithDateList.Where(x => x.ExaminationPlan.ExaminationDate.HasValue) .Concat( from notComplete in notCompleteExaminationPlanWithDateList join complete in examinationPlanList.Where(x => x.ExaminationPlan.ExaminationDate.HasValue) on notComplete.ExaminationPlan.CoursematerialID equals complete.ExaminationPlan.CoursematerialID group complete by new { complete.ExaminationPlan.ExaminationDate, complete.ExaminationPlan.StartTime, complete.ExaminationPlan.EndTime } into g select g.FirstOrDefault() ) .GroupBy(x => new { x.ExaminationPlan.ExaminationDate, x.ExaminationPlan.StartTime, x.ExaminationPlan.EndTime }) .Select(x => new TimeSegmentView { ExaminationDate = x.Key.ExaminationDate.Value, StartTime = x.Key.StartTime.Value, EndTime = x.Key.EndTime.Value }).OrderBy(x => x.ExaminationDate) .ThenBy(x => x.StartTime).ToList(); //先排指定了日期的考试计划 foreach (var examinationDate in existsExaminationDateList) { //先取出指定当前日期的考试计划和它们同科目的考试计划,在这个循环里面,必须排完这些内容, //如果排不完,直接抛出异常,结束整个排考 var notCompleteExaminationPlanCorrectTypeWithDateList = notCompleteExaminationPlanWithDateList .Where(x => ((!examinationDate.IsForResit) ? x.ExaminationPlan.ExamsCategoryID == (int)CF_ExamsCategory.FinalExam : x.ExaminationPlan.ExamsCategoryID != (int)CF_ExamsCategory.FinalExam)).ToList(); var fixedDateExaminationPlanList = notCompleteExaminationPlanCorrectTypeWithDateList .Where(x => x.ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate && x.ExaminationPlan.StartTime == examinationDate.StartTime && x.ExaminationPlan.EndTime == examinationDate.EndTime).ToList(); fixedDateExaminationPlanList = fixedDateExaminationPlanList.Concat( notCompleteExaminationPlanCorrectTypeWithDateList.Where(x => fixedDateExaminationPlanList.Select(w => w.ExaminationPlan.CoursematerialID).Contains(x.ExaminationPlan.CoursematerialID) && (!x.ExaminationPlan.ExaminationDate.HasValue || !x.ExaminationPlan.StartTime.HasValue || !x.ExaminationPlan.EndTime.HasValue)) ).Concat( from notComplete in notCompleteExaminationPlanCorrectTypeWithDateList join complete in examinationPlanList.Where(x => x.ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate && x.ExaminationPlan.StartTime == examinationDate.StartTime && x.ExaminationPlan.EndTime == examinationDate.EndTime) on notComplete.ExaminationPlan.CoursematerialID equals complete.ExaminationPlan.CoursematerialID group notComplete by notComplete.ExaminationPlan.ExaminationPlanID into g select g.FirstOrDefault() ).ToList(); //按照剩余座位数从多到少排序,拿出可排的教室备用,排考时,目的是为了 //(由于检查的是剩余座位数,这里已经解决了冲突的问题,没有剩余座位的考室不会被列出) var orderedExaminationRoomList = this.GetMaxSeatExaminationRoomList(examinationRoomList, examinationDate.ExaminationDate, examinationDate.StartTime, examinationDate.EndTime, scheduledStudentList); var result = this.AutoSchedule(fixedDateExaminationPlanList, orderedExaminationRoomList, classroomViewList, examinationDate, examinationTeacherList, scheduledStudentList, schedulingList, excessiveUseList); //如果排不完,直接抛出异常,结束整个排考 if (!result) { throw new Exception("自动排考失败:" + examinationDate.ExaminationDateString + "(" + examinationDate.StartTimeString + "-" + examinationDate.EndTimeString + ")无法排完所有指定日期的考试计划以及其相同科目的考试计划," + "排考过程中断,为了不影响后面的考试安排,请先调整好这些考试计划再进行排考。"); } } //再排未指定日期的考试计划,这些计划需要一个个课程地排,每个课程必须排在同一时间点,否则失败。 var courseList = notCompleteExaminationPlanWithOutDateList.GroupBy(x => new { x.ExaminationPlan.CoursematerialID, IsResit = x.ExaminationPlan.ExamsCategoryID != (int)CF_ExamsCategory.FinalExam }) .Select(x => new { Course = x.FirstOrDefault().ExaminationPlan.EM_Coursematerial, x.Key.IsResit }).ToList(); foreach (var course in courseList) { var courseExaminationPlanList = notCompleteExaminationPlanWithOutDateList .Where(x => x.ExaminationPlan.CoursematerialID == course.Course.CoursematerialID && (course.IsResit ? (x.ExaminationPlan.ExamsCategoryID != (int)CF_ExamsCategory.FinalExam) : (x.ExaminationPlan.ExamsCategoryID == (int)CF_ExamsCategory.FinalExam))) .ToList(); var courseExaminationDateList = examinationDateList.Where(x => x.IsForResit == course.IsResit) .OrderBy(x => x.ExaminationDate).ThenBy(x => x.StartTime); foreach (var examinationDate in courseExaminationDateList) { //先检查当时的学生冲突,有冲突的话需要移到下一个时段 var conflictStudentPlanEnum = (from plan in courseExaminationPlanList from student in plan.ExaminationPlan.CF_Student from scheduling in schedulingList.Where(x => x.Date == plan.ExaminationPlan.ExaminationDate && x.StartTime <= plan.ExaminationPlan.EndTime && x.EndTime >= plan.ExaminationPlan.StartTime) from schedulingStudent in scheduling.StudentList.Where(x => x.UserID == student.UserID) select plan); if (conflictStudentPlanEnum.Any()) { continue; } //按照剩余座位数从多到少排序,拿出可排的教室备用,排考时也先排人数多的考试计划, //目的是为了将同一场考试尽量安排在一个考场中 //(由于检查的是剩余座位数,这里已经解决了冲突的问题,没有剩余座位的考室不会被列出) var orderedExaminationRoomList = this.GetMaxSeatExaminationRoomList(examinationRoomList, examinationDate.ExaminationDate, examinationDate.StartTime, examinationDate.EndTime, scheduledStudentList); var result = this.AutoSchedule(courseExaminationPlanList, orderedExaminationRoomList, classroomViewList, examinationDate, examinationTeacherList, scheduledStudentList, schedulingList, excessiveUseList); //如果失败,需要从scheduledStudentList撤销这次排的记录,选另一个时间点重新排 if (!result) { //如果考试计划没指定日期,那初始状态下应该没有排到任何考场,可以清空排考信息再来过 scheduledStudentList.RemoveAll(x => courseExaminationPlanList.Select(w => w.ExaminationPlan.ExaminationPlanID).Contains(x.ExaminationPlanID)); courseExaminationPlanList.ForEach(x => x.StudentCount = 0); continue; } else { break; } } } //(需要增加逻辑显示具体排不进去的提示) var notSuccessList = notCompleteExaminationPlanWithOutDateList.Where(x => x.MissionStudentCount > x.StudentCount).ToList(); var successCount = notCompleteExaminationPlanList.Count - notSuccessList.Count; this.UnitOfWork.Commit(); if (successCount == 0) { throw new Exception("自动排课失败:" + string.Join("、", notSuccessList.Select(x => x.ExaminationPlan.ClassName)) + "由于时间冲突无法排入。"); } else if (notSuccessList.Count > 0) { throw new Exception("成功排课" + successCount.ToString() + "条记录,失败" + notSuccessList.Count.ToString() + "条记录,其中" + string.Join("、", notSuccessList.Select(x => x.ExaminationPlan.ClassName)) + "由于时间冲突无法排入。"); } } //一般情况下传输进来的未排考试必须是同一科目的考试 protected virtual 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(); var actualScheduleStudentList = needScheduleStudentList.Take(roomRemainSeat ?? 0).ToList(); //记录住这些数据需要修改,后面所有考场和考试任务都检查完后,再统一修改 var result = new AutoScheduleModifyView { ExaminationPlan = orderedExaminationPlanView.ExaminationPlan, ExaminationDate = examinationDate.ExaminationDate, StartTime = examinationDate.StartTime, EndTime = examinationDate.EndTime, ExaminationRoom = examinationRoom, ScheduleStudentList = actualScheduleStudentList }; 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(actualScheduleStudentList.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 += actualScheduleStudentList.Count(); if (actualScheduleStudentList.Count == needScheduleStudentList.Count) { scheduledCount++; } //如果教室塞满了,那后面的考试计划也排不动了,需要结束循环并跳转到下一个教室,以防浪费资源 if (actualScheduleStudentList.Count >= roomRemainSeat) { break; } } } //如果排不完,返回失败,让外部决定如何处理 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; } protected List GetNeedScheduleStudentList(ExamiationPlanAutoScheduleView examinationPlanView , List scheduledStudentList) { //这里只找出在当前考试计划里面排过的学生,这个功能是找需排的学生,如果是其他考试计划排过的话,算冲突 var needScheduleStudentList = (from student in examinationPlanView.ExaminationPlan.CF_Student join roomStudent in scheduledStudentList.Where(x => x.ExaminationPlanID == examinationPlanView.ExaminationPlan.ExaminationPlanID) on student.UserID equals roomStudent.UserID into dRoomStudent from eRoomStudent in dRoomStudent.DefaultIfEmpty() where eRoomStudent == null select student).ToList(); return needScheduleStudentList; } //判断考试方式是否有冲突 private bool IsExaminationStyleConflict(ExamiationPlanAutoScheduleView examinationPlanView, CF_Classroom examinationRoom, TimeSegmentView examinationDate) { if (examinationRoom.EX_ExaminationRoomLayout .Any(x => x.EX_ExaminationPlan.ExaminationStyleID != examinationPlanView.ExaminationPlan.ExaminationStyleID && x.EX_ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate && x.EX_ExaminationPlan.StartTime <= examinationDate.EndTime && x.EX_ExaminationPlan.EndTime >= examinationDate.StartTime)) { return true; } return false; } private bool IsExaminationStyleConflictForAuto(ExamiationPlanAutoScheduleView examinationPlanView, CF_Classroom examinationRoom, TimeSegmentView examinationDate, IList scheduledStudentList) { if (scheduledStudentList .Any(x => x.ExaminationStyleID != examinationPlanView.ExaminationPlan.ExaminationStyleID && x.ClassroomID == examinationRoom.ClassroomID && x.ExaminationDate == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime && x.EndTime >= examinationDate.StartTime)) { return true; } return false; } //判断考生时间有没有冲突 private bool IsStudentConflict(IList needScheduleStudentIDList, IList scheduledPlanList, TimeSegmentView examinationDate) { var conflictStudentList = (from student in needScheduleStudentIDList join scheduled in ( from scheduled in scheduledPlanList.Where(x => x.ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate && x.ExaminationPlan.StartTime <= examinationDate.EndTime && x.ExaminationPlan.EndTime >= examinationDate.StartTime) from layout in scheduled.ExaminationPlan.EX_ExaminationRoomLayout from student in layout.EX_ExaminationRoomStudent select student.UserID ) on student equals scheduled select student).ToList(); if (conflictStudentList.Count > 0) { return true; } return false; } //判断考生时间有没有冲突 private bool IsStudentConflictForAuto(IList needScheduleStudentIDList, IList scheduledStudentList, TimeSegmentView examinationDate) { var conflictStudentList = (from student in needScheduleStudentIDList join scheduled in scheduledStudentList.Where(x => x.ExaminationDate == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime && x.EndTime >= examinationDate.StartTime) on student equals scheduled.UserID select student).ToList(); if (conflictStudentList.Count > 0) { return true; } return false; } private bool IsClassroomConflictForAuto(CF_Classroom examinationRoom, TimeSegmentView examinationDate, IList scheduledStudentList) { var conflictRoomList = (from schedule in scheduledStudentList where schedule.ClassroomID == examinationRoom.ClassroomID && schedule.ExaminationDate == examinationDate.ExaminationDate && schedule.StartTime <= examinationDate.EndTime && schedule.EndTime >= examinationDate.StartTime select schedule).ToList(); if (conflictRoomList.Count >= examinationRoom.Examinationseating) { return true; } return false; } protected bool IsConflict(ExamiationPlanAutoScheduleView examinationPlanView, IList needScheduleStudentIDList, TimeSegmentView examinationDate, CF_Classroom examinationRoom, IList scheduledStudentList) { if (IsExaminationStyleConflictForAuto(examinationPlanView, examinationRoom, examinationDate, scheduledStudentList)) { return true; } if (IsClassroomConflictForAuto(examinationRoom, examinationDate, scheduledStudentList)) { return true; } if (IsStudentConflictForAuto(needScheduleStudentIDList, scheduledStudentList, examinationDate)) { return true; } return false; } private bool IsClassroomConflict(ExamiationPlanAutoScheduleView examinationPlanView, TimeSegmentView examinationDate, IList scheduledPlanList) { var conflictRoomList = (from room in examinationPlanView.ExaminationPlan.EX_ExaminationRoomLayout join layout in scheduledPlanList.Where(x => x.ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate && x.ExaminationPlan.StartTime <= examinationDate.EndTime && x.ExaminationPlan.EndTime >= examinationDate.StartTime && x.ExaminationPlan.ExaminationPlanID != examinationPlanView.ExaminationPlan.ExaminationPlanID) .SelectMany(x => x.ExaminationPlan.EX_ExaminationRoomLayout) on room.ClassroomID equals layout.ClassroomID group new { ExaminationRoom = room, Layout = layout } by new { room.ClassroomID, room.CF_Classroom.Examinationseating } into g where (g.Key.Examinationseating ?? 0) < (g.Sum(x => (x.Layout == null) ? 0 : x.Layout.EX_ExaminationRoomStudent.Count()) + g.Sum(x => x.ExaminationRoom.EX_ExaminationRoomStudent.Count())) select g.Select(x => x.ExaminationRoom).FirstOrDefault()).ToList(); if (conflictRoomList.Count > 0) { return true; } return false; } private bool IsConflictForSetTime(ExamiationPlanAutoScheduleView examinationPlanView, IList needScheduleStudentIDList, TimeSegmentView examinationDate, CF_Classroom examinationRoom, IList scheduledPlanList) { if (IsExaminationStyleConflict(examinationPlanView, examinationRoom, examinationDate)) { return true; } if (IsClassroomConflict(examinationPlanView, examinationDate, scheduledPlanList)) { return true; } if (IsStudentConflict(needScheduleStudentIDList, scheduledPlanList, examinationDate)) { return true; } return false; } protected List GetAvailableExaminationTeacher(IList examinationTeacherList, DateTime examinationDate, TimeSpan startTime, TimeSpan endTime, IList scheduledStudentList) { var q = (from teacher in examinationTeacherList from plan in scheduledStudentList.Where(x => x.ExaminationDate == examinationDate && x.StartTime <= endTime && x.EndTime >= startTime && x.TeacherIDList.Contains(teacher.CF_Staff.UserID)) .DefaultIfEmpty() where plan == null select teacher.CF_Staff).OrderBy(x => Guid.NewGuid()).ToList(); return q.Take(2).ToList(); } protected virtual List GetMaxSeatExaminationRoomList(IList examinationRoomList, DateTime examinationDate, TimeSpan startTime, TimeSpan endTime, IList scheduledStudentList) { var examinationRoom = (from room in examinationRoomList join layout in scheduledStudentList.Where(x => x.ExaminationDate == examinationDate && x.StartTime <= endTime && x.EndTime >= startTime) .GroupBy(x => x.ClassroomID) on room.ClassroomID equals layout.Key into dlayout from elayout in dlayout.DefaultIfEmpty() where (room.CF_Classroom.Examinationseating ?? 0) > (elayout == null ? 0 : elayout.Count()) orderby ((room.CF_Classroom.Examinationseating ?? 0) - (elayout == null ? 0 : elayout.Count())) descending select room); return examinationRoom.ToList(); } private List GetOrderedExaminationPlanViewList(IList source, IList scheduledPlanList, DateTime examinationDate) { //优先级顺序:有学生当天排过考的优先(OrderID)、需要排课的学生数大的优先(NeedScheduleCount)、同一课程优先级相同(CoursematerialID) var q = (from plan in source where plan.MissionStudentCount > plan.StudentCount select new { ExaminationPlan = plan, OrderID = (scheduledPlanList.Any(x => x.ExaminationPlan.ExaminationDate == examinationDate && x.ExaminationPlan.EX_ExaminationRoomLayout.SelectMany(w => w.EX_ExaminationRoomStudent).Select(w => w.UserID) .Intersect(plan.ExaminationPlan.CF_Student.Select(w => (Guid?)w.UserID)).Count() > 0) || source.Any(x => x.ExaminationPlan.ExaminationDate == examinationDate && x.ExaminationPlan.EX_ExaminationRoomLayout.SelectMany(w => w.EX_ExaminationRoomStudent).Select(w => w.UserID) .Intersect(plan.ExaminationPlan.CF_Student.Select(w => (Guid?)w.UserID)).Count() > 0) ) ? 0 : 1, NeedScheduleCount = plan.MissionStudentCount - plan.StudentCount }).OrderBy(x => x.ExaminationPlan.ExaminationPlan.CoursematerialID).ThenBy(x => x.OrderID).ThenByDescending(x => x.NeedScheduleCount) .Select(x => x.ExaminationPlan).ToList(); return q; } public void SetTime(IList examinationPlanIDList, DateTime? examinationDate, TimeSpan? startTime, TimeSpan? endTime) { if (!examinationDate.HasValue) { throw new Exception("必须指定考试日期。"); } if (!startTime.HasValue) { throw new Exception("必须指定开始时间。"); } if (!endTime.HasValue) { throw new Exception("必须指定结束时间。"); } if (examinationDate.Value.Date < DateTime.Today) { throw new Exception("考试日期需在当天之后。"); } if (startTime.Value >= endTime.Value) { throw new Exception("结束时间需在开始时间之后。"); } var errorMessageList = new List(); var curSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); if (examinationDate.HasValue && startTime.HasValue && endTime.HasValue) { var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var examinationPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => examinationPlanIDList.Contains(x.ExaminationPlanID)) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).ToList(); var submitedPlanClassNameList = examinationPlanViewList.Where(x => x.ExaminationPlan.RecordStatus != (int)EX_ExaminationPlanStatus.NotSubmitted).ToList(); if (submitedPlanClassNameList.Count > 0) { throw new Exception(string.Join("、", submitedPlanClassNameList.Select(x => x.ExaminationPlan.ClassName)) + "已经提交,不能设置时间。"); } var scheduledPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationDate == examinationDate && x.StartTime <= endTime && x.EndTime >= startTime && x.EX_ExaminationRoomLayout.Count() > 0 && !examinationPlanIDList.Contains(x.ExaminationPlanID)) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList() .Select(x => new ExamiationPlanAutoScheduleView { ExaminationPlan = x, MissionStudentCount = (x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) != null) ? x.CF_Student.Count(w => inschoolStatusList.Contains(w.InSchoolStatusID)) : 0, StudentCount = (x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) != null) ? x.EX_ExaminationRoomLayout.Sum(w => w.EX_ExaminationRoomStudent.Count()) : 0 }).ToList(); var timeSegment = new TimeSegmentView { ExaminationDate = examinationDate.Value, StartTime = startTime.Value, EndTime = endTime.Value }; var classroomIDList = examinationPlanViewList.SelectMany(x => x.ExaminationPlan.EX_ExaminationRoomLayout).Select(x => x.ClassroomID).ToList(); var classroomList = ClassroomDAL.ClassroomRepository.GetList(x => classroomIDList.Contains(x.ClassroomID)).ToList(); //获取排课 var scheduleDateTimeList = new List { new StartEndTimeView { StartTime = examinationDate.Value.Add(startTime.Value), EndTime = examinationDate.Value.Add(endTime.Value) } }; var schedulingList = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var classroomExcessiveUseList = this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); foreach (var examinationPlanView in examinationPlanViewList) { foreach (var layout in examinationPlanView.ExaminationPlan.EX_ExaminationRoomLayout) { var studentIDList = layout.EX_ExaminationRoomStudent .Select(x => x.UserID).ToList(); var classroomEntity = classroomList.FirstOrDefault(x => x.ClassroomID == layout.ClassroomID); var staffIDList = layout.CF_Staff.Select(x => x.UserID).ToList(); var existPlanViewList = scheduledPlanViewList .Concat(examinationPlanViewList .Where(x => x.ExaminationPlan.ExaminationPlanID != examinationPlanView.ExaminationPlan.ExaminationPlanID)) .ToList(); if (existPlanViewList.Any(x => x.ExaminationPlan.ExaminationStyleID != examinationPlanView.ExaminationPlan.ExaminationStyleID && x.ExaminationPlan.EX_ExaminationRoomLayout.Any(w => w.ClassroomID == layout.ClassroomID))) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考场" + classroomEntity.Name + "时间安排存在冲突,存在同一时间不同考试方式的排考,请重新核对后再进行设置。"); } if ((classroomEntity.Examinationseating - existPlanViewList.Sum(x => x.ExaminationPlan.EX_ExaminationRoomLayout.Where(w => w.ClassroomID == layout.ClassroomID).Sum(w => w.EX_ExaminationRoomStudent.Count))) < studentIDList.Count) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考场" + classroomEntity.Name + "时间安排存在冲突,考场无法容纳该考试任务的学生,请重新核对后再进行设置。"); } if (existPlanViewList.Any(x => x.ExaminationPlan.EX_ExaminationRoomLayout.SelectMany(w => w.EX_ExaminationRoomStudent) .Any(w => studentIDList.Contains(w.UserID)))) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考场" + classroomEntity.Name + "时间安排存在冲突,部分考生时间安排冲突,请重新核对后再进行设置。"); } var classroomScheduledTeacher = existPlanViewList.FirstOrDefault(x => x.ExaminationPlan.EX_ExaminationRoomLayout.Any(w => w.ClassroomID == layout.ClassroomID && w.CF_Staff.Count != w.CF_Staff.Where(y => staffIDList.Contains(y.UserID)).Count())); if (classroomScheduledTeacher != null) { var scheduledTeacher = string.Join("、", classroomScheduledTeacher.ExaminationPlan.EX_ExaminationRoomLayout.First(). CF_Staff.Select(x => x.Sys_User.Name).ToList()); errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考场" + classroomEntity.Name + "已由" + scheduledTeacher + "监考,请重新核对后再进行设置。"); } if (existPlanViewList.Any(x => x.ExaminationPlan.EX_ExaminationRoomLayout.Where(w => w.ClassroomID != layout.ClassroomID) .SelectMany(w => w.CF_Staff).Any(w => staffIDList.Contains(w.UserID)))) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的监考老师时间安排冲突,请重新核对后再进行设置。"); } var schedulingStudentConflictList = (from student in studentIDList from scheduling in schedulingList.Where(x => x.Date == examinationPlanView.ExaminationPlan.ExaminationDate && x.StartTime <= examinationPlanView.ExaminationPlan.EndTime && x.EndTime >= examinationPlanView.ExaminationPlan.StartTime) from schedulingStudent in scheduling.StudentList.Where(x => x.UserID == student) group student by student into g select g.FirstOrDefault() ).ToList(); if (schedulingStudentConflictList.Count > 0) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考试时间安排冲突,部分学生由于需要上课无法安排,请重新核对后再进行设置。"); } var schedulingClassroomConflictList = (from scheduling in schedulingList.Where(x => x.ClassroomID == classroomEntity.ClassroomID) select scheduling ).ToList(); if (schedulingClassroomConflictList.Count > 0) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考场" + classroomEntity.Name + "时间安排存在冲突,该考场被排课占用,请重新核对后再进行设置。"); } var excessiveUseConflictList = (from excessiveUse in classroomExcessiveUseList.Where(x => x.ClassroomID == classroomEntity.ClassroomID) select excessiveUse).ToList(); if (excessiveUseConflictList.Count > 0) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考场" + classroomEntity.Name + "时间安排存在冲突,该考场被其他活动占用,请重新核对后再进行设置。"); } var schedulingTeacherConflictList = (from teacher in staffIDList from scheduling in schedulingList from schedulingTeacher in scheduling.TeacherList.Where(x => x.UserID == teacher) group teacher by teacher into g select g.FirstOrDefault() ).ToList(); if (schedulingClassroomConflictList.Count > 0) { errorMessageList.Add("考试计划:" + examinationPlanView.ExaminationPlan.ClassName + "的考试时间安排冲突,部分老师由于需要上课无法安排,请重新核对后再进行设置。"); } } } } if (errorMessageList.Count > 0) { throw new Exception(string.Join(";", errorMessageList)); } this.UnitOfWork.Update((x => new EX_ExaminationPlan { ExaminationDate = examinationDate, StartTime = startTime, EndTime = endTime }), (x => examinationPlanIDList.Contains(x.ExaminationPlanID))); } public List GetAdultExaminationPlanExportViewList(ConfiguretView examinationPlanView, IList examinationPlanIDList, Guid? schoolyearID, Guid? collegeID, int? gradeYearID, int? standardID, Guid? coursematerialID, int? examsCategoryID, int? examinationModeID, int? examinationStyleID, int? starWeek, int? endWeek, int? courseEndWeek, int? statusID) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (examinationPlanIDList.Count > 0) { exp = exp.And(x => examinationPlanIDList.Contains(x.ExaminationPlanID)); } else { if (schoolyearID.HasValue) { exp = exp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { exp = exp.And(x => x.CollegeID == collegeID); } if (gradeYearID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.GradeID == gradeYearID) .Select(s => s.CF_Classmajor.CF_Grademajor.GradeID).FirstOrDefault() == gradeYearID); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.Where(w => w.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID) .Select(s => s.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID).FirstOrDefault() == standardID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.CoursematerialID == coursematerialID); } if (examsCategoryID.HasValue) { exp = exp.And(x => x.ExamsCategoryID == examsCategoryID); } if (examinationModeID.HasValue) { exp = exp.And(x => x.ExaminationModeID == examinationModeID); } if (examinationStyleID.HasValue) { exp = exp.And(x => x.ExaminationStyleID == examinationStyleID); } if (starWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum >= starWeek)); } if (endWeek.HasValue) { exp = exp.And(x => x.EM_EducationMission.EM_EducationMissionExamWeekNum.Any(w => w.WeeklyNum <= endWeek)); } if (statusID.HasValue) { exp = exp.And(x => x.RecordStatus == statusID); } } var q = ExaminationPlanDAL.GetAdultExaminationPlanExportView(exp); if (examinationPlanIDList.Count == 0) { if (!string.IsNullOrEmpty(examinationPlanView.ConditionValue) && !string.IsNullOrEmpty(examinationPlanView.Attribute)) q = q.DynamicWhere(examinationPlanView.Attribute, examinationPlanView.Condition, examinationPlanView.ConditionValue); } return q.OrderByDescending(x => x.SchoolyearCode).OrderBy(x => x.MaxExaminationWeekNum).ThenBy(x => x.ClassName).ToList(); } public virtual 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 (examinationPlanList.Where(x => x.ClassName == importRow.ClassName // && coursematerialIDList.Contains(x.CoursematerialID) && x.ExamsCategoryID == examsCategory.Value // && x.ExaminationModeID == examinationMode.Value ).Count() >= 2) //{ // errorMessage += "存在考试科目、考试班级名称、考试性质、考试方式相同的两条或以上的数据。"; //} 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); } } #endregion var checkList = successList; #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 sameDateExaminationPlanList = 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)).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 <= 0) { 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 IGridResultSet GetExaminationPlanStudentList(Guid examinationPlanID) { return ExaminationPlanDAL.GetExaminationPlanStudentList(examinationPlanID).OrderBy(x => x.LoginID).ToGridResultSet(); } /// /// 新增考试安排 /// /// public void AddExaminationPlan(ExaminationPlanView examinationPlanView) { try { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curSchoolyear = SchoolYearServices.Value.GetCurrentSchoolYear(); EX_ExaminationPlan examinationPlan = new EX_ExaminationPlan(); examinationPlan.ExaminationPlanID = Guid.NewGuid(); examinationPlan.SchoolyearID = examinationPlanView.SchoolyearID; examinationPlan.CollegeID = examinationPlanView.CollegeID; examinationPlan.CoursematerialID = examinationPlanView.CoursematerialID; examinationPlan.ExaminationModeID = examinationPlanView.ExaminationModeID; examinationPlan.ExaminationStyleID = examinationPlanView.ExaminationStyleID; examinationPlan.ExamsCategoryID = examinationPlanView.ExamsCategoryID; examinationPlan.ClassName = examinationPlanView.ClassName; examinationPlan.ExaminationDate = examinationPlanView.ExaminationDate; examinationPlan.StartTime = examinationPlanView.StartTime; examinationPlan.EndTime = examinationPlanView.EndTime; //examinationPlan.CF_Student = new HashSet(graduateExamPlan.Students); SetNewStatus(examinationPlan); examinationPlan.RecordStatus = (int)CF_ApprovalStatus.NotSubmitted; UnitOfWork.Add(examinationPlan); UnitOfWork.Commit(); //this.UnitOfWork.BulkInsert(examinationPlan, (x => x.CF_Student)); } catch (Exception) { throw; } //return null; } public ExaminationPlanView GetExaminationPlanView(Guid? examinationPlanID) { ExaminationPlanView examinationPlanView = null; if (examinationPlanID.HasValue) { examinationPlanView = ExaminationPlanDAL.GetExaminationPlanView(x => x.ExaminationPlanID == examinationPlanID).ToList().FirstOrDefault(); } return examinationPlanView; } /// /// 考试安排增加学生 /// /// /// public void ExaminationPlanStudentAdd(Guid? examinationPlanID, List listBaseStudentView) { try { } catch (Exception) { throw; } } /// /// 查询考试安排实体 /// /// /// public EX_ExaminationPlan GetExaminationPlan(Guid? examinationPlanID) { return ExaminationPlanDAL.ExaminationPlanRepository.GetSingle(x => x.ExaminationPlanID == examinationPlanID, (x => x.CF_Student), (x => x.EX_ExaminationRoomLayout)); } /// /// 重置考试安排 /// /// public void ResettingExaminationPlans(List examinationPlanIDs) { try { if (examinationPlanIDs != null) { DeleteExaminationClassroomLayoutByPlanID(examinationPlanIDs); List ExaminationPlans = ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => examinationPlanIDs.Contains(x.ExaminationPlanID)).ToList(); foreach (EX_ExaminationPlan examinationPlan in ExaminationPlans) { if (examinationPlan.RecordStatus == (int)CF_ApprovalStatus.Submitted) { throw new Exception("选中数据包括已提交数据,不能进行重置"); } examinationPlan.ExaminationDate = null; examinationPlan.StartTime = null; examinationPlan.EndTime = null; UnitOfWork.Update(examinationPlan); } UnitOfWork.Commit(); } } catch (Exception ex) { throw ex; } } public void DeleteExaminationClassroomLayoutByPlanID(List examinationPlanIDs) { var examinationLayoutList = this.ExaminationPlanDAL.ExaminationRoomLayoutRepository .GetList(x => examinationPlanIDs.Contains(x.ExaminationPlanID.Value), (x => x.EX_ExaminationRoomStudent), (x => x.CF_Staff)).ToList(); foreach (var examinationLayout in examinationLayoutList) { examinationLayout.CF_Staff = new HashSet(); examinationLayout.EX_ExaminationRoomStudent.ToList().ForEach(x => this.UnitOfWork.Remove(x)); this.UnitOfWork.Remove(examinationLayout); } this.UnitOfWork.Commit(); } /// /// 撤销考试安排 /// /// public void CancelExaminationPlans(List examinationPlanIDs) { try { if (examinationPlanIDs != null) { //DeleteExaminationClassroomLayout(examinationPlanIDs); List ExaminationPlans = ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => examinationPlanIDs.Contains(x.ExaminationPlanID), (x => x.EX_ExaminationRoomLayout)).ToList(); foreach (EX_ExaminationPlan examinationPlan in ExaminationPlans) { examinationPlan.RecordStatus = (int)CF_ApprovalStatus.NotSubmitted; UnitOfWork.Update(examinationPlan); } UnitOfWork.Commit(); } } catch (Exception ex) { throw ex; } } public void CheckStudentSchedule(Guid examinationPlanID, IList studentUserIDList) { var examinationPlan = ExaminationPlanDAL.ExaminationPlanRepository.GetSingle(x => x.ExaminationPlanID == examinationPlanID); var scheduleDateTimeList = new List { new StartEndTimeView { StartTime = examinationPlan.ExaminationDate.Value.Add(examinationPlan.StartTime.Value) , EndTime = examinationPlan.ExaminationDate.Value.Add(examinationPlan.EndTime.Value) } }; var schedulingList = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(examinationPlan.SchoolyearID.Value, scheduleDateTimeList); var scheduledUserIDList = schedulingList.SelectMany(x => x.StudentList) .Where(x => studentUserIDList.Contains(x.UserID)) .GroupBy(x => x.UserID) .Select(x => x.Key).ToList(); if (scheduledUserIDList.Count > 0) { var userList = UserRepository.GetList(x => scheduledUserIDList.Contains(x.UserID)).ToList(); throw new Exception(string.Format("学生{0}在该时段有课,不能参加考试,请检查考试时间设置。", string.Join("、", userList.Select(x => x.Name)))); } } public void SaveGdss(ExaminationPlanGdssView examinationPlanView, IList staffIDList, IList studentIDList) { if (!examinationPlanView.ExaminationDate.HasValue) { throw new Exception("必须指定考试日期。"); } if (!examinationPlanView.StartTime.HasValue) { throw new Exception("必须指定开始时间。"); } if (!examinationPlanView.EndTime.HasValue) { throw new Exception("必须指定结束时间。"); } if (examinationPlanView.ExaminationDate.Value.Date < DateTime.Today) { throw new Exception("考试日期需在当天之后。"); } if (examinationPlanView.StartTime.Value >= examinationPlanView.EndTime.Value) { throw new Exception("结束时间需在开始时间之后。"); } var curSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var examinationPlanEntity = ExaminationPlanDAL.ExaminationPlanRepository.GetSingle(x => x.ExaminationPlanID == examinationPlanView.ExaminationPlanID, (x => x.CF_Student), (x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)), (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student)))); if (examinationPlanEntity == null) { throw new Exception("指定的考试计划不存在。"); } else { this.SetModifyStatus(examinationPlanEntity); } examinationPlanEntity.ExaminationDate = examinationPlanView.ExaminationDate; examinationPlanEntity.StartTime = examinationPlanView.StartTime; examinationPlanEntity.EndTime = examinationPlanView.EndTime; var scheduledPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationDate == examinationPlanView.ExaminationDate && x.StartTime <= examinationPlanView.EndTime && x.EndTime >= examinationPlanView.StartTime && x.EX_ExaminationRoomLayout.Count() > 0 && x.ExaminationPlanID != examinationPlanView.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff.Select(e => e.Sys_User))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList(); var classroomEntity = ExaminationPlanDAL.ClassroomRepository.GetSingle(x => x.ClassroomID == examinationPlanView.ClassroomID, (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationPlan))); if (scheduledPlanViewList.Any(x => x.ExaminationStyleID != examinationPlanEntity.ExaminationStyleID && x.EX_ExaminationRoomLayout.Any(w => w.ClassroomID == examinationPlanView.ClassroomID))) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,存在同一时间不同考试方式的排考,请重新核对后再进行设置。"); } if ((classroomEntity.Examinationseating - scheduledPlanViewList.Sum(x => x.EX_ExaminationRoomLayout.Where(w => w.ClassroomID == examinationPlanView.ClassroomID).Sum(w => w.EX_ExaminationRoomStudent.Count))) < studentIDList.Count) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,考场无法容纳该考试任务的学生,请重新核对后再进行设置。"); } if (scheduledPlanViewList.Any(x => x.EX_ExaminationRoomLayout.SelectMany(w => w.EX_ExaminationRoomStudent) .Any(w => studentIDList.Contains(w.UserID)))) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,部分考生时间安排冲突,请重新核对后再进行设置。"); } var classroomScheduledTeacher = scheduledPlanViewList.FirstOrDefault(x => x.EX_ExaminationRoomLayout.Any(w => w.ClassroomID == examinationPlanView.ClassroomID && w.CF_Staff.Count != w.CF_Staff.Where(y => staffIDList.Contains(y.UserID)).Count())); if (classroomScheduledTeacher != null) { var scheduledTeacher = string.Join("、", classroomScheduledTeacher.EX_ExaminationRoomLayout.First().CF_Staff.Select(x => x.Sys_User.Name).ToList()); throw new Exception("考场" + classroomEntity.Name + "已由" + scheduledTeacher + "监考,请重新核对后再进行设置。"); } if (scheduledPlanViewList.Any(x => x.EX_ExaminationRoomLayout.Where(w => w.ClassroomID != examinationPlanView.ClassroomID) .SelectMany(w => w.CF_Staff).Any(w => staffIDList.Contains(w.UserID)))) { throw new Exception("监考老师时间安排冲突,请重新核对后再进行设置。"); } //获取排课 var scheduleDateTimeList = new List { new StartEndTimeView { StartTime = examinationPlanEntity.ExaminationDate.Value.Add(examinationPlanEntity.StartTime.Value), EndTime = examinationPlanEntity.ExaminationDate.Value.Add(examinationPlanEntity.EndTime.Value) } }; var schedulingList = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var classroomExcessiveUseList = this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var schedulingStudentConflictList = (from student in studentIDList from scheduling in schedulingList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime) from schedulingStudent in scheduling.StudentList.Where(x => x.UserID == student) group student by student into g select g.FirstOrDefault() ).ToList(); if (schedulingStudentConflictList.Count > 0) { throw new Exception("考试时间安排冲突,部分学生由于需要上课无法安排,请重新核对后再进行设置。"); } var schedulingClassroomConflictList = (from scheduling in schedulingList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime && x.ClassroomID == classroomEntity.ClassroomID) select scheduling ).ToList(); if (schedulingClassroomConflictList.Count > 0) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,该考场被排课占用,请重新核对后再进行设置。"); } var excessiveUseConflictList = (from excessiveUse in classroomExcessiveUseList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime && x.ClassroomID == classroomEntity.ClassroomID) select excessiveUse).ToList(); if (excessiveUseConflictList.Count > 0) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,该考场被其他活动占用,请重新核对后再进行设置。"); } var schedulingTeacherConflictList = (from teacher in staffIDList from scheduling in schedulingList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime) from schedulingTeacher in scheduling.TeacherList.Where(x => x.UserID == teacher) group teacher by teacher into g select g.FirstOrDefault() ).ToList(); if (schedulingClassroomConflictList.Count > 0) { throw new Exception("考试时间安排冲突,部分老师由于需要上课无法安排,请重新核对后再进行设置。"); } examinationPlanEntity.EX_ExaminationRoomLayout.ForEach(x => { x.CF_Staff = new HashSet(); UnitOfWork.RemoveRange(x.EX_ExaminationRoomStudent); }); UnitOfWork.RemoveRange(examinationPlanEntity.EX_ExaminationRoomLayout); examinationPlanEntity.EX_ExaminationRoomLayout = new HashSet(); var examinationRoomLayoutEntity = new EX_ExaminationRoomLayout(); examinationRoomLayoutEntity.ExaminationRoomLayoutID = Guid.NewGuid(); examinationRoomLayoutEntity.ExaminationPlanID = examinationPlanEntity.ExaminationPlanID; examinationRoomLayoutEntity.ClassroomID = classroomEntity.ClassroomID; examinationRoomLayoutEntity.RowSpacing = 0; examinationRoomLayoutEntity.ColumnSpacing = 0; examinationRoomLayoutEntity.StudentOrderType = examinationPlanView.StudentOrderType; var staffList = this.StaffDAL.staffRepository.GetList(x => staffIDList.Contains(x.UserID)).ToList(); examinationRoomLayoutEntity.CF_Staff = new HashSet(staffList); this.SetNewStatus(examinationRoomLayoutEntity); this.UnitOfWork.Add(examinationRoomLayoutEntity); var classroom = ClassroomDAL.GetClassroomViewQueryable(x => x.ClassroomID == classroomEntity.ClassroomID).FirstOrDefault(); var studentList = StudentsDAL.StudentRepository.GetList(x => studentIDList.Contains(x.UserID), (x => x.Sys_User)).ToList(); var roomStudentList = this.SetRoomStudentSeats(studentList.Select(x => new ExaminationRoomStudentView { LoginID = x.Sys_User.LoginID, UserID = x.UserID, ExaminationRoomLayoutID = examinationRoomLayoutEntity.ExaminationRoomLayoutID }).ToList(), classroom, 0, 0); roomStudentList.ForEach(x => { x.ExaminationRoomLayoutID = examinationRoomLayoutEntity.ExaminationRoomLayoutID; this.SetNewStatus(x); this.UnitOfWork.Add(x); }); //考试计划对应的学生同步修改 examinationPlanEntity.CF_Student = new HashSet(); roomStudentList.ForEach(x => { CF_Student stu = studentList.Where(y => y.UserID == x.UserID).FirstOrDefault(); examinationPlanEntity.CF_Student.Add(stu); }); UnitOfWork.Commit(); } public List CheckBeforeGdssSave(ExaminationPlanGdssView examinationPlanView, IList staffIDList, IList studentIDList) { //如果是补考学生和排课存在冲突的话提醒是否强行排考 var curSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); List studentViewList = new List(); var examinationPlanEntity = ExaminationPlanDAL.ExaminationPlanRepository.GetSingle(x => x.ExaminationPlanID == examinationPlanView.ExaminationPlanID, (x => x.CF_Student), (x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)), (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student)))); if (examinationPlanEntity.ExamsCategoryID == (int)CF_ExamsCategory.Resit || examinationPlanEntity.ExamsCategoryID == (int)CF_ExamsCategory.GraduationExam) { var scheduleDateTimeListforResit = new List { new StartEndTimeView { StartTime = examinationPlanView.ExaminationDate.Value.Add(examinationPlanView.StartTime.Value), EndTime = examinationPlanView.ExaminationDate.Value.Add(examinationPlanView.EndTime.Value) } }; var schedulingListforResit = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(curSchoolyear.SchoolYearID.Value, scheduleDateTimeListforResit); var schedulingStudentConflictListforResit = (from student in studentIDList from scheduling in schedulingListforResit.Where(x => x.Date == examinationPlanView.ExaminationDate && x.StartTime <= examinationPlanView.EndTime && x.EndTime >= examinationPlanView.StartTime) from schedulingStudent in scheduling.StudentList.Where(x => x.UserID == student) group student by student into g select g.FirstOrDefault() ).ToList(); if (schedulingStudentConflictListforResit.Count > 0) { //studentViewList = StudentsDAL.GetStudentQueryable(x => schedulingStudentConflictListforResit.Contains(x.UserID)).ToList(); var schedulingID = schedulingListforResit.Where(x => x.Date == examinationPlanView.ExaminationDate && x.StartTime <= examinationPlanView.EndTime && x.EndTime >= examinationPlanView.StartTime).Select(x => x.EducationSchedulingID).ToList(); var query = ExaminationPlanDAL.GetStudentAndCourseList(schedulingID, schedulingStudentConflictListforResit); studentViewList = query.Distinct().ToList(); //studentViewList = schedulingStudentConflictListforResit; } } return studentViewList; } /// /// 补考排考学生和排课冲突,强行排考(不考虑学生与排课的冲突) /// /// /// /// public void SaveGdssNotClashingStudent(ExaminationPlanGdssView examinationPlanView, IList staffIDList, IList studentIDList) { if (!examinationPlanView.ExaminationDate.HasValue) { throw new Exception("必须指定考试日期。"); } if (!examinationPlanView.StartTime.HasValue) { throw new Exception("必须指定开始时间。"); } if (!examinationPlanView.EndTime.HasValue) { throw new Exception("必须指定结束时间。"); } if (examinationPlanView.ExaminationDate.Value.Date < DateTime.Today) { throw new Exception("考试日期需在当天之后。"); } if (examinationPlanView.StartTime.Value >= examinationPlanView.EndTime.Value) { throw new Exception("结束时间需在开始时间之后。"); } var curSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); var inschoolStatusList = StudentsDAL.InSchoolSettingRepository.Entities.Where(x => x.IsSelected == true).Select(x => x.InSchoolStatusID).ToList(); var examinationPlanEntity = ExaminationPlanDAL.ExaminationPlanRepository.GetSingle(x => x.ExaminationPlanID == examinationPlanView.ExaminationPlanID, (x => x.CF_Student), (x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff)), (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student)))); if (examinationPlanEntity == null) { throw new Exception("指定的考试计划不存在。"); } else { this.SetModifyStatus(examinationPlanEntity); } examinationPlanEntity.ExaminationDate = examinationPlanView.ExaminationDate; examinationPlanEntity.StartTime = examinationPlanView.StartTime; examinationPlanEntity.EndTime = examinationPlanView.EndTime; var scheduledPlanViewList = this.ExaminationPlanDAL.ExaminationPlanRepository.GetList(x => x.ExaminationDate == examinationPlanView.ExaminationDate && x.StartTime <= examinationPlanView.EndTime && x.EndTime >= examinationPlanView.StartTime && x.EX_ExaminationRoomLayout.Count() > 0 && x.ExaminationPlanID != examinationPlanView.ExaminationPlanID) .Include(x => x.CF_Student) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Staff.Select(e => e.Sys_User))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.CF_Classroom.EX_ExaminationRoomLayout.Select(e => e.EX_ExaminationPlan))) .Include(x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationRoomStudent.Select(e => e.CF_Student))) .ToList(); var classroomEntity = ExaminationPlanDAL.ClassroomRepository.GetSingle(x => x.ClassroomID == examinationPlanView.ClassroomID, (x => x.EX_ExaminationRoomLayout.Select(w => w.EX_ExaminationPlan))); if (scheduledPlanViewList.Any(x => x.ExaminationStyleID != examinationPlanEntity.ExaminationStyleID && x.EX_ExaminationRoomLayout.Any(w => w.ClassroomID == examinationPlanView.ClassroomID))) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,存在同一时间不同考试方式的排考,请重新核对后再进行设置。"); } if ((classroomEntity.Examinationseating - scheduledPlanViewList.Sum(x => x.EX_ExaminationRoomLayout.Where(w => w.ClassroomID == examinationPlanView.ClassroomID).Sum(w => w.EX_ExaminationRoomStudent.Count))) < studentIDList.Count) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,考场无法容纳该考试任务的学生,请重新核对后再进行设置。"); } if (scheduledPlanViewList.Any(x => x.EX_ExaminationRoomLayout.SelectMany(w => w.EX_ExaminationRoomStudent) .Any(w => studentIDList.Contains(w.UserID)))) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,部分考生时间安排冲突,请重新核对后再进行设置。"); } var classroomScheduledTeacher = scheduledPlanViewList.FirstOrDefault(x => x.EX_ExaminationRoomLayout.Any(w => w.ClassroomID == examinationPlanView.ClassroomID && w.CF_Staff.Count != w.CF_Staff.Where(y => staffIDList.Contains(y.UserID)).Count())); if (classroomScheduledTeacher != null) { var scheduledTeacher = string.Join("、", classroomScheduledTeacher.EX_ExaminationRoomLayout.First().CF_Staff.Select(x => x.Sys_User.Name).ToList()); throw new Exception("考场" + classroomEntity.Name + "已由" + scheduledTeacher + "监考,请重新核对后再进行设置。"); } if (scheduledPlanViewList.Any(x => x.EX_ExaminationRoomLayout.Where(w => w.ClassroomID != examinationPlanView.ClassroomID) .SelectMany(w => w.CF_Staff).Any(w => staffIDList.Contains(w.UserID)))) { throw new Exception("监考老师时间安排冲突,请重新核对后再进行设置。"); } //获取排课 var scheduleDateTimeList = new List { new StartEndTimeView { StartTime = examinationPlanEntity.ExaminationDate.Value.Add(examinationPlanEntity.StartTime.Value), EndTime = examinationPlanEntity.ExaminationDate.Value.Add(examinationPlanEntity.EndTime.Value) } }; var schedulingList = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var classroomExcessiveUseList = this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( curSchoolyear.SchoolYearID.Value, scheduleDateTimeList); var schedulingClassroomConflictList = (from scheduling in schedulingList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime && x.ClassroomID == classroomEntity.ClassroomID) select scheduling ).ToList(); if (schedulingClassroomConflictList.Count > 0) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,该考场被排课占用,请重新核对后再进行设置。"); } var excessiveUseConflictList = (from excessiveUse in classroomExcessiveUseList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime && x.ClassroomID == classroomEntity.ClassroomID) select excessiveUse).ToList(); if (excessiveUseConflictList.Count > 0) { throw new Exception("考场" + classroomEntity.Name + "时间安排存在冲突,该考场被其他活动占用,请重新核对后再进行设置。"); } var schedulingTeacherConflictList = (from teacher in staffIDList from scheduling in schedulingList.Where(x => x.Date == examinationPlanEntity.ExaminationDate && x.StartTime <= examinationPlanEntity.EndTime && x.EndTime >= examinationPlanEntity.StartTime) from schedulingTeacher in scheduling.TeacherList.Where(x => x.UserID == teacher) group teacher by teacher into g select g.FirstOrDefault() ).ToList(); if (schedulingClassroomConflictList.Count > 0) { throw new Exception("考试时间安排冲突,部分老师由于需要上课无法安排,请重新核对后再进行设置。"); } examinationPlanEntity.EX_ExaminationRoomLayout.ForEach(x => { x.CF_Staff = new HashSet(); UnitOfWork.RemoveRange(x.EX_ExaminationRoomStudent); }); UnitOfWork.RemoveRange(examinationPlanEntity.EX_ExaminationRoomLayout); examinationPlanEntity.EX_ExaminationRoomLayout = new HashSet(); var examinationRoomLayoutEntity = new EX_ExaminationRoomLayout(); examinationRoomLayoutEntity.ExaminationRoomLayoutID = Guid.NewGuid(); examinationRoomLayoutEntity.ExaminationPlanID = examinationPlanEntity.ExaminationPlanID; examinationRoomLayoutEntity.ClassroomID = classroomEntity.ClassroomID; examinationRoomLayoutEntity.RowSpacing = 0; examinationRoomLayoutEntity.ColumnSpacing = 0; examinationRoomLayoutEntity.StudentOrderType = examinationPlanView.StudentOrderType; var staffList = this.StaffDAL.staffRepository.GetList(x => staffIDList.Contains(x.UserID)).ToList(); examinationRoomLayoutEntity.CF_Staff = new HashSet(staffList); this.SetNewStatus(examinationRoomLayoutEntity); this.UnitOfWork.Add(examinationRoomLayoutEntity); var classroom = ClassroomDAL.GetClassroomViewQueryable(x => x.ClassroomID == classroomEntity.ClassroomID).FirstOrDefault(); var studentList = StudentsDAL.StudentRepository.GetList(x => studentIDList.Contains(x.UserID), (x => x.Sys_User)).ToList(); var roomStudentList = this.SetRoomStudentSeats(studentList.Select(x => new ExaminationRoomStudentView { LoginID = x.Sys_User.LoginID, UserID = x.UserID, ExaminationRoomLayoutID = examinationRoomLayoutEntity.ExaminationRoomLayoutID }).ToList(), classroom, 0, 0); roomStudentList.ForEach(x => { x.ExaminationRoomLayoutID = examinationRoomLayoutEntity.ExaminationRoomLayoutID; this.SetNewStatus(x); this.UnitOfWork.Add(x); }); //考试计划对应的学生同步修改 examinationPlanEntity.CF_Student = new HashSet(); roomStudentList.ForEach(x => { CF_Student stu = studentList.Where(y => y.UserID == x.UserID).FirstOrDefault(); examinationPlanEntity.CF_Student.Add(stu); }); UnitOfWork.Commit(); } public IGridResultSet GetExaminationTeacherViewInfoGdss(Guid? examinationPlanID) { return ExaminationPlanDAL.GetExaminationTeacherView(x => x.ExaminationPlanID == examinationPlanID, (x => true)).ToGridResultSet(); } public ExaminationPlanGdssView GetExaminationPlanGdssViewInfo(Guid? examinationPlanID) { return ExaminationPlanDAL.GetExaminationPlanGdssView(x => x.ExaminationPlanID == examinationPlanID).FirstOrDefault(); } public IGridResultSet GetGdssStudentListForSelect(ConfiguretView configuretView, Guid? examinationPlanID, Guid? collegeID, Guid? grademajorID, Guid? classmajorID, int pageIndex, int pageSize) { Expression> exp = x => true; if(collegeID.HasValue) { exp = exp.And(x => x.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID); } if(grademajorID.HasValue) { exp = exp.And(x => x.CF_Classmajor.GrademajorID == grademajorID); } if(classmajorID.HasValue) { exp = exp.And(x => x.ClassmajorID == classmajorID); } var query = ExaminationPlanDAL.GetGdssStudentListView(examinationPlanID, exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); return query.OrderByDescending(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); //return null; } //public List GetClashingStudentView(ExaminationPlanGdssView examinationPlanView, IList studentIDList) //{ // var curSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); // var scheduleDateTimeList = new List { // new StartEndTimeView { StartTime = examinationPlanView.ExaminationDate.Value.Add(examinationPlanView.StartTime.Value), // EndTime = examinationPlanView.ExaminationDate.Value.Add(examinationPlanView.EndTime.Value) } }; // var schedulingList = this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewByDateTimeList(curSchoolyear.SchoolYearID.Value, // scheduleDateTimeList); // var classroomExcessiveUseList = this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList( // curSchoolyear.SchoolYearID.Value, // scheduleDateTimeList); // var schedulingStudentConflictList = (from student in studentIDList // from scheduling in schedulingList.Where(x => x.Date == examinationPlanView.ExaminationDate // && x.StartTime <= examinationPlanView.EndTime && x.EndTime >= examinationPlanView.StartTime) // from schedulingStudent in scheduling.StudentList.Where(x => x.UserID == student) // group student by student into g // select g.FirstOrDefault() // ).ToList(); // var studentViewList = StudentsDAL.GetStudentQueryable(x => schedulingStudentConflictList.Contains(x.UserID)).ToList(); // return studentViewList; //} } public class StudentClassroomView { public DateTime? ExaminationDate { get; set; } public TimeSpan? StartTime { get; set; } public TimeSpan? EndTime { get; set; } public Guid? ClassroomID { get; set; } public Guid ExaminationPlanID { get; set; } public Guid? UserID { get; set; } public List TeacherIDList { get; set; } public int? ExaminationStyleID { get; set; } } public class AutoScheduleModifyView { public EX_ExaminationPlan ExaminationPlan { get; set; } public HashSet CF_Staff { get; set; } public DateTime? ExaminationDate { get; set; } public TimeSpan? StartTime { get; set; } public TimeSpan? EndTime { get; set; } public EX_ExaminationRoomSetting ExaminationRoom { get; set; } public List ScheduleStudentList { get; set; } } }