Подменил ShopLogic
This commit is contained in:
parent
d19523fddb
commit
7d7b7d24e2
@ -153,6 +153,11 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
|||||||
|
|
||||||
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
|
_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))
|
if (shopElement.ShopSecures.TryGetValue(secure.Id, out var sameSecure))
|
||||||
{
|
{
|
||||||
shopElement.ShopSecures[secure.Id] = (secure, sameSecure.Item2 + count);
|
shopElement.ShopSecures[secure.Id] = (secure, sameSecure.Item2 + count);
|
||||||
@ -176,34 +181,44 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public bool SupplySecures(ISecureModel secure, int count) => throw new NotImplementedException();
|
public bool SupplySecures(ISecureModel secure, int count)
|
||||||
public bool SellSecures(ISecureModel model, int count) => throw new NotImplementedException();
|
|
||||||
public bool CheckSecuresCount(ISecureModel model, int count)
|
|
||||||
{
|
{
|
||||||
int securesInShops = _shopStorage.GetFullList()
|
if (!CheckSupplySecures(count))
|
||||||
.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)
|
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Required shop element not found in storage");
|
throw new InvalidOperationException("Невозможно пополнить: в магазинах не хватает места");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,6 @@ namespace SecuritySystemContracts.BusinessLogicsContracts
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
bool SellSecures(ISecureModel model, int count);
|
bool SellSecures(ISecureModel model, int count);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Проверяет наличие определенного количества продукта суммарно по всем магазинам
|
|
||||||
/// </summary>
|
|
||||||
bool CheckSecuresCount(ISecureModel model, int count);
|
|
||||||
/// <summary>
|
|
||||||
/// Проверяет можно ли пополнить конкретный магазин продукцией в указанном количестве
|
|
||||||
/// </summary>
|
|
||||||
bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count);
|
|
||||||
/// <summary>
|
|
||||||
/// Проверяет можно ли распределить во все магазины продукты в указанном количестве
|
/// Проверяет можно ли распределить во все магазины продукты в указанном количестве
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool CheckSupplySecures(int count);
|
bool CheckSupplySecures(int count);
|
||||||
|
Loading…
Reference in New Issue
Block a user