Compare commits

..

7 Commits

4 changed files with 81 additions and 83 deletions

View File

@ -4,6 +4,7 @@ using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDataModels.Enums;
using LawFirmDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -17,16 +18,18 @@ namespace LawFirmBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IDocumentStorage _documentStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage)
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage)
{
_logger = logger;
_orderStorage = orderStorage;
_shopLogic = shopLogic;
_documentStorage = documentStorage;
_shopStorage = shopStorage;
}
public bool CreateOrder(OrderBindingModel model)
@ -86,7 +89,7 @@ namespace LawFirmBusinessLogic.BusinessLogics
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found.");
return false;
}
if (_shopLogic.CheckThenSupplyMany(document, model.Count) == false)
if (CheckThenSupplyMany(document, model.Count) == false)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error.");
return false;
@ -104,6 +107,67 @@ namespace LawFirmBusinessLogic.BusinessLogics
return true;
}
public bool CheckThenSupplyMany(IDocumentModel document, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Document count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new docs in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.SupplyDocuments(new() { Id = shop.Id }, document, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется);

View File

@ -116,66 +116,6 @@ namespace LawFirmBusinessLogic.BusinessLogics
}
return true;
}
public bool CheckThenSupplyMany(IDocumentModel document, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Document count < 0.");
return false;
}
int freeSpace = 0;
foreach(var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new docs in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountDocuments;
foreach (var doc in shop.ShopDocuments)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (SupplyDocuments(new() { Id = shop.Id}, document, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count-= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool SellDocument(IDocumentModel document, int count)
{;

View File

@ -19,6 +19,5 @@ namespace LawFirmContracts.BusinessLogicContracts
bool Delete(ShopBindingModel model);
bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count);
bool SellDocument(IDocumentModel document, int count);
bool CheckThenSupplyMany(IDocumentModel document, int count);
}
}

View File

@ -85,27 +85,24 @@ namespace LawFirmFileImplement.Implements
{
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
var countStore = count;
if (document == null)
{
return false;
}
foreach (var shop in source.Shops)
{
foreach (var doc in shop.ShopDocuments)
{
if (doc.Value.Item1.Id == document.Id)
var countStore = count;
var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id));
foreach (var doc in shopDocuments)
{
count -= doc.Value.Item2;
}
if (count <= 0)
{
break;
}
}
}
if (count > 0)
{
@ -114,9 +111,8 @@ namespace LawFirmFileImplement.Implements
count = countStore;
for (int i = 0; i < source.Shops.Count; i++)
foreach (var shop in source.Shops)
{
var shop = source.Shops[i];
var documents = shop.ShopDocuments;
foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id))
@ -140,14 +136,13 @@ namespace LawFirmFileImplement.Implements
MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents
});
source.SaveShops();
if (count <= 0) break;
}
if (count > 0)
{
return false;
}
return true;
return count <= 0;
}
}
}