get AverageWeeklyDuration query handler

This commit is contained in:
SamSys
2025-12-14 16:38:25 +03:30
parent 54b3745276
commit cccccf084e
3 changed files with 110 additions and 10 deletions

View File

@@ -0,0 +1,86 @@
using GozareshgirProgramManager.Application._Common.Interfaces;
using GozareshgirProgramManager.Application._Common.Models;
using GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Commands.CreateSalarySettings;
using GozareshgirProgramManager.Domain.SalaryPaymentSettingAgg.Entities;
namespace GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Queries.GetAverageWeeklyDuration;
public class GetAverageWeeklyDurationQueryHandler : IBaseQueryHandler<GetAverageWeeklyDurationQuery, GetAverageWeeklyDurationResponse>
{
public async Task<OperationResult<GetAverageWeeklyDurationResponse>> Handle(GetAverageWeeklyDurationQuery request, CancellationToken cancellationToken)
{
TimeSpan shiftDurationResult = new TimeSpan();
int shiftDurationInMinutesResult = 0;
if (request.WorkingHoursList.Any())
{
var workingHoursList = request.WorkingHoursList.Where(x=> x.IsActiveDay).ToList();
foreach (var item in workingHoursList)
{
var startShiftOne = new TimeSpan();
var endShiftOne = new TimeSpan();
var startShiftTwo = new TimeSpan();
var endShiftTwo = new TimeSpan();
var restTime = new TimeSpan();
item.HasShiftOne = false;
item.HasRestTime = false;
item.HasShiftTow = false;
if (!string.IsNullOrWhiteSpace(item.StartShiftOne) && !string.IsNullOrWhiteSpace(item.EndShiftOne))
{
startShiftOne = TimeSpan.ParseExact(item.StartShiftOne, @"hh\:mm", null);
endShiftOne = TimeSpan.ParseExact(item.EndShiftOne, @"hh\:mm", null);
item.HasShiftOne = true;
if (!string.IsNullOrWhiteSpace(item.RestTime))
{
try
{
restTime = TimeSpan.ParseExact(item.RestTime, @"hh\:mm", null);
item.HasRestTime = true;
}
catch (Exception e)
{
var zeroResponse = new GetAverageWeeklyDurationResponse(new TimeSpan(), 0);
return OperationResult<GetAverageWeeklyDurationResponse>.Success(zeroResponse);
}
}
}
if (!string.IsNullOrWhiteSpace(item.StartShiftTwo) &&
!string.IsNullOrWhiteSpace(item.EndShiftTwo))
{
item.HasRestTime = false;
item.HasShiftTow = true;
startShiftTwo = TimeSpan.ParseExact(item.StartShiftTwo, @"hh\:mm", null);
endShiftTwo = TimeSpan.ParseExact(item.EndShiftTwo, @"hh\:mm", null);
}
var res = WorkingHours.ComputeShiftDuration(startShiftOne, endShiftOne, startShiftTwo,
endShiftTwo, restTime, item.HasShiftOne, item.HasShiftTow, item.HasRestTime);
shiftDurationResult = shiftDurationResult.Add(res.shiftDuration);
shiftDurationInMinutesResult += res.shiftDurationInMinutes;
}
}
var response = new GetAverageWeeklyDurationResponse(shiftDurationResult, shiftDurationInMinutesResult);
return OperationResult<GetAverageWeeklyDurationResponse>.Success(response);
}
}
public record GetAverageWeeklyDurationQuery(List<WorkingHoursListDto> WorkingHoursList) : IBaseQuery<GetAverageWeeklyDurationResponse>;
public record GetAverageWeeklyDurationResponse(TimeSpan AverageSpan, int AverageInt);

View File

@@ -22,7 +22,9 @@ public class WorkingHours
PersianDayOfWeek = persianDayOfWeek;
IsActiveDay = isActiveDay;
ComputeShiftDuration(startShiftOne, endShiftOne, startShiftTwo, endShiftTwo, restTime, hasShiftOne, hasShiftTwo,hasRestTime);
var computeResult = ComputeShiftDuration(startShiftOne, endShiftOne, startShiftTwo, endShiftTwo, restTime, hasShiftOne, hasShiftTwo,hasRestTime);
ShiftDuration = computeResult.shiftDuration;
}
@@ -109,10 +111,11 @@ public class WorkingHours
/// <param name="startShift"></param>
/// <param name="endShift"></param>
/// <returns></returns>
private void ComputeShiftDuration(TimeSpan startShift, TimeSpan endShift, TimeSpan startShiftTwo, TimeSpan endShiftTwo, TimeSpan restTime, bool hasShiftOne, bool hasShiftTwo, bool hasRestTime)
public static (TimeSpan shiftDuration, int shiftDurationInMinutes) ComputeShiftDuration(TimeSpan startShift, TimeSpan endShift, TimeSpan startShiftTwo, TimeSpan endShiftTwo, TimeSpan restTime, bool hasShiftOne, bool hasShiftTwo, bool hasRestTime)
{
var now = DateTime.Now.Date;
TimeSpan shiftDurationResult = new TimeSpan();
int shiftDurationInMinutesResult = 0;
if (hasShiftOne && !hasShiftTwo)
{
DateTime startOne = new DateTime(now.Year, now.Month, now.Day, startShift.Hours, startShift.Minutes,0);
@@ -123,7 +126,8 @@ public class WorkingHours
endOne = endOne.AddDays(1);
var shiftDuration = (endOne - startOne);
ShiftDuration = hasRestTime ? (shiftDuration - restTime) : shiftDuration;
shiftDurationResult = hasRestTime ? (shiftDuration - restTime) : shiftDuration;
shiftDurationInMinutesResult = (int)shiftDurationResult.TotalMinutes;
}
else if (!hasShiftOne && hasShiftTwo)
{
@@ -134,7 +138,8 @@ public class WorkingHours
endTow = endTow.AddDays(1);
ShiftDuration = (endTow - startTow);
shiftDurationResult = (endTow - startTow);
shiftDurationInMinutesResult = (int)shiftDurationResult.TotalMinutes;
}
else if (hasShiftOne && hasShiftTwo)
{
@@ -152,13 +157,12 @@ public class WorkingHours
endTow = endTow.AddDays(1);
var shiftDurationTow = (endTow - startTow);
ShiftDuration = shiftOneDuration.Add(shiftDurationTow);
}
else
{
ShiftDurationInMinutes = 0;
shiftDurationResult = shiftOneDuration.Add(shiftDurationTow);
shiftDurationInMinutesResult = (int)shiftDurationResult.TotalMinutes;
}
return (shiftDurationResult, shiftDurationInMinutesResult);
}
}

View File

@@ -1,6 +1,7 @@
using GozareshgirProgramManager.Application._Common.Models;
using GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Commands.CreateSalarySettings;
using GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Commands.EditSalarySettings;
using GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Queries.GetAverageWeeklyDuration;
using GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Queries.GetSalarySettingToEdit;
using GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Queries.GetUserListWhoHaveSettings;
using GozareshgirProgramManager.Domain.SalaryPaymentSettingAgg.Enums;
@@ -45,6 +46,15 @@ public class SalaryPaymentSettingsController : ProgramManagerBaseController
return res;
}
[HttpPost("GetAverageWeeklyDuration")]
public async Task<ActionResult<OperationResult<GetAverageWeeklyDurationResponse>>> GetAverageWeeklyDuration(GetAverageWeeklyDurationQuery command)
{
var res = await _mediator.Send(command);
return res;
}
[HttpPost("edit")]
public async Task<ActionResult<OperationResult>> Edit([FromBody] EditSalarySettingsCommand command)
{