2023-01-29 22:10:01 +04:00
|
|
|
|
using FlowerShopContracts.BindingModels;
|
|
|
|
|
using FlowerShopContracts.BusinessLogicsContracts;
|
|
|
|
|
using FlowerShopContracts.SearchModels;
|
|
|
|
|
using FlowerShopContracts.StoragesContracts;
|
|
|
|
|
using FlowerShopContracts.ViewModels;
|
2023-03-26 01:51:51 +04:00
|
|
|
|
using FlowerShopDataModels.Models;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
using FlowerShopDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-03-26 01:51:51 +04:00
|
|
|
|
using System.Text;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
|
|
|
|
|
namespace FlowerShopBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-03-26 01:51:51 +04:00
|
|
|
|
private readonly IShopStorage _shopStorage;
|
|
|
|
|
private readonly IShopLogic _shopLogic;
|
|
|
|
|
private readonly IBouquetStorage _bouquetStorage;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
|
2023-03-26 01:51:51 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IBouquetStorage bouquetStorage, IShopStorage shopStorage)
|
2023-01-29 22:10:01 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2023-03-26 01:51:51 +04:00
|
|
|
|
_shopLogic = shopLogic;
|
|
|
|
|
_bouquetStorage = bouquetStorage;
|
|
|
|
|
_shopStorage = shopStorage;
|
2023-01-29 22:10:01 +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);
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (model.Status != OrderStatus.Неизвестен)
|
2023-01-29 22:10:01 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Принят;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-12 18:21:48 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
2023-02-27 03:19:29 +04:00
|
|
|
|
if (_orderStorage.GetElement(new OrderSearchModel { Id=model.Id })?.Status != OrderStatus.Принят)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Выполняется;
|
2023-02-12 18:21:48 +04:00
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
2023-02-12 23:20:34 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-02-12 18:21:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
}
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-12 18:21:48 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
2023-04-21 01:59:14 +04:00
|
|
|
|
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
if (order?.Status != OrderStatus.Выполняется)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 01:59:14 +04:00
|
|
|
|
var bouquet = _bouquetStorage.GetElement(new BouquetSearchModel() { Id = order.BouquetId });
|
2023-03-26 01:51:51 +04:00
|
|
|
|
if (bouquet == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status update to Готов operation failed. Bouquet not found");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 01:59:14 +04:00
|
|
|
|
if (!CheckThenSupplyMany(bouquet, order.Count))
|
2023-03-26 01:51:51 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status update to Готов operation failed. Shop supply error.");
|
2023-04-21 01:59:14 +04:00
|
|
|
|
throw new Exception("В магазинах не хватает места!");
|
2023-03-26 01:51:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Готов;
|
2023-02-27 03:19:29 +04:00
|
|
|
|
model.DateImplement = DateTime.Now;
|
2023-02-12 18:21:48 +04:00
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
2023-02-12 23:20:34 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-02-12 18:21:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-12 18:21:48 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
2023-02-27 03:19:29 +04:00
|
|
|
|
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
|
|
|
|
|
if (order?.Status != OrderStatus.Готов)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Выдан;
|
2023-02-27 03:19:29 +04:00
|
|
|
|
model.DateImplement = order.DateImplement;
|
2023-02-12 18:21:48 +04:00
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
2023-02-12 23:20:34 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-02-12 18:21:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-01-29 22:10:01 +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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. BouquetId: {BouquetId}", model.Id, model.Sum, model.BouquetId);
|
|
|
|
|
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
if (element != null && element.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Заказ с таким идентификатором уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-26 01:51:51 +04:00
|
|
|
|
|
|
|
|
|
public bool CheckThenSupplyMany(IBouquetModel bouquet, int count)
|
|
|
|
|
{
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Check then supply operation error. Bouquet count < 0.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int freeSpace = 0;
|
|
|
|
|
foreach (var shop in _shopStorage.GetFullList())
|
|
|
|
|
{
|
|
|
|
|
freeSpace += shop.MaxCountBouquets;
|
2023-03-26 01:54:14 +04:00
|
|
|
|
foreach (var bouq in shop.ShopBouquets)
|
2023-03-26 01:51:51 +04:00
|
|
|
|
{
|
2023-03-26 01:54:14 +04:00
|
|
|
|
freeSpace -= bouq.Value.Item2;
|
2023-03-26 01:51:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (freeSpace - count < 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Check then supply operation error. There's no place for new bouquets in shops.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var shop in _shopStorage.GetFullList())
|
|
|
|
|
{
|
|
|
|
|
freeSpace = shop.MaxCountBouquets;
|
|
|
|
|
|
|
|
|
|
foreach (var bouq in shop.ShopBouquets)
|
|
|
|
|
{
|
|
|
|
|
freeSpace -= bouq.Value.Item2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (freeSpace == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (freeSpace - count >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (_shopLogic.SupplyBouquets(new() { Id = shop.Id }, bouquet, count)) count = 0;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Supply error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (freeSpace - count < 0)
|
|
|
|
|
{
|
|
|
|
|
if (_shopLogic.SupplyBouquets(new() { Id = shop.Id }, bouquet, freeSpace)) count -= freeSpace;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Supply error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-01-29 22:10:01 +04:00
|
|
|
|
}
|
|
|
|
|
}
|