Files
Backend-Api/CompanyManagment.Application/RollCallEmployeeStatusApplication.cs
2024-08-29 15:00:22 +03:30

75 lines
3.1 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;
namespace CompanyManagment.Application
{
public class RollCallEmployeeStatusApplication : IRollCallEmployeeStatusApplication
{
private readonly IRollCallEmployeeStatusRepository _employeeRollCallStatusRepository;
private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository;
private readonly ILeftWorkRepository _leftWorkRepository;
public RollCallEmployeeStatusApplication(IRollCallEmployeeStatusRepository employeeStatusRepository, IRollCallEmployeeRepository rollCallEmployeeRepository, ILeftWorkRepository leftWorkRepository)
{
_employeeRollCallStatusRepository = employeeStatusRepository;
_rollCallEmployeeRepository = rollCallEmployeeRepository;
_leftWorkRepository = leftWorkRepository;
}
public OperationResult Create(CreateRollCallEmployeeStatus cmd)
{
OperationResult op = new();
RollCallEmployee employee = _rollCallEmployeeRepository.Get(cmd.RollCallEmployeeId);
if (employee == null)
return op.Failed("کارمند مجاز نیست");
if (!_leftWorkRepository.Exists(x => x.EmployeeId == employee.EmployeeId && x.WorkshopId == employee.WorkshopId &&
x.LeftWorkDate.Date > DateTime.Now.Date && x.StartWorkDate.Date < DateTime.Now.Date))
return op.Failed("کارمند در کارگاه شروع به کار نکرده است");
if (_employeeRollCallStatusRepository.Exists(x => x.EndDate >= DateTime.Now.Date && employee.id == x.RollCallEmployeeId))
return op.Failed("تکراری است");
RollCallEmployeeStatus newRecord = new(employee.id, DateTime.Now.Date);
_employeeRollCallStatusRepository.Create(newRecord);
_employeeRollCallStatusRepository.SaveChanges();
return op.Succcedded();
}
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();
}
}
}