Files
Backend-Api/Company.Domain/RollCallAgg/DomainService/IRollCallDomainService.cs
2025-01-21 02:41:44 +03:30

144 lines
6.5 KiB
C#

using _0_Framework.Domain.CustomizeCheckoutShared.Base;
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
using _0_Framework.Domain.CustomizeCheckoutShared.ValueObjects;
using Company.Domain.CustomizeWorkshopEmployeeSettingsAgg;
using Company.Domain.CustomizeWorkshopSettingsAgg;
using System.Collections.Generic;
using System;
using System.Linq;
using _0_Framework.Application;
using OfficeOpenXml.Drawing.Chart;
namespace Company.Domain.RollCallAgg.DomainService;
public interface IRollCallDomainService
{
(WorkshopShiftStatus shiftType, IrregularShift irregularShift, ICollection<CustomizeSifts> regularShifts, ICollection<CustomizeRotatingShift> rotatingShifts, TimeSpan BreakTime) GetEmployeeShiftDetails(long employeeId, long workshopId);
TimeOnly GetEmployeeOffSetForRegularSettings(long employeeId, long workshopId);
}
public class RollCallDomainService : IRollCallDomainService
{
private readonly IRollCallRepository _rollCallRepository;
private readonly ICustomizeWorkshopEmployeeSettingsRepository _customizeWorkshopEmployeeSettingsRepository;
private readonly ICustomizeWorkshopSettingsRepository _customizeWorkshopSettingsRepository;
public RollCallDomainService(IRollCallRepository rollCallRepository, ICustomizeWorkshopEmployeeSettingsRepository customizeWorkshopEmployeeSettingsRepository, ICustomizeWorkshopSettingsRepository customizeWorkshopSettingsRepository)
{
_rollCallRepository = rollCallRepository;
_customizeWorkshopEmployeeSettingsRepository = customizeWorkshopEmployeeSettingsRepository;
_customizeWorkshopSettingsRepository = customizeWorkshopSettingsRepository;
}
public (WorkshopShiftStatus shiftType, IrregularShift irregularShift, ICollection<CustomizeSifts> regularShifts, ICollection<CustomizeRotatingShift> rotatingShifts, TimeSpan BreakTime) GetEmployeeShiftDetails(long employeeId,
long workshopId)
{
var employeeSettings = _customizeWorkshopEmployeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(workshopId, employeeId);
var offset = TimeOnly.MinValue;
WorkshopShiftStatus shiftType;
if (employeeSettings == null)
{
var workshopSettings = _customizeWorkshopSettingsRepository.GetBy(workshopId);
shiftType = workshopSettings.WorkshopShiftStatus;
return (shiftType, null,
workshopSettings.CustomizeWorkshopSettingsShifts.Select(x => (CustomizeSifts)x).ToList(),
null, TimeSpan.Zero);
}
else
{
shiftType = employeeSettings.WorkshopShiftStatus;
var breakTimeSpan = employeeSettings.BreakTime.BreakTimeType == BreakTimeType.WithTime
? employeeSettings.BreakTime.BreakTimeValue.ToTimeSpan()
: TimeSpan.Zero;
return (shiftType, employeeSettings.IrregularShift,
employeeSettings.CustomizeWorkshopEmployeeSettingsShifts.Select(x => (CustomizeSifts)x).ToList(),
employeeSettings.CustomizeRotatingShifts, breakTimeSpan);
}
}
public TimeOnly GetEmployeeOffSetForRegularSettings(long employeeId, long workshopId)
{
var workshopSettings = _customizeWorkshopSettingsRepository.GetBy(workshopId);
var employeeSettings = _customizeWorkshopEmployeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(workshopId, employeeId);
if (workshopSettings == null)
return TimeOnly.MinValue;
if (employeeSettings == null && workshopSettings.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
return Tools.CalculateOffset(workshopSettings.CustomizeWorkshopSettingsShifts
.Select(x => (CustomizeSifts)x).ToList());
if (employeeSettings == null)
return Tools.CalculateOffset(workshopSettings.CustomizeWorkshopSettingsShifts.Select(x => (CustomizeSifts)x).ToList());
if (workshopSettings.WorkshopShiftStatus == WorkshopShiftStatus.Regular && employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
// تعریف بازه‌های زمانی
var workshopStartTime = workshopSettings.CustomizeWorkshopSettingsShifts.MinBy(x => x.Placement).StartTime; // شروع کارگاه
var workshopEndTime = workshopSettings.CustomizeWorkshopSettingsShifts.MaxBy(x => x.Placement).EndTime; // پایان کارگاه
var employeeStartTime = employeeSettings.CustomizeWorkshopEmployeeSettingsShifts.MinBy(x => x.Placement).StartTime; // شروع بازه پرسنل
var employeeEndTime = employeeSettings.CustomizeWorkshopEmployeeSettingsShifts.MaxBy(x => x.Placement).EndTime; // پایان پرسنل
// تبدیل زمان‌ها به TimeSpan برای مقایسه
var workshopStartTimeSpan = workshopStartTime.ToTimeSpan();
var workshopEndTimeSpan = workshopEndTime.ToTimeSpan();
var employeeStartTimeSpan = employeeStartTime.ToTimeSpan();
var employeeEndTimeSpan = employeeEndTime.ToTimeSpan();
// مدیریت زمان‌های بعد از نیمه شب
if (workshopEndTimeSpan < workshopStartTimeSpan)
workshopEndTimeSpan = workshopEndTimeSpan.Add(TimeSpan.FromDays(1)); // افزودن یک روز به پایان بازه اول
if (employeeEndTimeSpan < employeeStartTimeSpan)
employeeEndTimeSpan = employeeEndTimeSpan.Add(TimeSpan.FromDays(1)); // افزودن یک روز به پایان بازه دوم
// محاسبه بزرگ‌ترین زمان شروع و کوچک‌ترین زمان پایان
var overlapStart = workshopStartTimeSpan > employeeStartTimeSpan ? workshopStartTimeSpan : employeeStartTimeSpan;
var overlapEnd = workshopEndTimeSpan < employeeEndTimeSpan ? workshopEndTimeSpan : employeeEndTimeSpan;
if (overlapStart >= overlapEnd) // اگر بازه هم‌پوشانی وجود ندارد
return Tools.CalculateOffset(workshopSettings.CustomizeWorkshopSettingsShifts
.Select(x => (CustomizeSifts)x).ToList());
var overlapDuration = (overlapEnd - overlapStart).TotalMinutes; // مدت زمان هم‌پوشانی بر حسب دقیقه
var duration2 = (employeeEndTime - employeeStartTime).TotalMinutes; // مدت زمان بازه دوم
var overlapPercentage = (overlapDuration / duration2) * 100; // درصد هم‌پوشانی
if (overlapPercentage < 40)
{
return Tools.CalculateOffset(employeeSettings.CustomizeWorkshopEmployeeSettingsShifts
.Select(x => (CustomizeSifts)x).ToList());
}
return Tools.CalculateOffset(workshopSettings.CustomizeWorkshopSettingsShifts
.Select(x => (CustomizeSifts)x).ToList());
}
else if (employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
return Tools.CalculateOffset(employeeSettings.CustomizeWorkshopEmployeeSettingsShifts
.Select(x => (CustomizeSifts)x).ToList());
}
else
{
return TimeOnly.MinValue;
}
}
}