diff --git a/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs index de7ac69..a55a4ad 100644 --- a/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs @@ -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; + } } }