SchoolYearServices.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 Bowin.Common.Utility;
  9. using EMIS.DataLogic.Common.CalendarManage;
  10. using EMIS.Entities;
  11. using EMIS.ViewModel;
  12. using EMIS.ViewModel.CalendarManage;
  13. using EMIS.Utility;
  14. namespace EMIS.CommonLogic.CalendarManage
  15. {
  16. public partial class SchoolYearServices : BaseServices, ISchoolYearServices
  17. {
  18. public SchoolYearDAL schoolYearDAL { get; set; }
  19. public CoursesTimeDAL CoursesTimeDAL { get; set; }
  20. /// <summary>
  21. /// 查询学年学期信息
  22. /// </summary>
  23. /// <param name="configuretView">查询条件实体</param>
  24. /// <param name="SchoolyearsType">活动类型</param>
  25. /// <param name="timesSegment">时间段</param>
  26. /// <param name="pageIndex">页码</param>
  27. /// <param name="pageSize">显示页数</param>
  28. /// <returns></returns>
  29. public IGridResultSet<SchoolYearView> GetSchoolYearViewGrid(ConfiguretView configuretView, int? isCurrent, int pageIndex, int pageSize)
  30. {
  31. var query = schoolYearDAL.GetSchoolYearQueryable(x => true);
  32. if (isCurrent > -1)
  33. {
  34. bool current = isCurrent == 1 ? true : false;
  35. query = query.Where(x => x.IsCurrent == current);
  36. }
  37. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  38. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.Years).ThenBy(t => t.SchoolcodeID).ToGridResultSet<SchoolYearView>(pageIndex, pageSize);
  39. return query.OrderByDescending(x => x.IsCurrent).ThenByDescending(x => x.Value).ToGridResultSet<SchoolYearView>(pageIndex, pageSize);
  40. }
  41. /// <summary>
  42. /// 查询学年学期信息
  43. /// </summary>
  44. /// <param name="configuretView">查询条件实体</param>
  45. /// <returns></returns>
  46. public List<SchoolYearView> GetSchoolYearViewList(ConfiguretView configuretView, int? isCurrent)
  47. {
  48. var query = schoolYearDAL.GetSchoolYearQueryable(x => true);
  49. if (isCurrent > -1)
  50. {
  51. bool current = isCurrent == 1 ? true : false;
  52. query = query.Where(x => x.IsCurrent == current);
  53. }
  54. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  55. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.Years).ThenBy(t => t.SchoolcodeID).ToList();
  56. return query.OrderByDescending(x => x.Value).ToList();
  57. }
  58. /// <summary>
  59. /// 查询之后的学年学期信息
  60. /// </summary>
  61. /// <returns></returns>
  62. public List<SchoolYearView> GetSchoolYearViewListAfterCurrent()
  63. {
  64. var currentSchoolYear = schoolYearDAL.schoolyearRepository.GetSingle(x => x.IsCurrent == true);
  65. if (currentSchoolYear == null)
  66. {
  67. throw new Exception("请先设置当前的学年学期。");
  68. }
  69. var query = schoolYearDAL.GetSchoolYearQueryable(x => x.Value >= currentSchoolYear.Value);
  70. return query.OrderByDescending(x => x.Value).ToList();
  71. }
  72. /// <summary>
  73. /// 查询之前的学年学期信息
  74. /// </summary>
  75. /// <returns></returns>
  76. public List<SchoolYearView> GetSchoolYearViewListBeforeCurrent()
  77. {
  78. var currentSchoolYear = schoolYearDAL.schoolyearRepository.GetSingle(x => x.IsCurrent == true);
  79. if (currentSchoolYear == null)
  80. {
  81. throw new Exception("请先设置当前的学年学期。");
  82. }
  83. var query = schoolYearDAL.GetSchoolYearQueryable(x => x.Value <= currentSchoolYear.Value);
  84. return query.OrderByDescending(x => x.Value).ToList();
  85. }
  86. /// <summary>
  87. /// 查询前一个学年学期信息
  88. /// </summary>
  89. /// <returns></returns>
  90. public SchoolYearView GetSchoolYearViewBeforeCurrent()
  91. {
  92. var currentSchoolYear = schoolYearDAL.schoolyearRepository.GetSingle(x => x.IsCurrent == true);
  93. if (currentSchoolYear == null)
  94. {
  95. throw new Exception("请先设置当前的学年学期。");
  96. }
  97. var query = schoolYearDAL.GetSchoolYearQueryable(x => x.Value ==currentSchoolYear.Value-1);
  98. return query.OrderByDescending(x => x.Value).FirstOrDefault();
  99. }
  100. /// <summary>
  101. /// 获取学年学期信息
  102. /// </summary>
  103. /// <param name="SchoolyearsID">主键ID</param>
  104. /// <returns></returns>
  105. public EMIS.Entities.CF_Schoolyear GetSchoolYear(Guid? schoolyearID)
  106. {
  107. //查询条件
  108. Expression<Func<EMIS.Entities.CF_Schoolyear, bool>> expression = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  109. expression = (x => x.SchoolyearID == schoolyearID);
  110. return schoolYearDAL.schoolyearRepository.GetSingle(expression);
  111. }
  112. /// <summary>
  113. /// 获取学年学期信息
  114. /// </summary>
  115. /// <param name="SchoolyearsID">主键ID</param>
  116. /// <returns></returns>
  117. public EMIS.Entities.CF_Schoolyear GetSchoolYearByValue(int value)
  118. {
  119. //查询条件
  120. Expression<Func<EMIS.Entities.CF_Schoolyear, bool>> expression = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  121. expression = (x => x.Value == value);
  122. return schoolYearDAL.schoolyearRepository.GetSingle(expression);
  123. }
  124. /// <summary>
  125. /// 获取学年学期信息
  126. /// </summary>
  127. /// <param name="schoolyearID">主键ID</param>
  128. /// <param name="IsCurrent">是否当前学期</param>
  129. /// <returns></returns>
  130. public List<EMIS.Entities.CF_Schoolyear> GetSchoolYearList(Guid? schoolyearID, bool IsCurrent)
  131. {
  132. var schoolyearList = schoolYearDAL.schoolyearRepository.Entities.Where(w => w.SchoolyearID != schoolyearID && w.IsCurrent == IsCurrent).ToList();
  133. return schoolyearList;
  134. }
  135. /// <summary>
  136. /// 获取学年学期信息
  137. /// </summary>
  138. /// <param name="Years">学年</param>
  139. /// <param name="Name">学期名称</param>
  140. /// <returns></returns>
  141. public EMIS.Entities.CF_Schoolyear GetSchoolYear(int Years, int SchoolcodeID)
  142. {
  143. //查询条件
  144. Expression<Func<EMIS.Entities.CF_Schoolyear, bool>> expression = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  145. expression = (x => x.Years == Years && x.SchoolcodeID == SchoolcodeID);
  146. return schoolYearDAL.schoolyearRepository.GetSingle(expression);
  147. }
  148. /// <summary>
  149. /// 获取学年学期信息
  150. /// </summary>
  151. /// <param name="schoolyearID">主键ID</param>
  152. /// <returns></returns>
  153. public SchoolYearView GetSchoolYearView(Guid? schoolyearID)
  154. {
  155. SchoolYearView schoolYearView = new SchoolYearView();
  156. if (schoolyearID.HasValue)
  157. schoolYearView = schoolYearDAL.GetSchoolYearQueryable(x => true).Where(x => x.SchoolYearID == schoolyearID).FirstOrDefault();
  158. return schoolYearView;
  159. }
  160. /// <summary>
  161. /// 添加
  162. /// </summary>
  163. /// <param name="schoolyear">实体</param>
  164. /// <returns></returns>
  165. public bool SchoolYearAdd(SchoolYearView schoolyearView)
  166. {
  167. try
  168. {
  169. if(this.schoolYearDAL.schoolyearRepository.GetList(x=>x.RecordStatus>(int)SYS_STATUS.UNUSABLE&&x.Code==schoolyearView.Code).Count()>0)
  170. {
  171. throw new Exception("学年学期"+schoolyearView.Code+"已存在,请重新输入!");
  172. }
  173. if (this.schoolYearDAL.schoolyearRepository
  174. .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE
  175. && x.Years == schoolyearView.Years && x.SchoolcodeID == schoolyearView.SchoolcodeID).Count() > 0)
  176. {
  177. var schoolCode = DictionaryHelper.GetSingleDictionary(schoolyearView.SchoolcodeID.Value, DictionaryItem.CF_Schoolcode.ToString());
  178. throw new Exception(schoolyearView.Years.Value.ToString() + "年下已存在" + schoolCode.Name + ",请重新输入!");
  179. }
  180. EMIS.Entities.CF_Schoolyear schoolyear = new EMIS.Entities.CF_Schoolyear();
  181. schoolyear.SchoolyearID = Guid.NewGuid();
  182. schoolyear.Code = schoolyearView.Code;
  183. schoolyear.Years = schoolyearView.Years.Value;
  184. schoolyear.SchoolcodeID = schoolyearView.SchoolcodeID.Value;
  185. schoolyear.WeeksNum = schoolyearView.WeeksNum.Value;
  186. schoolyear.FirstWeek = schoolyearView.FirstWeek.Value;
  187. schoolyear.IsCurrent = schoolyearView.IsCurrent;
  188. schoolyear.WeekDays = schoolyearView.WeekDays.Value;
  189. schoolyear.Value = (schoolyearView.Years.Value * 2) - 1 + (schoolyearView.SchoolcodeID.Value - 1);
  190. this.SetNewStatus(schoolyear);
  191. UnitOfWork.Add(schoolyear);
  192. if (schoolyearView.IsCurrent == true)//如果该数据为当前学期,则把其他数据的是否当前学期改成否
  193. {
  194. var schoolyearList = GetSchoolYearList(schoolyear.SchoolyearID, true);
  195. if (schoolyearList.Count > 0)
  196. {
  197. SchoolYearUpdateList(schoolyearList);
  198. }
  199. }
  200. UnitOfWork.Commit();
  201. return true;
  202. }
  203. catch (Exception)
  204. {
  205. throw;
  206. }
  207. }
  208. /// <summary>
  209. /// 修改
  210. /// </summary>
  211. /// <param name="SchoolyearsSets">实体</param>
  212. /// <returns></returns>
  213. public bool SchoolYearUpdate(SchoolYearView schoolyearView)
  214. {
  215. try
  216. {
  217. if (this.schoolYearDAL.schoolyearRepository.GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && x.Code == schoolyearView.Code&&x.SchoolyearID!=schoolyearView.SchoolYearID).Count() > 0)
  218. {
  219. throw new Exception("学年学期" + schoolyearView.Code + "已存在,请重新输入!");
  220. }
  221. if (this.schoolYearDAL.schoolyearRepository
  222. .GetList(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE
  223. && x.Years == schoolyearView.Years && x.SchoolcodeID == schoolyearView.SchoolcodeID
  224. && x.SchoolyearID != schoolyearView.SchoolYearID).Count() > 0)
  225. {
  226. var schoolCode = DictionaryHelper.GetSingleDictionary(schoolyearView.SchoolcodeID.Value,
  227. DictionaryItem.CF_Schoolcode.ToString());
  228. throw new Exception(schoolyearView.Years.Value.ToString() + "年下已存在" + schoolCode.Name + ",请重新输入!");
  229. }
  230. var schoolyear = GetSchoolYear(schoolyearView.SchoolYearID);
  231. if (schoolyear == null)
  232. {
  233. throw new Exception("保存失败,未能找到相对应的数据!");
  234. }
  235. schoolyear.Code = schoolyearView.Code;
  236. schoolyear.Years = schoolyearView.Years.Value;
  237. schoolyear.SchoolcodeID = schoolyearView.SchoolcodeID.Value;
  238. schoolyear.WeeksNum = schoolyearView.WeeksNum.Value;
  239. schoolyear.FirstWeek = schoolyearView.FirstWeek.Value;
  240. schoolyear.IsCurrent = schoolyearView.IsCurrent;
  241. schoolyear.WeekDays = schoolyearView.WeekDays.Value;
  242. schoolyear.Value = (schoolyearView.Years.Value * 2) - 1 + (schoolyearView.SchoolcodeID.Value - 1);
  243. this.SetModifyStatus(schoolyear);
  244. if (schoolyearView.IsCurrent == true)//如果该数据为当前学期,则把其他数据的是否当前学期改成否
  245. {
  246. var schoolyearList = GetSchoolYearList(schoolyear.SchoolyearID, true);
  247. if (schoolyearList.Count > 0)
  248. {
  249. SchoolYearUpdateList(schoolyearList);
  250. }
  251. }
  252. UnitOfWork.Commit();
  253. return true;
  254. }
  255. catch (Exception)
  256. {
  257. throw;
  258. }
  259. }
  260. /// <summary>
  261. /// 修改
  262. /// </summary>
  263. /// <param name="schoolyearList">实体集合</param>
  264. /// <returns></returns>
  265. private bool SchoolYearUpdateList(List<EMIS.Entities.CF_Schoolyear> schoolyearList)
  266. {
  267. try
  268. {
  269. foreach (var schoolyear in schoolyearList)
  270. {
  271. schoolyear.IsCurrent = false;
  272. }
  273. return true;
  274. }
  275. catch (Exception)
  276. {
  277. throw;
  278. }
  279. }
  280. /// <summary>
  281. /// 删除
  282. /// </summary>
  283. /// <param name="schoolyearIDs"></param>
  284. /// <returns></returns>
  285. public bool SchoolYearDelete(List<Guid> schoolyearIDs)
  286. {
  287. try
  288. {
  289. UnitOfWork.Delete<EMIS.Entities.CF_Schoolyear>(x => schoolyearIDs.Contains(x.SchoolyearID));
  290. UnitOfWork.Commit();
  291. return true;
  292. }
  293. catch (Exception)
  294. {
  295. throw;
  296. }
  297. }
  298. public IGridResultSet<SchoolYearView> GetYearsViewGrid(ConfiguretView configuretView, int? isCurrent, int pageIndex, int pageSize)
  299. {
  300. var query = schoolYearDAL.GetYearsQueryable(x => true);
  301. //if (isCurrent > -1)
  302. //{
  303. // bool current = isCurrent == 1 ? true : false;
  304. // query = query.Where(x => x.IsCurrent == current);
  305. //}
  306. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  307. return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.Years).ThenBy(t => t.SchoolcodeID).ToGridResultSet<SchoolYearView>(pageIndex, pageSize);
  308. return query.OrderByDescending(x => x.Years).ToGridResultSet<SchoolYearView>(pageIndex, pageSize);
  309. }
  310. /// <summary>
  311. /// 获取当前校历启用的学年学期ID
  312. /// </summary>
  313. /// <returns></returns>
  314. public SchoolYearView GetCurrentSchoolYear()
  315. {
  316. var curSchoolYear = schoolYearDAL.GetSchoolYearQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && x.IsCurrent == true)
  317. .FirstOrDefault();
  318. if (curSchoolYear == null)
  319. {
  320. throw new Exception("请先设置当前启用的学年学期。");
  321. }
  322. return curSchoolYear;
  323. }
  324. /// <summary>
  325. /// 获取当前校历启用的下一个学年学期ID
  326. /// </summary>
  327. /// <returns></returns>
  328. public SchoolYearView GetNextSchoolYear()
  329. {
  330. var curSchoolYear = GetCurrentSchoolYear();
  331. var nextSchoolYear = schoolYearDAL.GetSchoolYearQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && x.Value > curSchoolYear.Value)
  332. .OrderBy(x => x.Value).FirstOrDefault();
  333. if (nextSchoolYear == null)
  334. {
  335. throw new Exception("当前启用的学年学期对应的下一学期信息为空,请添加。");
  336. }
  337. return nextSchoolYear;
  338. }
  339. public void UpdateCurrentSchoolYear()
  340. {
  341. var currentSchoolYear = this.GetCurrentSchoolYear();
  342. this.UnitOfWork.Update<EMIS.Entities.CF_Schoolyear>((x => new EMIS.Entities.CF_Schoolyear { IsCurrent = false }), (x => true));
  343. var dbSchoolYear = this.GetSchoolYear(currentSchoolYear.SchoolYearID);
  344. dbSchoolYear.IsCurrent = true;
  345. this.UnitOfWork.Commit();
  346. }
  347. /// <summary>
  348. /// 获取当前学年学期
  349. /// </summary>
  350. /// <param name="IsCurrent"></param>
  351. /// <returns></returns>
  352. public Entities.CF_Schoolyear GetSchoolYearIsCurrent(bool IsCurrent)
  353. {
  354. //查询条件
  355. Expression<Func<EMIS.Entities.CF_Schoolyear, bool>> expression = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  356. expression = (x => x.IsCurrent == IsCurrent);
  357. return schoolYearDAL.schoolyearRepository.GetSingle(expression);
  358. }
  359. /// <summary>
  360. /// 自动创建充足的学年学期和学年字典
  361. /// (仅针对松山,松山的学年学期记法比较怪异)
  362. /// </summary>
  363. public void AutoCreateSchoolyearAndYearsSS()
  364. {
  365. int year = DateTime.Today.Year;
  366. //设置要预留多少年的数据
  367. int addYears = 3;
  368. int endYear = year + addYears;
  369. int weeksNum = 20; //默认20,因为没有特定的规律判断到底是多少周,只能按一般大学的标准设置成20,如果不对,用户可以自己在页面上调整
  370. int weekdays = 5; //一周5天工作制,如果不对,用户可以自己在页面上调整
  371. var schoolyearList = schoolYearDAL.schoolyearRepository.GetList(x => x.Years >= year && x.Years <= endYear).ToList();
  372. var yearList = schoolYearDAL.dictionaryItemRepository.GetList(x => x.DictionaryCode == typeof(EMIS.ViewModel.CF_Schoolyear).Name
  373. && x.Value >= year && x.Value <= endYear).ToList();
  374. for (int i = 0; i <= addYears; i++)
  375. {
  376. var curYear = (year + i);
  377. if (!yearList.Any(x => x.Value == curYear))
  378. {
  379. Sys_DictionaryItem dictionaryItem = new Sys_DictionaryItem();
  380. dictionaryItem.DictionaryItemID = Guid.NewGuid();
  381. dictionaryItem.Code = "Year" + curYear.ToString();
  382. dictionaryItem.DictionaryCode = typeof(EMIS.ViewModel.CF_Schoolyear).Name;
  383. dictionaryItem.Value = curYear;
  384. dictionaryItem.Name = curYear.ToString();
  385. dictionaryItem.OrderNo = (short)(curYear - 2005);
  386. dictionaryItem.RecordStatus = (int)SYS_STATUS.USABLE;
  387. dictionaryItem.IsEditable = false;
  388. this.UnitOfWork.Add(dictionaryItem);
  389. }
  390. if (!schoolyearList.Any(x => x.Years == curYear && x.SchoolcodeID == 1))
  391. {
  392. var firstDay = new DateTime(curYear, 3, 1);
  393. switch (firstDay.DayOfWeek)
  394. {
  395. case DayOfWeek.Sunday:
  396. firstDay = firstDay.AddDays(1);
  397. break;
  398. case DayOfWeek.Saturday:
  399. firstDay = firstDay.AddDays(2);
  400. break;
  401. default:
  402. firstDay = firstDay.AddDays(1 - (int)firstDay.DayOfWeek);
  403. break;
  404. }
  405. EMIS.Entities.CF_Schoolyear schoolyear = new Entities.CF_Schoolyear();
  406. schoolyear.SchoolyearID = Guid.NewGuid();
  407. schoolyear.Code = (curYear - 1).ToString() + "-" + curYear.ToString() + "02";
  408. schoolyear.Years = curYear;
  409. schoolyear.SchoolcodeID = 1;
  410. schoolyear.WeeksNum = weeksNum;
  411. schoolyear.FirstWeek = firstDay;
  412. schoolyear.IsCurrent = false;
  413. schoolyear.WeekDays = weekdays;
  414. schoolyear.RecordStatus = (int)SYS_STATUS.USABLE;
  415. schoolyear.CreateTime = schoolyear.ModifyTime = DateTime.Now;
  416. schoolyear.Value = (curYear * 2 - 1);
  417. this.UnitOfWork.Add(schoolyear);
  418. }
  419. if (!schoolyearList.Any(x => x.Years == curYear && x.SchoolcodeID == 2))
  420. {
  421. var firstDay = new DateTime(curYear, 9, 1);
  422. switch (firstDay.DayOfWeek)
  423. {
  424. case DayOfWeek.Sunday:
  425. firstDay = firstDay.AddDays(1);
  426. break;
  427. case DayOfWeek.Saturday:
  428. firstDay = firstDay.AddDays(2);
  429. break;
  430. default:
  431. firstDay = firstDay.AddDays(1 - (int)firstDay.DayOfWeek);
  432. break;
  433. }
  434. EMIS.Entities.CF_Schoolyear schoolyear = new Entities.CF_Schoolyear();
  435. schoolyear.SchoolyearID = Guid.NewGuid();
  436. schoolyear.Code = curYear.ToString() + "-" + (curYear + 1).ToString() + "01";
  437. schoolyear.Years = curYear;
  438. schoolyear.SchoolcodeID = 2;
  439. schoolyear.WeeksNum = weeksNum;
  440. schoolyear.FirstWeek = firstDay;
  441. schoolyear.IsCurrent = false;
  442. schoolyear.WeekDays = weekdays;
  443. schoolyear.RecordStatus = (int)SYS_STATUS.USABLE;
  444. schoolyear.CreateTime = schoolyear.ModifyTime = DateTime.Now;
  445. schoolyear.Value = (curYear * 2 - 1) + 1;
  446. this.UnitOfWork.Add(schoolyear);
  447. }
  448. }
  449. this.UnitOfWork.Commit();
  450. }
  451. /// <summary>
  452. /// 自动创建充足的学年学期和学年字典
  453. /// </summary>
  454. public void AutoCreateSchoolyearAndYears()
  455. {
  456. int year = DateTime.Today.Year;
  457. //设置要预留多少年的数据
  458. int addYears = 3;
  459. int endYear = year + addYears;
  460. int weeksNum = 20; //默认20,因为没有特定的规律判断到底是多少周,只能按一般大学的标准设置成20,如果不对,用户可以自己在页面上调整
  461. int weekdays = 5; //一周5天工作制,如果不对,用户可以自己在页面上调整
  462. var schoolyearList = schoolYearDAL.schoolyearRepository.GetList(x => x.Years >= year && x.Years <= endYear).ToList();
  463. var yearList = schoolYearDAL.dictionaryItemRepository.GetList(x => x.DictionaryCode == typeof(EMIS.ViewModel.CF_Schoolyear).Name
  464. && x.Value >= year && x.Value <= endYear).ToList();
  465. for (int i = 0; i <= addYears; i++)
  466. {
  467. var curYear = (year + i);
  468. if (!yearList.Any(x => x.Value == curYear))
  469. {
  470. Sys_DictionaryItem dictionaryItem = new Sys_DictionaryItem();
  471. dictionaryItem.DictionaryItemID = Guid.NewGuid();
  472. dictionaryItem.Code = "Year" + curYear.ToString();
  473. dictionaryItem.DictionaryCode = typeof(EMIS.ViewModel.CF_Schoolyear).Name;
  474. dictionaryItem.Value = curYear;
  475. dictionaryItem.Name = curYear.ToString();
  476. dictionaryItem.OrderNo = (short)(curYear - 2005);
  477. dictionaryItem.RecordStatus = (int)SYS_STATUS.USABLE;
  478. dictionaryItem.IsEditable = false;
  479. this.UnitOfWork.Add(dictionaryItem);
  480. }
  481. if (!schoolyearList.Any(x => x.Years == curYear && x.SchoolcodeID == 1))
  482. {
  483. var firstDay = new DateTime(curYear, 9, 1);
  484. switch (firstDay.DayOfWeek)
  485. {
  486. case DayOfWeek.Sunday:
  487. firstDay = firstDay.AddDays(1);
  488. break;
  489. case DayOfWeek.Saturday:
  490. firstDay = firstDay.AddDays(2);
  491. break;
  492. default:
  493. firstDay = firstDay.AddDays(1 - (int)firstDay.DayOfWeek);
  494. break;
  495. }
  496. EMIS.Entities.CF_Schoolyear schoolyear = new Entities.CF_Schoolyear();
  497. schoolyear.SchoolyearID = Guid.NewGuid();
  498. schoolyear.Code = curYear.ToString() + "-" + (curYear + 1).ToString() + "-1";
  499. schoolyear.Years = curYear;
  500. schoolyear.SchoolcodeID = 1;
  501. schoolyear.WeeksNum = weeksNum;
  502. schoolyear.FirstWeek = firstDay;
  503. schoolyear.IsCurrent = false;
  504. schoolyear.WeekDays = weekdays;
  505. schoolyear.RecordStatus = (int)SYS_STATUS.USABLE;
  506. schoolyear.CreateTime = schoolyear.ModifyTime = DateTime.Now;
  507. schoolyear.Value = (curYear * 2 - 1);
  508. this.UnitOfWork.Add(schoolyear);
  509. }
  510. if (!schoolyearList.Any(x => x.Years == curYear && x.SchoolcodeID == 2))
  511. {
  512. var firstDay = new DateTime(curYear + 1, 3, 1);
  513. switch (firstDay.DayOfWeek)
  514. {
  515. case DayOfWeek.Sunday:
  516. firstDay = firstDay.AddDays(1);
  517. break;
  518. case DayOfWeek.Saturday:
  519. firstDay = firstDay.AddDays(2);
  520. break;
  521. default:
  522. firstDay = firstDay.AddDays(1 - (int)firstDay.DayOfWeek);
  523. break;
  524. }
  525. EMIS.Entities.CF_Schoolyear schoolyear = new Entities.CF_Schoolyear();
  526. schoolyear.SchoolyearID = Guid.NewGuid();
  527. schoolyear.Code = curYear.ToString() + "-" + (curYear + 1).ToString() + "-2";
  528. schoolyear.Years = curYear;
  529. schoolyear.SchoolcodeID = 2;
  530. schoolyear.WeeksNum = weeksNum;
  531. schoolyear.FirstWeek = firstDay;
  532. schoolyear.IsCurrent = false;
  533. schoolyear.WeekDays = weekdays;
  534. schoolyear.RecordStatus = (int)SYS_STATUS.USABLE;
  535. schoolyear.CreateTime = schoolyear.ModifyTime = DateTime.Now;
  536. schoolyear.Value = (curYear * 2 - 1) + 1;
  537. this.UnitOfWork.Add(schoolyear);
  538. }
  539. }
  540. this.UnitOfWork.Commit();
  541. }
  542. public List<int> GetCurrentSchoolyearWeekNumList()
  543. {
  544. List<int> result = new List<int>();
  545. var schoolyear = this.GetCurrentSchoolYear();
  546. for (int i = 1; i <= schoolyear.WeeksNum; i++)
  547. {
  548. result.Add(i);
  549. }
  550. return result;
  551. }
  552. public List<int> GetSchoolyearWeekNumList(Guid? SchoolYearID)
  553. {
  554. List<int> result = new List<int>();
  555. if (SchoolYearID.HasValue)
  556. {
  557. var schoolyear = schoolYearDAL.schoolyearRepository.GetList(x => x.SchoolyearID == SchoolYearID).FirstOrDefault();
  558. if (schoolyear != null)
  559. {
  560. for (int i = 1; i <= schoolyear.WeeksNum; i++)
  561. {
  562. result.Add(i);
  563. }
  564. }
  565. }
  566. else
  567. {
  568. result=GetCurrentSchoolyearWeekNumList();
  569. }
  570. return result;
  571. }
  572. public List<WeekDayView> GetFutureWeekdayList(int weekNum)
  573. {
  574. var curDate = DateTime.Today;
  575. var curSchoolyear = this.schoolYearDAL.schoolyearRepository.GetSingle(x => x.IsCurrent == true);
  576. var firstWeek = curSchoolyear.FirstWeek.AddDays(1 - (int)curSchoolyear.FirstWeek.DayOfWeek);
  577. var nowWeekNum = (curDate.Subtract(firstWeek).Days + 7) / 7;
  578. var nowWeekday = (int)curDate.DayOfWeek == 0 ? 7 : (int)curDate.DayOfWeek;
  579. var weekList = WeekHelper.WeekDictionary;
  580. if (weekNum < nowWeekNum)
  581. {
  582. weekList = new Dictionary<int,string>();
  583. }
  584. if (weekNum == nowWeekNum)
  585. {
  586. weekList = weekList.Where(x => (x.Key == 0 ? 7 : x.Key) >= nowWeekday).ToDictionary(x => x.Key, x => x.Value);
  587. }
  588. return weekList.Select(x => new WeekDayView { WeekDay = x.Key, ChineseName = x.Value }).ToList();
  589. }
  590. public List<CoursesTimeView> GetFutureCourseTimeList(int weekNum, int weekday)
  591. {
  592. var curDate = DateTime.Today;
  593. var curTimeValue = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
  594. var curSchoolyear = this.schoolYearDAL.schoolyearRepository.GetSingle(x => x.IsCurrent == true);
  595. var firstWeek = curSchoolyear.FirstWeek.AddDays(1 - (int)curSchoolyear.FirstWeek.DayOfWeek);
  596. var nowWeekNum = (curDate.Subtract(firstWeek).Days + 7) / 7;
  597. var nowWeekday = (int)curDate.DayOfWeek == 0 ? 7 : (int)curDate.DayOfWeek;
  598. Expression<Func<EM_CoursesTime, bool>> exp = (x => true);
  599. if (weekNum < nowWeekNum)
  600. {
  601. return new List<CoursesTimeView>();
  602. }
  603. if (weekNum == nowWeekNum && (weekday == 0 ? 7 : weekday) < nowWeekday)
  604. {
  605. return new List<CoursesTimeView>();
  606. }
  607. if (weekNum == nowWeekNum && (weekday == 0 ? 7 : weekday) == nowWeekday)
  608. {
  609. exp = exp.And(x => (x.StartHour * 60 + x.StartMinutes) > curTimeValue);
  610. }
  611. var courseTimeList = CoursesTimeDAL.GetCoursesTimeQueryable(exp).ToList();
  612. return courseTimeList;
  613. }
  614. public bool IsTimePassed(SchoolYearView currentSchoolyear, int? weekNum, int? weekday, int? startHour, int? startMinute)
  615. {
  616. if (!weekNum.HasValue || !weekday.HasValue)
  617. {
  618. return true;
  619. }
  620. var curDate = DateTime.Today;
  621. var curTimeValue = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
  622. var firstWeek = currentSchoolyear.FirstWeek.Value.AddDays(1 - (int)currentSchoolyear.FirstWeek.Value.DayOfWeek);
  623. var nowWeekNum = (curDate.Subtract(firstWeek).Days + 7) / 7;
  624. var nowWeekday = (int)curDate.DayOfWeek == 0 ? 7 : (int)curDate.DayOfWeek;
  625. if (weekNum < nowWeekNum)
  626. {
  627. return true;
  628. }
  629. if (weekNum == nowWeekNum && (weekday == 0 ? 7 : weekday) < nowWeekday)
  630. {
  631. return true;
  632. }
  633. if (weekNum == nowWeekNum && (weekday == 0 ? 7 : weekday) == nowWeekday && (startHour * 60 + startMinute) < curTimeValue)
  634. {
  635. return true;
  636. }
  637. return false;
  638. }
  639. public List<FullCoursesTimeView> GetCoursesTimeByDateTime(Guid schoolyearID, IList<StartEndTimeView> datetimeList)
  640. {
  641. var schoolyear = this.GetSchoolYearView(schoolyearID);
  642. var firstWeek = schoolyear.FirstWeek.Value.AddDays(1 - (int)schoolyear.FirstWeek.Value.DayOfWeek);
  643. var lastDay = firstWeek.AddDays(7 * (schoolyear.WeeksNum ?? 0));
  644. var courseTimeList = CoursesTimeDAL.GetCoursesTimeQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList();
  645. var availableDateTimeList = datetimeList
  646. .Where(x => x.StartTime.HasValue && x.StartTime >= firstWeek && x.StartTime < lastDay
  647. && x.EndTime.HasValue && x.EndTime >= firstWeek && x.EndTime < lastDay
  648. && x.StartTime.Value.Date == x.EndTime.Value.Date).ToList();
  649. var fullCoursesTimeViewList = new List<FullCoursesTimeView>();
  650. availableDateTimeList.ForEach(x =>
  651. {
  652. var courseTime = courseTimeList.Where(w => new DateTime(x.StartTime.Value.Year, x.StartTime.Value.Month, x.StartTime.Value.Day,
  653. w.StartHour.Value, w.StartMinutes.Value, 0) <= x.EndTime.Value
  654. && new DateTime(x.StartTime.Value.Year, x.StartTime.Value.Month, x.StartTime.Value.Day,
  655. w.EndHour.Value, w.EndMinutes.Value, 0) >= x.StartTime.Value).ToList();
  656. courseTime.ForEach(w =>
  657. fullCoursesTimeViewList.Add(new FullCoursesTimeView
  658. {
  659. WeekNum = x.StartTime.Value.Date.Subtract(firstWeek).Days / 7 + 1,
  660. Weekday = (int)x.StartTime.Value.DayOfWeek == 0 ? 7 : (int)x.StartTime.Value.DayOfWeek,
  661. CoursesTimeID = w.CoursesTimeID,
  662. Date = x.StartTime.Value.Date,
  663. StartHour = w.StartHour,
  664. StartMinute = w.StartMinutes,
  665. EndHour = w.EndHour,
  666. EndMinute = w.EndMinutes
  667. })
  668. );
  669. });
  670. return fullCoursesTimeViewList;
  671. }
  672. public List<StartEndTimeView> GetCoursesTimeByDateTime(Guid schoolyearID, IList<FullCoursesTimeView> coursesTimeList)
  673. {
  674. var schoolyear = this.GetSchoolYearView(schoolyearID);
  675. var firstWeek = schoolyear.FirstWeek.Value.AddDays(1 - (int)schoolyear.FirstWeek.Value.DayOfWeek);
  676. var lastDay = firstWeek.AddDays(7 * (schoolyear.WeeksNum ?? 0));
  677. var courseTimeDbList = CoursesTimeDAL.GetCoursesTimeQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList();
  678. return coursesTimeList.Select(x => new StartEndTimeView
  679. {
  680. StartTime = firstWeek.AddDays((double)(((x.WeekNum ?? 0) - 1) * 7 + (x.Weekday == 0 ? 7 : x.Weekday ?? 1) - 1))
  681. .AddHours((double)x.StartHour.Value).AddMinutes((double)x.StartMinute.Value),
  682. EndTime = firstWeek.AddDays((double)(((x.WeekNum ?? 0) - 1) * 7 + (x.Weekday == 0 ? 7 : x.Weekday ?? 1) - 1))
  683. .AddHours((double)x.EndHour.Value).AddMinutes((double)x.EndMinute.Value)
  684. }).ToList();
  685. }
  686. }
  687. }