ClassroomExcessiveUseServices.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using Bowin.Common.Linq;
  7. using Bowin.Common.Linq.Entity;
  8. using EMIS.ViewModel.EducationSchedule;
  9. using EMIS.ViewModel;
  10. using EMIS.DataLogic.EducationSchedule;
  11. using EMIS.Entities;
  12. using EMIS.ViewModel.CalendarManage;
  13. using EMIS.DataLogic.Common.CalendarManage;
  14. using EMIS.CommonLogic.CalendarManage;
  15. using System.Globalization;
  16. namespace EMIS.CommonLogic.EducationSchedule
  17. {
  18. public class ClassroomExcessiveUseServices : BaseServices, IClassroomExcessiveUseServices
  19. {
  20. public ClassroomExcessiveUseDAL ClassroomExcessiveUseDAL { get; set; }
  21. public Lazy<IScheduleServices> ScheduleServices { get; set; }
  22. public Lazy<ISchoolYearServices> SchoolYearServices { get; set; }
  23. public CoursesTimeDAL coursesTimeDAL { get; set; }
  24. public ISchoolYearServices schoolYearServices { get; set; }
  25. public IGridResultSet<ClassroomExcessiveUseView> GetClassroomExcessiveUseViewList(ConfiguretView configuretView, Guid? schoolyearID,
  26. int? weekNum, int? weekday, Guid? courseTimeID, Guid? buildingID, Guid? classroomID, Guid? collegeID, int? pageIndex, int? pageSize)
  27. {
  28. Expression<Func<ES_ClassroomExcessiveUse, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  29. Expression<Func<CF_Classroom, bool>> classroomExp = (x => true);
  30. if (schoolyearID.HasValue)
  31. {
  32. exp = exp.And(x => x.SchoolyearID == schoolyearID);
  33. }
  34. if (weekNum.HasValue)
  35. {
  36. exp = exp.And(x => x.ES_ClassroomExcessiveUseScheduling.Any(w => w.ES_ClassroomExcessiveUseSchedulingWeekNum.Any(n => n.WeekNum == weekNum)));
  37. }
  38. if (weekday.HasValue)
  39. {
  40. exp = exp.And(x => x.ES_ClassroomExcessiveUseScheduling.Any(w => w.Weekday == weekday));
  41. }
  42. if (courseTimeID.HasValue)
  43. {
  44. exp = exp.And(x => x.ES_ClassroomExcessiveUseScheduling.Any(w => w.CoursesTimeID == courseTimeID));
  45. }
  46. if (buildingID.HasValue)
  47. {
  48. classroomExp = classroomExp.And(x => x.BuildingsInfoID == buildingID);
  49. }
  50. if (classroomID.HasValue)
  51. {
  52. classroomExp = classroomExp.And(x => x.ClassroomID == classroomID);
  53. }
  54. if (collegeID.HasValue)
  55. {
  56. exp = exp.And(x => x.CollegeID == collegeID);
  57. }
  58. var query = ClassroomExcessiveUseDAL.GetClassroomExcessiveUseViewQueryable(exp, classroomExp);
  59. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  60. {
  61. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  62. }
  63. query = this.GetQueryByDataRangeByCollege(query);
  64. var resultList = query.OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.CreateTime)
  65. .ToGridResultSet(pageIndex, pageSize);
  66. var resultIDList = resultList.rows.Select(x => x.ClassroomExcessiveUseID).ToList();
  67. var schedulingList = ClassroomExcessiveUseDAL.GetClassroomExcessiveUseSchedulingViewQueryable((x => resultIDList.Contains(x.ClassroomExcessiveUseID)), (x => true)).ToList();
  68. resultList.rows.ForEach(x => x.Scheduling = schedulingList.Where(w => w.ClassroomExcessiveUseID == x.ClassroomExcessiveUseID).ToList());
  69. return resultList;
  70. }
  71. public List<ClassroomExcessiveUseView> GetClassroomExcessiveUseViewList(ConfiguretView configuretView, Guid? schoolyearID,
  72. int? weekNum, int? weekday, Guid? courseTimeID, Guid? buildingID, Guid? classroomID, Guid? collegeID)
  73. {
  74. Expression<Func<ES_ClassroomExcessiveUse, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  75. Expression<Func<CF_Classroom, bool>> classroomExp = (x => true);
  76. if (schoolyearID.HasValue)
  77. {
  78. exp = exp.And(x => x.SchoolyearID == schoolyearID);
  79. }
  80. if (weekNum.HasValue)
  81. {
  82. exp = exp.And(x => x.ES_ClassroomExcessiveUseScheduling.Any(w => w.ES_ClassroomExcessiveUseSchedulingWeekNum.Any(n => n.WeekNum == weekNum)));
  83. }
  84. if (weekday.HasValue)
  85. {
  86. exp = exp.And(x => x.ES_ClassroomExcessiveUseScheduling.Any(w => w.Weekday == weekday));
  87. }
  88. if (courseTimeID.HasValue)
  89. {
  90. exp = exp.And(x => x.ES_ClassroomExcessiveUseScheduling.Any(w => w.CoursesTimeID == courseTimeID));
  91. }
  92. if (buildingID.HasValue)
  93. {
  94. classroomExp = classroomExp.And(x => x.BuildingsInfoID == buildingID);
  95. }
  96. if (classroomID.HasValue)
  97. {
  98. classroomExp = classroomExp.And(x => x.ClassroomID == classroomID);
  99. }
  100. if (collegeID.HasValue)
  101. {
  102. exp = exp.And(x => x.CollegeID == collegeID);
  103. }
  104. var query = ClassroomExcessiveUseDAL.GetClassroomExcessiveUseViewQueryable(exp, classroomExp);
  105. var schedulingQuery = ClassroomExcessiveUseDAL.GetClassroomExcessiveUseSchedulingViewQueryable(exp, classroomExp);
  106. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  107. {
  108. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  109. }
  110. query = this.GetQueryByDataRangeByCollege(query);
  111. schedulingQuery = this.GetQueryByDataRangeByCollege(schedulingQuery);
  112. var resultList = query.OrderByDescending(x => x.SchoolyearCode).ThenByDescending(x => x.CreateTime)
  113. .ToList();
  114. var schedulingList = schedulingQuery.ToList();
  115. resultList.ForEach(x => x.Scheduling = schedulingList.Where(w => w.ClassroomExcessiveUseID == x.ClassroomExcessiveUseID).ToList());
  116. return resultList;
  117. }
  118. public void Delete(IList<Guid> classroomExcessiveUseIDList)
  119. {
  120. this.UnitOfWork.Delete<ES_ClassroomExcessiveUseSchedulingWeekNum>(x => classroomExcessiveUseIDList.Contains(x.ES_ClassroomExcessiveUseScheduling.ClassroomExcessiveUseID.Value));
  121. this.UnitOfWork.Delete<ES_ClassroomExcessiveUseScheduling>(x => classroomExcessiveUseIDList.Contains(x.ClassroomExcessiveUseID.Value));
  122. this.UnitOfWork.Delete<ES_ClassroomExcessiveUse>(x => classroomExcessiveUseIDList.Contains(x.ClassroomExcessiveUseID));
  123. }
  124. public ClassroomExcessiveUseView GetClassroomExcessiveUseView(Guid classroomExcessiveUseID)
  125. {
  126. var query = ClassroomExcessiveUseDAL.GetClassroomExcessiveUseViewQueryable((x => x.ClassroomExcessiveUseID == classroomExcessiveUseID), (x => true));
  127. var result = query.FirstOrDefault();
  128. return result;
  129. }
  130. public List<ClassroomExcessiveUseSchedulingView> GetClassroomExcessiveUseSchedulingViewList(Guid classroomExcessiveUseID)
  131. {
  132. return ClassroomExcessiveUseDAL.GetClassroomExcessiveUseSchedulingViewQueryable(x => x.ClassroomExcessiveUseID == classroomExcessiveUseID, (x => true)).ToList();
  133. }
  134. public void Save(ClassroomExcessiveUseView classroomExcessiveUseView, List<ClassroomExcessiveUseSchedulingView> classroomExcessiveUseSchedulingViewList)
  135. {
  136. ES_ClassroomExcessiveUse classroomExcessiveUse = new ES_ClassroomExcessiveUse();
  137. if (!classroomExcessiveUseView.SchoolyearID.HasValue)
  138. {
  139. throw new Exception("学年学期不能为空。");
  140. }
  141. if (!classroomExcessiveUseView.ClassroomID.HasValue)
  142. {
  143. throw new Exception("教室不能为空。");
  144. }
  145. if (classroomExcessiveUseSchedulingViewList.Any(x => !x.Weekday.HasValue))
  146. {
  147. throw new Exception("星期不能为空。");
  148. }
  149. //if (classroomExcessiveUseSchedulingViewList.Any(x => !x.CoursesTimeID.HasValue))
  150. //{
  151. // throw new Exception("节次不能为空。");
  152. //}
  153. if (classroomExcessiveUseView.ClassroomExcessiveUseID == null || classroomExcessiveUseView.ClassroomExcessiveUseID == Guid.Empty)
  154. {
  155. classroomExcessiveUse = new ES_ClassroomExcessiveUse();
  156. classroomExcessiveUse.ClassroomExcessiveUseID = Guid.NewGuid();
  157. SetNewStatus(classroomExcessiveUse);
  158. UnitOfWork.Add(classroomExcessiveUse);
  159. }
  160. else
  161. {
  162. classroomExcessiveUse = ClassroomExcessiveUseDAL.ClassroomExcessiveUseRepository
  163. .GetSingle(x => x.ClassroomExcessiveUseID == classroomExcessiveUseView.ClassroomExcessiveUseID, (x => x.ES_ClassroomExcessiveUseScheduling.Select(w => w.ES_ClassroomExcessiveUseSchedulingWeekNum)));
  164. SetModifyStatus(classroomExcessiveUse);
  165. if (classroomExcessiveUse == null)
  166. {
  167. throw new Exception("数据有误,请核查。");
  168. }
  169. classroomExcessiveUse.ES_ClassroomExcessiveUseScheduling.SelectMany(x => x.ES_ClassroomExcessiveUseSchedulingWeekNum).ToList()
  170. .ForEach(x => UnitOfWork.Remove(x));
  171. classroomExcessiveUse.ES_ClassroomExcessiveUseScheduling.ToList()
  172. .ForEach(x => UnitOfWork.Remove(x));
  173. }
  174. classroomExcessiveUse.ClassroomID = classroomExcessiveUseView.ClassroomID;
  175. classroomExcessiveUse.SchoolyearID = classroomExcessiveUseView.SchoolyearID;
  176. classroomExcessiveUse.CollegeID = classroomExcessiveUseView.CollegeID;
  177. classroomExcessiveUse.UserID = classroomExcessiveUseView.UserID;
  178. classroomExcessiveUse.Content = classroomExcessiveUseView.Content;
  179. var coursesTimeList = ClassroomExcessiveUseDAL.CoursesTimeRepository.GetList(x => x.RecordStatus == (int)SYS_STATUS.USABLE).ToList();
  180. var ClassroomConflictList = ClassroomExcessiveUseDAL.GetExaminationPlanViewByClassroom(classroomExcessiveUseView.SchoolyearID, classroomExcessiveUseView.ClassroomID).ToList();
  181. var schoolYearView = schoolYearServices.GetSchoolYear(classroomExcessiveUseView.SchoolyearID);
  182. classroomExcessiveUseSchedulingViewList.ForEach(x => {
  183. if (x.CoursesTimeID == null)
  184. {
  185. foreach (var courseTime in coursesTimeList)
  186. {
  187. //排考冲突判断
  188. foreach (var classroomConflict in ClassroomConflictList)
  189. {
  190. var weekday = (int)classroomConflict.ExaminationDate.Value.DayOfWeek;
  191. var days = (classroomConflict.ExaminationDate.Value - schoolYearView.FirstWeek).Days + 1;
  192. var firstWeekday = (int)schoolYearView.FirstWeek.DayOfWeek;
  193. var weekNum = 0;
  194. if ((days - firstWeekday) % 7 == 0)
  195. {
  196. weekNum = (days - (8 - firstWeekday)) / 7 + 1;
  197. }
  198. else {
  199. weekNum = (days - (8 - firstWeekday)) / 7 + 2;
  200. }
  201. if (x.WeekNumList.Contains(weekNum) && x.Weekday == weekday)
  202. {
  203. throw new Exception("教室已被排考占用。");
  204. }
  205. }
  206. if (!ClassroomExcessiveUseDAL.GetAvailableClassroom(classroomExcessiveUse.SchoolyearID.Value,
  207. x.WeekNumList.ToList(), x.Weekday.Value, courseTime.CoursesTimeID, classroomExcessiveUse.ClassroomID.Value, x.ClassroomExcessiveUseID).Any())
  208. {
  209. throw new Exception("教室已被占用或预留。");
  210. }
  211. ES_ClassroomExcessiveUseScheduling scheduling = new ES_ClassroomExcessiveUseScheduling();
  212. scheduling.ClassroomExcessiveUseSchedulingID = Guid.NewGuid();
  213. scheduling.ClassroomExcessiveUseID = classroomExcessiveUse.ClassroomExcessiveUseID;
  214. scheduling.Weekday = x.Weekday;
  215. scheduling.CoursesTimeID = courseTime.CoursesTimeID;
  216. this.SetNewStatus(scheduling);
  217. UnitOfWork.Add(scheduling);
  218. foreach (var weekNum in x.WeekNumList)
  219. {
  220. ES_ClassroomExcessiveUseSchedulingWeekNum weekNumSet = new ES_ClassroomExcessiveUseSchedulingWeekNum();
  221. weekNumSet.ClassroomExcessiveUseSchedulingWeekNumID = Guid.NewGuid();
  222. weekNumSet.ClassroomExcessiveUseSchedulingID = scheduling.ClassroomExcessiveUseSchedulingID;
  223. weekNumSet.WeekNum = weekNum;
  224. this.SetNewStatus(weekNumSet);
  225. UnitOfWork.Add(weekNumSet);
  226. }
  227. }
  228. }
  229. else
  230. {
  231. //排考冲突判断
  232. //var coursesTime = coursesTimeList.Where(y => y.c)
  233. foreach (var classroomConflict in ClassroomConflictList)
  234. {
  235. var weekday = (int)classroomConflict.ExaminationDate.Value.DayOfWeek;
  236. var days = (classroomConflict.ExaminationDate.Value - schoolYearView.FirstWeek).Days + 1;
  237. var firstWeekday = (int)schoolYearView.FirstWeek.DayOfWeek;
  238. var weekNum = 0;
  239. if ((days - firstWeekday) % 7 == 0)
  240. {
  241. weekNum = (days - (8 - firstWeekday)) / 7 + 1;
  242. }
  243. else
  244. {
  245. weekNum = (days - (8 - firstWeekday)) / 7 + 2;
  246. }
  247. //将时间段都转换成当天的时间进行比较
  248. var startTimeStr = x.StartHour.ToString() + ":" + x.StartMinute.ToString();
  249. var startTime = DateTime.Parse(startTimeStr);
  250. var endTimeStr = x.EndHour.ToString() + ":" + x.EndMinute.ToString();
  251. var endTime = DateTime.Parse(endTimeStr);
  252. DateTime start = DateTime.Now.Date;
  253. DateTime constartTime = start.Add(classroomConflict.StartTime.Value);
  254. DateTime conendTime = start.Add(classroomConflict.EndTime.Value);
  255. if (x.WeekNumList.Contains(weekNum) && x.Weekday == weekday && ((startTime >= Convert.ToDateTime(constartTime) && startTime < Convert.ToDateTime(conendTime)) || (endTime > Convert.ToDateTime(constartTime) && endTime <= Convert.ToDateTime(conendTime)) || (startTime <= Convert.ToDateTime(constartTime) && endTime >= Convert.ToDateTime(conendTime))))
  256. {
  257. throw new Exception("教室已被排考占用,占用情况请参考考场安排表。");
  258. }
  259. }
  260. //这个检测暂时不支持批量,由于这类数据估计也就一两条,暂时用循环顶着,后续如果有处理量大的影响性能再优化
  261. if (!ClassroomExcessiveUseDAL.GetAvailableClassroom(classroomExcessiveUse.SchoolyearID.Value,
  262. x.WeekNumList.ToList(), x.Weekday.Value, x.CoursesTimeID.Value, classroomExcessiveUse.ClassroomID.Value, x.ClassroomExcessiveUseID).Any())
  263. {
  264. throw new Exception("教室已被占用或预留。");
  265. }
  266. ES_ClassroomExcessiveUseScheduling scheduling = new ES_ClassroomExcessiveUseScheduling();
  267. scheduling.ClassroomExcessiveUseSchedulingID = Guid.NewGuid();
  268. scheduling.ClassroomExcessiveUseID = classroomExcessiveUse.ClassroomExcessiveUseID;
  269. scheduling.Weekday = x.Weekday;
  270. scheduling.CoursesTimeID = x.CoursesTimeID;
  271. this.SetNewStatus(scheduling);
  272. UnitOfWork.Add(scheduling);
  273. foreach (var weekNum in x.WeekNumList)
  274. {
  275. ES_ClassroomExcessiveUseSchedulingWeekNum weekNumSet = new ES_ClassroomExcessiveUseSchedulingWeekNum();
  276. weekNumSet.ClassroomExcessiveUseSchedulingWeekNumID = Guid.NewGuid();
  277. weekNumSet.ClassroomExcessiveUseSchedulingID = scheduling.ClassroomExcessiveUseSchedulingID;
  278. weekNumSet.WeekNum = weekNum;
  279. this.SetNewStatus(weekNumSet);
  280. UnitOfWork.Add(weekNumSet);
  281. }
  282. }
  283. });
  284. UnitOfWork.Commit();
  285. }
  286. public IGridResultSet<CoursesTimeView> GetCoursesTimeViewGrid(ConfiguretView configuretView, Guid schoolYearID,
  287. List<int?> schedulingWeekList, int weekday, Guid classroomID, int pageIndex, int pageSize)
  288. {
  289. var usedCoursesTime = ClassroomExcessiveUseDAL.GetUsedCourseTime(schoolYearID, schedulingWeekList, weekday, classroomID);
  290. var query = coursesTimeDAL.GetCoursesTimeQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && !usedCoursesTime.Contains(x.CoursesTimeID));
  291. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  292. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.CreateTime).ToGridResultSet<CoursesTimeView>(pageIndex, pageSize);
  293. return query.OrderBy(x => x.StartTimes).ToGridResultSet<CoursesTimeView>(pageIndex, pageSize);
  294. }
  295. public List<ClassroomExcessiveUseWeekNumView> GetClassroomExcessiveUseWeekNumViewByDateTimeList(Guid schoolyearID, IList<StartEndTimeView> dateTimeList)
  296. {
  297. var fullCoursesTimeList = this.SchoolYearServices.Value.GetCoursesTimeByDateTime(schoolyearID, dateTimeList);
  298. var coursesTimeList = fullCoursesTimeList.Select(x => new { x.WeekNum, x.Weekday, x.CoursesTimeID }).ToList();
  299. var classroomExcessiveUseViewList = this.ClassroomExcessiveUseDAL.GetClassroomExcessiveUseWeekNumViewQueryable(x => x.SchoolyearID == schoolyearID, x => true)
  300. .SelectByKeys(coursesTimeList).ToList();
  301. classroomExcessiveUseViewList.ForEach(x =>
  302. {
  303. var fullCoursesTime = fullCoursesTimeList.FirstOrDefault(w => w.WeekNum == x.WeekNum && w.Weekday == x.Weekday && w.CoursesTimeID == x.CoursesTimeID);
  304. x.Date = fullCoursesTime.Date;
  305. x.StartTime = new TimeSpan(fullCoursesTime.StartHour ?? 0, fullCoursesTime.StartMinute ?? 0, 0);
  306. x.EndTime = new TimeSpan(fullCoursesTime.EndHour ?? 0, fullCoursesTime.EndMinute ?? 0, 0);
  307. });
  308. return classroomExcessiveUseViewList;
  309. }
  310. }
  311. }