forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
157 lines
5.6 KiB
C#
157 lines
5.6 KiB
C#
using MagicCarpetContracts.BuisnessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Extensions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetBusinessLogic.Implementations;
|
|
|
|
public class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract,IAgencyStorageContract agencyStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
|
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
|
|
|
|
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
|
}
|
|
|
|
public List<SaleDataModel> GetAllSalesByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSales params: {employeeId}, {fromDate}, {toDate}", employeeId, fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
if (employeeId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(employeeId));
|
|
}
|
|
if (!employeeId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
|
|
}
|
|
return _saleStorageContract.GetList(fromDate, toDate, employeeId: employeeId) ?? throw new NullListException();
|
|
}
|
|
|
|
public List<SaleDataModel> GetAllSalesByClientByPeriod(string clientId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSales params: {buyerId}, {fromDate}, {toDate}", clientId, fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
if (clientId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(clientId));
|
|
}
|
|
if (!clientId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field clientId is not a unique identifier.");
|
|
}
|
|
return _saleStorageContract.GetList(fromDate, toDate, clientId: clientId) ?? throw new NullListException();
|
|
}
|
|
|
|
public List<SaleDataModel> GetAllSalesByTourByPeriod(string tourId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSales params: {tourId}, {fromDate}, {toDate}", tourId, fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
if (tourId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(tourId));
|
|
}
|
|
if (!tourId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field tourId is not a unique identifier.");
|
|
}
|
|
return _saleStorageContract.GetList(fromDate, toDate, tourId: tourId) ?? throw new NullListException();
|
|
}
|
|
|
|
public SaleDataModel GetSaleByData(string data)
|
|
{
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
if (data.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(data));
|
|
}
|
|
if (!data.IsGuid())
|
|
{
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
}
|
|
return _saleStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
|
|
public void InsertSale(SaleDataModel saleDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
|
ArgumentNullException.ThrowIfNull(saleDataModel);
|
|
saleDataModel.Validate();
|
|
if (!_agencyStorageContract.CheckComponents(saleDataModel))
|
|
{
|
|
throw new InsufficientException("Dont have tour in agency");
|
|
}
|
|
_saleStorageContract.AddElement(saleDataModel);
|
|
}
|
|
|
|
public void CancelSale(string id)
|
|
{
|
|
_logger.LogInformation("Cancel by id: {id}", id);
|
|
if (id.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(id));
|
|
}
|
|
if (!id.IsGuid())
|
|
{
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
}
|
|
_saleStorageContract.DelElement(id);
|
|
}
|
|
//третье требование
|
|
public double CalculateSaleRevenue(DateTime fromDate, DateTime toDate)
|
|
{
|
|
_logger.LogInformation("CalculateSaleRevenue params: {fromDate}, {toDate}", fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
var sales = GetAllSalesByPeriod(fromDate, toDate);
|
|
if (sales == null || !sales.Any())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
double totalRevenue = 0;
|
|
object lockObj = new object();
|
|
|
|
Parallel.ForEach(sales, sale =>
|
|
{
|
|
if (sale.IsCancel)
|
|
return;
|
|
|
|
lock (lockObj)
|
|
{
|
|
totalRevenue += sale.Sum;
|
|
}
|
|
});
|
|
|
|
_logger.LogInformation("Total revenue calculated: {totalRevenue}", totalRevenue);
|
|
return totalRevenue;
|
|
}
|
|
}
|