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; using Microsoft.Extensions.Configuration; namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants { public class UserDutyTimeRepository : 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 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 != "") return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!)); else return Guid.Empty; } } #endregion #region " Methods " public void UpdateUserDutyTime() { try { var data = _dbContext.Set() .Where(u => !u.IsProcess) .Where(u => u.EffectiveDate.Value.Date <= DateTime.Now.Date) .ToList(); foreach (var d in data) { 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> GetListByProfileIdAsync(Guid profileId) { var data = await _dbContext.Set().AsQueryable() .Include(x => x.DutyTime) .Where(x => x.ProfileId == profileId) .ToListAsync(); return data; } public async Task GetExist(Guid profileId, DateTime effectiveDate) { var data = await _dbContext.Set() .Where(x => x.ProfileId == profileId) .Where(x => x.EffectiveDate == effectiveDate) .FirstOrDefaultAsync(); return data; } public async Task GetLastEffectRound(Guid profileId) { var data = await _dbContext.Set() .Where(x => x.ProfileId == profileId) .Where(x => x.IsProcess) .OrderByDescending(x => x.EffectiveDate) .FirstOrDefaultAsync(); return data; } public async Task GetFirstInUseRound(Guid id) { var data = await _dbContext.Set() .Where(x => x.DutyTimeId == id) .FirstOrDefaultAsync(); return data; } #endregion } }