using _0_Framework.Application; using _0_Framework.InfraStructure; using Company.Domain.RollCallEmployeeStatusAgg; using CompanyManagment.App.Contracts.RollCallEmployeeStatus; using System; using System.Collections.Generic; using System.Linq; using CompanyManagment.App.Contracts.RollCallEmployee; using Microsoft.EntityFrameworkCore; namespace CompanyManagment.EFCore.Repository; public class RollCallEmployeeStatusRepository : RepositoryBase, IRollCallEmployeeStatusRepository { private readonly CompanyContext _context; public RollCallEmployeeStatusRepository(CompanyContext context) : base(context) { _context = context; } #region Pooya public List GetAll() { return _context.RollCallEmployeesStatus.Select(x => new RollCallEmployeeStatusViewModel() { StartDate = x.StartDate.ToFarsi(), EndDate = x.EndDate.ToFarsi(), Id = x.id }).ToList(); } /// /// برای تغییر بازه فعالبت هنگام ترک کار /// public void AdjustRollCallStatusEndDates(List command) { //status ids var statusIds = command.Select(x => x.RollCallStatusId); //fetch by ids var finalList = _context.RollCallEmployeesStatus.Where(x => statusIds.Contains(x.id)).AsEnumerable(); //get the statuses which have higher end date than employee's leftwork finalList.Where(x => command.Any(y => !y.LeaveDate.IsDateUndefined() && y.LeaveDate.Date < x.EndDate.Date)).ToList().ForEach( z => { var cmd = command.FirstOrDefault(y => y.RollCallStatusId == z.id); if (cmd!.LeaveDate.Date > z.StartDate.Date) { z.Edit(z.StartDate, cmd!.LeaveDate.Date); } } ); _context.SaveChanges(); } public bool IsActiveInPeriod(long employeeId, long workshopId, DateTime start, DateTime end) { var statuses = _context.RollCallEmployees.Include(x => x.EmployeesStatus) .Select(x => new { x.WorkshopId, x.EmployeeId, x.EmployeesStatus }) .FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId)?.EmployeesStatus; if (statuses == null || !statuses.Any(x => x.StartDate <= start && x.EndDate >= end)) return false; return true; } public RollCallEmployeeStatus GetByRollCallEmployeeIdAndDate(long rollCallEmployeeId, DateTime date) { return _context.RollCallEmployeesStatus.FirstOrDefault(x => x.RollCallEmployeeId == rollCallEmployeeId && x.StartDate.Date <= date.Date && x.EndDate.Date >= date.Date); } public List GetActiveByWorkshopIdInDate(long workshopId, DateTime startDateGr, DateTime endDateGr) { return _context.RollCallEmployeesStatus.AsSplitQuery().Include(x => x.RollCallEmployee).Where(x => x.RollCallEmployee.WorkshopId == workshopId && x.StartDate.Date <= endDateGr.Date && x.EndDate.Date >= startDateGr.Date).Select(x => new RollCallEmployeeStatusViewModel() { EndDateGr = x.EndDate, EndDate = x.EndDate.ToFarsi(), Id = x.id, StartDateGr = x.StartDate, StartDate = x.StartDate.ToFarsi(), EmployeeId = x.RollCallEmployee.EmployeeId, EmployeeName = x.RollCallEmployee.EmployeeFullName }).ToList(); } public List GetByWorkshopIdInDates(long workshopId, DateTime start, DateTime end) { return _context.RollCallEmployeesStatus.Include(entity => entity.RollCallEmployee).Where(status => status.RollCallEmployee.WorkshopId == workshopId && status.EndDate.Date >= start.Date && status.StartDate.Date <= end.Date) .Select(entity => new RollCallEmployeeStatusViewModel() { EndDateGr = entity.EndDate, EndDate = entity.EndDate.ToFarsi(), Id = entity.id, StartDateGr = entity.StartDate, StartDate = entity.StartDate.ToFarsi(), EmployeeId = entity.RollCallEmployee.EmployeeId, EmployeeName = entity.RollCallEmployee.EmployeeFullName }).ToList(); } #endregion }