add sync employee status with left work method on rollcall employee upload picture

This commit is contained in:
2025-06-02 16:29:03 +03:30
parent f26fcba165
commit 7c1fe65cf2
5 changed files with 202 additions and 162 deletions

View File

@@ -38,4 +38,5 @@ public interface IRollCallEmployeeApplication
(int activeEmployees, int deActiveEmployees) GetActiveAndDeActiveRollCallEmployees(long workshopId); (int activeEmployees, int deActiveEmployees) GetActiveAndDeActiveRollCallEmployees(long workshopId);
bool HasEmployees(long workshopId); bool HasEmployees(long workshopId);
#endregion #endregion
} }

View File

@@ -17,5 +17,7 @@ namespace CompanyManagment.App.Contracts.RollCallEmployeeStatus
List<RollCallEmployeeStatusViewModel> GetActiveByWorkshopIdInDate(long workshopId, DateTime startDateGr, DateTime endDateGr); List<RollCallEmployeeStatusViewModel> GetActiveByWorkshopIdInDate(long workshopId, DateTime startDateGr, DateTime endDateGr);
bool IsActiveInPeriod(long employeeId, long workshopId, DateTime startDate, DateTime endDate); bool IsActiveInPeriod(long employeeId, long workshopId, DateTime startDate, DateTime endDate);
} void SyncRollCallEmployeeWithLeftWork(long rollCallEmployeeId);
}
} }

View File

