2023-04-21 15:19:48 +04:00
|
|
|
|
using DocumentFormat.OpenXml.EMMA;
|
|
|
|
|
using FoodOrdersBusinessLogic.MailWorker;
|
|
|
|
|
using FoodOrdersContracts.BindingModels;
|
2023-02-07 13:00:02 +04:00
|
|
|
|
using FoodOrdersContracts.BusinessLogicsContracts;
|
|
|
|
|
using FoodOrdersContracts.SearchModels;
|
|
|
|
|
using FoodOrdersContracts.StoragesContracts;
|
|
|
|
|
using FoodOrdersContracts.ViewModels;
|
|
|
|
|
using FoodOrdersDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace FoodOrdersBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-04-21 15:19:48 +04:00
|
|
|
|
private readonly AbstractMailWorker _mailWorker;
|
|
|
|
|
private readonly IClientLogic _clientLogic;
|
2023-03-12 14:01:34 +04:00
|
|
|
|
private readonly IShopLogic _logicS;
|
|
|
|
|
private readonly IDishStorage _dishStorage;
|
2023-04-29 15:35:07 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic logicS, IDishStorage dishStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic)
|
2023-02-07 13:00:02 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2023-04-21 15:19:48 +04:00
|
|
|
|
_mailWorker = mailWorker;
|
|
|
|
|
_clientLogic = clientLogic;
|
2023-03-12 14:01:34 +04:00
|
|
|
|
_logicS = logicS;
|
|
|
|
|
_dishStorage = dishStorage;
|
2023-02-07 13:00:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 00:50:15 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
|
|
|
|
|
var element = _orderStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 13:00:02 +04:00
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
2023-03-11 19:13:24 +04:00
|
|
|
|
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
|
2023-02-12 13:10:24 +04:00
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
2023-02-07 13:00:02 +04:00
|
|
|
|
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.Неизвестен)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
2023-04-21 16:16:14 +04:00
|
|
|
|
var order = _orderStorage.Insert(model);
|
|
|
|
|
if (order == null)
|
2023-02-07 13:00:02 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-21 16:16:14 +04:00
|
|
|
|
SendToClient(order.ClientId, $"Заказ №{order.Id}", $"Заказ №{order.Id} от {order.DateCreate} на сумму {order.Sum} принят.");
|
2023-02-07 13:00:02 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-21 21:02:35 +04:00
|
|
|
|
if (model.DishId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у блюда", nameof(model.DishId));
|
|
|
|
|
}
|
2023-02-07 13:00:02 +04:00
|
|
|
|
if (model.Id < 0)
|
|
|
|
|
{
|
2023-04-21 21:02:35 +04:00
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у заказа", nameof(model.Id));
|
2023-02-07 13:00:02 +04:00
|
|
|
|
}
|
2023-03-25 20:38:11 +04:00
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
|
|
|
|
|
}
|
2023-02-07 13:00:02 +04:00
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество продуктов в заказе должно быть больше 0", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
2023-02-12 10:11:34 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderID:{Id}. Sum:{ Sum}. DishId: { DishId}", model.Id, model.Sum, model.Id);
|
2023-02-07 13:00:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
|
|
|
|
{
|
2023-02-12 13:10:24 +04:00
|
|
|
|
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
if (viewModel == null)
|
2023-02-07 13:00:02 +04:00
|
|
|
|
{
|
2023-02-12 13:10:24 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2023-04-21 21:02:35 +04:00
|
|
|
|
if (viewModel.Status + 1 != newStatus && viewModel.Status != OrderStatus.Ожидание)
|
2023-02-12 13:10:24 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Change status operation failed");
|
2023-02-07 13:00:02 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-17 13:42:03 +04:00
|
|
|
|
if (viewModel.ImplementerId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.ImplementerId = viewModel.ImplementerId;
|
|
|
|
|
}
|
2023-02-07 13:00:02 +04:00
|
|
|
|
model.Status = newStatus;
|
2023-04-21 21:02:35 +04:00
|
|
|
|
if (model.Status == OrderStatus.Готов || viewModel.Status == OrderStatus.Ожидание)
|
|
|
|
|
{
|
|
|
|
|
if (!_logicS.AddDishes(_dishStorage.GetElement(new DishSearchModel { Id = viewModel.DishId })!, viewModel.Count))
|
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Ожидание;
|
|
|
|
|
_logger.LogWarning("В магазинах нет места под автомобили из заказа.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
2023-02-12 13:10:24 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
|
|
|
|
}
|
2023-03-11 19:13:24 +04:00
|
|
|
|
CheckModel(model, false);
|
2023-04-21 16:16:14 +04:00
|
|
|
|
var order = _orderStorage.Update(model);
|
|
|
|
|
if (order == null)
|
2023-02-07 13:00:02 +04:00
|
|
|
|
{
|
2023-02-12 13:10:24 +04:00
|
|
|
|
_logger.LogWarning("Change status operation failed");
|
2023-02-07 13:00:02 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-21 16:16:14 +04:00
|
|
|
|
|
|
|
|
|
SendToClient(order.ClientId, $"Заказ №{order.Id}", $"У заказа №{order.Id} изменен статус на {order.Status}.");
|
2023-02-07 13:00:02 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-04-21 15:19:48 +04:00
|
|
|
|
|
2023-04-21 16:16:14 +04:00
|
|
|
|
private bool SendToClient(int clientId, string subject, string text)
|
2023-04-21 15:19:48 +04:00
|
|
|
|
{
|
|
|
|
|
var client = _clientLogic.ReadElement(new() { Id = clientId });
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
2023-04-21 16:16:14 +04:00
|
|
|
|
return false;
|
2023-04-21 15:19:48 +04:00
|
|
|
|
}
|
|
|
|
|
_mailWorker.MailSendAsync(new()
|
|
|
|
|
{
|
|
|
|
|
MailAddress = client.Email,
|
|
|
|
|
Subject = subject,
|
|
|
|
|
Text = text
|
|
|
|
|
});
|
2023-02-07 13:00:02 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|