185 lines
6.6 KiB
C#
Raw Normal View History

using TypographyContracts.BusinessLogicsContracts;
2024-02-12 12:46:44 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TypographyContracts.BindingModels;
using TypographyContracts.SearchModels;
using TypographyContracts.StoragesContracts;
using TypographyContracts.ViewModels;
using TypographyDataModels.Enums;
2024-02-12 12:46:44 +04:00
using Microsoft.Extensions.Logging;
2024-04-21 16:26:49 +04:00
using TypographyDataModels.Models;
2024-02-12 12:46:44 +04:00
namespace TypographyBusinessLogic.BusinessLogics
2024-02-12 12:46:44 +04:00
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
2024-04-21 16:26:49 +04:00
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IPrintedStorage _printedStorage;
2024-02-12 12:46:44 +04:00
2024-04-21 16:26:49 +04:00
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IPrintedStorage printedStorage)
2024-02-12 12:46:44 +04:00
{
_logger = logger;
_orderStorage = orderStorage;
2024-04-21 16:26:49 +04:00
_shopStorage = shopStorage;
_shopLogic = shopLogic;
_printedStorage = printedStorage;
2024-02-12 12:46:44 +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)
{
2024-04-21 16:26:49 +04:00
model.Status = OrderStatus.Неизвестен;
2024-02-12 12:46:44 +04:00
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2024-04-22 11:48:48 +04:00
private void CheckModel(OrderBindingModel model)
2024-04-21 16:26:49 +04:00
{
2024-04-22 11:48:48 +04:00
if (model == null)
2024-04-21 16:26:49 +04:00
{
2024-04-22 11:48:48 +04:00
throw new ArgumentNullException(nameof(model));
2024-04-21 16:26:49 +04:00
}
2024-04-22 11:48:48 +04:00
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество заказов должно быть больше нуля");
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Цена заказа должна быть больше нуля");
}
_logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}", model.Id, model.Sum);
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выполняется);
}
2024-04-21 16:26:49 +04:00
2024-04-22 11:48:48 +04:00
public bool FinishOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выдан);
}
public bool CheckSupply(IPrintedModel printed, int count)
{
if (count <= 0)
2024-04-21 16:26:49 +04:00
{
2024-04-22 11:48:48 +04:00
_logger.LogWarning("Check supply operation error. Printed count < 0");
return false;
2024-04-21 16:26:49 +04:00
}
2024-04-22 11:48:48 +04:00
int Capacity = _shopStorage.GetFullList().Select(x => x.MaxCountPrinteds).Sum();
int currentCount = _shopStorage.GetFullList().Select(x => x.ShopPrinteds.Select(y => y.Value.Item2).Sum()).Sum();
int freeSpace = Capacity - currentCount;
2024-04-21 16:26:49 +04:00
if (freeSpace < count)
{
2024-04-22 11:48:48 +04:00
_logger.LogWarning("Check supply error. No place for new Printeds");
2024-04-21 16:26:49 +04:00
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountPrinteds;
2024-04-22 11:48:48 +04:00
foreach (var doc in shop.ShopPrinteds)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
2024-04-21 16:26:49 +04:00
continue;
2024-04-22 11:48:48 +04:00
}
2024-04-21 16:26:49 +04:00
if (freeSpace >= count)
{
2024-04-22 11:48:48 +04:00
if (_shopLogic.MakeShipment(new()
{
Id = shop.Id
}, printed, count))
{
2024-04-21 16:26:49 +04:00
count = 0;
2024-04-22 11:48:48 +04:00
}
2024-04-21 16:26:49 +04:00
else
{
_logger.LogWarning("Supply error");
return false;
}
}
2024-04-22 11:48:48 +04:00
else
2024-04-21 16:26:49 +04:00
{
2024-04-22 11:48:48 +04:00
if (_shopLogic.MakeShipment(new() { Id = shop.Id }, printed, freeSpace))
2024-04-21 16:26:49 +04:00
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
2024-04-22 11:48:48 +04:00
private bool ChangeStatus(OrderBindingModel model, OrderStatus status)
2024-02-12 12:46:44 +04:00
{
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{
2024-04-22 11:48:48 +04:00
_logger.LogWarning("Find order failed");
2024-02-12 12:46:44 +04:00
return false;
}
if (element.Status != status - 1)
{
2024-04-22 11:48:48 +04:00
_logger.LogWarning("Status change failed");
throw new InvalidOperationException("Невозможно перевести состояние заказа");
2024-02-12 12:46:44 +04:00
}
2024-04-22 11:48:48 +04:00
if (status == OrderStatus.Готов)
2024-02-12 12:46:44 +04:00
{
2024-04-22 11:48:48 +04:00
var printed = _printedStorage.GetElement(new PrintedSearchModel() { Id = model.PrintedId });
if (printed == null)
{
_logger.LogWarning("Status change error. Printed not found");
return false;
}
if (!CheckSupply(printed, model.Count))
{
_logger.LogWarning("Status change error. Shop doesnt have printedes");
return false;
}
2024-02-12 12:46:44 +04:00
}
2024-04-22 11:48:48 +04:00
model.Status = status;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
2024-02-12 12:46:44 +04:00
return true;
}
}
}