HolidayRepository.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using FMSAdmin.Data;
  8. using FMSAdmin.Helpers;
  9. using FMSAdmin.Entities;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using Microsoft.IdentityModel.Tokens;
  14. using FMSAdmin.Models;
  15. using System.Transactions;
  16. namespace FMSApp.Repositories {
  17. public class HolidayRepository {
  18. private readonly ILogger<HolidayRepository> _logger;
  19. private readonly FMSContext _context;
  20. private readonly AppSettings _appSettings;
  21. private readonly StorageHelper _storage;
  22. public HolidayRepository(
  23. ILogger<HolidayRepository> logger,
  24. FMSContext context,
  25. IOptions<AppSettings> appSettings,
  26. StorageHelper storage
  27. ) {
  28. _logger = logger;
  29. _context = context;
  30. _appSettings = appSettings.Value;
  31. _storage = storage;
  32. }
  33. public IList<DateTime> GetHolidays(int siteId) {
  34. var list = new List<DateTime>();
  35. var holidays = _context.CmHoliday.Where(
  36. x => x.SiteId == siteId
  37. && x.IsUse == true
  38. );
  39. int year = DateTime.Now.Year;
  40. foreach (var item in holidays) {
  41. if (item.IsLunar) {
  42. var solar = LunarSolarConverter.LunarToSolar(new Lunar() {
  43. lunarYear = year,
  44. lunarMonth = item.HolidayMonth,
  45. lunarDay = item.HolidayDay
  46. });
  47. list.Add(new DateTime(solar.solarYear, solar.solarMonth, solar.solarDay));
  48. } else {
  49. list.Add(new DateTime(year, item.HolidayMonth, item.HolidayDay));
  50. }
  51. }
  52. var customs = _context.CmHolidayCustom.Where(
  53. x => x.SiteId == siteId
  54. && x.HolidayDate >= DateTime.Now
  55. && x.IsUse == true
  56. );
  57. foreach (var item in customs) {
  58. list.Add(item.HolidayDate);
  59. }
  60. return list;
  61. }
  62. public bool IsHoliday(DateTime date, CmHolidayWeekend weekend, IList<DateTime> holidays) {
  63. if (date.DayOfWeek == DayOfWeek.Saturday && weekend.Saturday) return true;
  64. if (date.DayOfWeek == DayOfWeek.Sunday && weekend.Sunday) return true;
  65. if (holidays.Where(x => x.Month == date.Month && x.Day == date.Day).Any()) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. public CmHolidayWeekend GetHolidayWeekend(int siteId) {
  71. var result = _context.CmHolidayWeekend.FirstOrDefault(
  72. x => x.SiteId == siteId
  73. );
  74. if (result == null) {
  75. result = new CmHolidayWeekend();
  76. result.Saturday = true;
  77. result.Sunday = true;
  78. }
  79. return result;
  80. }
  81. }
  82. }