ExaminationPlanServices.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.Entities;
  6. using EMIS.ViewModel.ExaminationManage;
  7. using EMIS.ViewModel;
  8. using EMIS.CommonLogic.ExaminationManage;
  9. using Bowin.Common.Utility;
  10. using EMIS.ViewModel.EducationSchedule;
  11. using System.IO;
  12. using EMIS.Utility;
  13. using Bowin.Common.DataTime;
  14. using AutoMapper;
  15. using EMIS.ViewModel.UniversityManage.ClassroomManage;
  16. namespace EMIS.ExtensionLogic.ServiceLogic.ExaminationManage
  17. {
  18. public class ExaminationPlanServices : EMIS.CommonLogic.ExaminationManage.ExaminationPlanServices
  19. {
  20. protected override bool AutoSchedule(List<ExamiationPlanAutoScheduleView> notCompleteExaminationPlanList, List<EX_ExaminationRoomSetting> examinationRoomList,
  21. List<ClassroomView> classroomViewList, TimeSegmentView examinationDate, List<EX_ExaminationTeacher> examinationTeacherList,
  22. List<StudentClassroomView> scheduledStudentList, List<ViewModel.EducationSchedule.EducationSchedulingWeekNumView> schedulingList,
  23. List<ClassroomExcessiveUseWeekNumView> excessiveUseList)
  24. {
  25. var needScheduleCount = notCompleteExaminationPlanList.Count;
  26. var scheduledCount = 0;
  27. var needModifyRecordList = new List<AutoScheduleModifyView>();
  28. foreach (var examinationRoom in examinationRoomList)
  29. {
  30. //如果考场在当前时间有排课,跳过
  31. var schedule = schedulingList.FirstOrDefault(x => x.ClassroomID == examinationRoom.ClassroomID
  32. && x.Date == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime
  33. && x.EndTime >= examinationDate.StartTime);
  34. if (schedule != null)
  35. {
  36. continue;
  37. }
  38. var excessiveUse = excessiveUseList.FirstOrDefault(x => x.ClassroomID == examinationRoom.ClassroomID
  39. && x.Date == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime
  40. && x.EndTime >= examinationDate.StartTime);
  41. if (excessiveUse != null)
  42. {
  43. continue;
  44. }
  45. //按照排考人数从多到少排序,第一行拿出未排过的考试计划,然后从第二个计划开始,优先拿出排过考的考试计划,
  46. //这样做的目的是保证考试计划能排到最少的考场去,不至于学生太过分散
  47. var orderedExaminationPlanViewList = notCompleteExaminationPlanList
  48. .Where(x => (x.MissionStudentCount - x.StudentCount) > 0)
  49. .OrderBy(x => x.MissionStudentCount - x.StudentCount).Take(1).ToList();
  50. orderedExaminationPlanViewList = orderedExaminationPlanViewList
  51. .Concat(notCompleteExaminationPlanList.Where(x =>
  52. !orderedExaminationPlanViewList.Select(w => w.ExaminationPlan.ExaminationPlanID)
  53. .Contains(x.ExaminationPlan.ExaminationPlanID)
  54. && (x.MissionStudentCount - x.StudentCount) > 0)
  55. .OrderBy(x => x.StudentCount > 0 ? 0 : 1)
  56. .ThenByDescending(x => x.MissionStudentCount - x.StudentCount)).ToList();
  57. #region 获取目前有空的监考员,并随机抽出两个
  58. //先找出排课没有冲突的监考员
  59. var noScheduleTeacherList = (from allteacher in examinationTeacherList
  60. from scheduling in schedulingList.Where(x => x.TeacherList.Any(w => w.UserID == allteacher.UserID)
  61. && x.Date == examinationDate.ExaminationDate && x.StartTime <= examinationDate.EndTime
  62. && x.EndTime >= examinationDate.StartTime).DefaultIfEmpty()
  63. where scheduling == null
  64. select allteacher).ToList();
  65. var teacherList = this.GetAvailableExaminationTeacher(noScheduleTeacherList,
  66. examinationDate.ExaminationDate, examinationDate.StartTime, examinationDate.EndTime, scheduledStudentList);
  67. if (teacherList.Count < 2)
  68. {
  69. return false;
  70. }
  71. #endregion
  72. //这个循环需要把考室尽量塞满……
  73. foreach (var orderedExaminationPlanView in orderedExaminationPlanViewList)
  74. {
  75. //获取要排考的学生
  76. var needScheduleStudentList = this.GetNeedScheduleStudentList(orderedExaminationPlanView, scheduledStudentList);
  77. //首先判断是否指定了考试时间,但并不是当前时间,这些自动排考无法处理
  78. if (orderedExaminationPlanView.ExaminationPlan.ExaminationDate.HasValue
  79. && !(orderedExaminationPlanView.ExaminationPlan.ExaminationDate == examinationDate.ExaminationDate
  80. && orderedExaminationPlanView.ExaminationPlan.StartTime == examinationDate.StartTime
  81. && orderedExaminationPlanView.ExaminationPlan.EndTime == examinationDate.EndTime))
  82. {
  83. continue;
  84. }
  85. //然后判断冲突
  86. if (IsConflict(orderedExaminationPlanView, needScheduleStudentList.Select(x => x.UserID).ToList(), examinationDate, examinationRoom.CF_Classroom, scheduledStudentList))
  87. {
  88. continue;
  89. }
  90. var roomRemainSeat = examinationRoom.CF_Classroom.Examinationseating - scheduledStudentList
  91. .Where(x => x.ExaminationDate == examinationDate.ExaminationDate
  92. && x.ClassroomID == examinationRoom.ClassroomID
  93. && x.StartTime <= examinationDate.EndTime
  94. && x.EndTime >= examinationDate.StartTime).Count();
  95. if (roomRemainSeat < needScheduleStudentList.Count)
  96. {
  97. continue;
  98. }
  99. var actualScheduleStudentList = needScheduleStudentList.ToList();
  100. //记录住这些数据需要修改,后面所有考场和考试任务都检查完后,再统一修改
  101. var result = new AutoScheduleModifyView
  102. {
  103. ExaminationPlan = orderedExaminationPlanView.ExaminationPlan,
  104. ExaminationDate = examinationDate.ExaminationDate,
  105. StartTime = examinationDate.StartTime,
  106. EndTime = examinationDate.EndTime,
  107. ExaminationRoom = examinationRoom,
  108. ScheduleStudentList = needScheduleStudentList
  109. };
  110. var lastExaminationRoomLayout = scheduledStudentList.Where(x => x.ClassroomID == examinationRoom.ClassroomID).FirstOrDefault();
  111. if (lastExaminationRoomLayout != null)
  112. {
  113. result.CF_Staff = new HashSet<CF_Staff>(
  114. examinationTeacherList.Where(x => lastExaminationRoomLayout.TeacherIDList.Contains(x.UserID ?? Guid.NewGuid())).Select(x => x.CF_Staff)
  115. );
  116. }
  117. else
  118. {
  119. result.CF_Staff = new HashSet<CF_Staff>(teacherList);
  120. }
  121. needModifyRecordList.Add(result);
  122. //更新已排完的计划,这样后续的判断才会没问题
  123. scheduledStudentList.AddRange(needScheduleStudentList.Select(x => new StudentClassroomView
  124. {
  125. UserID = x.UserID,
  126. ExaminationDate = examinationDate.ExaminationDate,
  127. StartTime = examinationDate.StartTime,
  128. EndTime = examinationDate.EndTime,
  129. ClassroomID = examinationRoom.ClassroomID,
  130. ExaminationStyleID = orderedExaminationPlanView.ExaminationPlan.ExaminationStyleID,
  131. TeacherIDList = result.CF_Staff.Select(w => w.UserID).ToList(),
  132. ExaminationPlanID = orderedExaminationPlanView.ExaminationPlan.ExaminationPlanID
  133. }));
  134. orderedExaminationPlanView.StudentCount += needScheduleStudentList.Count();
  135. scheduledCount++;
  136. }
  137. }
  138. //如果排不完,返回失败,让外部决定如何处理
  139. if (scheduledCount < needScheduleCount)
  140. {
  141. return false;
  142. }
  143. foreach (var needModifyRecord in needModifyRecordList)
  144. {
  145. #region 判断成功,开始更新数据
  146. needModifyRecord.ExaminationPlan.ExaminationDate = examinationDate.ExaminationDate;
  147. needModifyRecord.ExaminationPlan.StartTime = examinationDate.StartTime;
  148. needModifyRecord.ExaminationPlan.EndTime = examinationDate.EndTime;
  149. var examinationRoomLayout = new EX_ExaminationRoomLayout
  150. {
  151. ExaminationRoomLayoutID = Guid.NewGuid(),
  152. ExaminationPlanID = needModifyRecord.ExaminationPlan.ExaminationPlanID,
  153. EX_ExaminationPlan = needModifyRecord.ExaminationPlan,
  154. ClassroomID = needModifyRecord.ExaminationRoom.ClassroomID,
  155. RowSpacing = 0,
  156. ColumnSpacing = 0,
  157. StudentOrderType = (int)CF_StudentOrderType.StudentNo,
  158. CF_Staff = needModifyRecord.CF_Staff
  159. };
  160. this.SetNewStatus(examinationRoomLayout);
  161. //这块需要挪到上面检测的地方,在上面如何判断这个考场能容纳多少学生?
  162. var examinationRoomStudentViewList = needModifyRecord.ScheduleStudentList.Select(x => new ExaminationRoomStudentView
  163. {
  164. ExaminationRoomStudentID = Guid.NewGuid(),
  165. ExaminationRoomLayoutID = examinationRoomLayout.ExaminationRoomLayoutID,
  166. UserID = x.UserID,
  167. LoginID = x.Sys_User.LoginID,
  168. Remark = ""
  169. }).ToList();
  170. var classroomView = classroomViewList.FirstOrDefault(x => x.ClassroomID == needModifyRecord.ExaminationRoom.ClassroomID);
  171. var examinationRoomStudentList = this.SetRoomStudentSeats(examinationRoomStudentViewList, classroomView, 0, 0, CF_StudentOrderType.StudentNo);
  172. examinationRoomLayout.EX_ExaminationRoomStudent = new HashSet<EX_ExaminationRoomStudent>(examinationRoomStudentList);
  173. needModifyRecord.ExaminationPlan.EX_ExaminationRoomLayout.Add(examinationRoomLayout);
  174. needModifyRecord.ExaminationRoom.CF_Classroom.EX_ExaminationRoomLayout.Add(examinationRoomLayout);
  175. #endregion
  176. }
  177. return true;
  178. }
  179. public override string AdultImport(string filePath, out string returnMessage)
  180. {
  181. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  182. List<AdultExaminationPlanImportView> errList = new List<AdultExaminationPlanImportView>(); //错误数据行
  183. List<AdultExaminationPlanImportView> successList = new List<AdultExaminationPlanImportView>(); //导入的数据行
  184. DataTimeHelper dth = new DataTimeHelper();
  185. #region 解析文件,存放到一个List集合里
  186. Dictionary<string, string> cellheader = new Dictionary<string, string>
  187. {
  188. { "CoursematerialName", "考试科目" }, { "ClassName", "考试班级名称" },
  189. { "MissionStudentCount", "学生数" }, { "StudentCount", "排考人数" },
  190. { "ExamsCategoryName", "考试性质" }, { "ExaminationModeName", "考试方式" },
  191. { "ExaminationWeekNumString", "考试周次" }, { "ExaminationDateString", "考试日期" },
  192. { "StartTimeString", "考试时间起"},{ "EndTimeString", "考试时间止" },
  193. { "ClassroomName", "考室"},{ "TeacherNames", "监考老师" }
  194. };
  195. List<AdultExaminationPlanImportView> importRowlist =
  196. NpoiExcelHelper.ExcelToEntityList<AdultExaminationPlanImportView>(cellheader, filePath, out errorMsg, out errList);
  197. File.Delete(filePath);
  198. cellheader.Add("ErrorMessage", "错误信息");//还原字典项
  199. #endregion
  200. #region 清空格
  201. for (int i = 0; i < importRowlist.Count(); i++)
  202. {
  203. if (importRowlist[i].CoursematerialName != null)
  204. importRowlist[i].CoursematerialName = importRowlist[i].CoursematerialName.Trim();
  205. if (importRowlist[i].ClassName != null)
  206. importRowlist[i].ClassName = importRowlist[i].ClassName.Trim();
  207. if (importRowlist[i].MissionStudentCount != null)
  208. importRowlist[i].MissionStudentCount = importRowlist[i].MissionStudentCount.Trim();
  209. if (importRowlist[i].StudentCount != null)
  210. importRowlist[i].StudentCount = importRowlist[i].StudentCount.Trim();
  211. if (importRowlist[i].ExamsCategoryName != null)
  212. importRowlist[i].ExamsCategoryName = importRowlist[i].ExamsCategoryName.Trim();
  213. if (importRowlist[i].ExaminationModeName != null)
  214. importRowlist[i].ExaminationModeName = importRowlist[i].ExaminationModeName.Trim();
  215. if (importRowlist[i].ExaminationWeekNumString != null)
  216. importRowlist[i].ExaminationWeekNumString = importRowlist[i].ExaminationWeekNumString.Trim();
  217. if (importRowlist[i].ExaminationDateString != null)
  218. importRowlist[i].ExaminationDateString = importRowlist[i].ExaminationDateString.Trim();
  219. if (importRowlist[i].StartTimeString != null)
  220. importRowlist[i].StartTimeString = importRowlist[i].StartTimeString.Trim();
  221. if (importRowlist[i].EndTimeString != null)
  222. importRowlist[i].EndTimeString = importRowlist[i].EndTimeString.Trim();
  223. if (importRowlist[i].ClassroomName != null)
  224. importRowlist[i].ClassroomName = importRowlist[i].ClassroomName.Trim();
  225. if (importRowlist[i].TeacherNames != null)
  226. importRowlist[i].TeacherNames = importRowlist[i].TeacherNames.Trim();
  227. }
  228. #endregion
  229. #region 检查文件中的内容
  230. #region 读取用于校验的数据,便于后续处理
  231. var coursematerialNameList = importRowlist.Select(x => x.CoursematerialName).ToList();
  232. var classNameList = importRowlist.Select(x => x.ClassName).ToList();
  233. //默认排当前学年学期的考试计划
  234. var curSchoolyear = SchoolYearServices.Value.GetCurrentSchoolYear();
  235. var classroomNameList = importRowlist.Select(x => x.ClassroomName).ToList();
  236. var teacherNameList = importRowlist.Where(x => !string.IsNullOrEmpty(x.TeacherNames))
  237. .SelectMany(x => x.TeacherNames.Split('/').ToList()).Distinct().ToList();
  238. var coursematerialList = ExaminationPlanDAL.CoursematerialRepository.GetList(x => coursematerialNameList.Contains(x.CourseName)).ToList();
  239. 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();
  240. var examsCategoryList = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_ExamsCategory);
  241. var examinationModeList = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_ExaminationMode);
  242. var classroomList = ExaminationRoomSettingRepository
  243. .GetList(x => classroomNameList.Contains(x.CF_Classroom.Name))
  244. .Select(x => x.CF_Classroom).ToList();
  245. var classroomViewList = ClassroomDAL.GetClassroomViewQueryable(x => classroomNameList.Contains(x.Name)).ToList();
  246. var teacherList = ExaminationPlanDAL.StaffDAL.staffRepository.GetList(x => teacherNameList.Contains(x.Sys_User.Name), (x => x.Sys_User)).ToList();
  247. #endregion
  248. foreach (var importRow in importRowlist)
  249. {
  250. string errorMessage = "";
  251. var errorItem = Mapper.DynamicMap<AdultExaminationPlanImportView>(importRow);
  252. #region 检查基本录入格式
  253. var curCoursematerialList = coursematerialList.Where(x => x.CourseName == (importRow.CoursematerialName ?? "").Trim()).ToList();
  254. var examsCategory = examsCategoryList.FirstOrDefault(x => x.Name == (importRow.ExamsCategoryName ?? "").Trim());
  255. var examinationMode = examinationModeList.FirstOrDefault(x => x.Name == (importRow.ExaminationModeName ?? "").Trim());
  256. var classroom = classroomList.FirstOrDefault(x => x.Name == (importRow.ClassroomName ?? "").Trim());
  257. var curTeacherNameList = (importRow.TeacherNames ?? "").Trim().Split('/').Where(x => !string.IsNullOrEmpty(x)).ToList();
  258. var teacher = teacherList.Where(x => curTeacherNameList.Contains(x.Sys_User.Name)).ToList();
  259. if (curCoursematerialList.Count == 0)
  260. {
  261. errorMessage += "没有对应的课程名称,请检查输入。";
  262. }
  263. if (examsCategory == null)
  264. {
  265. errorMessage += "考试性质错误,请检查输入。";
  266. }
  267. if (examinationMode == null)
  268. {
  269. errorMessage += "考试方式错误,请检查输入。";
  270. }
  271. if (importRow.StudentCount == null)
  272. {
  273. importRow.StudentCount = importRow.MissionStudentCount;
  274. }
  275. if (string.IsNullOrEmpty(importRow.ClassroomName))
  276. {
  277. errorMessage += "请填写考室。";
  278. }
  279. else if (classroom == null)
  280. {
  281. errorMessage += "考室信息错误,并未将" + importRow.ClassroomName + "安排为考室,请检查输入。";
  282. }
  283. var errorTeacherList = curTeacherNameList.Where(x => !teacher.Select(w => w.Sys_User.Name).Contains(x)).ToList();
  284. if (errorTeacherList.Count > 0)
  285. {
  286. errorMessage += "监考老师" + string.Join("/", errorTeacherList) + "名字输入错误,请检查输入。";
  287. }
  288. else if (teacher.Count == 0)
  289. {
  290. errorMessage += "请填写监考老师信息。";
  291. }
  292. var coursematerialIDList = curCoursematerialList.Select(x => (Guid?)x.CoursematerialID).ToList();
  293. var examinationPlan = examinationPlanList.FirstOrDefault(x => x.ClassName == importRow.ClassName
  294. && coursematerialIDList.Contains(x.CoursematerialID));
  295. if (examsCategory != null && examinationMode != null)
  296. {
  297. examinationPlan = examinationPlanList.FirstOrDefault(x => x.ClassName == importRow.ClassName
  298. && coursematerialIDList.Contains(x.CoursematerialID) && x.ExamsCategoryID == examsCategory.Value
  299. && x.ExaminationModeID == examinationMode.Value);
  300. }
  301. if (examinationPlan == null)
  302. {
  303. examinationPlan = examinationPlanList.FirstOrDefault(x => x.ClassName == importRow.ClassName
  304. && coursematerialIDList.Contains(x.CoursematerialID));
  305. if (examinationPlan == null)
  306. {
  307. errorMessage += "并没有该班级名称和课程名称的考试安排,请检查输入。";
  308. }
  309. }
  310. else if (examinationPlan.RecordStatus == (int)CF_ApprovalStatus.Submitted)
  311. {
  312. errorMessage += "已存在相同的已提交的考试计划";
  313. }
  314. if (classroom != null && examinationPlan != null && classroom.Examinationseating < examinationPlan.CF_Student.Count)
  315. {
  316. errorMessage += "所选考室可容纳的座位数不足,无法安排学生进行考试。";
  317. }
  318. DateTime examinationDate = default(DateTime);
  319. if (string.IsNullOrEmpty(importRow.ExaminationDateString))
  320. {
  321. errorMessage += "请填写考试日期。";
  322. }
  323. else
  324. {
  325. try
  326. {
  327. DateTime date;
  328. if (DateTime.TryParse(importRow.ExaminationDateString.Trim().ToString(), out date))
  329. {
  330. examinationDate = Convert.ToDateTime(importRow.ExaminationDateString.Trim().ToString());
  331. }
  332. else
  333. {
  334. var dateTime = DateTime.FromOADate(double.Parse(importRow.ExaminationDateString.Trim().ToString()));
  335. examinationDate = dateTime;
  336. }
  337. }
  338. catch
  339. {
  340. errorMessage += "考试日期必须为年-月-日的日期格式。";
  341. }
  342. }
  343. TimeSpan startTime = default(TimeSpan);
  344. if (string.IsNullOrEmpty(importRow.StartTimeString))
  345. {
  346. errorMessage += "请填写考试时间起。";
  347. }
  348. else
  349. {
  350. try
  351. {
  352. DateTime date;
  353. if (DateTime.TryParse(importRow.StartTimeString.Trim().ToString(), out date))
  354. {
  355. startTime = Convert.ToDateTime(importRow.StartTimeString.Trim().ToString()).TimeOfDay;
  356. }
  357. else
  358. {
  359. var dateTime = DateTime.FromOADate(double.Parse(importRow.StartTimeString.Trim().ToString()));
  360. startTime = dateTime.TimeOfDay;
  361. }
  362. }
  363. catch
  364. {
  365. errorMessage += "考试时间起必须为时:分的时间格式。";
  366. }
  367. }
  368. TimeSpan endTime = default(TimeSpan);
  369. if (string.IsNullOrEmpty(importRow.EndTimeString))
  370. {
  371. errorMessage += "请填写考试时间止。";
  372. }
  373. else
  374. {
  375. try
  376. {
  377. DateTime date;
  378. if (DateTime.TryParse(importRow.EndTimeString.Trim().ToString(), out date))
  379. {
  380. endTime = Convert.ToDateTime(importRow.EndTimeString.Trim().ToString()).TimeOfDay;
  381. }
  382. else
  383. {
  384. var dateTime = DateTime.FromOADate(double.Parse(importRow.EndTimeString.Trim().ToString()));
  385. endTime = dateTime.TimeOfDay;
  386. }
  387. }
  388. catch
  389. {
  390. errorMessage += "考试时间止必须为时:分的时间格式。";
  391. }
  392. }
  393. if (startTime >= endTime)
  394. {
  395. errorMessage += "结束时间需大于开始时间。";
  396. }
  397. #endregion
  398. if (errorMessage != "")
  399. {
  400. errorItem.ErrorMessage = errorMessage;
  401. if (endTime != default(TimeSpan))
  402. {
  403. errorItem.EndTimeString = " " + endTime;
  404. }
  405. if (startTime != default(TimeSpan))
  406. {
  407. errorItem.StartTimeString = " " + startTime;
  408. }
  409. if (examinationDate != default(DateTime))
  410. {
  411. errorItem.ExaminationDateString = " " + examinationDate;
  412. }
  413. errList.Add(errorItem);
  414. continue;
  415. }
  416. else
  417. {
  418. importRow.CoursematerialID = examinationPlan.CoursematerialID;
  419. importRow.ExaminationPlan = examinationPlan;
  420. importRow.ExamsCategoryID = examsCategory.Value;
  421. importRow.ExaminationModeID = examinationMode.Value;
  422. importRow.ExaminationStyleID = examinationPlan.ExaminationStyleID;
  423. importRow.ExaminationDate = examinationDate;
  424. importRow.StartTime = startTime;
  425. importRow.EndTime = endTime;
  426. importRow.ClassroomID = classroom.ClassroomID;
  427. importRow.Examinationseating = classroom.Examinationseating;
  428. importRow.TeacherList = teacher;
  429. successList.Add(importRow);
  430. }
  431. }
  432. var checkList = successList;
  433. #endregion
  434. #region 准备排课数据用于校验
  435. var scheduleDateTimeList = successList.GroupBy(x => x.ExaminationPlan.SchoolyearID)
  436. .Select(x => new
  437. {
  438. SchoolyearID = x.Key,
  439. DataTimeList = x.GroupBy(w => new
  440. {
  441. StartTime = w.ExaminationDate.Value.Add(w.StartTime.Value),
  442. EndTime = w.ExaminationDate.Value.Add(w.EndTime.Value)
  443. })
  444. .Select(w => new StartEndTimeView { StartTime = w.Key.StartTime, EndTime = w.Key.EndTime }).ToList()
  445. }).ToList();
  446. List<EducationSchedulingWeekNumView> schedulingList = new List<EducationSchedulingWeekNumView>();
  447. List<ClassroomExcessiveUseWeekNumView> excessiveUseList = new List<ClassroomExcessiveUseWeekNumView>();
  448. scheduleDateTimeList.ForEach(x =>
  449. {
  450. schedulingList.AddRange(this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewForExaminationTeacher(x.SchoolyearID.Value,
  451. x.DataTimeList));
  452. excessiveUseList.AddRange(this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList(
  453. x.SchoolyearID.Value, x.DataTimeList));
  454. });
  455. #endregion
  456. #region 检查导入文件内部数据的冲突
  457. #region 检查考室冲突
  458. //var classroomConflictList = (from source in successList
  459. // from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  460. // && x.ExaminationDate == source.ExaminationDate
  461. // && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  462. // && x.ClassroomID == source.ClassroomID)
  463. // select new { source, dest }).ToList();
  464. //classroomConflictList.ForEach(x =>
  465. //{
  466. // var errorItem = x.source;
  467. // errorItem.ErrorMessage = "考场安排和其他行存在时间冲突,请检查。";
  468. // errList.Add(errorItem);
  469. // successList.Remove(x.source);
  470. //});
  471. #endregion
  472. #region 检查导入文件导入同一考室,并且时间重复的,人数是否超过可排人次
  473. var classroomstudentCount = (from source in successList
  474. from studentcount in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  475. && x.ExaminationDate == source.ExaminationDate
  476. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  477. && x.ClassroomID == source.ClassroomID)
  478. group studentcount by studentcount.ExaminationPlan.ExaminationPlanID
  479. into gstucount
  480. join succ in successList
  481. on gstucount.Key equals succ.ExaminationPlan.ExaminationPlanID
  482. select new
  483. {
  484. //source,
  485. succ
  486. }).ToList();
  487. classroomstudentCount.ForEach(x =>
  488. {
  489. var count = int.Parse(x.succ.MissionStudentCount);
  490. var isrepeatList = checkList.Any(y => y.ExaminationPlan.ExaminationPlanID != x.succ.ExaminationPlan.ExaminationPlanID && y.ExaminationDate == x.succ.ExaminationDate
  491. && y.StartTime <= x.succ.EndTime && y.EndTime >= x.succ.StartTime && y.ClassroomID == x.succ.ClassroomID);
  492. if (isrepeatList)
  493. {
  494. var repeatList = checkList.Where(y => y.ExaminationPlan.ExaminationPlanID != x.succ.ExaminationPlan.ExaminationPlanID && y.ExaminationDate == x.succ.ExaminationDate
  495. && y.StartTime <= x.succ.EndTime && y.EndTime >= x.succ.StartTime && y.ClassroomID == x.succ.ClassroomID).ToList();
  496. foreach (var repeat in repeatList)
  497. {
  498. count = count + int.Parse(repeat.MissionStudentCount);
  499. }
  500. }
  501. if (count > classroomList.FirstOrDefault(w => w.ClassroomID == x.succ.ClassroomID).Examinationseating)
  502. {
  503. var errorItem = x.succ;
  504. errorItem.ErrorMessage = "考场座位数不足,请检查。";
  505. errList.Add(errorItem);
  506. successList.Remove(x.succ);
  507. }
  508. });
  509. #endregion
  510. #region 检查教师冲突
  511. var teacherConflictList = (from source in successList
  512. from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  513. && x.ExaminationDate == source.ExaminationDate
  514. && x.ClassroomID != source.ClassroomID
  515. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  516. && x.TeacherList.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() > 0).Take(1)
  517. select source).ToList();
  518. teacherConflictList.ForEach(x =>
  519. {
  520. var errorItem = x;
  521. errorItem.ErrorMessage = "监考老师和其他行存在时间冲突,请检查。";
  522. errList.Add(errorItem);
  523. successList.Remove(x);
  524. });
  525. var teacherClassroomConflictList = (from source in successList
  526. from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  527. && x.ExaminationDate == source.ExaminationDate
  528. && x.ClassroomID == source.ClassroomID
  529. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  530. && x.TeacherList.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() != x.TeacherList.Count).Take(1)
  531. select source).ToList();
  532. teacherClassroomConflictList.ForEach(x =>
  533. {
  534. var errorItem = x;
  535. errorItem.ErrorMessage = "同一考场的监考老师必须相同,请检查考室或监考老师是否存在冲突。";
  536. errList.Add(errorItem);
  537. successList.Remove(x);
  538. });
  539. #endregion
  540. #region 检查学生冲突
  541. var studentConflictList = (from source in successList
  542. from dest in successList.Where(x => x.ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  543. && x.ExaminationDate == source.ExaminationDate
  544. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  545. && x.ExaminationPlan.CF_Student.Select(w => w.UserID).Intersect(source.ExaminationPlan.CF_Student.Select(w => w.UserID)).Count() > 0)
  546. select new { source, dest }).ToList();
  547. studentConflictList.ForEach(x =>
  548. {
  549. var errorItem = x.source;
  550. errorItem.ErrorMessage = "考试学生和其他行存在时间冲突,请检查。";
  551. errList.Add(errorItem);
  552. successList.Remove(x.source);
  553. });
  554. #endregion
  555. #endregion
  556. #region 检查和其他考场安排之间的冲突
  557. var examinationDateList = successList.Select(x => x.ExaminationDate).Distinct().ToList();
  558. var sameDateExaminationPlanQuery = ExaminationPlanDAL.ExaminationRoomLayoutRepository
  559. .GetList(x => examinationDateList.Contains(x.EX_ExaminationPlan.ExaminationDate),
  560. (x => x.EX_ExaminationPlan),
  561. (x => x.EX_ExaminationPlan.CF_Student),
  562. (x => x.CF_Staff),
  563. (x => x.EX_ExaminationRoomStudent));
  564. var sameDateExaminationPlanList = sameDateExaminationPlanQuery.ToList();
  565. #region 检查考室冲突
  566. //var classroomOuterConflictList = (from source in successList
  567. // from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  568. // && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate
  569. // && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime
  570. // && x.ClassroomID == source.ClassroomID)
  571. // select new { source, dest }).ToList();
  572. //classroomOuterConflictList.ForEach(x =>
  573. //{
  574. // var errorItem1 = x.source;
  575. // errorItem1.ErrorMessage = "考场安排和已有的考试安排存在时间冲突,请检查。";
  576. // errList.Add(errorItem1);
  577. // successList.Remove(x.source);
  578. //});
  579. #endregion
  580. #region 检查教师冲突
  581. var teacherOuterConflictList = (from source in successList
  582. from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  583. && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate
  584. && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime
  585. && x.ClassroomID != source.ClassroomID
  586. && x.CF_Staff.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() > 0).Take(1)
  587. select new { source, dest }).ToList();
  588. teacherOuterConflictList.ForEach(x =>
  589. {
  590. var errorItem1 = x.source;
  591. errorItem1.ErrorMessage = "监考老师和已有的考试安排存在时间冲突,请检查。";
  592. errList.Add(errorItem1);
  593. successList.Remove(x.source);
  594. });
  595. var teacherOuterClassroomConflictList = (from source in successList
  596. from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  597. && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate
  598. && x.ClassroomID == source.ClassroomID
  599. && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime
  600. && x.CF_Staff.Select(w => w.UserID).Intersect(source.TeacherList.Select(w => w.UserID)).Count() != x.CF_Staff.Count).Take(1)
  601. select source).ToList();
  602. teacherOuterClassroomConflictList.ForEach(x =>
  603. {
  604. var errorItem = x;
  605. errorItem.ErrorMessage = "监考老师和已有的考试安排存在冲突,同一考场的监考老师必须相同,请检查。";
  606. errList.Add(errorItem);
  607. successList.Remove(x);
  608. });
  609. #endregion
  610. #region 检查学生冲突
  611. var studentOuterConflictList = (from source in successList
  612. from dest in sameDateExaminationPlanList.Where(x => x.EX_ExaminationPlan.ExaminationPlanID != source.ExaminationPlan.ExaminationPlanID
  613. && x.EX_ExaminationPlan.ExaminationDate == source.ExaminationDate
  614. && x.EX_ExaminationPlan.StartTime <= source.EndTime && x.EX_ExaminationPlan.EndTime >= source.StartTime
  615. && x.EX_ExaminationPlan.CF_Student.Select(w => w.UserID).Intersect(source.ExaminationPlan.CF_Student.Select(w => w.UserID)).Count() > 0)
  616. select new { source, dest }).ToList();
  617. studentOuterConflictList.ForEach(x =>
  618. {
  619. var errorItem = x.source;
  620. errorItem.ErrorMessage = "考试学生和已有的考试安排存在时间冲突,请检查。";
  621. errList.Add(errorItem);
  622. successList.Remove(x.source);
  623. });
  624. #endregion
  625. #endregion
  626. #region 检查教室占用冲突
  627. var classroomExcessUseConflictList = (from source in successList
  628. from dest in excessiveUseList.Where(x =>
  629. x.Date == source.ExaminationDate
  630. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  631. && x.ClassroomID == source.ClassroomID).Take(1)
  632. select new { source, dest }).ToList();
  633. classroomExcessUseConflictList.ForEach(x =>
  634. {
  635. var errorItem2 = x.source;
  636. errorItem2.ErrorMessage = "考场已被占用,请检查。";
  637. errList.Add(errorItem2);
  638. successList.Remove(x.source);
  639. });
  640. #endregion
  641. #region 检查和课表的冲突
  642. #region 检查考室冲突
  643. var classroomScheduleConflictList = (from source in successList
  644. from dest in schedulingList.Where(x =>
  645. x.Date == source.ExaminationDate
  646. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  647. && x.ClassroomID == source.ClassroomID).Take(1)
  648. select new { source, dest }).ToList();
  649. classroomScheduleConflictList.ForEach(x =>
  650. {
  651. var errorItem2 = x.source;
  652. errorItem2.ErrorMessage = "考场安排和已有的排课存在时间冲突,请检查。";
  653. errList.Add(errorItem2);
  654. successList.Remove(x.source);
  655. });
  656. #endregion
  657. #region 检查教师冲突
  658. var teacherScheduleConflictList = (from source in successList
  659. from dest in schedulingList.Where(x =>
  660. x.Date == source.ExaminationDate
  661. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  662. && x.TeacherList.Select(w => w.UserID)
  663. .Intersect(source.TeacherList.Select(w => (Guid?)w.UserID)).Count() > 0).Take(1)
  664. select new { source, dest }).ToList();
  665. teacherScheduleConflictList.ForEach(x =>
  666. {
  667. var errorItem1 = x.source;
  668. errorItem1.ErrorMessage = "监考老师和已有的排课存在时间冲突,请检查。";
  669. errList.Add(errorItem1);
  670. successList.Remove(x.source);
  671. });
  672. #endregion
  673. #region 检查学生冲突
  674. var studentScheduleConflictList = (from source in successList
  675. from dest in schedulingList.Where(x =>
  676. x.Date == source.ExaminationDate
  677. && x.StartTime <= source.EndTime && x.EndTime >= source.StartTime
  678. && x.StudentList.Select(w => w.UserID).Intersect(source.ExaminationPlan.CF_Student.Select(w => w.UserID)).Count() > 0).Take(1)
  679. select new { source, dest }).ToList();
  680. studentScheduleConflictList.ForEach(x =>
  681. {
  682. var errorItem = x.source;
  683. errorItem.ErrorMessage = "考试学生和已有的排课存在时间冲突,请检查。";
  684. errList.Add(errorItem);
  685. successList.Remove(x.source);
  686. });
  687. #endregion
  688. #endregion
  689. #region 添加数据
  690. var scheduledStudentList = new List<StudentClassroomView>();
  691. var deletedExaminationPlanIDList = new List<Guid>();
  692. foreach (var succesResult in successList)
  693. {
  694. succesResult.ExaminationPlan.ExaminationDate = succesResult.ExaminationDate;
  695. succesResult.ExaminationPlan.StartTime = succesResult.StartTime;
  696. succesResult.ExaminationPlan.EndTime = succesResult.EndTime;
  697. succesResult.ExaminationStyleID = succesResult.ExaminationStyleID;
  698. //succesResult.ExaminationPlan.EX_ExaminationRoomLayout.ForEach(x => this.UnitOfWork.Remove(x));
  699. //succesResult.ExaminationPlan.EX_ExaminationRoomLayout = new HashSet<EX_ExaminationRoomLayout>();
  700. EX_ExaminationRoomLayout newLayout = new EX_ExaminationRoomLayout()
  701. {
  702. ExaminationRoomLayoutID = Guid.NewGuid(),
  703. ExaminationPlanID = succesResult.ExaminationPlan.ExaminationPlanID,
  704. ClassroomID = succesResult.ClassroomID,
  705. RowSpacing = 0,
  706. ColumnSpacing = 0,
  707. StudentOrderType = (int)CF_StudentOrderType.StudentNo,
  708. CF_Staff = new HashSet<CF_Staff>(succesResult.TeacherList)
  709. };
  710. this.SetNewStatus(newLayout);
  711. var roomRemainSeat = succesResult.Examinationseating - sameDateExaminationPlanList
  712. .Where(x => x.EX_ExaminationPlan != null
  713. && x.EX_ExaminationPlan.ExaminationPlanID != succesResult.ExaminationPlan.ExaminationPlanID
  714. && x.EX_ExaminationPlan.ExaminationDate == succesResult.ExaminationDate
  715. && x.ClassroomID == succesResult.ClassroomID
  716. && x.EX_ExaminationPlan.StartTime <= succesResult.EndTime
  717. && x.EX_ExaminationPlan.EndTime >= succesResult.StartTime).Sum(x => x.EX_ExaminationRoomStudent.Count)
  718. - scheduledStudentList.Where(x => x.ClassroomID == succesResult.ClassroomID
  719. && x.ExaminationDate == succesResult.ExaminationDate
  720. && x.StartTime <= succesResult.EndTime
  721. && x.EndTime >= succesResult.StartTime)
  722. .Count();
  723. if (roomRemainSeat <= succesResult.ExaminationPlan.CF_Student.Count)
  724. {
  725. var errorItem = succesResult;
  726. errorItem.ErrorMessage = "考场剩余座位不足,无法安排考试,请检查考场的使用情况。";
  727. errList.Add(errorItem);
  728. continue;
  729. }
  730. var studentList = (from s in succesResult.ExaminationPlan.CF_Student
  731. join ss in scheduledStudentList.Where(x => x.ExaminationDate == succesResult.ExaminationDate
  732. && x.StartTime <= succesResult.EndTime
  733. && x.EndTime >= succesResult.StartTime)
  734. on s.UserID equals ss.UserID into dss
  735. from ess in dss.DefaultIfEmpty()
  736. where ess == null
  737. select s).ToList();
  738. var examinationRoomStudentViewList = studentList.Take(roomRemainSeat ?? 0)
  739. .Select(x => new ExaminationRoomStudentView
  740. {
  741. ExaminationRoomStudentID = Guid.NewGuid(),
  742. ExaminationRoomLayoutID = newLayout.ExaminationRoomLayoutID,
  743. UserID = x.UserID,
  744. LoginID = x.Sys_User.LoginID,
  745. Remark = ""
  746. }).ToList();
  747. var classroomView = classroomViewList.FirstOrDefault(x => x.ClassroomID == succesResult.ClassroomID);
  748. var examinationRoomStudentList = this.SetRoomStudentSeats(examinationRoomStudentViewList, classroomView, 0, 0, CF_StudentOrderType.StudentNo);
  749. if (!deletedExaminationPlanIDList.Contains(succesResult.ExaminationPlan.ExaminationPlanID))
  750. {
  751. UnitOfWork.Remove<EX_ExaminationRoomStudent>(x => x.EX_ExaminationRoomLayout.ExaminationPlanID == succesResult.ExaminationPlan.ExaminationPlanID);
  752. UnitOfWork.Remove<EX_ExaminationRoomLayout>(x => x.ExaminationPlanID == succesResult.ExaminationPlan.ExaminationPlanID);
  753. deletedExaminationPlanIDList.Add(succesResult.ExaminationPlan.ExaminationPlanID);
  754. }
  755. newLayout.EX_ExaminationRoomStudent = new HashSet<EX_ExaminationRoomStudent>(examinationRoomStudentList);
  756. scheduledStudentList.AddRange(newLayout.EX_ExaminationRoomStudent.Select(x => new StudentClassroomView
  757. {
  758. ExaminationDate = succesResult.ExaminationDate,
  759. StartTime = succesResult.StartTime,
  760. EndTime = succesResult.EndTime,
  761. ClassroomID = newLayout.ClassroomID,
  762. UserID = x.UserID
  763. }));
  764. this.UnitOfWork.Add(newLayout);
  765. succesResult.ExaminationPlan.EX_ExaminationRoomLayout.Add(newLayout);
  766. }
  767. #endregion
  768. this.UnitOfWork.Commit();
  769. returnMessage = string.Format("提示:成功导入{0}条记录,失败{1}条,详情请点击错误数据下载查看。",
  770. (importRowlist.Count - errList.Count), errList.Count);
  771. if (errList.Count > 0)
  772. {
  773. return NpoiExcelHelper.EntityListToExcel2003(cellheader, errList, "导入问题列表", "");
  774. }
  775. else
  776. {
  777. return "";
  778. }
  779. }
  780. /// <summary>
  781. /// 松山逻辑,提交时需要判断和课表或教室占用的冲突,判断课表主要是因为在排考的同时可能因为调课操作导致课表变动
  782. /// </summary>
  783. /// <param name="examinationPlanIDList"></param>
  784. public override void Submit(IList<Guid?> examinationPlanIDList)
  785. {
  786. var examinationPlanList = examinationPlanRepository.GetList(x => examinationPlanIDList.Contains(x.ExaminationPlanID),
  787. (x => x.EX_ExaminationRoomLayout)).ToList();
  788. var examinationPlanLayoutList = ExaminationPlanDAL.ExaminationRoomLayoutRepository
  789. .GetList(x => examinationPlanIDList.Contains(x.ExaminationPlanID),
  790. (x => x.CF_Staff.Select(w => w.Sys_User)),
  791. (x => x.EX_ExaminationRoomStudent.Select(w => w.CF_Student.Sys_User)),
  792. (x => x.CF_Classroom));
  793. var submitedPlan = examinationPlanList.Where(x => x.RecordStatus != (int)CF_ApprovalStatus.NotSubmitted).ToList();
  794. if (submitedPlan.Count > 0)
  795. {
  796. string submitedClassNames = string.Join(",", submitedPlan.Select(x => x.ClassName)) + "并不在未提交状态,请选择正确的记录进行提交。";
  797. throw new Exception(submitedClassNames);
  798. }
  799. var noLayoutPlanList = (from ep in examinationPlanList
  800. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID into depl
  801. from eepl in depl.DefaultIfEmpty()
  802. where eepl == null
  803. select ep).ToList();
  804. if (noLayoutPlanList.Count > 0)
  805. {
  806. string noLayoutPlanClassNames = string.Join(",", noLayoutPlanList.Select(x => x.ClassName)) + "没有设置对应的考场,请先设置再进行提交。";
  807. throw new Exception(noLayoutPlanClassNames);
  808. }
  809. var noTeacherPlanList = (from ep in examinationPlanList
  810. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID
  811. from s in epl.CF_Staff.DefaultIfEmpty()
  812. where s == null
  813. select new { Plan = ep, Classroom = epl.CF_Classroom }).ToList();
  814. if (noTeacherPlanList.Count > 0)
  815. {
  816. string noTeacherPlanClassNames = string.Join(";", noTeacherPlanList.Select(x => x.Plan.ClassName + ",考场" + x.Classroom.Name)) + "没有设置监考老师,请先设置再进行提交。";
  817. throw new Exception(noTeacherPlanClassNames);
  818. }
  819. var noStudentPlanList = (from ep in examinationPlanList
  820. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID
  821. from s in epl.EX_ExaminationRoomStudent.DefaultIfEmpty()
  822. where s == null
  823. select new { Plan = ep, Classroom = epl.CF_Classroom }).ToList();
  824. if (noStudentPlanList.Count > 0)
  825. {
  826. string noStudentPlanClassNames = string.Join(";", noStudentPlanList.Select(x => x.Plan.ClassName + ",考场" + x.Classroom.Name)) + "没有设置学生,请先设置再进行提交。";
  827. throw new Exception(noStudentPlanClassNames);
  828. }
  829. var scheduleDateTimeList = examinationPlanList.GroupBy(x => x.SchoolyearID)
  830. .Select(x => new
  831. {
  832. SchoolyearID = x.Key,
  833. DataTimeList = x.GroupBy(w => new
  834. {
  835. StartTime = w.ExaminationDate.Value.Add(w.StartTime.Value),
  836. EndTime = w.ExaminationDate.Value.Add(w.EndTime.Value)
  837. })
  838. .Select(w => new StartEndTimeView { StartTime = w.Key.StartTime, EndTime = w.Key.EndTime }).ToList()
  839. }).ToList();
  840. List<EducationSchedulingWeekNumView> schedulingList = new List<EducationSchedulingWeekNumView>();
  841. List<ClassroomExcessiveUseWeekNumView> excessiveUseList = new List<ClassroomExcessiveUseWeekNumView>();
  842. scheduleDateTimeList.ForEach(x =>
  843. {
  844. schedulingList.AddRange(this.ScheduleServices.Value.GetEducationSchedulingWeekNumViewForExaminationTeacher(x.SchoolyearID.Value,
  845. x.DataTimeList));
  846. excessiveUseList.AddRange(this.ClassroomExcessiveUseServices.Value.GetClassroomExcessiveUseWeekNumViewByDateTimeList(
  847. x.SchoolyearID.Value, x.DataTimeList));
  848. });
  849. #region 检查教室占用冲突
  850. var classroomExcessUseConflictList = (from ep in examinationPlanList
  851. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID
  852. from dest in excessiveUseList.Where(x => x.Date == ep.ExaminationDate
  853. && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime
  854. && epl.ClassroomID == x.ClassroomID).Take(1)
  855. group epl by new { ep.ExaminationPlanID, ep.ClassName } into g
  856. select g.Key.ClassName + "的教室" + string.Join("、", g.Select(w => w.CF_Classroom.Name))).ToList();
  857. if (classroomExcessUseConflictList.Count > 0)
  858. {
  859. throw new Exception(string.Join(";", classroomExcessUseConflictList) + "被占用,无法提交。");
  860. }
  861. #endregion
  862. #region 检查和课表的冲突
  863. #region 检查考室冲突
  864. var classroomScheduleConflictList = (from ep in examinationPlanList
  865. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID
  866. from dest in schedulingList.Where(x =>
  867. x.Date == ep.ExaminationDate
  868. && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime
  869. && x.ClassroomID == epl.ClassroomID).Take(1)
  870. group epl by new { ep.ExaminationPlanID, ep.ClassName } into g
  871. select g.Key.ClassName + "的教室" + string.Join("、", g.Select(w => w.CF_Classroom.Name))).ToList();
  872. if (classroomScheduleConflictList.Count > 0)
  873. {
  874. throw new Exception(string.Join(";", classroomScheduleConflictList) + "和已有的排课存在时间冲突,请检查。");
  875. }
  876. #endregion
  877. #region 检查教师冲突
  878. var teacherScheduleConflictList = (from ep in examinationPlanList
  879. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID
  880. from s in epl.CF_Staff
  881. from dest in schedulingList.Where(x =>
  882. x.Date == ep.ExaminationDate
  883. && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime
  884. && x.TeacherList.Select(w => w.UserID).Contains(s.UserID)).Take(1)
  885. group s by new { ep.ExaminationPlanID, ep.ClassName } into g
  886. select g.Key.ClassName + "的监考老师" + string.Join("、", g.Select(w => w.Sys_User.Name))).ToList();
  887. if (teacherScheduleConflictList.Count > 0)
  888. {
  889. throw new Exception(string.Join("、", teacherScheduleConflictList) + "和已有的排课存在时间冲突,请检查。");
  890. }
  891. #endregion
  892. #region 检查学生冲突
  893. var studentScheduleConflictList = (from ep in examinationPlanList
  894. join epl in examinationPlanLayoutList on ep.ExaminationPlanID equals epl.ExaminationPlanID
  895. from s in epl.EX_ExaminationRoomStudent
  896. from dest in schedulingList.Where(x =>
  897. x.Date == ep.ExaminationDate
  898. && x.StartTime <= ep.EndTime && x.EndTime >= ep.StartTime
  899. && x.StudentList.Select(w => (Guid?)w.UserID).Contains(s.UserID)).Take(1)
  900. group s by new { ep.ExaminationPlanID, ep.ClassName } into g
  901. select g.Key.ClassName + "的考试学生" + string.Join("、", g.Select(w => w.CF_Student.Sys_User.Name))).ToList();
  902. if (studentScheduleConflictList.Count > 0)
  903. {
  904. throw new Exception(string.Join("、", studentScheduleConflictList) + "和已有的排课存在时间冲突,请检查。");
  905. }
  906. #endregion
  907. #endregion
  908. if (examinationPlanIDList.Count > 0)
  909. {
  910. UnitOfWork.Update<EX_ExaminationPlan>((x => new EX_ExaminationPlan { RecordStatus = (int)CF_ApprovalStatus.Submitted }), (x => examinationPlanIDList.Contains(x.ExaminationPlanID)));
  911. }
  912. }
  913. }
  914. }