2024-02-25 19:43:57 +04:00
|
|
|
|
using SushiBarContracts.BindingModel;
|
|
|
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
|
|
|
using SushiBarContracts.SearchModel;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SushiBarDataModels.Enums;
|
2024-04-22 12:11:57 +04:00
|
|
|
|
using System.Xml.Linq;
|
2024-02-25 19:43:57 +04:00
|
|
|
|
|
|
|
|
|
namespace SushiBarBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2024-04-20 20:00:18 +04:00
|
|
|
|
static readonly object blocking = new object();
|
2024-02-25 19:43:57 +04:00
|
|
|
|
|
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 20:00:18 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
|
|
|
|
|
var elem = _orderStorage.GetElement(model);
|
|
|
|
|
if (elem == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement found. Id:{Id}", elem.Id);
|
|
|
|
|
return elem;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 19:43:57 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
2024-04-20 20:00:18 +04:00
|
|
|
|
lock (blocking)
|
|
|
|
|
{
|
|
|
|
|
return ChangeStatus(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
2024-02-25 19:43:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return ChangeStatus(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return ChangeStatus(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
2024-04-22 12:11:57 +04:00
|
|
|
|
if (order.ImplementerId.HasValue)
|
|
|
|
|
model.ImplementerId = order.ImplementerId;
|
2024-02-25 19:43:57 +04:00
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Change status operation failed. Order not found");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (order.Status + 1 != orderStatus)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Change status operation failed. Incorrect new status: {orderStatus}. Current status: {Status}",
|
|
|
|
|
orderStatus, order.Status);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = orderStatus;
|
|
|
|
|
|
|
|
|
|
if (model.Status == OrderStatus.Готов)
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = order.DateImplement;
|
|
|
|
|
}
|
|
|
|
|
if (_orderStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Change status operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
|
|
|
|
|
if (!withParams) return;
|
|
|
|
|
|
|
|
|
|
if (model.SushiId <= 0)
|
|
|
|
|
throw new ArgumentNullException("Неверный идентификатор суши", nameof(model.SushiId));
|
|
|
|
|
|
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
throw new ArgumentNullException("Количество суш в заказе не может быть меньше нуля или равно нулю", nameof(model.Count));
|
|
|
|
|
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
throw new ArgumentNullException("Стоимость заказа не может быть меньше нуля", nameof(model.Sum));
|
|
|
|
|
|
|
|
|
|
if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate)
|
|
|
|
|
throw new ArithmeticException("Заказ должен быть выдан позже, чем был создан");
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Sushi. SushiId:{SushiId}. Count:{ Count}. Sum:{ Sum}. Id: { Id}",
|
|
|
|
|
model.SushiId, model.Count, model.Sum, model.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|