Добавлена функция проверки наличия продукции в логику хранения

This commit is contained in:
Никита Потапов 2024-04-07 22:29:46 +04:00
parent 3112cfb639
commit 122db5c9af

View File

@ -125,5 +125,50 @@ namespace SecuritySystemFileImplement.Implements
return true;
}
public bool CheckCountSecure(ISecureModel model, int count)
{
int store = source.Shops.Select(x => x.ShopSecures.Select(y => (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum();
return store >= count;
}
public bool SellSecures(ISecureModel model, int count)
{
var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id);
if (secure == null || !CheckCountSecure(model, count))
{
throw new ArgumentNullException("В магазинах нет такого количества товара");
}
foreach (var shop in source.Shops)
{
var secures = shop.ShopSecures;
foreach (var elem in secures.Where(x => x.Value.Item1.Id == secure.Id))
{
var selling = Math.Min(elem.Value.Item2, count);
secures[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling);
count -= selling;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = model.Id,
Name = shop.Name,
Address = shop.Address,
OpeningDate = shop.OpeningDate,
ShopSecures = secures,
MaxSecuresCount = shop.MaxSecuresCount
});
}
source.SaveShops();
return true;
}
}
}