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 ProcessUserTimeStampRepository : 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 ProcessUserTimeStampRepository(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!), AccessToken); else return Guid.Empty; } } #endregion #region " Methods " public async Task> GetCheckInKeycloakId() { try { var data = await _dbContext.Set() .Select(x => x.KeycloakUserId) .Distinct() .ToListAsync(); return data; } catch { throw; } } public bool IsEditRequest(Guid userId, DateTime checkDate) { try { var data = _dbContext.Set().AsQueryable() .FirstOrDefault(x => x.KeycloakUserId == userId && x.CheckDate.Date == checkDate.Date); return !(data == null); } catch { throw; } } public async Task Copy() { var userTimeStamps = await _dbContext.Set().ToListAsync(); foreach (var u in userTimeStamps) { var p = new ProcessUserTimeStamp { KeycloakUserId = u.KeycloakUserId, CheckIn = u.CheckIn, CheckInLat = u.CheckInLat, CheckInLon = u.CheckInLon, CheckInPOI = u.CheckInPOI, CheckInImageUrl = u.CheckInImageUrl, IsLocationCheckIn = u.IsLocationCheckIn, CheckInLocationName = u.CheckInLocationName, CheckInRemark = u.CheckInRemark, CheckOut = u.CheckOut, CheckOutLat = u.CheckOutLat, CheckOutLon = u.CheckOutLon, CheckOutPOI = u.CheckOutPOI, CheckOutImageUrl = u.CheckOutImageUrl, IsLocationCheckOut = u.IsLocationCheckOut, CheckOutLocationName = u.CheckOutLocationName, CheckOutRemark = u.CheckOutRemark, }; _dbContext.Set().Add(p); } await _dbContext.SaveChangesAsync(); } public async Task CountRecordAsync() { var data = await _dbContext.Set().CountAsync(); return data; } public async Task GetTimestampByDateAsync(Guid keycloakId, DateTime date) { var data = await _dbContext.Set() .Where(u => u.KeycloakUserId == keycloakId) .Where(u => u.CheckIn.Date == date.Date) .FirstOrDefaultAsync(); return data; } public async Task> GetTimestampByDateLateAsync(string type, string role, string nodeId, int? node, string nodeIdByReq, int? nodeByReq, DateTime StartDate, DateTime EndDate) { var data = new List(); data = await _dbContext.Set().AsQueryable() .Where(x => x.CheckInStatus == "LATE") .Where(u => u.CheckIn.Date >= StartDate && u.CheckIn.Date <= EndDate) .Where(x => x.ProfileType == type.Trim().ToUpper()).ToListAsync(); // กรองตามสิทธิ์ admin ก่อน if (role == "CHILD") { data = data.Where(x => node == 4 ? x.Child4DnaId == Guid.Parse(nodeId) : node == 3 ? x.Child3DnaId == Guid.Parse(nodeId) : node == 2 ? x.Child2DnaId == Guid.Parse(nodeId) : node == 1 ? x.Child1DnaId == Guid.Parse(nodeId) : node == 0 ? x.RootDnaId == Guid.Parse(nodeId) : node == null ? true : true ).ToList(); } else if (role == "BROTHER") { data = data.Where(x => node == 4 ? x.Child3DnaId == Guid.Parse(nodeId) : node == 3 ? x.Child2DnaId == Guid.Parse(nodeId) : node == 2 ? x.Child1DnaId == Guid.Parse(nodeId) : node == 1 || node == 0 ? x.RootDnaId == Guid.Parse(nodeId) : node == null ? true : true ).ToList(); } else if (role == "ROOT") { data = data.Where(x => x.RootDnaId == Guid.Parse(nodeId)).ToList(); } else if (role == "PARENT") { data = data.Where(x => x.RootDnaId == Guid.Parse(nodeId) && x.Child1DnaId != null).ToList(); } else if (role == "NORMAL") { data = data.Where(x => node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) && x.Child1DnaId == null : node == 1 ? x.Child1DnaId == Guid.Parse(nodeId!) && x.Child2DnaId == null : node == 2 ? x.Child2DnaId == Guid.Parse(nodeId!) && x.Child3DnaId == null : node == 3 ? x.Child3DnaId == Guid.Parse(nodeId!) && x.Child4DnaId == null : node == 4 ? x.Child4DnaId == Guid.Parse(nodeId!) : true ).ToList(); } // กรองตามที่ fe ส่งมา if (role == "ROOT" || role == "OWNER" || role == "CHILD" || role == "BROTHER" || role == "PARENT") { data = data.Where(x => nodeByReq == 4 ? x.Child4Id == Guid.Parse(nodeIdByReq) : nodeByReq == 3 ? x.Child3Id == Guid.Parse(nodeIdByReq) : nodeByReq == 2 ? x.Child2Id == Guid.Parse(nodeIdByReq) : nodeByReq == 1 ? x.Child1Id == Guid.Parse(nodeIdByReq) : nodeByReq == 0 ? x.RootId == Guid.Parse(nodeIdByReq) : true ).ToList(); } return data; } public async Task GetLastRecord(Guid keycloakId) { var data = await _dbContext.Set() .Where(u => u.KeycloakUserId == keycloakId) .OrderByDescending(u => u.CheckIn) .FirstOrDefaultAsync(); return data; } public async Task> GetTimeStampHistoryAsync(Guid keycloakId, int year, int page = 1, int pageSize = 10, string keyword = "") { var fiscalDateStart = new DateTime(year - 1, 10, 1); var fiscalDateEnd = new DateTime(year, 9, 30); var data = await _dbContext.Set() .Where(u => u.KeycloakUserId == keycloakId) .Where(u => u.CheckIn.Date >= fiscalDateStart && u.CheckIn.Date <= fiscalDateEnd) .OrderByDescending(u => u.CheckIn.Date) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); return data; } public async Task GetTimeStampHistoryForAdminCountAsync(DateTime startDate, DateTime endDate) { var data = await _dbContext.Set() .Where(u => u.CheckIn.Date >= startDate.Date && u.CheckIn.Date <= endDate.Date) .ToListAsync(); return data.Count; } public async Task> GetTimeStampHistoryByRangeForUserAsync(Guid userId, DateTime startDate, DateTime endDate) { var data = await _dbContext.Set() .Where(x => x.KeycloakUserId == userId) .Where(u => u.CheckIn.Date >= startDate.Date && u.CheckIn.Date <= endDate.Date) .ToListAsync(); return data; } public async Task> GetTimeStampHistoryForAdminAsync(DateTime startDate, DateTime endDate) { var data = await _dbContext.Set() .Where(u => u.CheckIn.Date >= startDate.Date && u.CheckIn.Date <= endDate.Date) .OrderBy(u => u.CheckIn) .ToListAsync(); return data; } public async Task> GetTimeStampHistoryForAdminRoleAsync(DateTime startDate, DateTime endDate, string role, string nodeId, int? node) { var data = await _dbContext.Set() .Where(u => u.CheckIn.Date >= startDate.Date && u.CheckIn.Date <= endDate.Date) .OrderBy(u => u.CheckIn) .ToListAsync(); if (role == "OWNER") { node = null; } if (role == "OWNER" || role == "CHILD") { data = data .Where(x => node == 4 ? x.Child4DnaId == Guid.Parse(nodeId!) : (node == 3 ? x.Child3DnaId == Guid.Parse(nodeId!) : (node == 2 ? x.Child2DnaId == Guid.Parse(nodeId!) : (node == 1 ? x.Child1DnaId == Guid.Parse(nodeId!) : (node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) : (node == null ? true : true)))))) .ToList(); } else if (role == "BROTHER") { data = data .Where(x => node == 4 ? x.Child3DnaId == Guid.Parse(nodeId!) : (node == 3 ? x.Child2DnaId == Guid.Parse(nodeId!) : (node == 2 ? x.Child1DnaId == Guid.Parse(nodeId!) : (node == 1 || node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) : (node == null ? true : true))))) .ToList(); } else if (role == "ROOT") { data = data .Where(x => x.RootDnaId == Guid.Parse(nodeId!)) .ToList(); } else if (role == "PARENT") { data = data .Where(x => x.RootDnaId == Guid.Parse(nodeId!) && x.Child1DnaId != null) .ToList(); } else if (role == "NORMAL") { data = data.Where(x => node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) && x.Child1DnaId == null : node == 1 ? x.Child1DnaId == Guid.Parse(nodeId!) && x.Child2DnaId == null : node == 2 ? x.Child2DnaId == Guid.Parse(nodeId!) && x.Child3DnaId == null : node == 3 ? x.Child3DnaId == Guid.Parse(nodeId!) && x.Child4DnaId == null : node == 4 ? x.Child4DnaId == Guid.Parse(nodeId!) : true ).ToList(); } return data; } public async Task GetTimeStampById(Guid id) { var data = await _dbContext.Set() .Where(u => u.Id == id) .FirstOrDefaultAsync(); return data; } #endregion } }