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.Common.CalendarManage; using EMIS.CommonLogic.CalendarManage; using EMIS.DataLogic.EducationSchedule; using Bowin.Common.Log; using EMIS.ViewModel.CalendarManage; using EMIS.ViewModel; using EMIS.ViewModel.UniversityManage.ClassroomManage; using EMIS.Entities; using EMIS.CommonLogic.EducationManage; using EMIS.ViewModel.EducationManage; using EMIS.ViewModel.EducationSchedule; using EMIS.CommonLogic.SystemServices; using EMIS.DataLogic.UniversityManage.ClassroomManage; using EMIS.DataLogic.Common.Cultureplan; using EMIS.DataLogic.EducationManage; using EMIS.DataLogic.UniversityManage.TeacherManage; using EMIS.ViewModel.UniversityManage.TeacherManage; using EMIS.Utility; using EMIS.CommonLogic.SchedulingManage.ScheduleApproval; using EMIS.CommonLogic.SchedulingManage.SchedulingSettings; using EMIS.DataLogic.SchedulingManage.SchedulingSettings; using EMIS.ViewModel.Students; using EMIS.DataLogic.Common.Students; using EMIS.ViewModel.SchedulingManage.ScheduleApproval; using EMIS.Utility.FormValidate; using EMIS.ViewModel.EducationManagement; using EMIS.DataLogic.ExaminationManage; using EMIS.ViewModel.ExaminationManage; using EMIS.ViewModel.UniversityManage.AdministrativeOrgan; namespace EMIS.CommonLogic.EducationSchedule { /// /// 排课服务(前方高能,阅读小心……) /// public class ScheduleServices : BaseServices, IScheduleServices { public CollegeScheduleDAL CollegeScheduleDAL { get; set; } public CollegePriorityDAL CollegePriorityDAL { get; set; } public ClassroomDAL ClassroomDAL { get; set; } public ClassroomExcessiveUseDAL ClassroomExcessiveUseDAL { get; set; } public StaffDAL StaffDAL { get; set; } public ClassroomScheduleDAL ClassroomScheduleDAL { get; set; } public CourseScheduleDAL CourseScheduleDAL { get; set; } public CourseTypeScheduleDAL CourseTypeScheduleDAL { get; set; } public TeacherScheduleDAL TeacherScheduleDAL { get; set; } public ArrangementDAL ArrangementDAL { get; set; } public EducationSchedulingDAL EducationSchedulingDAL { get; set; } public EducationSchedulingClassDAL EducationSchedulingClassDAL { get; set; } public EducationMissionClassDAL EducationMissionClassDAL { get; set; } public CoursematerialDAL CoursematerialDAL { get; set; } public StudentsDAL StudentsDAL { get; set; } public ExaminationPlanDAL ExaminationPlanDAL { get; set; } public Lazy CoursesTimeServices { get; set; } public Lazy SchoolYearServices { get; set; } public Lazy EducationSchedulingClassServices { get; set; } public Lazy ParameterServices { get; set; } public Lazy CollegeScheduleApprovalServices { get; set; } public Lazy EducationSchedulingStopServices { get; set; } private int? TeacherScheduleContinuousCount { get; set; } private int? ClassScheduleContinuousCount { get; set; } private int? ClassCourseScheduleContinuousCount { get; set; } private SchoolYearView SchoolYear { get; set; } private CalendarView CalendarView { get; set; } private List CollegeList { get; set; } private List ClassroomList { get; set; } private List StaffList { get; set; } private List CollegeClassroomList { get; set; } private List CourseTypePriorityList { get; set; } private List CourseTimeList { get; set; } private List ArrangementList { get; set; } private List CourseSchedulingSettingList { get; set; } private List TeacherSchedulingSettingList { get; set; } private List ClassroomSchedulingSettingList { get; set; } private List EducationSchedulingClassViewList { get; set; } private List EducationSchedulingClassStudentViewList { get; set; } private List ScheduledEducationViewList { get; set; } private List ScheduledEducationStudentViewList { get; set; } //结果列表 private List SchedulingResult { get; set; } private List SchedulingWeekNumResult { get; set; } private List SchedulingTeacherResult { get; set; } //失败列表 private List NotScheduledList { get; set; } private void InitCommonInfo() { SchedulingResult = new List(); //初始化排课结果 SchedulingWeekNumResult = new List(); SchedulingTeacherResult = new List(); NotScheduledList = new List(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取学年学期。"); SchoolYear = SchoolYearServices.Value.GetNextSchoolYear(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取课程时间。"); CourseTimeList = CoursesTimeServices.Value.GetCoursesTimeViewList(new ConfiguretView(), null); CourseTimeList = CourseTimeList.OrderBy(x => x.StartTimes).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取周工作时间表。"); ArrangementList = ArrangementDAL.GetArrangementView(x => x.IsOnWork == true).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取课程可排时间设定。"); StaffList = StaffDAL.staffRepository.GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取教师信息。"); CourseSchedulingSettingList = CourseScheduleDAL.CourseScheduleSettingRepository .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取教师可排时间设定。"); TeacherSchedulingSettingList = TeacherScheduleDAL.TeacherScheduleSettingRepository .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取课程类型优先级设置。"); CourseTypePriorityList = this.CourseTypeScheduleDAL.CourseTypePriorityRepository.Entities.ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取连续上课限制"); TeacherScheduleContinuousCount = this.ParameterServices.Value.GetParameterValue(CF_ParameterType.TeacherScheduleContinuousCount); ClassScheduleContinuousCount = this.ParameterServices.Value.GetParameterValue(CF_ParameterType.ClassScheduleContinuousCount); ClassCourseScheduleContinuousCount = this.ParameterServices.Value.GetParameterValue(CF_ParameterType.ClassCourseScheduleContinuousCount); } private void InitScheduleClassInfo() { LogHelper.WriteLog(LogType.ServiceLog, "开始获取排课" + RSL.Get("College") + "。"); CollegeList = CollegeScheduleDAL.GetCollegeViewByTime(DateTime.Now).ToList(); var collegeIDList = CollegeList.Select(x => (Guid?)x.CollegeID).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取任务班。"); EducationSchedulingClassViewList = EducationSchedulingClassServices.Value .GetEducationSchedulingClassViewList(SchoolYear.SchoolYearID.Value, collegeIDList); EducationSchedulingClassStudentViewList = EducationSchedulingClassServices.Value .GetEducationSchedulingClassStudentViewList(SchoolYear.SchoolYearID.Value, collegeIDList); LogHelper.WriteLog(LogType.ServiceLog, "开始获取本学期已排任务。"); ScheduledEducationViewList = this.GetEducationScheduleView(new ConfiguretView(), (x => x.SchoolyearID == SchoolYear.SchoolYearID), false).rows; var scheduledEducationClassIDList = ScheduledEducationViewList.Select(x => x.EducationSchedulingClassID).Distinct().ToList(); ScheduledEducationStudentViewList = EducationSchedulingClassDAL .GetEducationSchedulingClassStudentViewQueryable(x => scheduledEducationClassIDList.Contains(x.EducationSchedulingClassID)) .ToList(); EducationSchedulingClassViewList = (from escv in EducationSchedulingClassViewList join sevl in ScheduledEducationViewList on escv.EducationSchedulingClassID equals sevl.EducationSchedulingClassID into dsevl from esevl in dsevl.DefaultIfEmpty() where esevl == null select escv).ToList(); } private void InitClassroomInfo() { LogHelper.WriteLog(LogType.ServiceLog, "开始获取可排教室。"); ClassroomList = ClassroomScheduleDAL.GetAvailableClassroom().ToList(); var classroomIDList = ClassroomList.Select(x => x.ClassroomID).Distinct().ToList(); var classroomTypeList = ClassroomDAL.classroomTypeRepository.GetList(x => classroomIDList.Contains(x.ClassroomID)).ToList(); ClassroomList.ForEach(x => x.ClassroomTypeID = classroomTypeList.Where(w => w.ClassroomID == x.ClassroomID) .Select(w => w.ClassroomType ?? -1).ToList()); LogHelper.WriteLog(LogType.ServiceLog, "开始获取教室可排时间设定。"); ClassroomSchedulingSettingList = ClassroomScheduleDAL.ClassroomScheduleSettingRepository .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); LogHelper.WriteLog(LogType.ServiceLog, "开始获取" + RSL.Get("College") + "分配教室设置。"); CollegeClassroomList = ClassroomScheduleDAL.CollegeClassroomRepository.Entities.ToList(); } private void InitSchedule() { InitCommonInfo(); InitScheduleClassInfo(); InitClassroomInfo(); } /// /// 获取所有排课记录的方法 /// /// /// private IGridResultSet GetEducationScheduleView(ConfiguretView configuretView, Expression> exp, bool isNeedDataRange = true, int? pageIndex = null, int? pageSize = null) { var educationScheduleViewQueryable = this.EducationSchedulingDAL.GetEducationSchedulingViewQueryable(exp); var educationScheduleWeekNumQueryable = this.EducationSchedulingDAL.GetEducationMissionWeekNumQueryable(exp); var classMajorQueryable = this.EducationSchedulingDAL.GetClassmajorQueryable(exp); var teacherViewQueryable = this.EducationSchedulingDAL.GetTeacherViewQueryable(exp); var courseProcessQueryable = this.EducationSchedulingDAL.GetCourseProcessQueryable(exp); var courseProcessTeacherQueryable = this.EducationSchedulingDAL.GetCourseProcessTeacherViewQueryable(exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { educationScheduleViewQueryable = educationScheduleViewQueryable .DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); } if (isNeedDataRange) { educationScheduleViewQueryable = this.GetQueryByDataRangeByCollege(educationScheduleViewQueryable); } IGridResultSet result; List educationScheduleWeekNumList; List classMajorList; List teacherViewList; List courseProcessList; var startStatus = CollegeScheduleApprovalServices.Value.GetStartStatus(); var collegeScheduleApprovalViewList = new List(); if (pageIndex.HasValue && pageSize.HasValue) { result = educationScheduleViewQueryable.OrderBy(x => x.Weekday).ThenBy(x => x.CoursesTime.StartTimes) .ToGridResultSet(pageIndex, pageSize); //由于分页后数据量较少,可以考虑用Contains方法在数据库端筛选一下,不分页的情况就不要了 var educationMissionClassIDList = result.rows.Select(x => x.EducationMissionClassID).ToList(); var educationSchedulingIDList = result.rows.Select(x => (Guid?)x.EducationSchedulingID).ToList(); collegeScheduleApprovalViewList = CollegeScheduleApprovalServices.Value.GetCollegeScheduleApprovalViewList( result.rows.GroupBy(x => new { x.CourseCollegeID, x.SchoolyearID }) .Select(x => new CollegeSchoolyearView { CollegeID = x.Key.CourseCollegeID.Value, SchoolyearID = x.Key.SchoolyearID.Value }).ToList() , isNeedDataRange); teacherViewList = teacherViewQueryable.Where(x => educationSchedulingIDList.Contains(x.EducationSchedulingID)).Distinct().ToList(); classMajorList = classMajorQueryable.Where(x => educationMissionClassIDList.Contains(x.EducationMissionClassID)).Distinct().ToList(); educationScheduleWeekNumList = educationScheduleWeekNumQueryable .Where(x => educationSchedulingIDList.Contains(x.EducationSchedulingID)).Distinct().ToList(); courseProcessList = courseProcessQueryable.Where(x => educationMissionClassIDList.Contains(x.EducationMissionClassID)).Distinct().ToList(); var courseProcessTeacherViewList = courseProcessTeacherQueryable.Where(x => educationMissionClassIDList.Contains(x.EducationMissionClassID)).Distinct().ToList(); courseProcessList.ForEach(x => x.CourseProcessTeacherViewList = courseProcessTeacherViewList.Where(w => w.CourseProcessID == x.CourseProcessID) .OrderBy(w => w.TeachingMethod).ThenBy(w => w.Name).ToList()); } else { var list = educationScheduleViewQueryable.ToList(); result = new GridResultSet() { rows = list, total = list.Count }; collegeScheduleApprovalViewList = CollegeScheduleApprovalServices.Value.GetCollegeScheduleApprovalViewList( result.rows.GroupBy(x => new { x.CourseCollegeID, x.SchoolyearID }) .Select(x => new CollegeSchoolyearView { CollegeID = x.Key.CourseCollegeID.Value, SchoolyearID = x.Key.SchoolyearID.Value }).ToList() , isNeedDataRange); teacherViewList = teacherViewQueryable.Distinct().ToList(); classMajorList = classMajorQueryable.Distinct().ToList(); educationScheduleWeekNumList = educationScheduleWeekNumQueryable.Distinct().ToList(); courseProcessList = courseProcessQueryable.Distinct().ToList(); var courseProcessTeacherViewList = courseProcessTeacherQueryable.Distinct().ToList(); courseProcessList.ForEach(x => x.CourseProcessTeacherViewList = courseProcessTeacherViewList.Where(w => w.CourseProcessID == x.CourseProcessID) .OrderBy(w => w.TeachingMethod).ThenBy(w => w.Name).ToList()); } var submitedCollegeScheduleApprovalViewList = collegeScheduleApprovalViewList.Where(x => x.CollegeScheduleStatusID.HasValue).ToList(); result.rows.ForEach(x => { var collegeScheduleApprovalView = submitedCollegeScheduleApprovalViewList.FirstOrDefault(w => w.CollegeID == x.CourseCollegeID && w.SchoolyearID == x.SchoolyearID); x.TeacherTeachingViewList = teacherViewList .Where(w => x.EducationSchedulingID == w.EducationSchedulingID) .OrderBy(w => w.TeachingMethod).ThenBy(w => w.Name) .Select(w => (BaseTeacherTeachingView)w).ToList(); x.WeekNumList = educationScheduleWeekNumList.Where(w => x.EducationSchedulingID == w.EducationSchedulingID) .Select(w => w.WeekNum).ToList(); x.ClassMajorIDList = classMajorList.Where(w => x.EducationMissionClassID == w.EducationMissionClassID) .Select(w => w.ClassMajor.ClassmajorID).ToList(); x.CourseProcessViewList = courseProcessList.Where(w => x.EducationMissionClassID == w.EducationMissionClassID) .ToList(); x.IsCanEdit = x.IsCanEdit && (collegeScheduleApprovalView == null || collegeScheduleApprovalView.RecordStatus == startStatus); }); return result; } private List GetOrderedEducationSchedulingClassViewList( IList educationSchedulingClassViewList) { var result = (from escv in educationSchedulingClassViewList select new { EducationSchedulingClassView = escv, //课程类型优先级 CourseTypeOrderID = this.CourseTypePriorityList.Where(x => x.CourseTypeID == escv.CourseTypeID) .Select(x => x.Priority).FirstOrDefault(), //上课周数优先级(总上课周数大的不好排,要优先安排) WeekCountOrderID = escv.WeeklyNumList.Count, //涉及学籍班多的不好排,要先排 ClassmajorCount = (escv.ClassMajorIDList == null) ? 0 : escv.ClassMajorIDList.Count, //指定教室优先级 AssignClassroomOrderID = (escv.ClassroomID.HasValue ? 1 : 0), //涉及学生多的不好排,要先排 StudentNum = escv.StudentNum }).OrderBy(x => x.CourseTypeOrderID) .ThenBy(x => x.EducationSchedulingClassView.CoursematerialName) .ThenBy(x => x.EducationSchedulingClassView.EducationMissionClassOrderNo) //.ThenBy(x => x.EducationSchedulingClassView.EducationMissionClassName) .ThenByDescending(x => x.ClassmajorCount) .ThenByDescending(x => x.AssignClassroomOrderID) .ThenByDescending(x => x.StudentNum) .ThenByDescending(x => x.WeekCountOrderID) .Select(x => x.EducationSchedulingClassView).ToList(); return result; } private List GetCurrentClassroomViewList(int weekday, CoursesTimeView courseTime, IList schedulingWeekList) { var fullSchedulingResult = (from sr in this.SchedulingResult from swnr in sr.ES_EducationSchedulingWeekNum select new { SchedulingResult = sr, SchedulingWeekNumResult = swnr }); //两个条件:1、当前时间点可排,2:当前时间点未被使用 var result = (from r in this.ClassroomList join css in this.ClassroomSchedulingSettingList on new { r.ClassroomID, Weekday = (int?)weekday, courseTime.CoursesTimeID } equals new { css.ClassroomID, css.Weekday, css.CoursesTimeID } into dcss from ecss in dcss.DefaultIfEmpty() where (ecss == null || ecss.IsOnWork == true) //当前时间点可排 && !this.ScheduledEducationViewList.Any(x => x.Weekday == weekday && x.CoursesTimeID == courseTime.CoursesTimeID && x.WeekNumList.Any(w => schedulingWeekList.Contains(w)) && x.ClassroomID == r.ClassroomID) && !fullSchedulingResult.Any(x => x.SchedulingResult.Weekday == weekday && x.SchedulingResult.CoursesTimeID == courseTime.CoursesTimeID && schedulingWeekList.Contains(x.SchedulingWeekNumResult.WeekNum) && x.SchedulingResult.ClassroomID == r.ClassroomID) //当前时间点未被使用 select r); return result.ToList(); } private List GetOrderedClassroomViewList(List classroomViewList, Guid collegeID) { var result = (from r in classroomViewList join ccl in this.CollegeClassroomList on new { CollegeID = (Guid?)collegeID, r.ClassroomID } equals new { ccl.CollegeID, ccl.ClassroomID } where (ccl.CollegeID == collegeID) select new { ClassroomView = r, CollegeClassroom = ccl }) .OrderBy(x => x.ClassroomView.Totalseating) .ThenBy(x => x.ClassroomView.Name).Select(x => x.ClassroomView); return result.ToList(); } private bool IsEducationSchedulingClassCanAddSchedule(EducationSchedulingClassScheduleView currentScheduleClass, int weekday, CoursesTimeView courseTime, List courseProcessViewList, List currentStaffIDList, Guid? currentClassroomID) { var weekNumList = courseProcessViewList.Select(x => x.Week); //检查课程在该节次是否可以上课 //(需要在课程时间设置那里将每次保存的全部记录,不管是否勾选都保存起来,否则这个逻辑就会失效,以下其他设置同理) var courseSchedulingSettingList = (from css in this.CourseSchedulingSettingList where css.CoursematerialID == currentScheduleClass.CoursematerialID && css.Weekday == weekday && css.CoursesTimeID == courseTime.CoursesTimeID && css.IsOnWork == false select css ).ToList(); if (courseSchedulingSettingList.Count > 0) { return false; } //检查各任课老师在该节次是否可以上课 var teacherSchedulingSettingList = (from t in currentStaffIDList join tss in this.TeacherSchedulingSettingList on new { UserID = t, Weekday = (int?)weekday, courseTime.CoursesTimeID } equals new { tss.UserID, tss.Weekday, tss.CoursesTimeID } where tss.IsOnWork == false select t ).ToList(); if (teacherSchedulingSettingList.Count > 0) { return false; } //检查指定教室在该节次是否可以上课 if (currentClassroomID.HasValue) { var classroomSchedulingSettingList = (from css in this.ClassroomSchedulingSettingList where css.ClassroomID == currentClassroomID && css.Weekday == weekday && css.CoursesTimeID == courseTime.CoursesTimeID && css.IsOnWork == false select css ).ToList(); if (classroomSchedulingSettingList.Count > 0) { return false; } } //教师是否忙 //第一步 //从排课结果数组(SchedulingResult)中获取除自身外的其他排课班,并带出它们的教师列表, // 检测是否有和目前周次有交集的,排课班上课周次取排课结果的周次数组(SchedulingWeekNumResult) //从之前已有的排课记录(ScheduledEducationViewList)中获取排课班,并带出它们的教师列表, // 检测是否有和目前周次有交集的,排课班上课周次取排课表中的周次(ScheduledEducationViewList.WeekNumList) //这一步结束后并未确定教师冲突,只是获取出同一周次所有教师的班表 var teacherWorkTimeList = ( from sr in SchedulingResult .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) join srs in SchedulingTeacherResult on sr.EducationSchedulingID equals srs.EducationSchedulingID where SchedulingWeekNumResult.Where(x => x.EducationSchedulingID == sr.EducationSchedulingID).Select(x => x.WeekNum) .Intersect(weekNumList).Count() > 0 select new { sr.Weekday, sr.CoursesTimeID, srs.UserID } ).Concat( from esv in this.ScheduledEducationViewList .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) from ttvl in esv.TeacherTeachingViewList where esv.WeekNumList.Intersect(weekNumList).Count() > 0 select new { esv.Weekday, esv.CoursesTimeID, UserID = (Guid?)ttvl.UserID } ).ToList(); //第二步 //从第一步的结果中找出具体星期几第几节都和本任务相同的记录,如果有则可以确定教师时间冲突 var teacherUsedList = (from srttvl in teacherWorkTimeList join csid in currentStaffIDList on srttvl.UserID equals csid where srttvl.Weekday == weekday && srttvl.CoursesTimeID == courseTime.CoursesTimeID select srttvl).ToList(); if (teacherUsedList.Count > 0) { return false; } //学生是否忙 var studentUsedList = (from c in ( (from sr in SchedulingResult .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) join escv in EducationSchedulingClassViewList on sr.EducationSchedulingClassID equals escv.EducationSchedulingClassID join s in EducationSchedulingClassStudentViewList on sr.EducationSchedulingClassID equals s.EducationSchedulingClassID where SchedulingWeekNumResult.Where(x => x.EducationSchedulingID == sr.EducationSchedulingID) .Select(x => x.WeekNum) .Intersect(weekNumList).Count() > 0 select new { sr.Weekday, sr.CoursesTimeID, UserID = s.UserID } ).Concat( from esv in this.ScheduledEducationViewList .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) join s in ScheduledEducationStudentViewList on esv.EducationSchedulingClassID equals s.EducationSchedulingClassID where esv.WeekNumList.Intersect(weekNumList).Count() > 0 select new { esv.Weekday, esv.CoursesTimeID, UserID = s.UserID }) ) join cc in EducationSchedulingClassStudentViewList.Where(x => x.EducationSchedulingClassID == currentScheduleClass.EducationSchedulingClassID) on c.UserID equals cc.UserID where c.Weekday == weekday && c.CoursesTimeID == courseTime.CoursesTimeID select c).ToList(); if (studentUsedList.Count > 0) { return false; } #region 检查指定教室的冲突(没有指定就跳过) if (currentClassroomID.HasValue) { var currentClassroom = this.ClassroomList.FirstOrDefault(x => x.ClassroomID == currentClassroomID); //一般会找不到都是因为指定的教室被预留了,这种情况不能排 if (currentClassroom == null) { return false; } if (currentClassroom.IsConcurrentUse != true) { var classroomScheduleList = (from c in ( (from sr in SchedulingResult .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) join escv in EducationSchedulingClassViewList on sr.EducationSchedulingClassID equals escv.EducationSchedulingClassID where SchedulingWeekNumResult.Where(x => x.EducationSchedulingID == sr.EducationSchedulingID) .Select(x => x.WeekNum) .Intersect(weekNumList).Count() > 0 select new { sr.Weekday, sr.CoursesTimeID, sr.ClassroomID } ).Concat( from esv in this.ScheduledEducationViewList .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) from c in esv.ClassMajorIDList where esv.WeekNumList.Intersect(weekNumList).Count() > 0 && esv.ClassMajorIDList.Count == 0 select new { esv.Weekday, esv.CoursesTimeID, esv.ClassroomID }) ) where c.Weekday == weekday && c.CoursesTimeID == courseTime.CoursesTimeID && c.ClassroomID == currentClassroomID select c).ToList(); if (classroomScheduleList.Count > 0) { return false; } } } #endregion //是否有连续上课超限 #region 判断教师连续上课超限 var teacherContinuousList = (from sr in teacherWorkTimeList join ct in CourseTimeList on sr.CoursesTimeID equals ct.CoursesTimeID join csid in currentStaffIDList on sr.UserID equals csid where sr.Weekday == weekday group ct by sr.UserID into g select new { UserID = g.Key, Count = g.Sum(x => x.Workhours) + courseTime.Workhours }) .Where(x => x.Count > TeacherScheduleContinuousCount).ToList(); if (teacherContinuousList.Count > 0) { return false; } #endregion return true; } private bool IsClassScheduleContinuousExcessed(EducationSchedulingClassScheduleView currentScheduleClass, int weekday, CoursesTimeView courseTime, IList weeknumList) { #region 判断班级连续上课超限 var classContinuousList = (from sc in ( from sr in SchedulingResult join ct in this.CourseTimeList on sr.CoursesTimeID equals ct.CoursesTimeID join swn in SchedulingWeekNumResult on sr.EducationSchedulingID equals swn.EducationSchedulingID join sc in this.EducationSchedulingClassViewList on sr.EducationSchedulingClassID equals sc.EducationSchedulingClassID from c in sc.ClassMajorIDList join cc in currentScheduleClass.ClassMajorIDList on c equals cc where sr.Weekday == weekday && weeknumList.Contains(swn.WeekNum) select new { WeekNum = swn.WeekNum, ClassMajorID = c, Times = ct.Workhours, sc }).Concat( from esv in ScheduledEducationViewList from esvwn in esv.WeekNumList join ct in this.CourseTimeList on esv.CoursesTimeID equals ct.CoursesTimeID join sc in EducationSchedulingClassViewList on esv.EducationSchedulingClassID equals sc.EducationSchedulingClassID from c in esv.ClassMajorIDList join cc in currentScheduleClass.ClassMajorIDList on c equals cc where esv.Weekday == weekday && weeknumList.Contains(esvwn) select new { WeekNum = esvwn, ClassMajorID = c, Times = ct.Workhours, sc } ) group sc by new { WeekNum = sc.WeekNum, ClassMajorID = sc.ClassMajorID, sc.sc } into g where g.Sum(x => x.Times) + courseTime.Workhours > ClassScheduleContinuousCount select new { WeekNum = g.Key.WeekNum, ClassMajorID = g.Key.ClassMajorID, Count = g.Sum(x => x.Times) + courseTime.Workhours }); if (classContinuousList.Count() > 0) { return true; } #endregion #region 判断班级相同课程连续上课超限 var classCourseContinuousList = (from sc in (from sr in SchedulingResult join ct in this.CourseTimeList on sr.CoursesTimeID equals ct.CoursesTimeID join swn in SchedulingWeekNumResult on sr.EducationSchedulingID equals swn.EducationSchedulingID join sc in EducationSchedulingClassViewList on sr.EducationSchedulingClassID equals sc.EducationSchedulingClassID from c in sc.ClassMajorIDList join cc in currentScheduleClass.ClassMajorIDList on new { ClassMajorID = c, sc.CoursematerialID } equals new { ClassMajorID = cc, CoursematerialID = currentScheduleClass.CoursematerialID } where sr.Weekday == weekday && weeknumList.Contains(swn.WeekNum) select new { WeekNum = swn.WeekNum, ClassMajorID = c, sc.CoursematerialID, Times = ct.Workhours, sc } ).Concat(from esv in ScheduledEducationViewList from esvwn in esv.WeekNumList join ct in this.CourseTimeList on esv.CoursesTimeID equals ct.CoursesTimeID join sc in EducationSchedulingClassViewList on esv.EducationSchedulingClassID equals sc.EducationSchedulingClassID from c in sc.ClassMajorIDList join cc in currentScheduleClass.ClassMajorIDList on new { ClassMajorID = c, sc.CoursematerialID } equals new { ClassMajorID = cc, CoursematerialID = currentScheduleClass.CoursematerialID } where esv.Weekday == weekday && weeknumList.Contains(esvwn) select new { WeekNum = esvwn, ClassMajorID = c, sc.CoursematerialID, Times = ct.Workhours, sc } ) group sc by new { WeekNum = sc.WeekNum, ClassMajorID = sc.ClassMajorID, sc.CoursematerialID, sc.sc } into g where g.Sum(x => x.Times) + courseTime.Workhours > ClassCourseScheduleContinuousCount select new { WeekNum = g.Key.WeekNum, ClassMajorID = g.Key.ClassMajorID, CoursematerialID = g.Key.CoursematerialID, Count = g.Sum(x => x.Times) + courseTime.Workhours }); if (classCourseContinuousList.Count() > 0) { return true; } #endregion return false; } private List GetOrderedWeekdays(EducationSchedulingClassScheduleView currentScheduleClass, IList weekdayList) { //计算每周要排多少天课 var weekCount = ArrangementList.Select(x => (x.Weekday == 0) ? 7 : x.Weekday).Max(); //获取已经排了的课程 var allScheduledWeekdays = SchedulingResult.Where(x => x.EducationSchedulingClassID == currentScheduleClass.EducationSchedulingClassID) .Join(CourseTimeList, (x => x.CoursesTimeID), (x => x.CoursesTimeID), ((x, y) => new { x.Weekday, y.Workhours })) .Concat( ScheduledEducationViewList.Where(x => x.EducationSchedulingClassID == currentScheduleClass.EducationSchedulingClassID) .Join(CourseTimeList, (x => x.CoursesTimeID), (x => x.CoursesTimeID), ((x, y) => new { x.Weekday, y.Workhours }))) .ToList(); //计算已经排过的课程中,最后一课是周几 var lastScheduledWeekday = allScheduledWeekdays.Max(x => x.Weekday) ?? 0; var schedulingWeekList = currentScheduleClass.CourseProcessViewList.Select(x => new { x.Week, x.Times }); allScheduledWeekdays.ForEach(x => { schedulingWeekList = schedulingWeekList.Select(m => new { m.Week, Times = (x.Weekday == m.Week) ? (m.Times - x.Workhours) : m.Times }); }); var maxToBeScheduleCount = schedulingWeekList.Where(x => x.Times > 0).Max(x => x.Times); //1、((double)weekCount - (double)lastScheduledWeekday)=剩余多少天需要排课 //2、((double)maxToBeScheduleCount) / (double)ClassCourseScheduleContinuousCount)计算出需要排课的课程时间最多的周次,按照班级课程连续排课限制计算,需要排多少天 //1/2得出需要间隔开的天数 //(该逻辑基于自动排课,是按固定周次顺序一直排下来的循环中使用) var skipDayCount = Convert.ToInt32(Math.Ceiling(((double)weekCount - (double)lastScheduledWeekday) / ((double)maxToBeScheduleCount) / (double)ClassCourseScheduleContinuousCount)); //预留排课后又被删掉一天的工作安排的情况 if (skipDayCount < 0) { skipDayCount = 0; } var startWeekday = lastScheduledWeekday == 0 ? weekdayList.Min() : lastScheduledWeekday; return weekdayList.OrderBy(x => ( ((x == 0) ? 7 : x) > lastScheduledWeekday && (((x == 0) ? 7 : x) - startWeekday) % (skipDayCount + 1) == 0) ? 0 : 1) .ThenBy(x => (x == 0) ? 7 : x).ToList(); } public void GenerateSchedule() { this.InitSchedule(); this.GenerateScheduleByInited(); } private void GenerateScheduleByInited() { var totalScheduleTime = ArrangementList.Select(x => new { x.Weekday, x.CoursesTimeID }).ToList(); var weekdayList = ArrangementList.Select(x => x.Weekday).Distinct().ToList(); ////自动排课时,保证涉及的院校都是重新排的,所以以前的记录要清掉 //this.ScheduledEducationViewList = this.ScheduledEducationViewList.Where(x => !CollegeList.Select(w => w.CollegeID).Contains(x.CollegeID)).ToList(); var allMissionClassIDList = EducationSchedulingClassViewList.Select(x => x.EducationMissionClassID).Distinct().ToList(); var educationMissionClassList = this.EducationSchedulingClassDAL.EducationMissionClassDAL.Value. EducationMissionClassRepository.GetList(x => allMissionClassIDList.Contains(x.EducationMissionClassID)).ToList(); foreach (var college in CollegeList) { LogHelper.WriteLog(LogType.ServiceLog, "开始对" + college.Name + "进行排课。"); var currentScheduleClassList = this.EducationSchedulingClassViewList.Where(c => c.CourseCollegeID == college.CollegeID).ToList(); //排序 currentScheduleClassList = this.GetOrderedEducationSchedulingClassViewList(currentScheduleClassList); //var skipDayCount = this.GetCommonSkipDays(currentScheduleClassList); foreach (var currentScheduleClass in currentScheduleClassList) { var scheduleClassProcess = (from cpv in currentScheduleClass.CourseProcessViewList group cpv by new { cpv.ClassroomTypeID, cpv.ClassroomID, teacherKey = string.Join(",", cpv.CourseProcessTeacherViewList .OrderBy(x => x.TeacherType).ThenBy(x => x.UserID) .Select(x => x.UserID.ToString() + "|" + x.TeacherType.ToString())) } into g select new { ClassroomTypeID = g.Key.ClassroomTypeID, ClassroomID = g.Key.ClassroomID, TeacherViewList = g.FirstOrDefault().CourseProcessTeacherViewList, CourseProcessViewList = g.Select(x => x).ToList() }).ToList(); foreach (var classroomAndStaff in scheduleClassProcess) { var currentTeacherViewList = new List(); int? currentClassroomTypeID = classroomAndStaff.ClassroomTypeID ?? currentScheduleClass.ClassroomTypeID; Guid? currentClassroomID = classroomAndStaff.ClassroomID ?? currentScheduleClass.ClassroomID; var currentScheduleTimes = 0; var maxScheduleTimes = classroomAndStaff.CourseProcessViewList.Max(x => x.Times); var courseProcessViewList = classroomAndStaff.CourseProcessViewList.OrderBy(x => x.Times).ToList(); if (classroomAndStaff.TeacherViewList.Count == 0) { currentTeacherViewList = currentScheduleClass.TeacherTeachingViewList; } else { currentTeacherViewList = classroomAndStaff.TeacherViewList.Select(x => (BaseTeacherTeachingView)x).ToList(); } //需要根据skipDayCount计算出weekday循环的顺序(不一定是1到7) weekdayList = GetOrderedWeekdays(currentScheduleClass, weekdayList); for (int i = 0; i < weekdayList.Count; i ++) { var weekday = weekdayList[i] ?? 0; if (!totalScheduleTime.Any(w => w.Weekday == weekday)) { continue; } //这里只需要计算出,参与这节课的老师里面的其中一个人在某个节次上过课就行了 var staffUsedCourseTime = (from ct in ( from sr in SchedulingResult .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) join str in SchedulingTeacherResult on sr.EducationSchedulingID equals str.EducationSchedulingID join s in currentTeacherViewList on str.UserID equals s.UserID select sr.CoursesTimeID) .Concat( from esv in this.ScheduledEducationViewList .Where(x => x.EducationSchedulingClassID != currentScheduleClass.EducationSchedulingClassID) from esvs in esv.TeacherTeachingViewList where currentTeacherViewList.Select(x => x.UserID).Contains(esvs.UserID) select esv.CoursesTimeID ) select ct).Distinct(); //当前教师之前有排过课的节次往后移,实在排不下才排到那些节次去,保证各个教师的上课时间相对公平些 var staffCourseTimeList = (from ct in CourseTimeList join sr in staffUsedCourseTime on ct.CoursesTimeID equals sr into dsr from esr in dsr.DefaultIfEmpty() select new { ct, esr }) .OrderBy(x => (x.esr == null ? 0 : 1)) .ThenBy(x => x.ct.StartTimes) .Select(x => x.ct) .ToList(); foreach (var courseTime in staffCourseTimeList) { if (!totalScheduleTime.Any(w => w.Weekday == weekday && w.CoursesTimeID == courseTime.CoursesTimeID)) { continue; } //获取该次排课排的周次 var tmpScheduleTimes = currentScheduleTimes + (courseTime.EndTimes.Value - courseTime.StartTimes.Value + 1); if (tmpScheduleTimes > maxScheduleTimes) { break; } var weeknumList = courseProcessViewList.Where(x => x.Times >= tmpScheduleTimes).ToList(); if (!this.IsEducationSchedulingClassCanAddSchedule(currentScheduleClass, weekday, courseTime, weeknumList, currentTeacherViewList.Select(x => (Guid?)x.UserID).ToList(), currentClassroomID)) { continue; } if (this.IsClassScheduleContinuousExcessed(currentScheduleClass, weekday, courseTime, weeknumList.Select(x => x.Week).ToList())) { break; } //获取该时间点可以使用的教室 var currentClassroomList = this.GetCurrentClassroomViewList(weekday, courseTime, weeknumList.Select(x => x.Week).ToList()); //没有教室,就什么都不用排了 if (currentClassroomList.Count == 0) { continue; } //排序 currentClassroomList = this.GetOrderedClassroomViewList(currentClassroomList, currentScheduleClass.CollegeID.Value); if (currentClassroomID.HasValue) { currentClassroomList = currentClassroomList.Where(x => x.ClassroomID == currentClassroomID).ToList(); } else if (currentClassroomTypeID.HasValue) { currentClassroomList = currentClassroomList.Where(x => x.ClassroomTypeID.Contains(currentClassroomTypeID.Value) && ((x.Totalseating ?? 0) > currentScheduleClass.StudentNum || x.IsConcurrentUse == true) ).ToList(); } //筛选完以后再看看还有没有 if (currentClassroomList.Count == 0) { continue; } var currentClassroom = currentClassroomList.First(); var educationScheduling = new ES_EducationScheduling { EducationSchedulingID = Guid.NewGuid(), SchoolyearID = this.SchoolYear.SchoolYearID, EducationSchedulingClassID = currentScheduleClass.EducationSchedulingClassID, Weekday = weekday, CoursesTimeID = courseTime.CoursesTimeID, ClassroomID = currentClassroom.ClassroomID, IsAuto = true, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = DateTime.Now, ModifyTime = DateTime.Now }; //增加排课教师 var staffList = this.StaffList.Where(x => currentTeacherViewList.Any(w => w.UserID == x.UserID)).ToList(); staffList.ForEach(x => { var educationSchedulingTeacher = new ES_EducationSchedulingTeacher { EducationSchedulingTeacherID = Guid.NewGuid(), EducationSchedulingID = educationScheduling.EducationSchedulingID, UserID = x.UserID, TeachingMethod = (int)EM_TeachingMethod.Lecturer, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = DateTime.Now, ModifyTime = DateTime.Now }; this.SchedulingTeacherResult.Add(educationSchedulingTeacher); }); this.SchedulingResult.Add(educationScheduling); //确实排课了,这时候才更改currentScheduleTimes的值 currentScheduleTimes = tmpScheduleTimes; weeknumList.ForEach(x => { var educationSchedulingWeekNum = new ES_EducationSchedulingWeekNum { EducationSchedulingWeekNumID = Guid.NewGuid(), EducationSchedulingID = educationScheduling.EducationSchedulingID, WeekNum = x.Week, RecordStatus = (int)SYS_STATUS.USABLE, CreateTime = DateTime.Now, ModifyTime = DateTime.Now }; this.SchedulingWeekNumResult.Add(educationSchedulingWeekNum); }); var educatiomMissionClass = educationMissionClassList.FirstOrDefault(x => x.EducationMissionClassID == currentScheduleClass.EducationMissionClassID); if (educatiomMissionClass != null) { educatiomMissionClass.RecordStatus = (int)EM_EducationMissionClassStatus.Scheduled; } } } } } LogHelper.WriteLog(LogType.ServiceLog, college.Name + "排课结束。"); } this.UnitOfWork.AddRange(this.SchedulingResult); this.UnitOfWork.AddRange(this.SchedulingWeekNumResult); this.UnitOfWork.AddRange(this.SchedulingTeacherResult); this.UnitOfWork.Commit(); } public void ClearAutoSchedule(Guid schoolYearID, Guid? collegeID, int? standardID, int? year, Guid? classmajorID) { Expression> exp = (x => x.SchoolyearID == schoolYearID && x.IsAuto == true); Expression> expTeacher = (x => x.ES_EducationScheduling.SchoolyearID == schoolYearID && x.ES_EducationScheduling.IsAuto == true); Expression> expWeekNum = (x => x.ES_EducationScheduling.SchoolyearID == schoolYearID && x.ES_EducationScheduling.IsAuto == true); if (collegeID.HasValue) { exp = exp.And(x => x.EM_EducationSchedulingClass.EM_EducationMissionClass.EM_EducationMission.CollegeID == collegeID); expTeacher = expTeacher.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.EM_EducationMission.CollegeID == collegeID); expWeekNum = expWeekNum.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.EM_EducationMission.CollegeID == collegeID); } if (year.HasValue) { exp = exp.And(x => x.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.CF_Grademajor.GradeID == year)); expTeacher = expTeacher.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.CF_Grademajor.GradeID == year)); expWeekNum = expWeekNum.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.CF_Grademajor.GradeID == year)); } if (standardID.HasValue) { exp = exp.And(x => x.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.CF_Grademajor.CF_Facultymajor.StandardID == standardID)); expTeacher = expTeacher.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.CF_Grademajor.CF_Facultymajor.StandardID == standardID)); expWeekNum = expWeekNum.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.CF_Grademajor.CF_Facultymajor.StandardID == standardID)); } if (classmajorID.HasValue) { exp = exp.And(x => x.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.ClassmajorID == classmajorID)); expTeacher = expTeacher.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.ClassmajorID == classmajorID)); expWeekNum = expWeekNum.And(x => x.ES_EducationScheduling.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor.Any(w => w.ClassmajorID == classmajorID)); } var educationSchedulingList = this.EducationSchedulingDAL.EducationSchedulingRepository.GetList(exp, (x => x.EM_EducationSchedulingClass.EM_EducationMissionClass)).ToList(); educationSchedulingList.ForEach(x => x.EM_EducationSchedulingClass.EM_EducationMissionClass.RecordStatus = (int)EM_EducationMissionClassStatus.Submitted); this.UnitOfWork.Remove(expTeacher); this.UnitOfWork.Remove(expWeekNum); this.UnitOfWork.RemoveRange(new HashSet(educationSchedulingList)); this.UnitOfWork.Commit(); } public void ClearClassmajorSchedule(Guid schoolYearID, Guid classmajorID) { this.UnitOfWork.Delete(x => x.SchoolyearID == schoolYearID && x.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor .Any(w => w.ClassmajorID == classmajorID)); } public List GetClassmajorSchedule(Guid schoolYearID, Guid classmajorID) { var classmajorScheduleList = this.GetEducationScheduleView(new ConfiguretView(), (x => x.SchoolyearID == schoolYearID && x.EM_EducationSchedulingClass.EM_EducationMissionClass.CF_Classmajor .Any(w => w.ClassmajorID == classmajorID)), false); return classmajorScheduleList.rows; } public List GetDepartmentSchedule(Guid schoolYearID, Guid departmentID, IList handleModeList) { var departmentScheduleList = this.GetEducationScheduleView(new ConfiguretView(), (x => x.SchoolyearID == schoolYearID && x.EM_EducationSchedulingClass.EM_EducationMissionClass.EM_EducationMission.DepartmentID == departmentID && handleModeList.Contains(x.EM_EducationSchedulingClass.EM_EducationMissionClass.HandleModeID)), false); return departmentScheduleList.rows; } public List GetAvailableClassroomList(Guid collegeID, Guid schoolYearID, IList schedulingWeekList, int weekday, Guid courseTimeID, int? studentNum, int? classroomTypeID = null) { return ClassroomScheduleDAL.GetAvailableClassroom(collegeID, schoolYearID, schedulingWeekList, weekday, courseTimeID, studentNum, classroomTypeID).ToList() .OrderBy(x => x.BuildingsInfoName) .ThenBy(x => string.Join(",", x.CF_ClassroomType.OrderBy(w => w.ClassroomType).Select(w => w.ClassroomType).ToList())) .ThenBy(x => x.Totalseating).ToList(); } public void CheckClassroomIsAvailable(Guid collegeID, Guid schoolYearID, IList schedulingWeekList, int weekday, Guid courseTimeID, Guid classroomID) { //this.InitClassroomInfo(); //this.ClassroomList = this.ClassroomList.Where(x => x.ClassroomID == classroomID).ToList(); var classroom = this.ClassroomScheduleDAL.GetAvailableClassroom().Where(x => x.ClassroomID == classroomID) .FirstOrDefault(); if (classroom == null) { throw new Exception("指定的教室不可用或被预留。"); } var courseTimeView = this.CoursesTimeServices.Value.GetCoursesTimeView(courseTimeID); var cannotScheduleList = this.ClassroomScheduleDAL.ClassroomScheduleSettingRepository.GetList(x => x.ClassroomID == classroomID && x.Weekday == weekday && x.CoursesTimeID == courseTimeID && x.IsOnWork == false).ToList(); if (cannotScheduleList.Count > 0) { throw new Exception("教室在当前时间点不可以排课,请检查教室可排时间设置。"); } var classroomList = this.ClassroomScheduleDAL.GetAvailableClassroom(schoolYearID, schedulingWeekList, weekday, courseTimeID, null) .Where(x => x.ClassroomID == classroomID) .ToList(); //当前时间点未被使用 if (classroomList.Count == 0) { throw new ClassroomConflictException("指定的教室已被其他任务班占用。"); } } public void CheckClassroomIsAvailable(Guid schoolYearID, IList schedulingWeekList, int weekday, Guid courseTimeID, Guid classroomID) { var classroom = this.ClassroomScheduleDAL.GetAvailableClassroom().Where(x => x.ClassroomID == classroomID) .FirstOrDefault(); if (classroom == null) { throw new Exception("指定的教室不可用或被预留。"); } var courseTimeView = this.CoursesTimeServices.Value.GetCoursesTimeView(courseTimeID); var classroomList = this.ClassroomScheduleDAL.GetAvailableClassroom(schoolYearID, schedulingWeekList, weekday, courseTimeID, null) .Where(x => x.ClassroomID == classroomID) .ToList(); //当前时间点未被使用 if (classroomList.Count == 0) { throw new ClassroomConflictException("指定的教室已被其他任务班占用。"); } } public void CheckEducationSchedulingClassCanAddManualSchedule( Guid educationScheduleClassID, IList schedulingWeekList, Guid schoolYearID, int weekday, Guid courseTimeID) { var schedulingClassView = this.EducationSchedulingClassServices.Value.GetEducationSchedulingClassView(educationScheduleClassID); var studentQuery = this.EducationSchedulingClassDAL.GetStudentQueryable(x => x.EducationSchedulingClassID == educationScheduleClassID); var teacherWeekList = (from swl in schedulingWeekList from ctv in swl.CourseProcessTeacherViewList select new { UserID = ctv.UserID, WeekNum = swl.Week }).ToList(); var weekList = schedulingWeekList.Select(x => x.Week).ToList(); var teacherIsBusyQuery = (from es in this.EducationSchedulingDAL.EducationSchedulingRepository.GetList( x => x.SchoolyearID == schoolYearID && x.EducationSchedulingClassID != educationScheduleClassID) from mct in es.ES_EducationSchedulingTeacher from eswn in es.ES_EducationSchedulingWeekNum where es.Weekday == weekday && es.CoursesTimeID == courseTimeID select new { es.EducationSchedulingID, mct.UserID, mct.CF_Staff.Sys_User.Name, eswn.WeekNum }); var teacherIsBusy = teacherIsBusyQuery.SelectByKeys(teacherWeekList).ToList(); if (teacherIsBusy.Count > 0) { var ex = new Exception("教师" + string.Join(",", teacherIsBusy.Select(x => x.Name).Distinct()) + "在其他班级有课,不能上课。"); teacherIsBusy.GroupBy(x => x.UserID, (x, y) => y.FirstOrDefault()).ToList().ForEach(x => ex.Data.Add(x.UserID, x.Name)); throw ex; } var studentIsBusy = (from es in this.EducationSchedulingDAL.EducationSchedulingRepository.GetList( x => x.SchoolyearID == schoolYearID && x.EducationSchedulingClassID != educationScheduleClassID) from s in es.EM_EducationSchedulingClass.CF_Student .Join(studentQuery, (x => x.UserID), (x => x.UserID), (x, y) => x) where es.Weekday == weekday && es.CoursesTimeID == courseTimeID && es.ES_EducationSchedulingWeekNum.Any(x => weekList.Contains(x.WeekNum)) select s).Include(x => x.Sys_User).ToList(); if (studentIsBusy.Count > 0) { throw new Exception("排课班涉及的部分学生在该时段有课,不能上课。"); } } public void CheckCourseIsOnWork(Guid coursematerialID, int weekday, Guid courseTimeID) { var course = this.CoursematerialDAL.CoursematerialRepository.GetSingle(x => x.CoursematerialID == coursematerialID); var courseIsOnWork = this.CourseScheduleDAL.CourseScheduleSettingRepository .GetList(x => x.Weekday == weekday && x.CoursesTimeID == courseTimeID && x.CoursematerialID == coursematerialID).Select(x => x.IsOnWork) .FirstOrDefault(); //这里的逻辑和自动排课有点不同,自动排课没有设置的话,则按照周工作时间来,但手工排课不需要按照周工作时间的限制排课, //如果课程时间没有设置,则默认可排 if (!(courseIsOnWork ?? true)) { throw new Exception("课程" + course.CourseName + "在该节次不能排课。"); } } public void CheckTeacherIsOnWork(Guid educationScheduleClassID, IList schedulingWeekList, int weekday, Guid courseTimeID) { var weekTeacherList = schedulingWeekList.SelectMany(w => w.CourseProcessTeacherViewList).GroupBy(w => w.UserID) .Select(w => w.FirstOrDefault()).ToList(); var teacherIDList = weekTeacherList.Select(w => (Guid?)w.UserID).Distinct().ToList(); var teacherIsOnWorkList = this.TeacherScheduleDAL.TeacherScheduleSettingRepository .GetList(x => x.Weekday == weekday && x.CoursesTimeID == courseTimeID && teacherIDList.Contains(x.UserID)) .Select(x => new { x.UserID, x.IsOnWork }).ToList(); weekTeacherList.ForEach(x => { if (teacherIsOnWorkList.Any(w => w.UserID == x.UserID && w.IsOnWork == false)) { throw new Exception("教师" + x.Name + "在该节次不能排课。"); } }); } public List GetTeacherContinuousWorkTime(Guid educationScheduleClassID, IList schedulingWeekList, Guid schoolYearID, int weekday) { var weekTeacherList = (from swl in schedulingWeekList from ctv in swl.CourseProcessTeacherViewList select new { UserID = ctv.UserID, Name = ctv.Name, WeekNum = swl.Week }).ToList(); var teacherIDList = weekTeacherList.Select(x => x.UserID).Distinct().ToList(); var teacherWorkTime = this.TeacherScheduleDAL.GetTeacherWorkTimeView(x => x.SchoolyearID == schoolYearID && x.Weekday == weekday, (x => teacherIDList.Contains(x.UserID)) ).ToList(); return (from t in weekTeacherList join tw in teacherWorkTime on new { UserID = t.UserID, WeekNum = t.WeekNum } equals new { UserID = tw.UserID, WeekNum = (int?)tw.WeekNum } into dtw from etw in dtw.DefaultIfEmpty() select new TeacherWorkTimeView { UserID = t.UserID, Name = t.Name, WeekNum = t.WeekNum ?? 0, WorkTime = (etw == null ? 0 : etw.WorkTime) }).ToList(); } public void AddScheduling(EducationSchedulingView educationSchedulingView, IList schedulingWeekList, IList teacherList) { var educationSchedulingClassWithCourseProcess = this.EducationSchedulingClassDAL.EducationSchedulingClassRepository .GetSingle(x => x.EducationSchedulingClassID == educationSchedulingView.EducationSchedulingClassID, (x => x.EM_EducationMissionClass), (x => x.EM_EducationMissionClass.EM_EducationSchedulingClass.Select(w => w.ES_EducationScheduling.Select(y => y.ES_EducationSchedulingTeacher))), (x => x.EM_EducationMissionClass.EM_EducationSchedulingClass.Select(w => w.ES_EducationScheduling.Select(y => y.ES_EducationSchedulingWeekNum))), (x => x.EM_EducationMissionClass.EM_CourseProcess), (x => x.EM_EducationMissionClass.EM_MissionClassTeacher), (x => x.EM_EducationMissionClass.EM_MissionClassTeacher.Select(w => w.CF_Staff)), (x => x.EM_EducationMissionClass.EM_CourseProcess.Select(w => w.EM_CourseProcessTeacher)), (x => x.EM_EducationMissionClass.EM_CourseProcess.Select(w => w.EM_CourseProcessTeacher.Select(v => v.CF_Staff)))); //var teacherScheduleList = from es in educationSchedulingClassWithCourseProcess.EM_EducationMissionClass.EM_EducationSchedulingClass.SelectMany(x => x.ES_EducationScheduling) // from est in es.ES_EducationSchedulingTeacher // from esw in es.ES_EducationSchedulingWeekNum // from cpt in courseProcessTeacherList.Where(x => x.CourseProcessTeacher.Teachers.Select(w => w.UserID).Contains() var educationScheduling = new ES_EducationScheduling { EducationSchedulingID = Guid.NewGuid(), SchoolyearID = educationSchedulingView.SchoolyearID, EducationSchedulingClassID = educationSchedulingView.EducationSchedulingClassID, Weekday = educationSchedulingView.Weekday, CoursesTimeID = educationSchedulingView.CoursesTimeID, ClassroomID = educationSchedulingView.ClassroomID }; this.SetNewStatus(educationScheduling); this.UnitOfWork.Add(educationScheduling); teacherList.ToList().ForEach(x => { var educationSchedulingTeacher = new ES_EducationSchedulingTeacher { EducationSchedulingTeacherID = Guid.NewGuid(), EducationSchedulingID = educationScheduling.EducationSchedulingID, UserID = x.UserID, TeachingMethod = x.TeachingMethod }; this.SetNewStatus(educationSchedulingTeacher); this.UnitOfWork.Add(educationSchedulingTeacher); }); foreach (var schedulingWeek in schedulingWeekList) { var educationSchedulingWeekNum = new ES_EducationSchedulingWeekNum { EducationSchedulingWeekNumID = Guid.NewGuid(), EducationSchedulingID = educationScheduling.EducationSchedulingID, WeekNum = schedulingWeek }; this.SetNewStatus(educationSchedulingWeekNum); this.UnitOfWork.Add(educationSchedulingWeekNum); } //更改教学任务状态 var educationSchedulingClass = this.EducationSchedulingClassDAL.EducationSchedulingClassRepository.GetSingle(x => x.EducationSchedulingClassID == educationSchedulingView.EducationSchedulingClassID, (x => x.EM_EducationMissionClass.EM_EducationMission), (x => x.EM_EducationMissionClass.EM_CourseProcess.Select(w => w.EM_CourseProcessTeacher))); var courseProcessList = educationSchedulingClass.EM_EducationMissionClass.EM_CourseProcess.Where(x => schedulingWeekList.Contains(x.Week) && string.Join(",", teacherList.Select(w => w.UserID.ToString() + "," + w.TeachingMethod).OrderBy(w => w)) == string.Join(",", x.EM_CourseProcessTeacher.Select(w => (w.UserID ?? Guid.Empty).ToString() + "," + w.TeachType).OrderBy(w => w))); educationSchedulingClass.EM_EducationMissionClass.RecordStatus = (int)EM_EducationMissionClassStatus.Scheduled; this.UnitOfWork.Commit(); } public void DelScheduling(Guid educationSchedulingID) { var educationScheduling = this.EducationSchedulingDAL.EducationSchedulingRepository. GetSingle(x => x.EducationSchedulingID == educationSchedulingID, (x => x.EM_EducationSchedulingClass.EM_EducationMissionClass)); var educationSchedulingClass = this.EducationSchedulingClassDAL.EducationSchedulingClassRepository .GetSingle((x => x.EducationSchedulingClassID == educationScheduling.EducationSchedulingClassID && x.EM_EducationMissionClass.EM_EducationSchedulingClass .SelectMany(w => w.ES_EducationScheduling) .Where(w => w.EducationSchedulingID != educationSchedulingID).Count() == 0), (x => x.EM_EducationMissionClass)); if (educationSchedulingClass != null) { educationSchedulingClass.EM_EducationMissionClass.RecordStatus = (int)EM_EducationMissionClassStatus.Submitted; } this.UnitOfWork.Remove(x => x.EducationSchedulingID == educationSchedulingID); this.UnitOfWork.Remove(x => x.EducationSchedulingID == educationSchedulingID); this.UnitOfWork.Remove(educationScheduling); this.UnitOfWork.Commit(); } public void DelWeeks(Guid educationSchedulingID, IList weekList) { this.UnitOfWork.Remove(x => x.EducationSchedulingID == educationSchedulingID && weekList.Contains(x.WeekNum)); this.UnitOfWork.Commit(); } public void Submit(Guid collegeID) { var collegePriority = this.CollegeScheduleDAL.CollegePriorityRepository.GetList(x => x.CollegeID == collegeID).FirstOrDefault(); if (collegePriority == null) { collegePriority = new ES_CollegePriority(); collegePriority.CollegePriorityID = Guid.NewGuid(); collegePriority.CollegeID = collegeID; collegePriority.Priority = 1; collegePriority.StartDate = DateTime.Today; collegePriority.EndDate = DateTime.Today; SetNewStatus(collegePriority); this.UnitOfWork.Add(collegePriority); } collegePriority.ApprovalStatus = (int)CF_ApprovalStatus.Submitted; this.UnitOfWork.Commit(); } public List GetTeacherScheduleAdjustmentViewList(Guid schoolyearID, Guid userID, Guid? coursematerialID, Guid? educationMissionClassID, int? weekNum, int? weekday, Guid? courseTimesID, Guid? classroomID) { Expression> educationSchedulingExp = x => x.SchoolyearID == schoolyearID; Expression> missionClassExp = (x => true); Expression> weeknumExp = (x => true); var stopApprovedStatus = this.EducationSchedulingStopServices.Value.GetCorrectEndStatus().Value; if (coursematerialID.HasValue) { missionClassExp = missionClassExp.And(x => x.CoursematerialID == coursematerialID); } if (educationMissionClassID.HasValue) { missionClassExp = missionClassExp.And(x => x.EducationMissionClassID == educationMissionClassID); } if (weekNum.HasValue) { weeknumExp = weeknumExp.And(x => x.WeekNum == weekNum); } if (weekday.HasValue) { educationSchedulingExp = educationSchedulingExp.And(x => x.Weekday == weekday); } if (courseTimesID.HasValue) { educationSchedulingExp = educationSchedulingExp.And(x => x.CoursesTimeID == courseTimesID); } if (classroomID.HasValue) { educationSchedulingExp = educationSchedulingExp.And(x => x.ClassroomID == classroomID); } var scheduleAdjustmentViewList = this.EducationSchedulingDAL.GetScheduleAdjustmentViewQueryable( x => x.UserID == userID, educationSchedulingExp, missionClassExp, weeknumExp, stopApprovedStatus).ToList(); var educationSchedulingIDList = scheduleAdjustmentViewList.Select(x => x.EducationSchedulingID).ToList(); var teacherList = this.EducationSchedulingDAL.EducationSchedulingTeacherRepository.GetList(x => educationSchedulingIDList.Contains(x.EducationSchedulingID), (x => x.CF_Staff.Sys_User)).ToList(); var result = scheduleAdjustmentViewList.OrderBy(x => x.WeekNum) .ThenBy(x => (x.Weekday == 0 ? 7 : x.Weekday)).ThenBy(x => x.TimesSegment).ThenBy(x => x.CoursesTimeName) .ThenBy(x => x.EducationMissionClassName).ToList(); result.ForEach(x => x.TeacherList = teacherList.Where(w => w.EducationSchedulingID == x.EducationSchedulingID) .Select(w => new MissionClassTeacherView { EducationMissionClassID = x.EducationMissionClassID, UserID = w.UserID, Name = w.CF_Staff.Sys_User.Name, TeachingMethod = w.TeachingMethod }).ToList()); return result; } public List GetTeacherScheduleStopViewList(Guid schoolyearID, Guid userID, Guid? coursematerialID, Guid? educationMissionClassID, int? weekNum, int? weekday, Guid? courseTimesID, Guid? classroomID) { Expression> educationSchedulingExp = x => x.SchoolyearID == schoolyearID; Expression> missionClassExp = (x => true); Expression> weeknumExp = (x => true); var stopApprovedStatus = this.EducationSchedulingStopServices.Value.GetCorrectEndStatus().Value; if (coursematerialID.HasValue) { missionClassExp = missionClassExp.And(x => x.CoursematerialID == coursematerialID); } if (educationMissionClassID.HasValue) { missionClassExp = missionClassExp.And(x => x.EducationMissionClassID == educationMissionClassID); } if (weekNum.HasValue) { weeknumExp = weeknumExp.And(x => x.WeekNum == weekNum); } if (weekday.HasValue) { educationSchedulingExp = educationSchedulingExp.And(x => x.Weekday == weekday); } if (courseTimesID.HasValue) { educationSchedulingExp = educationSchedulingExp.And(x => x.CoursesTimeID == courseTimesID); } if (classroomID.HasValue) { educationSchedulingExp = educationSchedulingExp.And(x => x.ClassroomID == classroomID); } var scheduleStopViewList = this.EducationSchedulingDAL.GetScheduleStopViewQueryable( x => x.UserID == userID, educationSchedulingExp, missionClassExp, weeknumExp, stopApprovedStatus).ToList(); var educationSchedulingIDList = scheduleStopViewList.Select(x => x.EducationSchedulingID).ToList(); var teacherList = this.EducationSchedulingDAL.EducationSchedulingTeacherRepository.GetList(x => educationSchedulingIDList.Contains(x.EducationSchedulingID), (x => x.CF_Staff.Sys_User)).ToList(); var result = scheduleStopViewList.OrderBy(x => x.WeekNum) .ThenBy(x => (x.Weekday == 0 ? 7 : x.Weekday)).ThenBy(x => x.TimesSegment).ThenBy(x => x.CoursesTimeName) .ThenBy(x => x.EducationMissionClassName).ToList(); result.ForEach(x => x.TeacherList = teacherList.Where(w => w.EducationSchedulingID == x.EducationSchedulingID) .Select(w => new MissionClassTeacherView { EducationMissionClassID = x.EducationMissionClassID, UserID = w.UserID, Name = w.CF_Staff.Sys_User.Name, TeachingMethod = w.TeachingMethod }).ToList()); return result; } /// /// 批量调课获取可调课节次 /// /// /// /// /// /// /// /// public List GetNotUseCoursesTimeViewListForBatchAdjustment(Guid? schoolyearID, int? toWeekday, int? toWeekNum, Guid? teacherID, List educationSchedulingWeekNumIDList) { var educationSchedulingWeekNumList = this.EducationSchedulingDAL.EducationSchedulingWeekNumRepository .GetList(x => educationSchedulingWeekNumIDList.Contains(x.EducationSchedulingWeekNumID), (x => x.ES_EducationScheduling), (x => x.ES_EducationScheduling.ES_EducationSchedulingTeacher)).ToList(); var weekTeacherList = educationSchedulingWeekNumList.Select(x => new { SchoolyearID = schoolyearID ?? x.ES_EducationScheduling.SchoolyearID, Weekday = toWeekday ?? x.ES_EducationScheduling.Weekday, WeekNum = toWeekNum ?? x.WeekNum }); var dateList = weekTeacherList.GroupBy(x => x.SchoolyearID).Select(x => SchoolYearServices.Value.GetDateByCoursesTime(x.Key.Value, x.Select(w => new FullCoursesTimeView { Weekday = w.Weekday, WeekNum = w.WeekNum }).ToList()) ).SelectMany(x => x).Select(x => (DateTime?)x).Distinct().ToList(); var examinationPlanList = ExaminationPlanDAL.GetExaminationPlanView(x => dateList.Contains(x.ExaminationDate) && x.RecordStatus == (int)EX_ExaminationPlanStatus.Submitted).ToList(); var examinationPlanIDList = examinationPlanList.Select(x => x.ExaminationPlanID).ToList(); var examinationRoomLayoutViewList = ExaminationPlanDAL.GetExaminationRoomLayoutView(x => examinationPlanIDList.Contains((Guid)x.ExaminationPlanID)).ToList(); examinationPlanList.ForEach(x => x.ExaminationRoomLayoutView = new HashSet(examinationRoomLayoutViewList.Where(y => y.ExaminationPlanID == x.ExaminationPlanID))); List teacherList = new List(); List result = new List(); List allCoursesTimeView = CoursesTimeServices.Value.GetCoursesTimeViewList(new ConfiguretView(), null); List examinationCourseTimeIDList = new List(); foreach (var educationSchedulingWeekNumID in educationSchedulingWeekNumIDList) { var EducationSchedulingWeekNum = educationSchedulingWeekNumList.FirstOrDefault(x => x.EducationSchedulingWeekNumID == educationSchedulingWeekNumID); List query = new List(); if (!schoolyearID.HasValue) { schoolyearID = EducationSchedulingWeekNum.ES_EducationScheduling.SchoolyearID; } if (!toWeekday.HasValue) { toWeekday = EducationSchedulingWeekNum.ES_EducationScheduling.Weekday; } if (!toWeekNum.HasValue) { toWeekNum = EducationSchedulingWeekNum.WeekNum; } if (!teacherID.HasValue) { var dbteacherList = EducationSchedulingWeekNum.ES_EducationScheduling.ES_EducationSchedulingTeacher.Select(x => x.UserID).ToList(); foreach (var id in dbteacherList) { teacherList.Add(id.Value); } } else { teacherList.Add(teacherID.Value); } query = this.EducationSchedulingDAL.GetNotUseCoursesTime(schoolyearID.Value, toWeekday.Value, teacherList, toWeekNum, educationSchedulingWeekNumID.Value).ToList(); var arrangementsNotWork = EducationSchedulingDAL.ArrangementsRepository.Entities.Where(x => !x.IsOnWork.Value && x.Weekday == toWeekday && x.EM_CoursesTime.RecordStatus == 1).ToList(); var notWorkCoursesTimeIDList = arrangementsNotWork.Select(x => x.CoursesTimeID).ToList(); var examinationDate = SchoolYearServices.Value.GetDateByCoursesTime(schoolyearID.Value, new List { new FullCoursesTimeView { Weekday = toWeekday.Value, WeekNum = toWeekNum.Value } }).FirstOrDefault(); if (examinationDate != null) { var dateExaminationPlanList = examinationPlanList.Where(x => x.ExaminationDate == examinationDate).ToList(); examinationCourseTimeIDList.AddRange(query.Where(x => dateExaminationPlanList.Any(w => w.StartTime < new TimeSpan(x.EndHour ?? 0, x.EndMinutes ?? 0, 0).Add(new TimeSpan(0, 1, 0)) && w.EndTime >= new TimeSpan(x.StartHour ?? 0, x.StartMinutes ?? 0, 0) && w.ExaminationRoomLayoutView.SelectMany(v => v.Sys_User).Select(v => v.UserID).Intersect(teacherList).Count() > 0)) .Select(x => x.CoursesTimeID.Value) .ToList()); } result = result.Concat(query.Where(x => !notWorkCoursesTimeIDList.Contains(x.CoursesTimeID))).ToList(); } //var arrangementsNotWork = EducationSchedulingDAL.ArrangementsRepository.Entities.Where(x => !x.IsOnWork.Value && x.Weekday == toWeekday && x.EM_CoursesTime.RecordStatus == (int)SYS_STATUS.USABLE).ToList(); //var notWorkCoursesTimeIDList = arrangementsNotWork.Select(x => x.CoursesTimeID).ToList(); var coursesTimeViewList = result.GroupBy(x => x.CoursesTimeID).Select(x => new { x.Key, count = x.Count() }).ToList(); var coursesTimeIDList = coursesTimeViewList.Where(x => x.count >= educationSchedulingWeekNumIDList.Count).Select(x => x.Key).ToList(); examinationCourseTimeIDList = examinationCourseTimeIDList.Distinct().ToList(); result = allCoursesTimeView.Where(x => coursesTimeIDList.Contains(x.CoursesTimeID) && !examinationCourseTimeIDList.Contains(x.CoursesTimeID.Value)).Distinct().ToList(); return result.OrderBy(x => x.StartTimes).ToList(); } public List GetNotUseCoursesTimeViewList(Guid schoolyearID, int weekday, int toWeekday, Guid courseTimeID, int? weekNum, int? toWeekNum, Guid? fromTeacherID, Guid? replaceTeacherID, Guid educationMissionClassID) { var schedule = this.GetEducationScheduleView(new ConfiguretView(), (x => x.EM_EducationSchedulingClass.EducationMissionClassID == educationMissionClassID && x.Weekday == weekday && x.CoursesTimeID == courseTimeID && x.ES_EducationSchedulingWeekNum.Any(w => weekNum == w.WeekNum)), false).rows.FirstOrDefault(); if (schedule == null) { return new List(); } var scheduleWeekNum = this.EducationSchedulingDAL.EducationSchedulingWeekNumRepository .GetSingle(x => x.EducationSchedulingID == schedule.EducationSchedulingID && x.WeekNum == weekNum); var teacherList = schedule.TeacherTeachingViewList.Select(x => x.UserID).ToList(); if (replaceTeacherID.HasValue) { teacherList.Remove(fromTeacherID ?? Guid.Empty); teacherList.Add(replaceTeacherID.Value); } var arrangementsNotWork = EducationSchedulingDAL.ArrangementsRepository.Entities.Where(x => !x.IsOnWork.Value && x.Weekday == toWeekday && x.EM_CoursesTime.RecordStatus == (int)SYS_STATUS.USABLE).ToList(); var notWorkCoursesTimeIDList = arrangementsNotWork.Select(x => x.CoursesTimeID).ToList(); var result = this.EducationSchedulingDAL.GetNotUseCoursesTime(schoolyearID, toWeekday, teacherList, toWeekNum, scheduleWeekNum.EducationSchedulingWeekNumID) .OrderBy(x => x.TimesSegment).ThenBy(x => x.StartTimes).ToList(); var schoolyear = this.SchoolYearServices.Value.GetSchoolYearView(schoolyearID); result = result.Where(x => !this.SchoolYearServices.Value.IsTimePassed(schoolyear, toWeekNum, toWeekday, x.StartHour, x.StartMinutes) && !notWorkCoursesTimeIDList.Contains(x.CoursesTimeID)) .ToList(); var scheduleDate = this.SchoolYearServices.Value.GetDateByCoursesTime(schoolyear.SchoolYearID.Value, new List { new FullCoursesTimeView { WeekNum = toWeekNum, Weekday = toWeekday } }).FirstOrDefault(); if (scheduleDate != null) { var examinationPlanList = ExaminationPlanDAL.GetExaminationPlanView(x => x.ExaminationDate == scheduleDate && x.SchoolyearID == schoolyear.SchoolYearID && x.RecordStatus == (int)EX_ExaminationPlanStatus.Submitted).ToList(); var examinationPlanIDList = examinationPlanList.Select(x => x.ExaminationPlanID).ToList(); var examinationRoomLayoutViewList = ExaminationPlanDAL.GetExaminationRoomLayoutView(x => examinationPlanIDList.Contains((Guid)x.ExaminationPlanID)).ToList(); examinationPlanList.ForEach(x => x.ExaminationRoomLayoutView = new HashSet(examinationRoomLayoutViewList.Where(y => y.ExaminationPlanID == x.ExaminationPlanID))); result = result.Where(x => !examinationPlanList.Any(w => w.StartTime < new TimeSpan(x.EndHour ?? 0, x.EndMinutes ?? 0, 0).Add(new TimeSpan(0, 1, 0)) && w.EndTime >= new TimeSpan(x.StartHour ?? 0, x.StartMinutes ?? 0, 0) && w.ExaminationRoomLayoutView.SelectMany(v => v.Sys_User).Select(v => (Guid?)v.UserID).Contains(replaceTeacherID)) ).ToList(); } return result; } public IGridResultSet GetNotUseClassroomViewList(ConfiguretView configuretView, Guid schoolyearID, int weekday, int toWeekday, Guid coursesTimeID, Guid toCoursesTimeID, IList weekNumList, IList toWeekNumList, int classroomTypeID, Guid educationMissionClassID, int? pageIndex = null, int? pageSize = null) { var schedule = this.GetEducationScheduleView(new ConfiguretView(), (x => x.EM_EducationSchedulingClass.EducationMissionClassID == educationMissionClassID && x.Weekday == weekday && x.CoursesTimeID == coursesTimeID && x.ES_EducationSchedulingWeekNum.Any(w => weekNumList.Contains(w.WeekNum))), false).rows.FirstOrDefault(); if (schedule == null) { return new GridResultSet() { rows = new List(), total = 0 }; } var result = this.EducationSchedulingDAL.GetNotUseClassroomForAdjustment(schoolyearID, toWeekday, toCoursesTimeID, classroomTypeID, toWeekNumList, schedule.EducationSchedulingID); var schoolyear = this.SchoolYearServices.Value.GetSchoolYearView(schoolyearID); var coursesTime = CoursesTimeServices.Value.GetCoursesTimeView(coursesTimeID); var scheduleDateList = this.SchoolYearServices.Value.GetDateByCoursesTime(schoolyear.SchoolYearID.Value, toWeekNumList.Select(x => new FullCoursesTimeView { WeekNum = x, Weekday = toWeekday }).ToList()).Select(x => (DateTime?)x).ToList(); if (scheduleDateList.Count > 0) { var examinationPlanList = ExaminationPlanDAL.GetExaminationPlanView(x => scheduleDateList.Contains(x.ExaminationDate) && x.SchoolyearID == schoolyear.SchoolYearID && x.RecordStatus == (int)EX_ExaminationPlanStatus.Submitted).ToList(); examinationPlanList = examinationPlanList.Where(x => x.StartTime < new TimeSpan(coursesTime.EndHour ?? 0, coursesTime.EndMinutes ?? 0, 0).Add(new TimeSpan(0, 1, 0)) && x.EndTime >= new TimeSpan(coursesTime.StartHour ?? 0, coursesTime.StartMinutes ?? 0, 0)) .ToList(); var examinationPlanIDList = examinationPlanList.Select(x => x.ExaminationPlanID).ToList(); var examinationRoomLayoutViewList = ExaminationPlanDAL.GetExaminationRoomLayoutView(x => examinationPlanIDList.Contains((Guid)x.ExaminationPlanID)).ToList(); examinationPlanList.ForEach(x => x.ExaminationRoomLayoutView = new HashSet(examinationRoomLayoutViewList.Where(y => y.ExaminationPlanID == x.ExaminationPlanID))); var examinationClassRoomIDList = examinationPlanList.SelectMany(x => x.ExaminationRoomLayoutView).Select(x => x.ClassroomID).Distinct().ToList(); result = result.Where(x => !examinationClassRoomIDList.Contains(x.ClassroomID)); } if (!string.IsNullOrEmpty(configuretView.ConditionValue)) { result = result .DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); } result = result.OrderBy(x => x.Code); return result.ToGridResultSet(pageIndex, pageSize); } public ScheduleAdjustmentEditView GetScheduleAdjustmentView(Guid educationSchedulingWeekNumID) { var stopApprovalStatus = this.EducationSchedulingStopServices.Value.GetCorrectEndStatus().Value; var scheduleAdjustmentView = this.EducationSchedulingDAL.GetScheduleAdjustmentViewQueryable(x => true, x => true, (x => true), (x => x.EducationSchedulingWeekNumID == educationSchedulingWeekNumID), stopApprovalStatus).FirstOrDefault(); return scheduleAdjustmentView; } public EducationSchedulingStopView GetEducationSchedulingStopView(Guid educationSchedulingID) { var scheduleStopView = this.EducationSchedulingDAL.GetEducationSchedulingStopViewQueryable(x => true, x => x.EducationSchedulingID == educationSchedulingID).FirstOrDefault(); var weekNumList = this.EducationSchedulingDAL.GetEducationMissionWeekNumQueryable( x => x.EducationSchedulingID == educationSchedulingID); scheduleStopView.ScheduleWeekNumList = weekNumList.Select(w => w.WeekNum) .ToList(); return scheduleStopView; } public List GetClassroomScheduleViewList(Guid schoolyearID, Guid classroomID) { var scheduleQuery = (from es in this.EducationSchedulingDAL.EducationSchedulingRepository.GetList(x => x.SchoolyearID == schoolyearID && x.ClassroomID == classroomID) from esw in es.ES_EducationSchedulingWeekNum select new { es.CoursesTimeID, es.Weekday, WeekNum = esw.WeekNum } ).Concat( from ceu in this.ClassroomExcessiveUseDAL.ClassroomExcessiveUseRepository.GetList(x => x.SchoolyearID == schoolyearID && x.ClassroomID == classroomID, (x => x.ES_ClassroomExcessiveUseScheduling.Select(w => w.ES_ClassroomExcessiveUseSchedulingWeekNum))) from ceus in ceu.ES_ClassroomExcessiveUseScheduling from ceusw in ceus.ES_ClassroomExcessiveUseSchedulingWeekNum select new { ceus.CoursesTimeID, ceus.Weekday, WeekNum = ceusw.WeekNum } ).GroupBy(x => new { x.CoursesTimeID, x.Weekday }) .Select(x => new { x.Key.CoursesTimeID, x.Key.Weekday, WeekNumList = x.Select(w => w.WeekNum).Distinct() }).ToList(); var classroomScheduleList = ( from ct in this.CoursesTimeServices.Value.GetCoursesTimeViewList(new ConfiguretView(), null).ToList() join es in scheduleQuery on ct.CoursesTimeID equals es.CoursesTimeID into des from ees in des.DefaultIfEmpty() select new { CoursesTime = ct, Weekday = (ees == null) ? null : ees.Weekday, WeekNumList = (ees == null) ? new List() : ees.WeekNumList }).ToList(); var result = (from cs in classroomScheduleList group cs by cs.CoursesTime.CoursesTimeID into g select new ClassroomScheduleView { ClassroomID = classroomID, CoursesTimeID = g.FirstOrDefault().CoursesTime.CoursesTimeID ?? Guid.Empty, StartTimes = g.FirstOrDefault().CoursesTime.StartTimes, EndTimes = g.FirstOrDefault().CoursesTime.EndTimes, StartHour = g.FirstOrDefault().CoursesTime.StartHour, EndHour = g.FirstOrDefault().CoursesTime.EndHour, StartMinutes = g.FirstOrDefault().CoursesTime.StartMinutes, EndMinutes = g.FirstOrDefault().CoursesTime.EndMinutes, Monday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Monday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString(), Tuesday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Tuesday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString(), Wednesday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Wednesday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString(), Thurday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Thursday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString(), Friday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Friday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString(), Saturday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Saturday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString(), Sunday = g.Where(x => x.Weekday != null && x.Weekday == (int)DayOfWeek.Sunday) .SelectMany(x => x.WeekNumList).Distinct().ToList() .GetWeekNumString() }).OrderBy(x => x.StartTimes).ToList(); return result; } private void ChangeCourseProcess(ES_EducationScheduling educationScheduling, IList lastTeacherViewList, IList newTeacherList) { var coursesTime = this.CoursesTimeServices.Value.GetCoursesTime(educationScheduling.CoursesTimeID); var weekList = educationScheduling.ES_EducationSchedulingWeekNum.Select(x => x.WeekNum).ToList(); var userIDList = newTeacherList.Select(x => x.UserID).ToList(); var lastTeacherList = lastTeacherViewList.Select(x => new { x.UserID, x.TeachingMethod }).ToList(); var courseProcessList = this.EducationSchedulingClassDAL.EducationMissionClassDAL.Value.CourseProcessRepository .GetList(x => x.EducationMissionClassID == educationScheduling.EM_EducationSchedulingClass.EducationMissionClassID, (x => x.EM_CourseProcessTeacher)) .ToList(); var educationSchedulingList = this.EducationSchedulingDAL.EducationSchedulingRepository .GetList(x => x.EM_EducationSchedulingClass.EducationMissionClassID == educationScheduling.EM_EducationSchedulingClass.EducationMissionClassID, (x => x.ES_EducationSchedulingTeacher), (x => x.ES_EducationSchedulingWeekNum), (x => x.EM_CoursesTime)) .ToList(); var courseTimes = coursesTime.EndTimes - coursesTime.StartTimes + 1; //先找出涉及周内,之前的老师上课的课程进度记录,这批是要扣减课时的,但扣到多少为止,后面查一下课表再说 var decreaseCourseProcessList = courseProcessList.Where(x => weekList.Contains(x.Week) && (lastTeacherList.Join(x.EM_CourseProcessTeacher.ToList(), (w => new { UserID = (Guid?)w.UserID, TeachType = w.TeachingMethod }), (w => new { w.UserID, w.TeachType }), ((w, y) => w)).Count() == lastTeacherList.Count && lastTeacherList.Count == x.EM_CourseProcessTeacher.Count)) .ToList(); var increaseCourseProcessList = courseProcessList.Where(x => weekList.Contains(x.Week) && (newTeacherList.Join(x.EM_CourseProcessTeacher.ToList(), (w => new { UserID = (Guid?)w.UserID, TeachType = w.TeachingMethod }), (w => new { w.UserID, w.TeachType }), ((w, y) => w)).Count() == newTeacherList.Count && newTeacherList.Count == x.EM_CourseProcessTeacher.Count)) .ToList(); var addCourseProcessWeekList = (from w in weekList join icp in increaseCourseProcessList on w equals icp.Week into dicp from icp in dicp.DefaultIfEmpty() where icp == null select w).ToList(); //保存起要扣减的记录,添加时要找回对应的教室类型和教室 var needAddCourseProcessList = decreaseCourseProcessList.Select(x => new { x.Week, x.ClassroomTypeID, x.ClassroomID }).ToList(); decreaseCourseProcessList.ForEach(x => { var minTimes = educationSchedulingList.Where(w => w.ES_EducationSchedulingWeekNum.Any(y => y.WeekNum == x.Week) && w.ES_EducationSchedulingTeacher.Join(x.EM_CourseProcessTeacher, (y => new { y.UserID, TeachType = y.TeachingMethod }), (y => new { y.UserID, y.TeachType }), ((y, z) => y)).Count() == w.ES_EducationSchedulingTeacher.Count && w.ES_EducationSchedulingTeacher.Count == x.EM_CourseProcessTeacher.Count) .Sum(w => w.EM_CoursesTime.EndTimes - w.EM_CoursesTime.StartTimes + 1); if (x.Times - courseTimes < minTimes) { x.Times = minTimes; } else { x.Times -= courseTimes; } if (x.Times <= 0) { UnitOfWork.Remove(x); } else { SetModifyStatus(x); } }); increaseCourseProcessList.ForEach(x => { x.Times += courseTimes; SetModifyStatus(x); }); addCourseProcessWeekList.ForEach(week => { var currentLastCourseProcess = needAddCourseProcessList.FirstOrDefault(x => x.Week == week); var newCourseProcess = new EM_CourseProcess { CourseProcessID = Guid.NewGuid(), EducationMissionClassID = educationScheduling.EM_EducationSchedulingClass.EducationMissionClassID, ClassroomTypeID = currentLastCourseProcess.ClassroomTypeID, ClassroomID = currentLastCourseProcess.ClassroomID, Times = courseTimes, Week = week }; SetNewStatus(newCourseProcess); newTeacherList.ToList().ForEach(x => { var newCourseProcessTeacher = new EM_CourseProcessTeacher { CourseProcessTeacherID = Guid.NewGuid(), CourseProcessID = newCourseProcess.CourseProcessID, UserID = x.UserID, TeachType = x.TeachingMethod }; SetNewStatus(newCourseProcessTeacher); newCourseProcess.EM_CourseProcessTeacher.Add(newCourseProcessTeacher); }); UnitOfWork.Add(newCourseProcess); }); } public void ChangeClassroom(Guid educationSchedulingID, Guid classroomID) { var educationScheduling = this.EducationSchedulingDAL.EducationSchedulingRepository .GetSingle(x => x.EducationSchedulingID == educationSchedulingID, (x => x.ES_EducationSchedulingTeacher), (x => x.ES_EducationSchedulingWeekNum), (x => x.EM_EducationSchedulingClass), (x => x.CF_Classroom.CF_ClassroomType)); educationScheduling.ClassroomID = classroomID; this.UnitOfWork.Commit(); } public List GetEducationSchedulingTeacherViewList(Guid educationSchedulingID) { var teacherViewQueryable = this.EducationSchedulingDAL.GetTeacherViewQueryable(x => x.EducationSchedulingID == educationSchedulingID); return teacherViewQueryable.ToList(); } private void CheckTeacherIsAvailable(ES_EducationScheduling educationScheduling, List teacherIDList) { var notAvailableTeacherMessage = ""; var teacherIsOnWorkList = this.EducationSchedulingDAL.CheckTeacherIsOnWork(x => x.EducationSchedulingID == educationScheduling.EducationSchedulingID, (x => teacherIDList.Contains(x.UserID))).ToList(); if (teacherIsOnWorkList.Count > 0) { teacherIsOnWorkList.ForEach(x => notAvailableTeacherMessage += string.Format("教师{0}在该节次不能排课。\n", x.Name, x.WeekdayDesc, x.Times)); } var teacherIsBusyList = this.EducationSchedulingDAL.CheckTeacherIsBusy(x => x.EducationSchedulingID == educationScheduling.EducationSchedulingID, (x => teacherIDList.Contains(x.UserID))).ToList(); if (teacherIsBusyList.Count > 0) { teacherIsBusyList.ForEach(x => notAvailableTeacherMessage += string.Format("教师{0}在该节次在其他班级有课,不能排课。\n", x.Name, x.WeekdayDesc, x.Times)); } if (!string.IsNullOrEmpty(notAvailableTeacherMessage)) { throw new Exception(notAvailableTeacherMessage); } } private void RefreshEducationMissionClassTeachers(Guid educationMissionClassID) { var educationMissionClass = EducationSchedulingClassDAL.EducationMissionClassRepository .GetSingle(x => x.EducationMissionClassID == educationMissionClassID, (x => x.EM_MissionClassTeacher), (x => x.EM_CourseProcess.Select(w => w.EM_CourseProcessTeacher))); var teacherList = (from courseProcess in educationMissionClass.EM_CourseProcess from teacher in courseProcess.EM_CourseProcessTeacher group teacher by new { teacher.UserID, teacher.TeachType } into g select g.FirstOrDefault()).ToList(); educationMissionClass.EM_MissionClassTeacher.ToList().ForEach(x => UnitOfWork.Remove(x)); teacherList.ForEach(x => { var missionClassTeacher = new EM_MissionClassTeacher { MissionClassTeacherID = Guid.NewGuid(), MissionClassID = educationMissionClassID, UserID = x.UserID, TeachType = x.TeachType }; this.SetNewStatus(missionClassTeacher); this.UnitOfWork.Add(missionClassTeacher); }); } public void ChangeTeacher(Guid educationSchedulingID, IList teacherList) { var educationScheduling = this.EducationSchedulingDAL.EducationSchedulingRepository.GetSingle(x => x.EducationSchedulingID == educationSchedulingID, (x => x.ES_EducationSchedulingTeacher), (x => x.ES_EducationSchedulingWeekNum), (x => x.EM_EducationSchedulingClass), (x => x.CF_Classroom.CF_ClassroomType)); this.CheckTeacherIsAvailable(educationScheduling, teacherList.Select(x => x.UserID).ToList()); var lastTeacherList = this.EducationSchedulingDAL.GetTeacherViewQueryable(x => x.EducationSchedulingID == educationSchedulingID).ToList(); this.UnitOfWork.Remove(x => x.EducationSchedulingID == educationSchedulingID); teacherList.ToList().ForEach(x => { var educationSchedulingTeacher = new ES_EducationSchedulingTeacher { EducationSchedulingTeacherID = Guid.NewGuid(), EducationSchedulingID = educationSchedulingID, UserID = x.UserID, TeachingMethod = x.TeachingMethod }; this.SetNewStatus(educationSchedulingTeacher); this.UnitOfWork.Add(educationSchedulingTeacher); }); this.ChangeCourseProcess(educationScheduling, lastTeacherList, teacherList); this.RefreshEducationMissionClassTeachers(educationScheduling.EM_EducationSchedulingClass.EducationMissionClassID.Value); this.UnitOfWork.Commit(); } private void InitCommonByEducationSchedulingClassList(IList educationSchedulingClassList) { SchedulingResult = new List(); //初始化排课结果 SchedulingWeekNumResult = new List(); SchedulingTeacherResult = new List(); NotScheduledList = new List(); SchoolYear = SchoolYearServices.Value.GetSchoolYearView(educationSchedulingClassList.First().SchoolYearID); CourseTimeList = CoursesTimeServices.Value.GetCoursesTimeViewList(new ConfiguretView(), null); CourseTimeList = CourseTimeList.OrderBy(x => x.StartTimes).ToList(); ArrangementList = ArrangementDAL.GetArrangementView(x => x.IsOnWork == true).ToList(); var staffIDList = educationSchedulingClassList.SelectMany(x => x.CourseProcessViewList) .SelectMany(x => x.CourseProcessTeacherViewList) .Select(x => x.UserID) .Union( educationSchedulingClassList.SelectMany(x => x.TeacherTeachingViewList) .Select(x => x.UserID) ).ToList(); StaffList = StaffDAL.staffRepository.GetList(x => staffIDList.Contains(x.UserID)).ToList(); CourseSchedulingSettingList = CourseScheduleDAL.CourseScheduleSettingRepository .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); TeacherSchedulingSettingList = TeacherScheduleDAL.TeacherScheduleSettingRepository .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList(); CourseTypePriorityList = this.CourseTypeScheduleDAL.CourseTypePriorityRepository.Entities.ToList(); TeacherScheduleContinuousCount = this.ParameterServices.Value.GetParameterValue(CF_ParameterType.TeacherScheduleContinuousCount); ClassScheduleContinuousCount = this.ParameterServices.Value.GetParameterValue(CF_ParameterType.ClassScheduleContinuousCount); ClassCourseScheduleContinuousCount = this.ParameterServices.Value.GetParameterValue(CF_ParameterType.ClassCourseScheduleContinuousCount); } private void InitScheduleClassInfoByEducationSchedulingClassList(IList educationSchedulingClassList) { var collegeIDList = educationSchedulingClassList.Select(x => x.CourseCollegeID).ToList(); CollegeList = CollegeScheduleDAL.CollegeDAL.Value.GetCollegeViewQueryable().Where(x => collegeIDList.Contains(x.CollegeID)).ToList(); var submittedCollegeQuery = this.CollegeScheduleDAL.CollegeScheduleStatusRepository .GetList(x => collegeIDList.Contains(x.CollegeID) && x.RecordStatus == (int)ES_CollegeScheduleStatusCode.Submited && x.SchoolyearID == SchoolYear.SchoolYearID) .Include(x => x.CF_College); var submittedCollege = submittedCollegeQuery.ToList(); if (submittedCollege.Count > 0) { throw new Exception(string.Join("、", submittedCollege.Select(x => x.CF_College.Name)) + "的相关课表已经提交,请先撤销提交后再进行排课。"); } var today = DateTime.Today; var collegePriority = this.CollegePriorityDAL.GetCollegePriorityViewQueryable(x => collegeIDList.Contains(x.CollegeID)).ToList(); var noPriorityCollege = (from c in CollegeList join cp in collegePriority on c.CollegeID equals cp.CollegeID into dcp from ecp in dcp.DefaultIfEmpty() where ecp == null select c).ToList(); if (noPriorityCollege.Count > 0) { throw new Exception(string.Join("、", noPriorityCollege.Select(x => x.Name)) + "的未设置排课时间,无法排课,请通知相关管理人员调整排课优先级设置。"); } var expireCollege = collegePriority.Where(x => x.StartDate > today || x.EndDate < today).ToList(); if (expireCollege.Count > 0) { throw new Exception(string.Join("、", expireCollege.Select(x => x.CollegeName)) + "的已不在排课时间内,无法排课,请通知相关管理人员调整排课优先级设置。"); } var educationSchedulingClassIDList = educationSchedulingClassList.Select(x => x.EducationSchedulingClassID).Distinct().ToList(); EducationSchedulingClassViewList = educationSchedulingClassList.ToList(); EducationSchedulingClassStudentViewList = EducationSchedulingClassDAL.GetEducationSchedulingClassStudentViewQueryable(x => educationSchedulingClassIDList.Contains(x.EducationSchedulingClassID)) .ToList(); ScheduledEducationViewList = this.GetEducationScheduleView(new ConfiguretView(), (x => x.SchoolyearID == SchoolYear.SchoolYearID), false).rows; var scheduledClassIDList = ScheduledEducationViewList.Select(x => x.EducationSchedulingClassID).Distinct().ToList(); ScheduledEducationStudentViewList = EducationSchedulingClassDAL.GetEducationSchedulingClassStudentViewQueryable(x => scheduledClassIDList.Contains(x.EducationSchedulingClassID)).ToList(); } public void GenerateSchedule(IList educationMissionClassIDList) { var educationSchedulingClassList = EducationSchedulingClassServices.Value.GetEducationSchedulingClassView(new ConfiguretView(), (x => educationMissionClassIDList.Contains(x.EducationMissionClassID))).rows; if (educationSchedulingClassList.Count == 0) { return; } if (educationSchedulingClassList.Select(x => x.SchoolYearID).Distinct().Count() > 1) { throw new Exception("所选记录包括不同学年学期的教学任务班,多个学期智能排课会使逻辑比较混乱,目前只支持同一个学年学期的智能排课。"); } this.InitCommonByEducationSchedulingClassList(educationSchedulingClassList); this.InitScheduleClassInfoByEducationSchedulingClassList(educationSchedulingClassList); this.InitClassroomInfo(); this.GenerateScheduleByInited(); } public List GetMobileStudentSchedule(Guid userID, DateTime date) { Expression> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); var schoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); var firstMonday = schoolyear.FirstWeek.Value.AddDays(1 - (int)schoolyear.FirstWeek.Value.DayOfWeek); var todayWeekNum = Math.Ceiling((Math.Floor(date.Subtract(schoolyear.FirstWeek.Value).TotalDays + 1)) / 7); var todayWeekday = date.DayOfWeek == DayOfWeek.Sunday ? 0 : (int)date.DayOfWeek; exp = exp.And(x => x.SchoolyearID == schoolyear.SchoolYearID && x.Weekday == todayWeekday); exp = exp.And(x => x.EM_EducationSchedulingClass.CF_Student.Any(w => w.UserID == userID)); exp = exp.And(x => x.ES_EducationSchedulingWeekNum.Any(w => w.WeekNum == todayWeekNum)); var scheduleViewList = this.GetEducationScheduleView(new ConfiguretView(), exp, false).rows; var resultGroup = scheduleViewList.GroupBy(x => new { SchoolyearID = x.SchoolyearID, SchoolyearCode = x.SchoolyearCode, CoursematerialID = x.CoursematerialID, CoursematerialName = x.CoursematerialName, TeachingModeID = x.TeachingModeID, Weekday = x.Weekday, TimeGroup = (x.CoursesTime.StartTimes - 1) / 2, ClassroomID = x.ClassroomID, ClassroomName = x.ClassroomName //TeacherTeachingViewList = x.TeacherTeachingViewList }); var resultList = resultGroup .Select(x => new MobileUserEducationScheduleView { SchoolyearID = x.Key.SchoolyearID, SchoolyearCode = x.Key.SchoolyearCode, CoursematerialID = x.Key.CoursematerialID, CoursematerialName = x.Key.CoursematerialName, TeachingModeID = x.Key.TeachingModeID, Weekday = x.Key.Weekday, TimeGroup = x.Key.TimeGroup, CoursesTimeList = x.Select(w => w.CoursesTime).ToList(), ClassroomID = x.Key.ClassroomID, ClassroomName = x.Key.ClassroomName //TeacherTeachingViewList = x.Key.TeacherTeachingViewList }); return resultList.OrderBy(x => x.TimeGroup) .ToList(); } public EducationMissionClassView GetEducationMissionClassViewBySchedule(Guid? schoolyearID, DateTime date, Guid coursesTimeID, Guid teacherUserID) { var currentSchoolyear = this.SchoolYearServices.Value.GetCurrentSchoolYear(); var firstWeek = currentSchoolyear.FirstWeek.Value.AddDays(1 - (int)currentSchoolyear.FirstWeek.Value.DayOfWeek); var nowWeekNum = (date.Subtract(firstWeek).Days + 7) / 7; var nowWeekday = (int)date.DayOfWeek == 0 ? 7 : (int)date.DayOfWeek; var scheduling = this.EducationSchedulingDAL.GetEducationSchedulingViewQueryable(x => x.ES_EducationSchedulingTeacher.Any(w => w.UserID == teacherUserID) && x.ES_EducationSchedulingWeekNum.Any(w => w.WeekNum == nowWeekNum) && x.Weekday == nowWeekday && x.CoursesTimeID == coursesTimeID && x.SchoolyearID == schoolyearID).FirstOrDefault(); if (scheduling == null) return null; var educationMissionClass = this.EducationMissionClassDAL .GetEducationMissionClassViewQueryble(x => x.EducationMissionClassID == scheduling.EducationMissionClassID) .FirstOrDefault(); return educationMissionClass; } public List GetEducationSchedulingWeekNumViewByDateTimeList(Guid schoolyearID, IList dateTimeList) { var fullCoursesTimeList = this.SchoolYearServices.Value.GetCoursesTimeByDateTime(schoolyearID, dateTimeList); var coursesTimeList = fullCoursesTimeList.Select(x => new { SchoolyearID = schoolyearID, x.WeekNum, x.Weekday, x.CoursesTimeID }).ToList(); var educationSchedulingViewList = this.EducationSchedulingDAL.GetEducationSchedulingWeekNumViewQueryable(x => true) .SelectByKeys(coursesTimeList).ToList(); var educationMissionClassIDList = educationSchedulingViewList.Select(x => x.EducationMissionClassID).Distinct().ToList(); var teacherList = this.EducationMissionClassDAL.GetMissionClassTeacherQuery(x => educationMissionClassIDList.Contains(x.EducationMissionClassID)).ToList(); var studentList = this.EducationSchedulingClassDAL.EducationSchedulingClassRepository.GetList(x => educationMissionClassIDList.Contains(x.EducationMissionClassID), (x => x.CF_Student)).ToList(); educationSchedulingViewList.ForEach(x => { var fullCoursesTime = fullCoursesTimeList.FirstOrDefault(w => w.WeekNum == x.WeekNum && w.Weekday == x.Weekday && w.CoursesTimeID == x.CoursesTimeID); x.TeacherList = teacherList.Where(w => w.EducationMissionClassID == x.EducationMissionClassID).ToList(); x.Date = fullCoursesTime.Date; x.StartTime = new TimeSpan(fullCoursesTime.StartHour ?? 0, fullCoursesTime.StartMinute ?? 0, 0); x.EndTime = new TimeSpan(fullCoursesTime.EndHour ?? 0, fullCoursesTime.EndMinute ?? 0, 0); x.StudentList = studentList.Where(w => w.EducationMissionClassID == x.EducationMissionClassID).SelectMany(w => w.CF_Student).ToList(); }); return educationSchedulingViewList; } /// /// 手动排考查询排考时间有课的老师 /// /// /// /// public List GetEducationSchedulingWeekNumViewForExaminationTeacher(Guid schoolyearID, IList dateTimeList) { var fullCoursesTimeList = this.SchoolYearServices.Value.GetCoursesTimeByDateTime(schoolyearID, dateTimeList); var coursesTimeList = fullCoursesTimeList.Select(x => new { SchoolyearID = schoolyearID, x.WeekNum, x.Weekday, x.CoursesTimeID }).ToList(); var educationSchedulingViewList = this.EducationSchedulingDAL.GetEducationSchedulingWeekNumViewQueryable(x => true) .SelectByKeys(coursesTimeList).ToList(); var educationMissionClassIDList = educationSchedulingViewList.Select(x => x.EducationMissionClassID).Distinct().ToList(); var teacherListScheduling = this.EducationMissionClassDAL.GetEducationSchedulingTeacherQuery(x => educationMissionClassIDList.Contains(x.EducationMissionClassID)).ToList(); var teacherList = this.EducationMissionClassDAL.GetMissionClassTeacherQuery(x => educationMissionClassIDList.Contains(x.EducationMissionClassID)).ToList(); var studentList = this.EducationSchedulingClassDAL.EducationSchedulingClassRepository.GetList(x => educationMissionClassIDList.Contains(x.EducationMissionClassID), (x => x.CF_Student)).ToList(); educationSchedulingViewList.ForEach(x => { var fullCoursesTime = fullCoursesTimeList.FirstOrDefault(w => w.WeekNum == x.WeekNum && w.Weekday == x.Weekday && w.CoursesTimeID == x.CoursesTimeID); //x.TeacherList = teacherList.Where(w => w.EducationMissionClassID == x.EducationMissionClassID).ToList(); x.TeacherList = teacherListScheduling.Where(w => w.WeekNum == x.WeekNum && w.Weekday == x.Weekday && w.CoursesTimeID == x.CoursesTimeID).ToList(); x.Date = fullCoursesTime.Date; x.StartTime = new TimeSpan(fullCoursesTime.StartHour ?? 0, fullCoursesTime.StartMinute ?? 0, 0); x.EndTime = new TimeSpan(fullCoursesTime.EndHour ?? 0, fullCoursesTime.EndMinute ?? 0, 0); x.StudentList = studentList.Where(w => w.EducationMissionClassID == x.EducationMissionClassID).SelectMany(w => w.CF_Student).ToList(); }); return educationSchedulingViewList; } } public class ClassroomConflictException : Exception { public ClassroomConflictException(string message) : base(message) { } public ClassroomConflictException(string message, Exception ex) : base(message, ex) { } } }