CalendarViewServices.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Bowin.Common.Utility;
  6. using EMIS.DataLogic.Common.CalendarManage;
  7. using EMIS.Entities;
  8. using EMIS.DataLogic.UniversityManage.AdministrativeOrgan;
  9. using EMIS.ViewModel.CalendarManage;
  10. using EMIS.ViewModel;
  11. using Bowin.Common.Data;
  12. using NPOI.HPSF;
  13. using NPOI.HSSF.UserModel;
  14. using NPOI.HSSF.Util;
  15. using NPOI.POIFS.FileSystem;
  16. using NPOI.SS.UserModel;
  17. using System.IO;
  18. using System.Web;
  19. using NPOI.SS.Util;
  20. namespace EMIS.CommonLogic.CalendarManage
  21. {
  22. public partial class CalendarViewServices : BaseServices, ICalendarViewServices
  23. {
  24. public SchoolYearDAL SchoolYearDAL { get; set; }
  25. public UniversityDAL UniversityDAL { get; set; }
  26. public ActivitieDAL ActivitieDAL { get; set; }
  27. public SCalendarDAL scalendarDAL { get; set; }
  28. internal static List<MonthView> GetMonths(SchoolYearView schoolYear)
  29. {
  30. if (schoolYear == null) throw new Exception("请求的学年学期不存在。");
  31. List<MonthView> result = new List<MonthView>();
  32. DateTime startDate = schoolYear.FirstWeek.Value;
  33. DateTime endDate = schoolYear.FirstWeek.Value.AddDays((schoolYear.WeeksNum.Value * 7) - 1);
  34. int spanMonths = endDate.MonthSubstract(startDate);
  35. int startMonth = startDate.Month;
  36. result.Add(new MonthView { Month = startMonth, ChineseName = startMonth.ConvertToChinese() + "月" });
  37. for (int i = 1; i <= spanMonths; i++)
  38. {
  39. result.Add(new MonthView { Month = startDate.AddMonths(i).Month, ChineseName = startDate.AddMonths(i).Month.ConvertToChinese() + "月" });
  40. }
  41. return result;
  42. }
  43. internal static List<WeekNumView> GetWeekNums(SchoolYearView schoolYear)
  44. {
  45. if (schoolYear == null) throw new Exception("请求的学年学期不存在。");
  46. List<WeekNumView> result = new List<WeekNumView>();
  47. for (int i = 1; i <= schoolYear.WeeksNum; i++)
  48. {
  49. result.Add(new WeekNumView { WeekNum = i, ChineseName = i.ConvertToChinese() });
  50. }
  51. return result;
  52. }
  53. internal static List<WeekDayView> GetWeekDays()
  54. {
  55. List<WeekDayView> result = new List<WeekDayView>();
  56. for (int i = 0; i < 6; i++)
  57. {
  58. result.Add(new WeekDayView { WeekDay = i, ChineseName = (i + 1).ConvertToChinese() });
  59. }
  60. result.Add(new WeekDayView { WeekDay = 7, ChineseName = "日" });
  61. return result;
  62. }
  63. internal static DateTime GetStartDate(SchoolYearView schoolYear)
  64. {
  65. if (schoolYear == null) throw new Exception("请求的学年学期不存在。");
  66. DateTime monday = schoolYear.FirstWeek.Value.AddDays((double)(1 - schoolYear.FirstWeek.Value.DayOfWeek));
  67. if (monday.Month == schoolYear.FirstWeek.Value.Month && monday.Day != 1)
  68. {
  69. monday = GetMonthStartDate(monday, schoolYear.FirstWeek.Value);
  70. }
  71. return monday;
  72. }
  73. internal static DateTime GetEndDate(SchoolYearView schoolYear)
  74. {
  75. if (schoolYear == null) throw new Exception("请求的学年学期不存在。");
  76. DateTime lastDate = schoolYear.FirstWeek.Value.AddDays(7 * schoolYear.WeeksNum.Value);
  77. DateTime sunday = lastDate.AddDays((double)(7 - lastDate.DayOfWeek));
  78. if (sunday.Month == lastDate.Month && sunday.Day != DateTime.DaysInMonth(lastDate.Year, lastDate.Month))
  79. {
  80. sunday = GetMonthEndDate(sunday, lastDate);
  81. }
  82. return sunday;
  83. }
  84. internal static DateTime GetMonthStartDate(DateTime curDate, DateTime startDate)
  85. {
  86. DateTime newDate = curDate.AddDays(-7);
  87. if (newDate.Month == startDate.Month && newDate.Day != 1)
  88. {
  89. return GetMonthStartDate(newDate, startDate);
  90. }
  91. else
  92. {
  93. return newDate;
  94. }
  95. }
  96. internal static DateTime GetMonthEndDate(DateTime curDate, DateTime endDate)
  97. {
  98. DateTime newDate = curDate.AddDays(7);
  99. if (newDate.Month == endDate.Month && newDate.Day != DateTime.DaysInMonth(endDate.Year, endDate.Month))
  100. {
  101. return GetMonthEndDate(newDate, endDate);
  102. }
  103. else
  104. {
  105. return newDate;
  106. }
  107. }
  108. public CalendarView GetCalendarViewBySchoolYearID(Guid schoolYearID)
  109. {
  110. var university = UniversityDAL.UniversityRepository.GetSingle(x => true);
  111. if (university == null)
  112. {
  113. throw new Exception("请先设置学校信息。");
  114. }
  115. var schoolYear = SchoolYearDAL.GetSchoolYearQueryable(x => x.SchoolyearID == schoolYearID).FirstOrDefault();
  116. var calendarView = new CalendarView();
  117. calendarView.StartDate = GetStartDate(schoolYear);
  118. calendarView.EndDate = GetEndDate(schoolYear);
  119. List<int> yearList = new List<int>();
  120. for (int i = calendarView.StartDate.Year; i <= calendarView.EndDate.Year; i++)
  121. {
  122. yearList.Add(i);
  123. }
  124. var holidays = (from a in ActivitieDAL.GetActivitiesQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList()
  125. .Where(x => x.ActivitiesType == (int)CF_ActivitiesType.Holiday || x.ActivitiesType == (int)CF_ActivitiesType.Festival)
  126. join yl in yearList on true equals true
  127. select new { Activity = a, Year = yl }
  128. )
  129. .Select(x => new DateTime(x.Year, x.Activity.ActivitiesTimeMonth ?? 1, x.Activity.ActivitiesTimeDay ?? 1)).ToList();
  130. StringBuilder holidayStr = new StringBuilder();
  131. var holidaysList = (from a in ActivitieDAL.GetActivitiesQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList()
  132. .Where(x => x.ActivitiesType == (int)CF_ActivitiesType.Holiday || x.ActivitiesType == (int)CF_ActivitiesType.Festival)
  133. join yl in yearList on true equals true
  134. select new { Activity = a, Year = yl}
  135. ).OrderByDescending(x => x.Year).ThenByDescending(x => x.Activity.ActivitiesTimeMonth).ThenBy(x => x.Activity.ActivitiesTimeDay).ThenBy(x => x.Activity.Name).ToList();
  136. foreach(var h in holidaysList)
  137. {
  138. if(calendarView.StartDate.Year==h.Year){
  139. if (!holidayStr.ToString().Contains(h.Activity.Name))
  140. {
  141. var holidayName = holidaysList.Where(x => x.Activity.Name == h.Activity.Name && calendarView.StartDate.Year == x.Year).ToList();
  142. if (holidayName.Count==1)
  143. {
  144. holidayStr.Append(h.Activity.Name + h.Activity.ActivitiesTimeMonth + "月" + h.Activity.ActivitiesTimeDay + "日;");
  145. }
  146. else
  147. {
  148. holidayStr.Append(h.Activity.Name + h.Activity.ActivitiesTimeMonth + "月" + h.Activity.ActivitiesTimeDay);
  149. }
  150. }
  151. else
  152. {
  153. var holiday = holidaysList.Where(x => x.Activity.Name == h.Activity.Name && calendarView.StartDate.Year == x.Year).Max(x=>x.Activity.ActivitiesTimeDay);
  154. if (h.Activity.ActivitiesTimeDay == holiday)
  155. {
  156. holidayStr.Append("、" + h.Activity.ActivitiesTimeDay + "日;");
  157. }
  158. else
  159. {
  160. holidayStr.Append("、" + h.Activity.ActivitiesTimeDay);
  161. }
  162. }
  163. }
  164. }
  165. holidayStr.Replace(";", "。",holidayStr.Length-1,1);
  166. //校历假日
  167. StringBuilder scalendarStr = new StringBuilder();
  168. var scalendarList = (from a in scalendarDAL.GetSCalendarQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE).ToList()
  169. join yl in yearList on true equals true
  170. select new { Activity = a, Year = yl }
  171. ).OrderByDescending(x => x.Year).ThenBy(x => x.Activity.ActivitiesTime).ThenBy(x=>x.Activity.Name).ToList();
  172. foreach (var s in scalendarList)
  173. {
  174. if (calendarView.StartDate.Year == s.Year)
  175. {
  176. if (!scalendarStr.ToString().Contains(s.Activity.Name))
  177. {
  178. var scalendarName = scalendarList.Where(x => x.Activity.Name == s.Activity.Name && calendarView.StartDate.Year == x.Year).ToList();
  179. if (scalendarName.Count == 1)
  180. {
  181. scalendarStr.Append(s.Activity.Name + s.Activity.ActivitiesTime.Value.Month + "月" + s.Activity.ActivitiesTime.Value.Day + "日;");
  182. }
  183. else
  184. {
  185. scalendarStr.Append(s.Activity.Name + s.Activity.ActivitiesTime.Value.Month + "月" + s.Activity.ActivitiesTime.Value.Day);
  186. }
  187. }
  188. else
  189. {
  190. var scalendar = scalendarList.Where(x => x.Activity.Name == s.Activity.Name && calendarView.StartDate.Year == x.Year).Max(x => x.Activity.ActivitiesTime);
  191. if (s.Activity.ActivitiesTime == scalendar)
  192. {
  193. scalendarStr.Append("、" + s.Activity.ActivitiesTime.Value.Day + "日;");
  194. }
  195. else
  196. {
  197. scalendarStr.Append("、" + s.Activity.ActivitiesTime.Value.Day);
  198. }
  199. }
  200. }
  201. //if (calendarView.StartDate.Year == s.Year)
  202. //{
  203. // if (!scalendarStr.ToString().Contains(s.Activity.Name))
  204. // {
  205. // var scalendarName = scalendarList.Where(x => x.Activity.Name == s.Activity.Name && calendarView.StartDate.Year == x.Year).ToList();
  206. // if (scalendarName.Count == 1)
  207. // {
  208. // scalendarStr.Append(" "+s.Activity.ActivitiesTime.Value.Month + "月" + s.Activity.ActivitiesTime.Value.Day + "日 " + s.Activity.Name+";");
  209. // }
  210. // else
  211. // {
  212. // scalendarStr.Append(" " + s.Activity.ActivitiesTime.Value.Month + "月" + s.Activity.ActivitiesTime.Value.Day + s.Activity.Name);
  213. // }
  214. // }
  215. // else
  216. // {
  217. // var scalendar = scalendarList.Where(x => x.Activity.Name == s.Activity.Name && calendarView.StartDate.Year == x.Year).Max(x => x.Activity.ActivitiesTime);
  218. // if (s.Activity.ActivitiesTime == scalendar)
  219. // {
  220. // scalendarStr.Append("、" + s.Activity.ActivitiesTime.Value.Day + "日 " + s.Activity.Name);
  221. // }
  222. // else
  223. // {
  224. // scalendarStr.Append("、" + s.Activity.ActivitiesTime.Value.Day);
  225. // }
  226. // }
  227. //}
  228. }
  229. calendarView.UniversityName = university.Name;
  230. calendarView.SchoolYear = schoolYear;
  231. calendarView.Months = GetMonths(schoolYear);
  232. calendarView.WeekNums = GetWeekNums(schoolYear);
  233. calendarView.WeekDays = GetWeekDays();
  234. calendarView.Holidays = holidays;
  235. calendarView.HolidayStr = holidayStr.ToString();
  236. calendarView.ScalendarStr = scalendarStr.ToString();
  237. return calendarView;
  238. }
  239. public void Excel(Guid schoolYearID, string HeaderText)
  240. {
  241. string fileName = HeaderText + ".xls";
  242. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  243. if (HttpContext.Current.Request.UserAgent.ToLower().IndexOf("firefox") > -1)
  244. {
  245. HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
  246. }
  247. else
  248. {
  249. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  250. }
  251. HttpContext.Current.Response.Clear();
  252. var view = GetCalendarViewBySchoolYearID(schoolYearID);
  253. //创建工作薄
  254. HSSFWorkbook hssfworkbook = new HSSFWorkbook();
  255. //创建表
  256. ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
  257. //默认宽度-高度
  258. sheet1.DefaultColumnWidth = 6;
  259. sheet1.DefaultRowHeight = 10;
  260. short color_BLACK = HSSFColor.BLACK.index;
  261. ICellStyle style = hssfworkbook.CreateCellStyle();
  262. style.BorderBottom = BorderStyle.THIN;
  263. style.BorderLeft = BorderStyle.THIN;
  264. style.BorderRight = BorderStyle.THIN;
  265. style.BorderTop = BorderStyle.THIN;
  266. //设置背景(根据上面的定义的颜色 进行赋值)
  267. //style.FillForegroundColor = color_BLACK;
  268. //style.FillPattern = FillPatternType.SOLID_FOREGROUND;
  269. //style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.BLUE_GREY.index;
  270. //居中
  271. style.Alignment = HorizontalAlignment.CENTER;
  272. style.VerticalAlignment = VerticalAlignment.CENTER;
  273. //设置字体
  274. IFont font = hssfworkbook.CreateFont();
  275. font.FontHeightInPoints = 12;//字号
  276. style.SetFont(font);
  277. //周数前面的空列数
  278. var emptyWeeks = (int)Math.Ceiling((decimal)view.SchoolYear.FirstWeek.Value.Subtract(view.StartDate).Days / 7);
  279. //周数后面的空列数
  280. var emptyWeeksEnd = (int)Math.Floor((double)view.EndDate.Subtract(view.SchoolYear.FirstWeek.Value.AddDays((double)view.SchoolYear.WeeksNum * 7)).Days / 7);
  281. //总周数包括空列
  282. var totalWeeks = view.SchoolYear.WeeksNum + emptyWeeks + emptyWeeksEnd;
  283. //表头
  284. IRow firstRow = sheet1.CreateRow(0);
  285. firstRow.HeightInPoints = 2 * 16;
  286. ICell firstRowCell = firstRow.CreateCell(0);
  287. ICellStyle firstRowStyle = hssfworkbook.CreateCellStyle();
  288. firstRowStyle.BorderBottom = BorderStyle.THIN;
  289. firstRowStyle.BorderLeft = BorderStyle.THIN;
  290. firstRowStyle.BorderRight = BorderStyle.THIN;
  291. firstRowStyle.BorderTop = BorderStyle.THIN;
  292. //居中
  293. firstRowStyle.Alignment = HorizontalAlignment.CENTER;
  294. firstRowStyle.VerticalAlignment = VerticalAlignment.CENTER;
  295. IFont font1 = hssfworkbook.CreateFont();
  296. font1.FontHeightInPoints = 18;//字号
  297. font1.Boldweight = (short)FontBoldWeight.BOLD;//粗体
  298. firstRowStyle.SetFont(font1);
  299. firstRowCell.CellStyle = firstRowStyle;
  300. for (int i = 1; i < totalWeeks.Value; i++)
  301. {
  302. ICell cell = firstRow.CreateCell(i);
  303. cell.CellStyle = firstRowStyle;
  304. }
  305. firstRow.Cells[0].SetCellValue(view.UniversityName + view.SchoolYear.Code + "学期校历");
  306. //将表头合并
  307. sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, totalWeeks.Value));
  308. //firstRow.Cells.ForEach(x => x.CellStyle = firstRowStyle);
  309. //第二行
  310. IRow secondRow = sheet1.CreateRow(1);
  311. secondRow.CreateCell(0).SetCellValue("星期\\月份");
  312. secondRow.Cells[0].CellStyle = style;
  313. for (var i = 0; i < view.Months.Count; i++)
  314. {
  315. var startDate = view.StartDate;
  316. var beginDate = startDate.AddDays(-1);
  317. int weekCount = 0;
  318. for (var j = 0; j < totalWeeks; j++)
  319. {
  320. var curWeekStart = beginDate.AddDays(1 + (j * 7));
  321. var curWeekEnd = beginDate.AddDays((j + 1) * 7);
  322. if (curWeekEnd.Month == (view.Months[i].Month))
  323. {
  324. weekCount++;
  325. }
  326. }
  327. int count = secondRow.LastCellNum;
  328. ICell secondRowCell = secondRow.CreateCell(secondRow.LastCellNum);
  329. secondRowCell.SetCellValue(view.Months[i].ChineseName);
  330. secondRowCell.CellStyle = style;
  331. for (int k = 1; k < weekCount; k++)
  332. {
  333. secondRow.CreateCell(secondRow.LastCellNum);
  334. secondRow.Cells[secondRow.LastCellNum - 1].CellStyle = style;
  335. }
  336. sheet1.AddMergedRegion(new CellRangeAddress(1, 1, count, count + weekCount - 1));
  337. }
  338. //中间部分
  339. for (int i = 0; i < 7; i++)
  340. {
  341. IRow row = sheet1.CreateRow(1 + sheet1.LastRowNum);
  342. ICell cell = row.CreateCell(0);
  343. cell.SetCellValue(view.WeekDays[i].ChineseName);
  344. cell.CellStyle = style;
  345. var rowStartDate = view.StartDate.AddDays(i);
  346. for (var j = 0; j < totalWeeks; j++)
  347. {
  348. ICellStyle tmpStyle = hssfworkbook.CreateCellStyle();
  349. tmpStyle.BorderBottom = BorderStyle.THIN;
  350. tmpStyle.BorderLeft = BorderStyle.THIN;
  351. tmpStyle.BorderRight = BorderStyle.THIN;
  352. tmpStyle.BorderTop = BorderStyle.THIN;
  353. tmpStyle.Alignment = HorizontalAlignment.CENTER;
  354. tmpStyle.VerticalAlignment = VerticalAlignment.CENTER;
  355. tmpStyle.SetFont(font);
  356. ICell cells = row.CreateCell(j + 1);
  357. cells.CellStyle = tmpStyle;
  358. var curDate = rowStartDate.AddDays(j * 7);
  359. string str = curDate.Day.ToString();
  360. if (curDate < view.SchoolYear.FirstWeek ||
  361. curDate > view.SchoolYear.FirstWeek.Value.AddDays((double)view.SchoolYear.WeeksNum * 7))
  362. {
  363. cells.CellStyle.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
  364. cells.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
  365. }
  366. else
  367. {
  368. cells.CellStyle.FillForegroundColor = HSSFColor.WHITE.index;
  369. cells.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
  370. }
  371. if (view.Holidays.Where(x => x.Date == curDate).ToList().Count > 0)
  372. str += '★';
  373. cells.SetCellValue(str);
  374. }
  375. }
  376. //底部
  377. IRow bottomRow = sheet1.CreateRow(sheet1.LastRowNum + 1);
  378. ICell bottomCell = bottomRow.CreateCell(0);
  379. bottomCell.SetCellValue("周次");
  380. bottomCell.CellStyle = style;
  381. for (int k = 0; k < totalWeeks; k++)
  382. {
  383. ICell cells = bottomRow.CreateCell(k + 1);
  384. cells.CellStyle = style;
  385. if (emptyWeeks <= k && k < totalWeeks - emptyWeeksEnd)
  386. cells.SetCellValue(view.WeekNums[k - emptyWeeks].ChineseName);
  387. }
  388. sheet1.SetColumnWidth(0, 10 * 256);
  389. //sheet1.SetColumnWidth(1, 5 * 256);
  390. MemoryStream file = new MemoryStream();
  391. hssfworkbook.Write(file);
  392. HttpContext.Current.Response.BinaryWrite(file.GetBuffer());
  393. HttpContext.Current.Response.End();
  394. }
  395. }
  396. }