@@ -17,204 +17,206 @@ namespace CompanyManagment.Application;
public class RollCallEmployeeApplication : IRollCallEmployeeApplication public class RollCallEmployeeApplication : IRollCallEmployeeApplication
{ {
private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository; private readonly IRollCallEmployeeRepository _rollCallEmployeeRepository;
private readonly IRollCallEmployeeStatusApplication _rollCallEmployeeStatusApplication; private readonly IRollCallEmployeeStatusApplication _rollCallEmployeeStatusApplication;
private readonly IEmployeeRepository _employeeRepository; private readonly IEmployeeRepository _employeeRepository;
private readonly ILeftWorkRepository _leftWorkRepository; private readonly ILeftWorkRepository _leftWorkRepository;
private readonly IRollCallEmployeeStatusRepository _rollCallEmployeeStatusRepository; private readonly IRollCallEmployeeStatusRepository _rollCallEmployeeStatusRepository;
private readonly IWebHostEnvironment _webHostEnvironment; private readonly IWebHostEnvironment _webHostEnvironment;
public RollCallEmployeeApplication(IRollCallEmployeeRepository rollCallEmployeeRepository, IEmployeeRepository employeeRepository, IRollCallEmployeeStatusApplication rollCallEmployeeStatusApplication, ILeftWorkRepository leftWorkRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, IWebHostEnvironment webHostEnvironment) public RollCallEmployeeApplication(IRollCallEmployeeRepository rollCallEmployeeRepository, IEmployeeRepository employeeRepository, IRollCallEmployeeStatusApplication rollCallEmployeeStatusApplication, ILeftWorkRepository leftWorkRepository, IRollCallEmployeeStatusRepository rollCallEmployeeStatusRepository, IWebHostEnvironment webHostEnvironment)
{ {
_rollCallEmployeeRepository = rollCallEmployeeRepository; _rollCallEmployeeRepository = rollCallEmployeeRepository;
_employeeRepository = employeeRepository; _employeeRepository = employeeRepository;
_rollCallEmployeeStatusApplication = rollCallEmployeeStatusApplication; _rollCallEmployeeStatusApplication = rollCallEmployeeStatusApplication;
_leftWorkRepository = leftWorkRepository; _leftWorkRepository = leftWorkRepository;
_rollCallEmployeeStatusRepository = rollCallEmployeeStatusRepository; _rollCallEmployeeStatusRepository = rollCallEmployeeStatusRepository;
_webHostEnvironment = webHostEnvironment; _webHostEnvironment = webHostEnvironment;
} }
public OperationResult Create(CreateRollCallEmployee command) public OperationResult Create(CreateRollCallEmployee command)
{ {
var opreation = new OperationResult(); var opreation = new OperationResult();
if (_rollCallEmployeeRepository.Exists(x => if (_rollCallEmployeeRepository.Exists(x =>
x.EmployeeId == command.EmployeeId && x.WorkshopId == command.EmployeeId)) x.EmployeeId == command.EmployeeId && x.WorkshopId == command.EmployeeId))
return opreation.Succcedded(); return opreation.Succcedded();
var fullname = _employeeRepository.GetDetails(command.EmployeeId); var fullname = _employeeRepository.GetDetails(command.EmployeeId);
if (fullname == null) if (fullname == null)
return opreation.Failed("پرسنل یافت نشد"); return opreation.Failed("پرسنل یافت نشد");
var create = new RollCallEmployee(command.WorkshopId, command.EmployeeId, fullname.FName, fullname.LName); var create = new RollCallEmployee(command.WorkshopId, command.EmployeeId, fullname.FName, fullname.LName);
_rollCallEmployeeRepository.Create(create); _rollCallEmployeeRepository.Create(create);
if (command.HasUploadedImage == "true") if (command.HasUploadedImage == "true")
create.HasImage(); create.HasImage();
_rollCallEmployeeRepository.SaveChanges(); _rollCallEmployeeRepository.SaveChanges();
return opreation.Succcedded(create.id); return opreation.Succcedded(create.id);
} }
public OperationResult Active(long id) public OperationResult Active(long id)
{ {
var opreation = new OperationResult(); var opreation = new OperationResult();
var emp = _rollCallEmployeeRepository.Get(id); var emp = _rollCallEmployeeRepository.Get(id);
if (emp == null) if (emp == null)
return opreation.Failed("پرسنل یافت نشد"); return opreation.Failed("پرسنل یافت نشد");
if (!_leftWorkRepository.Exists(x => x.EmployeeId == emp.EmployeeId && x.WorkshopId == emp.WorkshopId && if (!_leftWorkRepository.Exists(x => x.EmployeeId == emp.EmployeeId && x.WorkshopId == emp.WorkshopId &&
x.StartWorkDate <= DateTime.Now && x.LeftWorkDate > DateTime.Now)) x.StartWorkDate <= DateTime.Now && x.LeftWorkDate > DateTime.Now))
return opreation.Failed("کارمند شروع به کار ندارد"); return opreation.Failed("کارمند شروع به کار ندارد");
if (emp.HasUploadedImage == "false") if (emp.HasUploadedImage == "false")
return opreation.Failed("لطفا ابتدا عکس پرسنل را آپلود کنید"); return opreation.Failed("لطفا ابتدا عکس پرسنل را آپلود کنید");
using var transaction = new TransactionScope(); using var transaction = new TransactionScope();
emp.Active(); emp.Active();
var operation2 = _rollCallEmployeeStatusApplication.Create(new CreateRollCallEmployeeStatus() { RollCallEmployeeId = id }); var operation2 = _rollCallEmployeeStatusApplication.Create(new CreateRollCallEmployeeStatus() { RollCallEmployeeId = id });
if (operation2.IsSuccedded == false) if (operation2.IsSuccedded == false)
return operation2; return operation2;
_rollCallEmployeeRepository.SaveChanges(); _rollCallEmployeeRepository.SaveChanges();
transaction.Complete(); transaction.Complete();
return operation2; return operation2;
} }
public OperationResult DeActive(long id) public OperationResult DeActive(long id)
{ {
var opreation = new OperationResult(); var opreation = new OperationResult();
var emp = _rollCallEmployeeRepository.GetWithRollCallStatus(id); var emp = _rollCallEmployeeRepository.GetWithRollCallStatus(id);
if (emp == null) if (emp == null)
return opreation.Failed("پرسنل یافت نشد"); return opreation.Failed("پرسنل یافت نشد");
var lastStatus = emp.EmployeesStatus.MaxBy(x => x.StartDate); var lastStatus = emp.EmployeesStatus.MaxBy(x => x.StartDate);
emp.DeActive(); emp.DeActive();
_rollCallEmployeeRepository.SaveChanges(); _rollCallEmployeeRepository.SaveChanges();
_rollCallEmployeeStatusApplication.Deactivate(lastStatus.id); _rollCallEmployeeStatusApplication.Deactivate(lastStatus.id);
return opreation.Succcedded(); return opreation.Succcedded();
} }
public OperationResult UploadedImage(long Employeeid, long WorkshopId) public OperationResult UploadedImage(long Employeeid, long WorkshopId)
{ {
var opreation = new OperationResult(); var opreation = new OperationResult();
var emp = _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(Employeeid, WorkshopId); var emp = _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(Employeeid, WorkshopId);
if (emp == null) if (emp == null)
return opreation.Failed("پرسنل یافت نشد"); return opreation.Failed("پرسنل یافت نشد");
var rollCall = _rollCallEmployeeRepository.Get(emp.Id); var rollCall = _rollCallEmployeeRepository.Get(emp.Id);
rollCall.HasImage(); rollCall.HasImage();
_rollCallEmployeeRepository.SaveChanges(); _rollCallEmployeeRepository.SaveChanges();
var path = Path.Combine(_webHostEnvironment.ContentRootPath, "Faces", WorkshopId.ToString(), Employeeid.ToString()); var path = Path.Combine(_webHostEnvironment.ContentRootPath, "Faces", WorkshopId.ToString(), Employeeid.ToString());
var thumbnailPath = Path.Combine(path, "Thumbnail.jpg"); var thumbnailPath = Path.Combine(path, "Thumbnail.jpg");
try try
{ {
var thumbnail = Tools.ResizeImage(Path.Combine(path, "1.jpg"), 150, 150); var thumbnail = Tools.ResizeImage(Path.Combine(path, "1.jpg"), 150, 150);
System.IO.File.WriteAllBytes(thumbnailPath, Convert.FromBase64String(thumbnail)); System.IO.File.WriteAllBytes(thumbnailPath, Convert.FromBase64String(thumbnail));
} }
catch catch
{ {
// ignored // ignored
} }
return opreation.Succcedded(); return opreation.Succcedded();
} }
public List<RollCallEmployeeViewModel> GetByWorkshopId(long workshopId) public List<RollCallEmployeeViewModel> GetByWorkshopId(long workshopId)
{ {
return _rollCallEmployeeRepository.GetByWorkshopId(workshopId); return _rollCallEmployeeRepository.GetByWorkshopId(workshopId);
} }
public EditRollCallEmployee GetDetails(long id) public EditRollCallEmployee GetDetails(long id)
{ {
return _rollCallEmployeeRepository.GetDetails(id); return _rollCallEmployeeRepository.GetDetails(id);
} }
public RollCallEmployeeViewModel GetByEmployeeIdAndWorkshopId(long employeeId, long workshopId) public RollCallEmployeeViewModel GetByEmployeeIdAndWorkshopId(long employeeId, long workshopId)
{ {
return _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId, workshopId); return _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId, workshopId);
} }
public List<RollCallEmployeeViewModel> GetPersonnelRollCallListPaginate(RollCallEmployeeSearchModel command) public List<RollCallEmployeeViewModel> GetPersonnelRollCallListPaginate(RollCallEmployeeSearchModel command)
{ {
return _rollCallEmployeeRepository.GetPersonnelRollCallListPaginate(command); return _rollCallEmployeeRepository.GetPersonnelRollCallListPaginate(command);
} }
public int activedPerson(long workshopId) public int activedPerson(long workshopId)
{ {
return _rollCallEmployeeRepository.activedPerson(workshopId); return _rollCallEmployeeRepository.activedPerson(workshopId);
} }
#region Pooya #region Pooya
public List<RollCallEmployeeViewModel> GetRollCallEmployeesByWorkshopId(long workshopId) public List<RollCallEmployeeViewModel> GetRollCallEmployeesByWorkshopId(long workshopId)
{ {
return _rollCallEmployeeRepository.GetRollCallEmployeesByWorkshopId(workshopId); return _rollCallEmployeeRepository.GetRollCallEmployeesByWorkshopId(workshopId);
} }
public List<RollCallEmployeeViewModel> GetActivePersonnelByWorkshopId(long workshopId) public List<RollCallEmployeeViewModel> GetActivePersonnelByWorkshopId(long workshopId)
{ {
return _rollCallEmployeeRepository.GetActivePersonnelByWorkshopId(workshopId); return _rollCallEmployeeRepository.GetActivePersonnelByWorkshopId(workshopId);
} }
public bool IsEmployeeRollCallActive(long employeeId, long workshopId) public bool IsEmployeeRollCallActive(long employeeId, long workshopId)
{ {
RollCallEmployeeViewModel rollCallEmployee = _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId, workshopId); RollCallEmployeeViewModel rollCallEmployee = _rollCallEmployeeRepository.GetByEmployeeIdAndWorkshopId(employeeId, workshopId);
if (rollCallEmployee == null || rollCallEmployee.IsActiveString != "true" || rollCallEmployee.HasUploadedImage != "true") if (rollCallEmployee == null || rollCallEmployee.IsActiveString != "true" || rollCallEmployee.HasUploadedImage != "true")
return false; return false;
var now = DateTime.Now; var now = DateTime.Now;
return _rollCallEmployeeStatusRepository.Exists(x => x.RollCallEmployeeId == rollCallEmployee.Id && x.StartDate < now && x.EndDate > now); return _rollCallEmployeeStatusRepository.Exists(x => x.RollCallEmployeeId == rollCallEmployee.Id && x.StartDate < now && x.EndDate > now);
}
public List<RollCallEmployeeViewModel> GetEmployeeRollCalls(long workshopId)
{
return _rollCallEmployeeRepository.GetEmployeeRollCalls(workshopId);
}
public (int activeEmployees, int deActiveEmployees) GetActiveAndDeActiveRollCallEmployees(long workshopId)
{
return _rollCallEmployeeRepository.GetActiveAndDeActiveRollCallEmployees(workshopId);
}
public List<RollCallEmployeeViewModel> GetPersonnelRollCallListAll(long workshopId)
{
return _rollCallEmployeeRepository.GetPersonnelRollCallListAll(workshopId);
}
public bool HasEmployees(long workshopId)
{
return _rollCallEmployeeRepository.HasEmployees(workshopId);
}
public OperationResult ChangeEmployeeRollCallName(long rollCallEmployeeId, string fName, string lName)
{
OperationResult result = new();
if (string.IsNullOrWhiteSpace(lName) || string.IsNullOrWhiteSpace(fName))
return result.Failed("نام و نام خانوادگی نمی توانند خالی باشند");
fName = fName.Trim();
lName = lName.Trim();
var fullName = $"{fName} {lName}";
var entity = _rollCallEmployeeRepository.Get(rollCallEmployeeId);
if (entity == null)
return result.Failed(ApplicationMessages.RecordNotFound);
if (_rollCallEmployeeRepository.Exists(x => x.WorkshopId == entity.WorkshopId && x.EmployeeFullName == fullName && x.id != rollCallEmployeeId)) }
return result.Failed("نام و نام خانوادگی کارمند نمی تواند با نام و نام خانوادگی کارمند دیگری در آن کارگاه یکسان باشد");
if (entity.IsActiveString != "true") public List<RollCallEmployeeViewModel> GetEmployeeRollCalls(long workshopId)
return result.Failed("امکان تغییر نام برای کارمند غیر فعال وجود ندارد"); {
return _rollCallEmployeeRepository.GetEmployeeRollCalls(workshopId);
}
public (int activeEmployees, int deActiveEmployees) GetActiveAndDeActiveRollCallEmployees(long workshopId)
{
return _rollCallEmployeeRepository.GetActiveAndDeActiveRollCallEmployees(workshopId);
}
public List<RollCallEmployeeViewModel> GetPersonnelRollCallListAll(long workshopId)
{
return _rollCallEmployeeRepository.GetPersonnelRollCallListAll(workshopId);
}
public bool HasEmployees(long workshopId)
{
return _rollCallEmployeeRepository.HasEmployees(workshopId);
}
public OperationResult ChangeEmployeeRollCallName(long rollCallEmployeeId, string fName, string lName)
{
OperationResult result = new();
if (string.IsNullOrWhiteSpace(lName) || string.IsNullOrWhiteSpace(fName))
return result.Failed("نام و نام خانوادگی نمی توانند خالی باشند");
fName = fName.Trim();
lName = lName.Trim();
var fullName = $"{fName} {lName}";
var entity = _rollCallEmployeeRepository.Get(rollCallEmployeeId);
entity.ChangeName(fName, lName); if (entity == null)
_rollCallEmployeeRepository.SaveChanges(); return result.Failed(ApplicationMessages.RecordNotFound);
return result.Succcedded();
} if (_rollCallEmployeeRepository.Exists(x => x.WorkshopId == entity.WorkshopId && x.EmployeeFullName == fullName && x.id != rollCallEmployeeId))
return result.Failed("نام و نام خانوادگی کارمند نمی تواند با نام و نام خانوادگی کارمند دیگری در آن کارگاه یکسان باشد");
if (entity.IsActiveString != "true")
return result.Failed("امکان تغییر نام برای کارمند غیر فعال وجود ندارد");
entity.ChangeName(fName, lName);
_rollCallEmployeeRepository.SaveChanges();
return result.Succcedded();
}
#endregion #endregion
} }

