Files
Backend-Api/CompanyManagment.Application/TaxLeftWorkItemApplication.cs
2024-07-05 21:36:15 +03:30

99 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using _0_Framework.Application;
using Company.Domain.TaxLeftWorkItemAgg;
using CompanyManagment.App.Contracts.TaxLeftWorkItem;
namespace CompanyManagment.Application;
public class TaxLeftWorkItemApplication : ITaxLeftWorkItemApplication
{
private readonly ITaxLeftWorkItemRepository _taxLeftWorkItemRepository;
public TaxLeftWorkItemApplication(ITaxLeftWorkItemRepository taxLeftWorkItemRepository)
{
_taxLeftWorkItemRepository = taxLeftWorkItemRepository;
}
public OperationResult Create(CreateTaxLeftWorkItem command)
{
var op = new OperationResult();
var initialDate = new DateTime(2151, 1, 1);
var startDate = new DateTime();
var leftDate = new DateTime();
try
{
startDate = command.StartWorkFa.ToGeorgianDateTime();
}
catch (Exception e)
{
return op.Failed("خطا در ورود تاریخ شروع یکار");
}
try
{
leftDate = !string.IsNullOrWhiteSpace(command.LeftWorkFa) ? command.LeftWorkFa.ToGeorgianDateTime() : initialDate;
}
catch (Exception e)
{
return op.Failed("خطا در ورود تاریخ ترک کار");
}
if(_taxLeftWorkItemRepository.Exists(l => (l.TaxLeftWorkCategoryId == command.TaxLeftWorkCategoryId) &&
((l.StartWork <= startDate && l.LeftWork >= startDate) ||
(l.StartWork <= leftDate && l.LeftWork >= leftDate) ||
(l.StartWork >= startDate && l.StartWork <= leftDate) ||
(l.LeftWork >= startDate && l.LeftWork <= leftDate))));
return op.Failed("خطا در ورود تاریخ");
var create = new TaxLeftWorkItem(startDate, leftDate, command.TaxLeftWorkCategoryId);
_taxLeftWorkItemRepository.Create(create);
_taxLeftWorkItemRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult Edit(EditTaxLeftWorkItem command)
{
var op = new OperationResult();
var initialDate = new DateTime(2151, 1, 1);
var startDate = new DateTime();
var leftDate = new DateTime();
try
{
startDate = command.StartWorkFa.ToGeorgianDateTime();
}
catch (Exception e)
{
return op.Failed("خطا در ورود تاریخ شروع یکار");
}
try
{
leftDate = !string.IsNullOrWhiteSpace(command.LeftWorkFa) ? command.LeftWorkFa.ToGeorgianDateTime() : initialDate;
}
catch (Exception e)
{
return op.Failed("خطا در ورود تاریخ ترک کار");
}
if(_taxLeftWorkItemRepository.Exists(l => (l.TaxLeftWorkCategoryId == command.TaxLeftWorkCategoryId && l.id != command.Id) &&
((l.StartWork <= startDate && l.LeftWork >= startDate) ||
(l.StartWork <= leftDate && l.LeftWork >= leftDate) ||
(l.StartWork >= startDate && l.StartWork <= leftDate) ||
(l.LeftWork >= startDate && l.LeftWork <= leftDate))));
return op.Failed("خطا در ورود تاریخ");
var editItem = _taxLeftWorkItemRepository.Get(command.Id);
if (editItem == null)
return op.Failed("رکورد مورد نظر یافت نشد");
editItem.Edit(startDate,leftDate);
_taxLeftWorkItemRepository.SaveChanges();
return op.Succcedded();
}
public EditTaxLeftWorkItem GetDetails(long id)
{
return _taxLeftWorkItemRepository.GetDetails(id);
}
public List<TaxLeftWorkItemViewModel> Search(TaxLeftWorkItemSerachModel searchModel)
{
return _taxLeftWorkItemRepository.Search(searchModel);
}
}