WorktimeAdjustmentServices.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Bowin.Common.Linq;
  6. using Bowin.Common.Linq.Entity;
  7. using EMIS.ViewModel.PaymentManage;
  8. using EMIS.ViewModel;
  9. using EMIS.Entities;
  10. using EMIS.DataLogic.PaymentManage;
  11. using System.Linq.Expressions;
  12. using EMIS.ViewModel.EducationManage;
  13. using EMIS.DataLogic.Common.CalendarManage;
  14. using EMIS.DataLogic.EducationSchedule;
  15. using EMIS.CommonLogic.EducationSchedule;
  16. namespace EMIS.CommonLogic.PaymentManage
  17. {
  18. public class WorktimeAdjustmentServices : BaseServices, IWorktimeAdjustmentServices
  19. {
  20. public WorktimeAdjustmentDAL WorktimeAdjustmentDAL { get; set; }
  21. public SchoolYearDAL SchoolYearDAL { get; set; }
  22. public ActivitieDAL ActivitieDAL { get; set; }
  23. public Lazy<IEducationSchedulingStopServices> EducationSchedulingStopServices { get; set; }
  24. public IEducationSchedulingAdjustmentServices EducationSchedulingAdjustmentServices { get; set; }
  25. public IGridResultSet<WorktimeAdjustmentView> GetWorktimeAdjustmentViewList(ConfiguretView worktimeAdjustmentConditionView,
  26. Guid? schoolyearID, Guid? coursematerialID, Guid? teacherUserID, int? worktimeAdjustmentTypeID, int? pageIndex, int? pageSize)
  27. {
  28. Expression<Func<TP_WorktimeAdjustment, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  29. Expression<Func<EM_Coursematerial, bool>> courseExp = (x => true);
  30. Expression<Func<EMIS.Entities.CF_Schoolyear, bool>> schoolyearExp = (x => true);
  31. if (schoolyearID.HasValue)
  32. {
  33. schoolyearExp = schoolyearExp.And(x => x.SchoolyearID == schoolyearID);
  34. }
  35. if (coursematerialID.HasValue)
  36. {
  37. courseExp = courseExp.And(x => x.CoursematerialID == coursematerialID);
  38. }
  39. if (teacherUserID.HasValue)
  40. {
  41. exp = exp.And(x => x.UserID == teacherUserID);
  42. }
  43. if (worktimeAdjustmentTypeID.HasValue)
  44. {
  45. exp = exp.And(x => x.WorktimeAdjustmentTypeID == worktimeAdjustmentTypeID);
  46. }
  47. var query = WorktimeAdjustmentDAL.GetWorktimeAdjustmentViewQueryable(exp, courseExp, schoolyearExp);
  48. if (!string.IsNullOrEmpty(worktimeAdjustmentConditionView.ConditionValue) && !string.IsNullOrEmpty(worktimeAdjustmentConditionView.Attribute))
  49. query = query.DynamicWhere(worktimeAdjustmentConditionView.Attribute, worktimeAdjustmentConditionView.Condition, worktimeAdjustmentConditionView.ConditionValue);
  50. query = query.OrderByDescending(x => x.SchoolyearCode)
  51. .ThenBy(x => x.WorktimeAdjustmentID)
  52. .ThenBy(x => x.TeacherName)
  53. .ThenBy(x => x.CourseCode);
  54. return query.ToGridResultSet(pageIndex, pageSize);
  55. }
  56. public List<WorktimeAdjustmentView> GetWorktimeAdjustmentViewList(ConfiguretView worktimeAdjustmentConditionView,
  57. Guid? schoolyearID, Guid? coursematerialID, Guid? teacherUserID, int? worktimeAdjustmentTypeID)
  58. {
  59. Expression<Func<TP_WorktimeAdjustment, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  60. Expression<Func<EM_Coursematerial, bool>> courseExp = (x => true);
  61. Expression<Func<EMIS.Entities.CF_Schoolyear, bool>> schoolyearExp = (x => true);
  62. if (schoolyearID.HasValue)
  63. {
  64. schoolyearExp = schoolyearExp.And(x => x.SchoolyearID == schoolyearID);
  65. }
  66. if (coursematerialID.HasValue)
  67. {
  68. courseExp = courseExp.And(x => x.CoursematerialID == coursematerialID);
  69. }
  70. if (teacherUserID.HasValue)
  71. {
  72. exp = exp.And(x => x.UserID == teacherUserID);
  73. }
  74. if (worktimeAdjustmentTypeID.HasValue)
  75. {
  76. exp = exp.And(x => x.WorktimeAdjustmentTypeID == worktimeAdjustmentTypeID);
  77. }
  78. var query = WorktimeAdjustmentDAL.GetWorktimeAdjustmentViewQueryable(exp, courseExp, schoolyearExp);
  79. if (!string.IsNullOrEmpty(worktimeAdjustmentConditionView.ConditionValue) && !string.IsNullOrEmpty(worktimeAdjustmentConditionView.Attribute))
  80. query = query.DynamicWhere(worktimeAdjustmentConditionView.Attribute, worktimeAdjustmentConditionView.Condition, worktimeAdjustmentConditionView.ConditionValue);
  81. query = query.OrderByDescending(x => x.SchoolyearCode)
  82. .ThenBy(x => x.WorktimeAdjustmentID)
  83. .ThenBy(x => x.TeacherName)
  84. .ThenBy(x => x.CourseCode);
  85. return query.ToList();
  86. }
  87. public WorktimeAdjustmentView GetWorktimeAdjustmentView(Guid? worktimeAdjustmentID)
  88. {
  89. var query = WorktimeAdjustmentDAL.GetWorktimeAdjustmentViewQueryable(x => x.WorktimeAdjustmentID == worktimeAdjustmentID,
  90. (x => true), (x => true));
  91. return query.FirstOrDefault();
  92. }
  93. public void Save(WorktimeAdjustmentView worktimeAdjustmentView)
  94. {
  95. TP_WorktimeAdjustment worktimeAdjustment = new TP_WorktimeAdjustment();
  96. if (worktimeAdjustmentView.WorktimeAdjustmentID == null || worktimeAdjustmentView.WorktimeAdjustmentID == Guid.Empty)
  97. {
  98. worktimeAdjustment = new TP_WorktimeAdjustment();
  99. worktimeAdjustment.WorktimeAdjustmentID = Guid.NewGuid();
  100. SetNewStatus(worktimeAdjustment);
  101. UnitOfWork.Add(worktimeAdjustment);
  102. }
  103. else
  104. {
  105. worktimeAdjustment = WorktimeAdjustmentDAL.WorktimeAdjustmentRepository.GetSingle(x =>
  106. x.WorktimeAdjustmentID == worktimeAdjustmentView.WorktimeAdjustmentID);
  107. if (worktimeAdjustment == null)
  108. {
  109. throw new Exception("未能找到需要修改的数据,数据可能已被其他用户更改。");
  110. }
  111. SetModifyStatus(worktimeAdjustment);
  112. }
  113. worktimeAdjustment.EducationMissionClassID = worktimeAdjustmentView.EducationMissionClassID;
  114. worktimeAdjustment.AdjustDate = worktimeAdjustmentView.AdjustDate;
  115. worktimeAdjustment.UserID = worktimeAdjustmentView.UserID;
  116. worktimeAdjustment.WorktimeAdjustmentTypeID = worktimeAdjustmentView.WorktimeAdjustmentTypeID;
  117. worktimeAdjustment.Worktime = worktimeAdjustmentView.Worktime;
  118. UnitOfWork.Commit();
  119. }
  120. public void Delete(IList<Guid?> worktimeAdjustmentIDList)
  121. {
  122. if (worktimeAdjustmentIDList.Count > 0)
  123. {
  124. UnitOfWork.Delete<TP_WorktimeAdjustment>(x => worktimeAdjustmentIDList.Contains(x.WorktimeAdjustmentID));
  125. }
  126. }
  127. public IGridResultSet<EducationMissionClassComboView> GetEducationMissionClassList(ConfiguretView missionClassConditionView,
  128. Guid? schoolyearID, Guid? userID, DateTime? adjustDate, Guid? collegeID, int? gradeYearID, int? standardID, Guid? coursematerialID,
  129. int? pageIndex, int? pageSize)
  130. {
  131. Expression<Func<EM_EducationMissionClass, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  132. Expression<Func<ES_EducationSchedulingTeacher, bool>> teacherExp = (x => true);
  133. Expression<Func<ES_EducationScheduling, bool>> scheduleExp = (x => true);
  134. if (schoolyearID.HasValue)
  135. {
  136. scheduleExp = scheduleExp.And(x => x.SchoolyearID == schoolyearID);
  137. }
  138. if (userID.HasValue)
  139. {
  140. teacherExp = teacherExp.And(x => x.UserID == userID);
  141. }
  142. if (adjustDate.HasValue)
  143. {
  144. if (schoolyearID.HasValue)
  145. {
  146. var schoolyear = SchoolYearDAL.GetSchoolYearQueryable(x => x.SchoolyearID == schoolyearID).First();
  147. var adjustWeekNum = Convert.ToInt32(Math.Ceiling(adjustDate.Value.Subtract(schoolyear.FirstWeek.Value).TotalDays / 7));
  148. var adjustWeekday = (int)adjustDate.Value.DayOfWeek;
  149. scheduleExp = scheduleExp.And(x => x.Weekday == adjustWeekday && x.ES_EducationSchedulingWeekNum.Any(w => w.WeekNum == adjustWeekNum));
  150. }
  151. else
  152. {
  153. scheduleExp = scheduleExp.And(x => false);
  154. }
  155. }
  156. if (collegeID.HasValue)
  157. {
  158. exp = exp.And(x => x.CF_Classmajor.Any(w => w.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID));
  159. }
  160. if (gradeYearID.HasValue)
  161. {
  162. exp = exp.And(x => x.CF_Classmajor.Any(w => w.CF_Grademajor.GradeID == gradeYearID));
  163. }
  164. if (standardID.HasValue)
  165. {
  166. exp = exp.And(x => x.CF_Classmajor.Any(w => w.CF_Grademajor.CF_Facultymajor.StandardID == standardID));
  167. }
  168. if (coursematerialID.HasValue)
  169. {
  170. exp = exp.And(x => x.CoursematerialID == coursematerialID);
  171. }
  172. var query = WorktimeAdjustmentDAL.GetEducationMissionClassViewQueryable(exp, teacherExp, scheduleExp);
  173. if (!string.IsNullOrEmpty(missionClassConditionView.ConditionValue) && !string.IsNullOrEmpty(missionClassConditionView.Attribute))
  174. query = query.DynamicWhere(missionClassConditionView.Attribute, missionClassConditionView.Condition, missionClassConditionView.ConditionValue);
  175. query = query.OrderBy(x => x.Name);
  176. return query.ToGridResultSet(pageIndex, pageSize);
  177. }
  178. public void GenerateChargeAgainst(Guid schoolyearID)
  179. {
  180. var activitieList = ActivitieDAL.GetActivitiesQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList();
  181. var schoolyear = SchoolYearDAL.GetSchoolYearQueryable(x => x.SchoolyearID == schoolyearID).First();
  182. var schoolyearEndDate = schoolyear.FirstWeek.Value.AddDays(4 + (((schoolyear.WeeksNum ?? 0) - 1) * 7));
  183. #region 校历节假日
  184. var activitieDateList = activitieList.Where(x =>
  185. (new DateTime(schoolyear.FirstWeek.Value.Year, x.ActivitiesTimeMonth.Value, x.ActivitiesTimeDay.Value) >= schoolyear.FirstWeek.Value
  186. && new DateTime(schoolyear.FirstWeek.Value.Year, x.ActivitiesTimeMonth.Value, x.ActivitiesTimeDay.Value) <= schoolyearEndDate))
  187. .Select(x => new DateTime(schoolyear.FirstWeek.Value.Year, x.ActivitiesTimeMonth.Value, x.ActivitiesTimeDay.Value))
  188. .Concat(activitieList.Where(x =>
  189. (new DateTime(schoolyearEndDate.Year, x.ActivitiesTimeMonth.Value, x.ActivitiesTimeDay.Value) >= schoolyear.FirstWeek.Value
  190. && new DateTime(schoolyearEndDate.Year, x.ActivitiesTimeMonth.Value, x.ActivitiesTimeDay.Value) <= schoolyearEndDate))
  191. .Select(x => new DateTime(schoolyearEndDate.Year, x.ActivitiesTimeMonth.Value, x.ActivitiesTimeDay.Value))).Distinct().ToList();
  192. var weekdayList = activitieDateList.Select(x =>
  193. new
  194. {
  195. AdjustDate = x,
  196. Weekday = (int)x.DayOfWeek,
  197. WeekNum = Convert.ToInt32(Math.Ceiling(x.Subtract(schoolyear.FirstWeek.Value).TotalDays / 7))
  198. }).ToList();
  199. var scheduleQuery = WorktimeAdjustmentDAL.GetWorktimeSchedulingViewQueryable(x => x.SchoolyearID == schoolyearID);
  200. var weekdayAndNumList = weekdayList.Select(x => new { x.Weekday, x.WeekNum }).ToList();
  201. var weekdayAndNumScheduleList = scheduleQuery.SelectByKeys(weekdayAndNumList);
  202. #endregion
  203. #region 停课记录
  204. var schedulingStopEndStatus = EducationSchedulingStopServices.Value.GetCorrectEndStatus();
  205. var schedulingStop = WorktimeAdjustmentDAL.GetWorktimeSchedulingViewByStopQueryable(
  206. x => x.RecordStatus == schedulingStopEndStatus,
  207. x => x.SchoolyearID == schoolyearID).ToList();
  208. #endregion
  209. #region 调课补课记录
  210. var schedulingAdjustmentEndStatus = EducationSchedulingAdjustmentServices.GetCorrectEndStatus();
  211. var schedulingAdjustment = WorktimeAdjustmentDAL.GetWorktimeSchedulingViewByAdjustmentQueryable(
  212. x => x.RecordStatus == schedulingStopEndStatus,
  213. x => x.SchoolyearID == schoolyearID).ToList();
  214. #endregion
  215. var scheduleList = weekdayAndNumScheduleList
  216. .Join(weekdayList, (x => new { Weekday = x.Weekday ?? 0, WeekNum = x.WeekNum ?? 0 }), (x => new { x.Weekday, x.WeekNum }),
  217. ((x, y) => new
  218. {
  219. x.EducationMissionClassID,
  220. y.AdjustDate,
  221. x.UserID,
  222. x.Worktime
  223. })).Concat(
  224. schedulingStop.Select(x => new
  225. {
  226. x.EducationMissionClassID,
  227. AdjustDate = schoolyear.FirstWeek.Value.AddDays((double)((x.WeekNum - 1) * 7 + (x.Weekday == 0 ? 7 : x.Weekday) - 1)),
  228. x.UserID,
  229. x.Worktime
  230. })
  231. );
  232. var achedulingAdjustmentList = schedulingAdjustment.Select(x => new
  233. {
  234. x.EducationMissionClassID,
  235. AdjustDate = schoolyear.FirstWeek.Value.AddDays((double)((x.WeekNum - 1) * 7 + (x.Weekday == 0 ? 7 : x.Weekday) - 1)),
  236. x.UserID,
  237. x.Worktime
  238. });
  239. var existsAdjustmentList = WorktimeAdjustmentDAL.WorktimeAdjustmentRepository.Entities.SelectByKeys(
  240. scheduleList.Select(x => new { x.EducationMissionClassID, x.AdjustDate, x.UserID }).ToList()
  241. );
  242. var deleteAdjustmentList = WorktimeAdjustmentDAL.WorktimeAdjustmentRepository.Entities.SelectByKeys(
  243. achedulingAdjustmentList.Select(x => new { x.EducationMissionClassID, x.AdjustDate, x.UserID }).ToList()
  244. );
  245. var delID = deleteAdjustmentList.Select(x => x.WorktimeAdjustmentID).ToList();
  246. var adjustmenntList = WorktimeAdjustmentDAL.WorktimeAdjustmentRepository.Entities.ToList();
  247. var insertList = new List<TP_WorktimeAdjustment>();
  248. var updateList = new List<TP_WorktimeAdjustment>();
  249. foreach (var schedule in scheduleList)
  250. {
  251. if (!existsAdjustmentList.Any(x => x.EducationMissionClassID == schedule.EducationMissionClassID
  252. && x.UserID == schedule.UserID && x.AdjustDate == schedule.AdjustDate
  253. && x.WorktimeAdjustmentTypeID == (int)TP_WorktimeAdjustmentType.ChargeAgainst))
  254. {
  255. var adjustment = new TP_WorktimeAdjustment
  256. {
  257. WorktimeAdjustmentID = Guid.NewGuid(),
  258. EducationMissionClassID = schedule.EducationMissionClassID,
  259. AdjustDate = schedule.AdjustDate,
  260. UserID = schedule.UserID,
  261. WorktimeAdjustmentTypeID = (int)TP_WorktimeAdjustmentType.ChargeAgainst,
  262. Worktime = schedule.Worktime
  263. };
  264. this.SetNewStatus(adjustment);
  265. insertList.Add(adjustment);
  266. }
  267. //else if (existsAdjustmentList.Any(x => x.EducationMissionClassID == schedule.EducationMissionClassID
  268. // && x.UserID == schedule.UserID && x.AdjustDate == schedule.AdjustDate
  269. // && x.WorktimeAdjustmentTypeID == (int)TP_WorktimeAdjustmentType.ChargeAgainst))
  270. //{
  271. // var adjustment = adjustmenntList.Where(x => x.EducationMissionClassID == schedule.EducationMissionClassID && x.UserID == schedule.UserID
  272. // && x.AdjustDate == schedule.AdjustDate && x.WorktimeAdjustmentTypeID == (int)TP_WorktimeAdjustmentType.ChargeAgainst).FirstOrDefault();
  273. //}
  274. }
  275. UnitOfWork.Delete<TP_WorktimeAdjustment>(x => delID.Contains(x.WorktimeAdjustmentID));
  276. UnitOfWork.BulkInsert(insertList);
  277. }
  278. }
  279. }