fix LeaveBeggining Add รายการใหม่เมื่อมีการตรวจสอบ auto ไม่ต้องใช้ cronjob เพราะว่ารายการจะเขอะเกินไป

This commit is contained in:
Suphonchai Phoonsawat 2025-04-29 15:21:42 +07:00
parent 746f435ca8
commit 749b2b68a1
3 changed files with 172 additions and 83 deletions

View file

@ -1,6 +1,10 @@
using BMA.EHR.Application.Common.Interfaces;
using Amazon.S3.Model;
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Messaging;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Models.Leave.Commons;
using BMA.EHR.Domain.Models.Leave.Requests;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@ -64,9 +68,81 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
public async Task<LeaveBeginning?> GetByYearAndTypeIdAsync(int year, Guid typeId)
{
return await _dbContext.Set<LeaveBeginning>()
var data = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId);
return data;
}
public async Task<LeaveBeginning?> GetByYearAndTypeIdForUserAsync(int year, Guid typeId, Guid userId)
{
var pf = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId, AccessToken);
if (pf == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
var govAge = (pf?.DateStart?.Date ?? DateTime.Now.Date).DiffDay(DateTime.Now.Date);
var leaveType = await _dbContext.Set<LeaveType>().FirstOrDefaultAsync(x => x.Id == typeId);
var data = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId && x.ProfileId == pf.Id);
if (data == null)
{
var limit = 0.0;
var prev = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year - 1 && x.LeaveTypeId == typeId && x.ProfileId == pf.Id);
var prevRemain = 0.0;
if (prev != null)
{
prevRemain = prev.LeaveDays - prev.LeaveDaysUsed;
}
if (govAge >= 180)
{
if (govAge >= 3650)
{
limit = 10 + prevRemain;
if (limit > 30) limit = 30;
}
else
{
limit = 10 + prevRemain;
if (limit > 20) limit = 20;
}
}
else
{
limit = 0.0;
}
data = new LeaveBeginning
{
LeaveYear = year,
LeaveTypeId = typeId,
ProfileId = pf.Id,
Prefix = pf.Prefix,
FirstName = pf.FirstName,
LastName = pf.LastName,
LeaveDaysUsed = 0,
LeaveDays = leaveType?.Code == "LV-005" ? limit : 0
};
_dbContext.Set<LeaveBeginning>().Add(data);
await _dbContext.SaveChangesAsync();
}
return data;
}
}
}