Files
Backend-Api/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/SalaryPaymentSettings/Commands/CreateSalarySettings/CreateSalarySettingsCommandHandler.cs
2026-01-29 11:52:27 +03:30

174 lines
6.2 KiB
C#

using GozareshgirProgramManager.Application._Common.Interfaces;
using GozareshgirProgramManager.Application._Common.Models;
using GozareshgirProgramManager.Domain._Common;
using GozareshgirProgramManager.Domain.SalaryPaymentSettingAgg.Entities;
using GozareshgirProgramManager.Domain.SalaryPaymentSettingAgg.Enums;
using GozareshgirProgramManager.Domain.SalaryPaymentSettingAgg.Repositories;
namespace GozareshgirProgramManager.Application.Modules.SalaryPaymentSettings.Commands.CreateSalarySettings;
public class CreateSalarySettingsCommandHandler : IBaseCommandHandler<CreateSalarySettingsCommand>
{
readonly ISalaryPaymentSettingRepository _salaryPaymentSettingRepository;
readonly IUnitOfWork _unitOfWork;
public CreateSalarySettingsCommandHandler(ISalaryPaymentSettingRepository salaryPaymentSettingRepository, IUnitOfWork unitOfWork)
{
_salaryPaymentSettingRepository = salaryPaymentSettingRepository;
_unitOfWork = unitOfWork;
}
public async Task<OperationResult> Handle(CreateSalarySettingsCommand request, CancellationToken cancellationToken)
{
if(_salaryPaymentSettingRepository.Exists(x=>x.UserId == request.UserId))
return OperationResult.Failure(" برای این پرسنل قبلا تنظیمات ایجاد شده است");
if (string.IsNullOrWhiteSpace(request.SalaryPerMinute))
return OperationResult.Failure("حقوق ماهانه وارد نشده اشت");
double monthlySalary = 0;
try
{
monthlySalary = request.SalaryPerMinute.MoneyToDouble();
}
catch (Exception e)
{
return OperationResult.Failure("مبلغ حقوق به درستی وارد نشده است");
}
if (monthlySalary == 0)
return OperationResult.Failure("مبلغ حقوق به درستی وارد نشده است");
var workingHoursList = new List<WorkingHours>();
foreach (var workingHours in request.WorkingHoursList)
{
var startShiftOne = new TimeSpan();
var endShiftOne = new TimeSpan();
var startShiftTwo = new TimeSpan();
var endShiftTwo = new TimeSpan();
var restTime = new TimeSpan();
workingHours.HasShiftOne = false;
workingHours.HasRestTime = false;
workingHours.HasShiftTow = false;
try
{
if (!string.IsNullOrWhiteSpace(workingHours.StartShiftOne) && !string.IsNullOrWhiteSpace(workingHours.EndShiftOne))
{
startShiftOne = TimeSpan.ParseExact(workingHours.StartShiftOne, @"hh\:mm", null);
endShiftOne = TimeSpan.ParseExact(workingHours.EndShiftOne, @"hh\:mm", null);
workingHours.HasShiftOne = true;
if (!string.IsNullOrWhiteSpace(workingHours.RestTime))
{
try
{
restTime = TimeSpan.ParseExact(workingHours.RestTime, @"hh\:mm", null);
workingHours.HasRestTime = true;
}
catch (Exception e)
{
return OperationResult.Failure("فرمت ساعت استراحت اشتباه وارد شده است");
}
}
}
if (!string.IsNullOrWhiteSpace(workingHours.StartShiftTwo) &&
!string.IsNullOrWhiteSpace(workingHours.EndShiftTwo))
{
workingHours.HasRestTime = false;
workingHours.HasShiftTow = true;
startShiftTwo = TimeSpan.ParseExact(workingHours.StartShiftTwo, @"hh\:mm", null);
endShiftTwo = TimeSpan.ParseExact(workingHours.EndShiftTwo, @"hh\:mm", null);
}
}
catch (Exception)
{
return OperationResult.Failure("فرمت ساعت اشتباه وارد شده است");
}
workingHoursList.Add(new WorkingHours(startShiftOne,endShiftOne,startShiftTwo, endShiftTwo,restTime,workingHours.HasShiftOne,workingHours.HasShiftTow,workingHours.HasRestTime, workingHours.PersianDayOfWeek, workingHours.IsActiveDay));
}
if(workingHoursList.Count < 7)
return OperationResult.Failure("خطا در تعداد روز های ارسال شده");
var salarySetting = new SalaryPaymentSetting(request.HolidayWorking, request.UserId,monthlySalary, workingHoursList);
await _salaryPaymentSettingRepository.CreateAsync(salarySetting);
await _unitOfWork.SaveChangesAsync();
return OperationResult.Success();
}
}
public record CreateSalarySettingsCommand(bool HolidayWorking, long UserId, string? SalaryPerMinute, List<WorkingHoursListDto> WorkingHoursList) : IBaseCommand;
public record WorkingHoursListDto
{
/// <summary>
/// ساعت شروع شیفت کاری
/// </summary>
public string? StartShiftOne { get; set; }
/// <summary>
/// ساعت پایان شیفت کاری
/// </summary>
public string? EndShiftOne { get; set; }
/// <summary>
/// ساعت شروع شیفت دوم کاری
/// </summary>
public string? StartShiftTwo { get; set; }
/// <summary>
/// ساعت پایان شیفت دوم کاری
/// </summary>
public string? EndShiftTwo { get; set; }
/// <summary>
/// مدت استراحت
/// </summary>
public string? RestTime { get; set; }
/// <summary>
/// آیا مقطع مار اول دارد
/// </summary>
public bool HasShiftOne { get; set; }
/// <summary>
/// آیا مقطع کار دوم دارد
/// </summary>
public bool HasShiftTow { get; set; }
/// <summary>
/// آیا ساعت استراحت دارد
/// </summary>
public bool HasRestTime { get; set; }
/// <summary>
/// عدد روز از ماه
/// </summary>
public PersianDayOfWeek PersianDayOfWeek { get; set; }
/// <summary>
/// آیا این روز هفته
/// فعال است
/// </summary>
public bool IsActiveDay { get; set; }
}