2023-11-23 15:48:09 +07:00
|
|
|
|
using BMA.EHR.Application.Common.Interfaces;
|
|
|
|
|
|
using BMA.EHR.Application.Messaging;
|
|
|
|
|
|
using BMA.EHR.Domain.Models.Leave.TimeAttendants;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-11-23 17:04:45 +07:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-11-23 15:48:09 +07:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|
|
|
|
|
{
|
|
|
|
|
|
public class UserDutyTimeRepository : GenericLeaveRepository<Guid, UserDutyTime>
|
|
|
|
|
|
{
|
|
|
|
|
|
#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 UserDutyTimeRepository(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 != "")
|
2024-06-20 13:37:25 +07:00
|
|
|
|
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!), AccessToken);
|
2023-11-23 15:48:09 +07:00
|
|
|
|
else
|
|
|
|
|
|
return Guid.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-23 17:04:45 +07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region " Methods "
|
|
|
|
|
|
|
2023-11-24 11:23:18 +07:00
|
|
|
|
public void UpdateUserDutyTime()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = _dbContext.Set<UserDutyTime>()
|
|
|
|
|
|
.Where(u => !u.IsProcess)
|
2025-03-10 09:37:34 +07:00
|
|
|
|
.Where(u => u.EffectiveDate.Value.Date <= DateTime.Now.AddHours(7).Date)
|
2023-11-24 11:23:18 +07:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var d in data)
|
|
|
|
|
|
{
|
2023-12-18 15:50:42 +07:00
|
|
|
|
d.IsProcess = true;
|
|
|
|
|
|
d.LastUpdateFullName = "Automation System";
|
|
|
|
|
|
d.LastUpdateUserId = UserId ?? "";
|
|
|
|
|
|
d.LastUpdatedAt = DateTime.Now;
|
|
|
|
|
|
var result = UpdateAsync(d).Result;
|
|
|
|
|
|
//var result = _userProfileRepository.UpdateDutyTimeAsync(d.ProfileId, d.DutyTimeId, d.EffectiveDate.Value.Date).Result;
|
2023-11-24 11:23:18 +07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-23 17:04:45 +07:00
|
|
|
|
public async Task<List<UserDutyTime>> GetListByProfileIdAsync(Guid profileId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = await _dbContext.Set<UserDutyTime>().AsQueryable()
|
|
|
|
|
|
.Include(x => x.DutyTime)
|
|
|
|
|
|
.Where(x => x.ProfileId == profileId)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 15:50:42 +07:00
|
|
|
|
public async Task<UserDutyTime?> GetExist(Guid profileId, DateTime effectiveDate)
|
2023-11-23 17:04:45 +07:00
|
|
|
|
{
|
|
|
|
|
|
var data = await _dbContext.Set<UserDutyTime>()
|
|
|
|
|
|
.Where(x => x.ProfileId == profileId)
|
|
|
|
|
|
.Where(x => x.EffectiveDate == effectiveDate)
|
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-30 13:35:58 +07:00
|
|
|
|
public async Task<UserDutyTime?> GetLastEffectRound(Guid profileId, DateTime? effectiveDate = null, CancellationToken cancellationToken = default)
|
2023-12-18 15:50:42 +07:00
|
|
|
|
{
|
2026-01-30 13:35:58 +07:00
|
|
|
|
// กำหนด timeout เป็น 30 นาที
|
|
|
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
|
|
|
|
|
|
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
2026-01-09 18:57:24 +07:00
|
|
|
|
effectiveDate ??= DateTime.Now;
|
2023-12-18 15:50:42 +07:00
|
|
|
|
var data = await _dbContext.Set<UserDutyTime>()
|
|
|
|
|
|
.Where(x => x.ProfileId == profileId)
|
2026-01-09 18:57:24 +07:00
|
|
|
|
.Where(x => x.EffectiveDate.Value.Date <= effectiveDate.Value.Date)
|
2023-12-18 15:50:42 +07:00
|
|
|
|
.OrderByDescending(x => x.EffectiveDate)
|
2026-01-30 13:35:58 +07:00
|
|
|
|
.FirstOrDefaultAsync(combinedCts.Token);
|
2023-12-18 15:50:42 +07:00
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 16:03:45 +07:00
|
|
|
|
public async Task<UserDutyTime?> GetFirstInUseRound(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = await _dbContext.Set<UserDutyTime>()
|
|
|
|
|
|
.Where(x => x.DutyTimeId == id)
|
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-23 17:04:45 +07:00
|
|
|
|
|
2023-11-23 15:48:09 +07:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|