199 lines
8 KiB
C#
199 lines
8 KiB
C#
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.EntityFrameworkCore.Query;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
|
|
{
|
|
public class LeaveRequestRepository : GenericLeaveRepository<Guid, LeaveRequest>
|
|
{
|
|
#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 "
|
|
|
|
#region " Overrides "
|
|
|
|
public override async Task<LeaveRequest?> GetByIdAsync(Guid id)
|
|
{
|
|
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
|
|
.Include(x => x.LeaveDocument)
|
|
.Include(x => x.LeaveDraftDocument)
|
|
.Include(x => x.Type)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
return data;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public async Task<List<LeaveRequest>> GetLeaveRequestByYearAsync(int year)
|
|
{
|
|
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
|
|
.Include(x => x.Type)
|
|
.Where(x => x.LeaveStartDate.Year == year)
|
|
.ToListAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<LeaveRequest>> GetLeaveRequestByUserIdAsync(Guid keycloakUserId, int year, Guid type, string status)
|
|
{
|
|
var rawData = _dbContext.Set<LeaveRequest>().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<List<LeaveRequest>> GetLeaveRequestForAdminAsync(int year, Guid type, string status)
|
|
{
|
|
var rawData = _dbContext.Set<LeaveRequest>().AsQueryable()
|
|
.Include(x => x.Type);
|
|
|
|
|
|
if (year != 0)
|
|
rawData = (IIncludableQueryable<LeaveRequest, LeaveType>)rawData.Where(x => x.LeaveStartDate.Year == year);
|
|
|
|
if (type != Guid.Empty)
|
|
rawData = (IIncludableQueryable<LeaveRequest, LeaveType>)rawData.Where(x => x.Type.Id == type);
|
|
|
|
if (status.Trim().ToUpper() != "ALL")
|
|
rawData = (IIncludableQueryable<LeaveRequest, LeaveType>)rawData.Where(x => x.LeaveStatus == status);
|
|
|
|
return await rawData.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> GetRestDayTotalByYearForUserAsync(Guid keycloakUserId, int year)
|
|
{
|
|
var leaveType = await _dbContext.Set<LeaveType>().AsQueryable().FirstOrDefaultAsync(l => l.Code.Trim().ToUpper() == "LV-005");
|
|
|
|
if (leaveType == null)
|
|
{
|
|
throw new Exception("ไม่พบข้อมูลประเภทการลาพักผ่อน โปรดติดต่อผู้ดูและระบบ");
|
|
}
|
|
|
|
var data = _dbContext.Set<LeaveRequest>().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<int> GetSumLeaveByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId, int year)
|
|
{
|
|
var data = await _dbContext.Set<LeaveRequest>().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<DateTime?> GetLeaveLastByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId)
|
|
{
|
|
var data = await _dbContext.Set<LeaveRequest>().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;
|
|
}
|
|
|
|
public async Task<LeaveRequest?> GetLastLeaveRequestByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId)
|
|
{
|
|
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
|
|
.Include(x => x.Type)
|
|
.Where(x => x.KeycloakUserId == keycloakUserId)
|
|
.Where(x => x.Type.Id == leaveTypeId)
|
|
.OrderByDescending(x => x.LeaveStartDate.Date)
|
|
.FirstOrDefaultAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<LeaveRequest>> GetCancelLeaveRequestForAdminAsync(int year, Guid type, string status)
|
|
{
|
|
var rawData = _dbContext.Set<LeaveRequest>().AsQueryable()
|
|
.Include(x => x.Type)
|
|
.Where(x => x.LeaveStatus == "DELETE");
|
|
|
|
if (year != 0)
|
|
rawData = (IIncludableQueryable<LeaveRequest, LeaveType>)rawData.Where(x => x.LeaveStartDate.Year == year);
|
|
|
|
if (type != Guid.Empty)
|
|
rawData = (IIncludableQueryable<LeaveRequest, LeaveType>)rawData.Where(x => x.Type.Id == type);
|
|
|
|
if (status.Trim().ToUpper() != "ALL")
|
|
rawData = (IIncludableQueryable<LeaveRequest, LeaveType>)rawData.Where(x => x.LeaveCancelStatus == status);
|
|
|
|
return await rawData.ToListAsync();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|