using Amazon.S3.Model.Internal.MarshallTransformations; using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Messaging; using BMA.EHR.Domain.Models.Leave.Commons; using BMA.EHR.Domain.Models.Leave.Requests; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests { public class LeaveRequestRepository : GenericLeaveRepository { #region " Fields " private readonly ILeaveDbContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; private readonly OrganizationCommonRepository _organizationCommonRepository; private readonly UserProfileRepository _userProfileRepository; private readonly IConfiguration _configuration; private readonly EmailSenderService _emailSenderService; #endregion #region " Constructor and Destuctor " public LeaveRequestRepository(ILeaveDbContext dbContext, IHttpContextAccessor httpContextAccessor, OrganizationCommonRepository organizationCommonRepository, UserProfileRepository userProfileRepository, IConfiguration configuration, EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; _organizationCommonRepository = organizationCommonRepository; _userProfileRepository = userProfileRepository; _configuration = configuration; _emailSenderService = emailSenderService; } #endregion #region " Properties " protected Guid UserOrganizationId { get { if (UserId != null || UserId != "") return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!)); else return Guid.Empty; } } #endregion #region " Methods " public async Task> GetLeaveRequestByYearAsync(int year) { var data = await _dbContext.Set().AsQueryable() .Include(x => x.Type) .Where(x => x.LeaveStartDate.Year == year) .ToListAsync(); return data; } public async Task> GetLeaveRequestByUserIdAsync(Guid keycloakUserId, int year, Guid type, string status) { var rawData = _dbContext.Set().AsQueryable() .Include(x => x.Type) .Where(x => x.KeycloakUserId == keycloakUserId); if (year != 0) rawData = rawData.Where(x => x.LeaveStartDate.Year == year); if (type != Guid.Empty) rawData = rawData.Where(x => x.Type.Id == type); if (status.Trim().ToUpper() != "ALL") rawData = rawData.Where(x => x.LeaveStatus == status); return await rawData.ToListAsync(); } public async Task GetRestDayTotalByYearForUserAsync(Guid keycloakUserId, int year) { var leaveType = await _dbContext.Set().AsQueryable().FirstOrDefaultAsync(l => l.Code.Trim().ToUpper() == "LV-005"); if (leaveType == null) { throw new Exception("ไม่พบข้อมูลประเภทการลาพักผ่อน โปรดติดต่อผู้ดูและระบบ"); } var data = _dbContext.Set().AsQueryable() .Include(x => x.Type) .Where(x => x.KeycloakUserId == keycloakUserId) .Where(x => x.Type.Id == leaveType.Id) .Where(x => x.LeaveStartDate.Year == year) .Sum(x => x.LeaveTotal); return data; } public async Task GetSumLeaveByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId, int year) { var data = await _dbContext.Set().AsQueryable() .Include(x => x.Type) .Where(x => x.KeycloakUserId == keycloakUserId) .Where(x => x.Type.Id == leaveTypeId) .Where(x => x.LeaveStartDate.Year == year) .ToListAsync(); return data.Sum(x => x.LeaveTotal); } public async Task GetLeaveLastByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId) { var data = await _dbContext.Set().AsQueryable() .Include(x => x.Type) .Where(x => x.KeycloakUserId == keycloakUserId) .Where(x => x.Type.Id == leaveTypeId) .OrderByDescending(x => x.LeaveStartDate.Date) .Select(x => x.LeaveStartDate.Date) .FirstOrDefaultAsync(); return data; } #endregion } }