170 lines
6.7 KiB
C#
170 lines
6.7 KiB
C#
using BMA.EHR.Application.Common.Interfaces;
|
|
using BMA.EHR.Application.Messaging;
|
|
using BMA.EHR.Domain.Extensions;
|
|
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 UserTimeStampRepository : GenericLeaveRepository<Guid, UserTimeStamp>
|
|
{
|
|
#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 UserTimeStampRepository(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<int> CountRecordAsync()
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>().CountAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<UserTimeStamp?> GetTimestampByDateAsync(Guid keycloakId, DateTime date)
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>()
|
|
.Where(u => u.KeycloakUserId == keycloakId)
|
|
.Where(u => u.CheckIn.Date == date.Date)
|
|
.FirstOrDefaultAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<UserTimeStamp?> GetLastRecord(Guid keycloakId)
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>()
|
|
.Where(u => u.KeycloakUserId == keycloakId)
|
|
.OrderByDescending(u => u.CheckIn)
|
|
.FirstOrDefaultAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<UserTimeStamp>> GetTimeStampHistoryAsync(Guid keycloakId, int year)
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>()
|
|
.Where(u => u.KeycloakUserId == keycloakId)
|
|
.Where(u => u.CheckIn.Year == year)
|
|
.OrderBy(u => u.CheckIn)
|
|
|
|
.ToListAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<UserTimeStamp>> GetTimeStampHistoryForAdminAsync(DateTime startDate, DateTime endDate)
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>()
|
|
.Where(u => u.CheckIn.Date >= startDate.Date && u.CheckIn.Date <= endDate.Date)
|
|
.OrderBy(u => u.CheckIn)
|
|
|
|
.ToListAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<List<UserTimeStamp>> GetTimeStampHistoryForAdminRoleAsync(DateTime startDate, DateTime endDate, string role, string nodeId, int? node)
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>()
|
|
.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<UserTimeStamp?> GetTimeStampById(Guid id)
|
|
{
|
|
var data = await _dbContext.Set<UserTimeStamp>()
|
|
.Where(u => u.Id == id)
|
|
.FirstOrDefaultAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|