Продолжение

This commit is contained in:
GokaPek 2024-06-21 17:24:02 +04:00
parent acdae9d645
commit 8bb9a0fb5b

View File

@ -18,17 +18,18 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage; private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic; private readonly IShopLogic _shopLogic;
private readonly IDocumentStorage _documentStorage; private readonly IDocumentStorage _documentStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage)
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
_shopLogic = shopLogic; _shopLogic = shopLogic;
_documentStorage = documentStorage; _documentStorage = documentStorage;
_shopStorage = shopStorage; _shopStorage = shopStorage;
} }
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
@ -51,8 +52,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
model.Status = OrderStatus.Принят; model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null) if (_orderStorage.Insert(model) == null)
{ {
model.Status = OrderStatus.Неизвестен; model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed"); _logger.LogWarning("Insert operation failed");
return false; return false;
} }
return true; return true;
@ -60,8 +61,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
public bool ChangeStatus(OrderBindingModel model, OrderStatus status) public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{ {
CheckModel(model, false); CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null) if (element == null)
{ {
_logger.LogWarning("Read operation failed"); _logger.LogWarning("Read operation failed");
@ -72,31 +73,32 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic
_logger.LogWarning("Status change operation failed"); _logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
} }
if (status == OrderStatus.Готов) if (status == OrderStatus.Готов)
{ {
var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId }); var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId });
if (document == null) if (document == null)
{ {
_logger.LogWarning("Status change operation failed. Car not found."); _logger.LogWarning("Status change operation failed. Car not found.");
return false; return false;
} }
if (!CheckThenSupplyMany(document, model.Count)) if (!CheckThenSupplyMany(document, model.Count))
{ {
_logger.LogWarning("Status change operation failed. Shop supply error."); _logger.LogWarning("Status change operation failed. Shop supply error.");
return false; return false;
} }
} }
model.Status = status;
model.Status = status;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
_orderStorage.Update(model); _orderStorage.Update(model);
if (_orderStorage.Update(model) == null) if (_orderStorage.Update(model) == null)
{ {
model.Status--; model.Status--;
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Update operation failed");
return false; return false;
} }
return true; return true;
} }
public bool TakeOrderInWork(OrderBindingModel model) public bool TakeOrderInWork(OrderBindingModel model)
@ -125,11 +127,11 @@ true)
{ {
return; return;
} }
if (model.DocumentId < 0) if (model.DocumentId < 0)
{ {
throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId)); throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum));
} }
@ -138,67 +140,68 @@ true)
throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count));
} }
_logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id);
public bool CheckThenSupplyMany(IDocumentModel document, int count) }
{ public bool CheckThenSupplyMany(IDocumentModel document, int count)
if (count <= 0) {
{ if (count <= 0)
_logger.LogWarning("Check then supply operation error. Car count < 0."); {
return false; _logger.LogWarning("Check then supply operation error. Car count < 0.");
} return false;
}
int freeSpace = 0; int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList()) foreach (var shop in _shopStorage.GetFullList())
{ {
freeSpace += shop.MaxCountDocuments; freeSpace += shop.MaxCountDocuments;
foreach (var c in shop.ShopDocuments) foreach (var c in shop.ShopDocuments)
{ {
freeSpace -= c.Value.Item2; freeSpace -= c.Value.Item2;
} }
} }
if (freeSpace < count) if (freeSpace < count)
{ {
_logger.LogWarning("Check then supply operation error. There's no place for new cars in shops."); _logger.LogWarning("Check then supply operation error. There's no place for new cars in shops.");
return false; return false;
} }
foreach (var shop in _shopStorage.GetFullList()) foreach (var shop in _shopStorage.GetFullList())
{ {
freeSpace = shop.MaxCountDocuments; freeSpace = shop.MaxCountDocuments;
foreach (var c in shop.ShopDocuments) foreach (var c in shop.ShopDocuments)
freeSpace -= c.Value.Item2; freeSpace -= c.Value.Item2;
if (freeSpace <= 0) if (freeSpace <= 0)
continue; continue;
if (freeSpace >= count)
{
if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, count))
count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace < count)
{
if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
if (freeSpace >= count)
{
if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, count))
count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace < count)
{
if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
}
} }
} }