From 122db5c9af805149e6765b8d9416726d1de7dcbe Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 7 Apr 2024 22:29:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D1=87=D0=B8=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B4=D1=83?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B8=20=D0=B2=20=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D0=BA=D1=83=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) 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; + } } }