rollcall admin report

This commit is contained in:
SamSys
2025-01-07 17:49:46 +03:30
parent 31cc61c6b9
commit 9b1f141dec
10 changed files with 297 additions and 145 deletions

View File

@@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore;
using Query.AdminReports.Models;
using System.Data;
using _0_Framework.Application;
using WorkFlow.Application.Contracts.WorkFlow;
namespace Query.AdminReports.Handlers
{
@@ -12,97 +14,102 @@ namespace Query.AdminReports.Handlers
List<WorkshopWithRollCallServiceQueryModel> Handle(WorkshopWithRollCallServiceQueryParameters parameters);
}
public class GetWorkshopWithRollCallHandler : IGetWorkshopWithRollCallHandler
public class GetWorkshopWithRollCallHandler: IGetWorkshopWithRollCallHandler
{
private readonly CompanyContext _companyContext;
private readonly CompanyContext _companyContext;
private readonly IWorkFlowApplication workFlowApplication;
public GetWorkshopWithRollCallHandler(CompanyContext companyContext)
{
_companyContext = companyContext;
}
public GetWorkshopWithRollCallHandler(CompanyContext companyContext, IWorkFlowApplication workFlowApplication)
{
_companyContext = companyContext;
this.workFlowApplication = workFlowApplication;
}
public List<WorkshopWithRollCallServiceQueryModel> Handle(WorkshopWithRollCallServiceQueryParameters parameters)
{
var now = DateTime.Now.Date;
var lastWeek = now.AddDays(-7);
public List<WorkshopWithRollCallServiceQueryModel> Handle(WorkshopWithRollCallServiceQueryParameters parameters)
{
var now = DateTime.Now.Date;
var lastWeek = now.AddDays(-7);
//workshop filter by parameters
var rollCallServiceQuery = _companyContext.RollCallServices.AsQueryable();
var allWorkshops = _companyContext.Workshops.Select(x => new { x.id, x.WorkshopFullName });
//workshop filter by parameters
var rollCallServiceQuery = _companyContext.RollCallServices.AsQueryable();
if (!string.IsNullOrWhiteSpace(parameters.WorkshopName))
allWorkshops = allWorkshops.Where(x => x.WorkshopFullName.Contains(parameters.WorkshopName));
var allWorkshops = _companyContext.Workshops.Select(x => new { x.id, x.WorkshopFullName });
if (!string.IsNullOrWhiteSpace(parameters.RollCallServiceType))
rollCallServiceQuery = rollCallServiceQuery.Where(x => x.ServiceType == parameters.RollCallServiceType);
if (!string.IsNullOrWhiteSpace(parameters.IsActive))
{
if (parameters.IsActive.ToLower() == "active")
rollCallServiceQuery = rollCallServiceQuery.Where(x => x.StartService.Date <= now && x.EndService.Date >= now);
else if (parameters.IsActive.ToLower() == "deactive")
rollCallServiceQuery = rollCallServiceQuery.Where(x => x.EndService.Date < now || x.StartService.Date > now);
if (parameters.WorkshopId != 0)
allWorkshops = allWorkshops.Where(x => x.id == parameters.WorkshopId);
}
var workshopsWithService = rollCallServiceQuery.Join(allWorkshops, x => x.WorkshopId, y => y.id, (rcs, workshop) =>
new WorkshopWithRollCallServiceQueryModel()
{
WorkshopId = workshop.id,
RollCallServiceType = rcs.ServiceType,
WorkshopName = workshop.WorkshopFullName,
MaxPersonValid = rcs.MaxPersonValid,
IsActive = rcs.StartService <= DateTime.Now && rcs.EndService >= DateTime.Now,
ServiceStart = rcs.StartService,
ServiceEnd = rcs.EndService
if (!string.IsNullOrWhiteSpace(parameters.WorkshopName))
allWorkshops = allWorkshops.Where(x => x.WorkshopFullName.Contains(parameters.WorkshopName));
if (!string.IsNullOrWhiteSpace(parameters.RollCallServiceType))
rollCallServiceQuery = rollCallServiceQuery.Where(x => x.ServiceType == parameters.RollCallServiceType);
if (parameters.FilterMode == FilterMode.Active)
rollCallServiceQuery = rollCallServiceQuery.Where(x => x.StartService.Date <= now && x.EndService.Date >= now);
else if(parameters.FilterMode == FilterMode.DeActive)
rollCallServiceQuery = rollCallServiceQuery.Where(x => x.EndService.Date < now || x.StartService.Date > now);
var workshopsWithService = rollCallServiceQuery.Join(allWorkshops, x => x.WorkshopId, y => y.id, (rcs, workshop) =>
new WorkshopWithRollCallServiceQueryModel()
{
WorkshopId = workshop.id,
RollCallServiceType = rcs.ServiceType,
WorkshopName = workshop.WorkshopFullName,
MaxPersonValid = rcs.MaxPersonValid,
IsActive = rcs.StartService <= DateTime.Now && rcs.EndService >= DateTime.Now,
ServiceStart = rcs.StartService,
ServiceEnd = rcs.EndService
});
//workshop population
var workshopLeftWorks = _companyContext.Workshops.Where(x => workshopsWithService.Any(y => y.WorkshopId == x.id)).Include(x => x.LeftWorks).Include(x => x.LeftWorkInsurances)
.Select(x => new
{
WorkshopId = x.id,
LeftWorks = x.LeftWorks.Where(y => y.StartWorkDate <= lastWeek && y.LeftWorkDate > now).Select(y => y.EmployeeId),
LeftWorkInsurances = x.LeftWorkInsurances.Where(y => y.StartWorkDate <= lastWeek && (y.LeftWorkDate > now || y.LeftWorkDate == null)).Select(y => y.EmployeeId)
}).ToList();
//workshop population
var workshopLeftWorks = _companyContext.Workshops.Where(x => workshopsWithService.Any(y => y.WorkshopId == x.id)).Include(x => x.LeftWorks).Include(x => x.LeftWorkInsurances)
.Select(x => new
{
WorkshopId = x.id,
LeftWorks = x.LeftWorks.Where(y => y.StartWorkDate <= lastWeek && y.LeftWorkDate > now).Select(y => y.EmployeeId),
LeftWorkInsurances = x.LeftWorkInsurances.Where(y => y.StartWorkDate <= lastWeek && (y.LeftWorkDate > now || y.LeftWorkDate == null)).Select(y => y.EmployeeId)
}).ToList();
var workshopsWorkingEmployeesList = workshopLeftWorks
.Select(x => new { x.WorkshopId, TotalWorkingEmployeesCount = x.LeftWorks.Concat(x.LeftWorkInsurances).Distinct().Count() }).ToList();
var workshopsWorkingEmployeesList = workshopLeftWorks
.Select(x => new { x.WorkshopId, TotalWorkingEmployeesCount = x.LeftWorks.Concat(x.LeftWorkInsurances).Distinct().Count() }).ToList();
var activeEmployees = _companyContext.RollCallEmployees.Include(x => x.EmployeesStatus).Where(x => x.EmployeesStatus.Any(y =>
y.EndDate.Date >= now) && workshopsWithService.Any(y => y.WorkshopId == x.WorkshopId)).Select(x => new { x.WorkshopId, x.EmployeeId });
var activeEmployees = _companyContext.RollCallEmployees.Include(x => x.EmployeesStatus).Where(x => x.EmployeesStatus.Any(y =>
y.EndDate.Date >= now) && workshopsWithService.Any(y => y.WorkshopId == x.WorkshopId)).Select(x => new { x.WorkshopId, x.EmployeeId });
var lastWeekRollCalls = _companyContext.RollCalls.Where(x => x.StartDate.HasValue && x.StartDate.Value >= lastWeek
&& workshopsWithService.Any(y => y.WorkshopId == x.WorkshopId)).GroupBy(x => x.EmployeeId).Select(x => x.Key);
var lastWeekRollCalls = _companyContext.RollCalls.Where(x => x.StartDate.HasValue && x.StartDate.Value >= lastWeek
&& workshopsWithService.Any(y => y.WorkshopId == x.WorkshopId)).GroupBy(x => x.EmployeeId).Select(x => x.Key);
var leaves = _companyContext.LeaveList.Where(x => x.StartLeave <= lastWeek && x.EndLeave >= now && (x.LeaveType == "استعلاجی" || x.PaidLeaveType == "روزانه")
&& workshopsWithService.Any(y => y.WorkshopId == x.WorkshopId)).Select(x => x.EmployeeId);
var leaves = _companyContext.LeaveList.Where(x => x.StartLeave <= lastWeek && x.EndLeave >= now && (x.LeaveType == "استعلاجی" || x.PaidLeaveType == "روزانه")
&& workshopsWithService.Any(y => y.WorkshopId == x.WorkshopId)).Select(x => x.EmployeeId);
var activeEmployeesList = activeEmployees.ToList();
var lastWeekRollCallsList = lastWeekRollCalls.ToList();
var leavesList = leaves.ToList();
var workshopsWithServiceList = workshopsWithService.ToList();
return workshopsWithServiceList.GroupBy(x => x.WorkshopId).Select(x =>
x.OrderByDescending(y => y.ServiceStart).First()).Select(x => new WorkshopWithRollCallServiceQueryModel()
{
IsActive = x.IsActive,
ServiceStartFa = x.ServiceStart.ToFarsi(),
ServiceEndFa = x.ServiceEnd.ToFarsi(),
WorkshopId = x.WorkshopId,
RollCallServiceType = x.RollCallServiceType,
MaxPersonValid = x.MaxPersonValid,
WorkshopName = x.WorkshopName,
ActiveEmployeesCount = activeEmployeesList.Count(y => y.WorkshopId == x.WorkshopId),
ActiveEmployeesWithRollCallInLastWeekCount = activeEmployeesList.Count(y => y.WorkshopId == x.WorkshopId &&
lastWeekRollCallsList.Contains(y.EmployeeId) && !leavesList.Contains(y.EmployeeId)),
TotalEmployeesCount = workshopsWorkingEmployeesList.FirstOrDefault(y => y.WorkshopId == x.WorkshopId)?.TotalWorkingEmployeesCount ?? 0
}).OrderByDescending(x => x.IsActive).ToList();
var activeEmployeesList = activeEmployees.ToList();
var lastWeekRollCallsList = lastWeekRollCalls.ToList();
var leavesList = leaves.ToList();
var workshopsWithServiceList = workshopsWithService.ToList();
return workshopsWithServiceList.GroupBy(x=>x.WorkshopId)
.Select(x=>x.OrderByDescending(y=>y.ServiceStartFa).First())
.Select(x => new WorkshopWithRollCallServiceQueryModel()
{
IsActive = x.IsActive,
ServiceStartFa = x.ServiceStart.ToFarsi(),
ServiceEndFa = x.ServiceEnd.ToFarsi(),
WorkshopId = x.WorkshopId,
RollCallServiceType = x.RollCallServiceType,
MaxPersonValid = x.MaxPersonValid,
WorkshopName = x.WorkshopName,
ActiveEmployeesCount = activeEmployeesList.Count(y => y.WorkshopId == x.WorkshopId),
ActiveEmployeesWithRollCallInLastWeekCount = activeEmployeesList.Count(y => y.WorkshopId == x.WorkshopId &&
lastWeekRollCallsList.Contains(y.EmployeeId) && !leavesList.Contains(y.EmployeeId)),
TotalEmployeesCount = workshopsWorkingEmployeesList.FirstOrDefault(y => y.WorkshopId == x.WorkshopId)?.TotalWorkingEmployeesCount ?? 0,
UndoneWorkFlowsCount = workFlowApplication.GetAllWorkFlowCount(x.WorkshopId).Result
}).OrderByDescending(x=>x.IsActive).ToList();
}
}
}
}
}

