hrms-api-backend/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/UserDutyTimeRepository.cs

132 lines
4.9 KiB
C#
Raw Normal View History

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;
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;
}
}
#endregion
#region " Methods "
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)
.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;
}
}
catch
{
throw;
}
}
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)
{
var data = await _dbContext.Set<UserDutyTime>()
.Where(x => x.ProfileId == profileId)
.Where(x => x.EffectiveDate == effectiveDate)
.FirstOrDefaultAsync();
return data;
}
public async Task<UserDutyTime?> GetLastEffectRound(Guid profileId, DateTime? effectiveDate = null, CancellationToken cancellationToken = default)
2023-12-18 15:50:42 +07:00
{
// กำหนด timeout เป็น 30 นาที
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
effectiveDate ??= DateTime.Now;
2023-12-18 15:50:42 +07:00
var data = await _dbContext.Set<UserDutyTime>()
.Where(x => x.ProfileId == profileId)
.Where(x => x.EffectiveDate.Value.Date <= effectiveDate.Value.Date)
2023-12-18 15:50:42 +07:00
.OrderByDescending(x => x.EffectiveDate)
.FirstOrDefaultAsync(combinedCts.Token);
2023-12-18 15:50:42 +07:00
return data;
}
public async Task<UserDutyTime?> GetFirstInUseRound(Guid id)
{
var data = await _dbContext.Set<UserDutyTime>()
.Where(x => x.DutyTimeId == id)
.FirstOrDefaultAsync();
return data;
}
2023-11-23 15:48:09 +07:00
#endregion
}
}