hrms-api-backend/BMA.EHR.Application/Repositories/MetaData/HolidayRepository.cs
Suphonchai Phoonsawat 28544df284 fix #1382
2025-04-28 09:45:48 +07:00

95 lines
3.4 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Domain.Models.MetaData;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Application.Repositories.MetaData
{
public class HolidayRepository : GenericRepository<Guid, Holiday>
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Costructor and Destructor "
public HolidayRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Methods "
public async Task<string> GetHolidayAsync(DateTime date, string category = "NORMAL")
{
var data = await _dbContext.Set<Holiday>().AsQueryable()
.Where(x => x.Category == category)
.Where(x => x.HolidayDate.Date == date.Date)
.Select(x => x.Name)
.FirstOrDefaultAsync();
return data ?? string.Empty;
}
public async Task<List<DateTime>> GetHolidayAsync(DateTime startDate, DateTime endDate, string category = "NORMAL")
{
var data = await _dbContext.Set<Holiday>().AsQueryable()
.Where(x => x.Category == category)
.Where(x => x.HolidayDate.Date >= startDate && x.HolidayDate.Date <= endDate)
.Select(x => x.HolidayDate.Date)
.ToListAsync();
return data;
}
public async Task<int> GetHolidayCountAsync(DateTime startDate, DateTime endDate, string category = "NORMAL")
{
var data = await _dbContext.Set<Holiday>().AsQueryable()
.Where(x => x.Category == category)
.Where(x => x.HolidayDate.Date >= startDate && x.HolidayDate.Date <= endDate)
.CountAsync();
return data;
}
public List<DateTime> GetWeekEnd(DateTime startDate, DateTime endDate, string category = "NORMAL")
{
var dates = new List<DateTime>();
for (DateTime i = startDate; i <= endDate; i = i.AddDays(1))
{
dates.Add(i);
}
if (category == "NORMAL")
return dates.Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday).ToList();
else
return dates.Where(d => d.DayOfWeek == DayOfWeek.Sunday).ToList();
}
public int GetWeekEndCount(DateTime startDate, DateTime endDate, string category = "NORMAL")
{
var dates = new List<DateTime>();
for (DateTime i = startDate; i <= endDate; i = i.AddDays(1))
{
dates.Add(i);
}
var count = 0;
if (category == "NORMAL")
count = dates.Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday).Count();
else
count = dates.Where(d => d.DayOfWeek == DayOfWeek.Sunday).Count();
return count;
}
#endregion
}
}