Подменил ShopLogic

This commit is contained in:
Никита Потапов 2024-05-20 11:31:55 +04:00
parent d19523fddb
commit 7d7b7d24e2
2 changed files with 35 additions and 28 deletions

View File

@ -153,6 +153,11 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
if (GetFreeSpace(shopElement.Id) < count)
{
throw new InvalidOperationException("В магазине не хватает места");
}
if (shopElement.ShopSecures.TryGetValue(secure.Id, out var sameSecure))
{
shopElement.ShopSecures[secure.Id] = (secure, sameSecure.Item2 + count);
@ -176,34 +181,44 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
return true;
}
public bool SupplySecures(ISecureModel secure, int count) => throw new NotImplementedException();
public bool SellSecures(ISecureModel model, int count) => throw new NotImplementedException();
public bool CheckSecuresCount(ISecureModel model, int count)
public bool SupplySecures(ISecureModel secure, int count)
{
int securesInShops = _shopStorage.GetFullList()
.Select(x => x.ShopSecures.Select(y => y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0).Sum()).Sum();
return securesInShops >= count;
}
public bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count)
{
if (shopSearchModel == null)
throw new ArgumentNullException(nameof(shopSearchModel));
var shop = _shopStorage.GetElement(shopSearchModel);
if (shop == null)
if (!CheckSupplySecures(count))
{
_logger.LogWarning("Required shop element not found in storage");
return false;
throw new InvalidOperationException("Невозможно пополнить: в магазинах не хватает места");
}
int securesInShop = _shopStorage.GetFullList().Select(x => x.ShopSecures.Select(y => y.Value.Item2).Sum()).Sum();
var shops = _shopStorage.GetFullList();
foreach (var shop in shops)
{
int shopFreeSpace = GetFreeSpace(shop.Id);
if (shopFreeSpace > 0 && count > 0)
{
int min = Math.Min(count, shopFreeSpace);
count -= min;
SupplySecures(new ShopSearchModel { Id = shop.Id }, secure, min);
}
}
return securesInShop + count <= shop.MaxSecuresCount;
return true;
}
public bool SellSecures(ISecureModel model, int count)
{
return _shopStorage.SellSecures(model, count);
}
public bool CheckSupplySecures(int count)
{
throw new NotImplementedException();
return GetFreeSpace() >= count;
}
private int GetFreeSpace()
{
var shops = _shopStorage.GetFullList();
return shops.Select(shop => shop.MaxSecuresCount - shop.ShopSecures.Select(shopSecure => shopSecure.Value.Item2).Sum()).Sum();
}
private int GetFreeSpace(int shopId)
{
var shop = _shopStorage.GetElement(new ShopSearchModel { Id = shopId });
return shop.MaxSecuresCount - shop.ShopSecures.Select(shopSecure => shopSecure.Value.Item2).Sum();
}
}
}

View File

@ -17,14 +17,6 @@ namespace SecuritySystemContracts.BusinessLogicsContracts
/// </summary>
bool SellSecures(ISecureModel model, int count);
/// <summary>
/// Проверяет наличие определенного количества продукта суммарно по всем магазинам
/// </summary>
bool CheckSecuresCount(ISecureModel model, int count);
/// <summary>
/// Проверяет можно ли пополнить конкретный магазин продукцией в указанном количестве
/// </summary>
bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count);
/// <summary>
/// Проверяет можно ли распределить во все магазины продукты в указанном количестве
/// </summary>
bool CheckSupplySecures(int count);