SchoolYearServices.cs 36 KB

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