Files
Backend-Api/CompanyManagment.Application/RollCallEmployeeStatusApplication.cs
2025-01-22 03:40:20 +03:30

122 lines
5.4 KiB
C#

using _0_Framework.Application;
using Company.Domain.LeftWorkAgg;
using Company.Domain.RollCallEmployeeAgg;
using Company.Domain.RollCallEmployeeStatusAgg;
using CompanyManagment.App.Contracts.RollCallEmployeeStatus;
using System;
using System.Collections.Generic;
using System.Linq;
using Company.Domain.RollCallServiceAgg;
namespace CompanyManagment.Application
{
public class RollCallEmployeeStatusApplication : IRollCallEmployeeStatusApplication
{
private readonly IRollCallEmployeeStatusRepository _employeeRollCallStatusRepository;
private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository;
private readonly ILeftWorkRepository _leftWorkRepository;
private readonly IRollCallServiceRepository _rollCallServiceRepository;
public RollCallEmployeeStatusApplication(IRollCallEmployeeStatusRepository employeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, ILeftWorkRepository leftWorkRepository, IRollCallServiceRepository rollCallServiceRepository)
{
_employeeRollCallStatusRepository = employeeStatusRepository;
_rollCallEmployeeRepository = rollCallEmployeeRepository;
_leftWorkRepository = leftWorkRepository;
_rollCallServiceRepository = rollCallServiceRepository;
}
public OperationResult Create(CreateRollCallEmployeeStatus cmd)
{
OperationResult op = new();
RollCallEmployee rollCallEmployee = _rollCallEmployeeRepository.Get(cmd.RollCallEmployeeId);
if (rollCallEmployee == null)
return op.Failed("کارمند مجاز نیست");
if (!_leftWorkRepository.Exists(x => x.EmployeeId == rollCallEmployee.EmployeeId && x.WorkshopId == rollCallEmployee.WorkshopId &&
x.LeftWorkDate.Date > DateTime.Now.Date && x.StartWorkDate.Date <= DateTime.Now.Date))
return op.Failed("کارمند در کارگاه شروع به کار نکرده است");
if (_employeeRollCallStatusRepository.Exists(y =>
rollCallEmployee.id == y.RollCallEmployeeId && y.EndDate.Date > DateTime.Now.Date))
return op.Failed("کارمند فعال می باشد");
if (_employeeRollCallStatusRepository.Exists(y =>
rollCallEmployee.id == y.RollCallEmployeeId && y.EndDate.Date == DateTime.Now.Date))
{
RollCallEmployeeStatus previousStatusInDate = _employeeRollCallStatusRepository.GetByRollCallEmployeeIdAndDate(cmd.RollCallEmployeeId, DateTime.Now.Date);
previousStatusInDate.Edit(previousStatusInDate.StartDate, Tools.GetUndefinedDateTime());
}
else
{
RollCallEmployeeStatus newRecord = new(rollCallEmployee.id, DateTime.Now.Date);
_employeeRollCallStatusRepository.Create(newRecord);
}
_employeeRollCallStatusRepository.SaveChanges();
return op.Succcedded();
}
public bool HasRollCallRecord(long employeeId, long workshopId, DateTime contractStart, DateTime contractEnd)
{
//موقت
// دادمهرگستر 11
//585 کاشی گالری سرامیس (بابک ابراهیمی )
//آموزشگاه ملل 604
//کاریابی ملل 605
if(workshopId == 11 || workshopId == 585 || workshopId == 604 || workshopId == 605)
return false;
var service = _rollCallServiceRepository.GetAllServiceByWorkshopId(workshopId);
if (!service.Any(x => x.StartService.Date <= contractStart.Date && x.EndService.Date >= contractEnd.Date))
return false;
var rollCallEmployee =
_rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId,workshopId);
if (rollCallEmployee == null)
return false;
return _employeeRollCallStatusRepository.Exists(x => x.RollCallEmployeeId == rollCallEmployee.Id &&
x.StartDate.Date <= contractStart.Date &&
x.EndDate.Date >= contractEnd.Date);
}
public List<RollCallEmployeeStatusViewModel> GetActiveByWorkshopIdInDate(long workshopId, DateTime startDateGr, DateTime endDateGr)
{
return _employeeRollCallStatusRepository.GetActiveByWorkshopIdInDate(workshopId, startDateGr, endDateGr);
}
public OperationResult Deactivate(long id)
{
OperationResult op = new();
RollCallEmployeeStatus entity = _employeeRollCallStatusRepository.Get(id);
if (entity == null)
return op.Failed(ApplicationMessages.RecordNotFound);
if (!entity.EndDate.IsDateUndefined())
return op.Failed("کارمند قبلا غیر فعال شده است");
entity.Deactivate(DateTime.Now.Date);
_employeeRollCallStatusRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult Edit(EditRollCallEmployeeStatus cmd)
{
OperationResult op = new();
RollCallEmployeeStatus entity = _employeeRollCallStatusRepository.Get(cmd.Id);
if (entity == null)
return op.Failed(ApplicationMessages.RecordNotFound);
entity.Edit(cmd.StartDate, cmd.EndDate);
_employeeRollCallStatusRepository.SaveChanges();
return op.Succcedded();
}
public List<RollCallEmployeeStatusViewModel> GetAll()
{
return _employeeRollCallStatusRepository.GetAll();
}
}
}