View File

@@ -134,5 +134,38 @@ namespace CompanyManagment.Application
{ {
return _employeeRollCallStatusRepository.GetAll(); return _employeeRollCallStatusRepository.GetAll();
} }
}
public void SyncRollCallEmployeeWithLeftWork(long rollCallEmployeeId)
{
var rollCallEmployee = _rollCallEmployeeRepository.GetWithRollCallStatus(rollCallEmployeeId);
if (rollCallEmployee == null)
return;
var rollCallStatus = rollCallEmployee.EmployeesStatus.MaxBy(x => x.StartDate);
if (rollCallStatus == null)
return;
var today = DateTime.Today;
var firstDayOfMonth = today.FindFirstDayOfMonthGr();
var employeeId = rollCallEmployee.EmployeeId;
var workshopId = rollCallEmployee.WorkshopId;
var leftWork = _leftWorkRepository.GetLastLeftWorkByEmployeeIdAndWorkshopId(workshopId, employeeId);
if (leftWork == null)
return;
var startWork = leftWork.StartWorkDate;
rollCallStatus.Edit(startWork < firstDayOfMonth ? firstDayOfMonth : startWork, rollCallStatus.EndDate);
_employeeRollCallStatusRepository.SaveChanges();
}
}
} }

View File

@@ -360,8 +360,10 @@ namespace ServiceHost.Areas.Client.Pages.Company.RollCall
message = createRollCallEmployeeStatus.Message, message = createRollCallEmployeeStatus.Message,
}); });
} }
} _rollCallEmployeeStatusApplication.SyncRollCallEmployeeWithLeftWork(result.SendId);
else
}
else
{ {
if ( rollCallEmployee.Statuses == null || rollCallEmployee.Statuses?.Any(x => x.StartDateGr <= DateTime.Now.Date && x.EndDateGr >= DateTime.Now.Date)== false) if ( rollCallEmployee.Statuses == null || rollCallEmployee.Statuses?.Any(x => x.StartDateGr <= DateTime.Now.Date && x.EndDateGr >= DateTime.Now.Date)== false)
{ {