View File

@@ -1,32 +1,34 @@
namespace Query.AdminReports.Models
{
public class WorkshopWithRollCallServiceQueryModel
{
public string WorkshopName { get; set; }
public long WorkshopId { get; set; }
public string RollCallServiceType { get; set; }
public class WorkshopWithRollCallServiceQueryModel
{
public string WorkshopName { get; set; }
public long WorkshopId { get; set; }
public string RollCallServiceType { get; set; }
public int TotalEmployeesCount { get; set; }
public int TotalEmployeesCount { get; set; }
public int ActiveEmployeesCount { get; set; }
public int ActiveEmployeesCount { get; set; }
public int ActiveEmployeesWithRollCallInLastWeekCount { get; set; }
public int ActiveEmployeesWithRollCallInLastWeekCount { get; set; }
public int UndoneWorkFlowsCount { get; set; }
public float ActiveEmployeesWithRollCallPercentage
{
get
{
return ((float)ActiveEmployeesWithRollCallInLastWeekCount / ActiveEmployeesCount) * 100;
}
}
public float ActiveEmployeesWithRollCallPercentage
{
get
{
return ((float)ActiveEmployeesWithRollCallInLastWeekCount / ActiveEmployeesCount) * 100;
}
}
public int MaxPersonValid { get; set; }
public bool IsActive { get; set; }
public DateTime ServiceStart { get; set; }
public DateTime ServiceEnd { get; set; }
public int MaxPersonValid { get; set; }
public bool IsActive { get; set; }
public DateTime ServiceStart { get; set; }
public DateTime ServiceEnd { get; set; }
public string ServiceStartFa { get; set; }
public string ServiceEndFa { get; set; }
}
}
public string ServiceStartFa { get; set; }
public string ServiceEndFa { get; set; }
}
}

View File

@@ -2,7 +2,15 @@
public class WorkshopWithRollCallServiceQueryParameters
{
public string WorkshopName { get; set; }
public string RollCallServiceType { get; set; }
public string IsActive { get; set; }
}
public string WorkshopName { get; set; }
public long WorkshopId { get; set; }
public string RollCallServiceType { get; set; }
public FilterMode FilterMode { get; set; } = FilterMode.All;
}
public enum FilterMode
{
All,
Active,
DeActive
}

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\CompanyManagment.EFCore\CompanyManagment.EFCore.csproj" />
<ProjectReference Include="..\WorkFlow\Application\WorkFlow.Application.Contracts\WorkFlow.Application.Contracts.csproj" />
</ItemGroup>
</Project>