2023-02-20 08:36:19 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TravelCompanyContracts.BindingModels;
|
|
|
|
|
using TravelCompanyContracts.BusinessLogicsContracts;
|
|
|
|
|
using TravelCompanyContracts.SearchModels;
|
|
|
|
|
using TravelCompanyContracts.StoragesContracts;
|
|
|
|
|
using TravelCompanyContracts.ViewModels;
|
|
|
|
|
using TravelCompanyDataModels.Enums;
|
|
|
|
|
|
|
|
|
|
namespace TravelCompanyBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-05-15 02:51:05 +04:00
|
|
|
|
static readonly object _lock = new object();
|
2023-02-20 08:36:19 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (model.Status != OrderStatus.Неизвестен) return false;
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
});
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Read operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (element.Status != OrderStatus.Готов)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status change operation failed");
|
|
|
|
|
throw new InvalidOperationException("Нельзя выдать заказ, если его статус не 'Готов'!");
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.Выдан;
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
_orderStorage.Update(model);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
});
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Read operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (element.Status != OrderStatus.Выполняется)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status change operation failed");
|
|
|
|
|
throw new InvalidOperationException("Статус заказа 'Готов' может быть присвоен только заказам со статусом 'Выполняется'!");
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.Готов;
|
2023-05-15 02:51:05 +04:00
|
|
|
|
model.DateImplement = DateTime.Now;
|
2023-02-20 08:36:19 +04:00
|
|
|
|
_orderStorage.Update(model);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
|
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 02:51:05 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel? model)
|
2023-02-20 08:36:19 +04:00
|
|
|
|
{
|
2023-05-15 02:51:05 +04:00
|
|
|
|
if (model == null)
|
2023-02-20 08:36:19 +04:00
|
|
|
|
{
|
2023-05-15 02:51:05 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. ImplementerId:{ImplementerId}. OrderStatus:{OrderStatus}. Id:{Id}", model.ImplementerId, model.OrderStatus, model.Id);
|
|
|
|
|
var element = _orderStorage.GetElement(model);
|
2023-02-20 08:36:19 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
2023-05-15 02:51:05 +04:00
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
2023-02-20 08:36:19 +04:00
|
|
|
|
}
|
2023-05-15 02:51:05 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
2023-02-20 08:36:19 +04:00
|
|
|
|
{
|
2023-05-15 02:51:05 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
});
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Read operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (element.Status != OrderStatus.Принят)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status change operation failed");
|
|
|
|
|
throw new InvalidOperationException("Заказ должен быть переведен в статус принятого перед его выполнением!");
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.Выполняется;
|
|
|
|
|
_orderStorage.Update(model);
|
|
|
|
|
return true;
|
2023-02-20 08:36:19 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма заказа должна быть больше 0!", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество изделий должно быть более 0!", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Order. Sum:{Sum}. Id:{Id}", model.Sum, model.Id);
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Заказ с таким ID уже существует");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|