From 35ee01f5528965c82dc7e370bae12de2e0adbc5e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Mon, 27 Feb 2023 09:49:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=B0=D0=BA=D0=B0=D1=8F-=D1=82=D0=BE=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=D0=B6=D0=B8=20=D0=B2=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8?= =?UTF-8?q?=D0=BD=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 106 ++++++++++++++---- 1 file changed, 85 insertions(+), 21 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs index 68a4838..fc2f25f 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -11,27 +11,6 @@ using System.Threading.Tasks; namespace LawFirmFileImplement.Implements { - - - // Нужно узнать, проверять ли на наличие всех товаров, или только - // наличие определенного товара - // То же самое с продажей - - - - - - - - - - - - - - - - public class ShopStorage : IShopStorage { private readonly DataFileSingleton source; @@ -101,5 +80,90 @@ namespace LawFirmFileImplement.Implements return shop.GetViewModel; } + public bool SellDocument(DocumentSearchModel model, int count) + { + var document = source.Documents.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id); + + var countStore = count; + + if (document == null) + { + return false; + } + + foreach (var shop in source.Shops) + { + foreach (var doc in shop.ShopDocuments) + { + if (doc.Value.Item1.Id == document.Id) + { + if (count > 0) + { + count = count - doc.Value.Item2; + } + } + if (count < 1) + { + break; + } + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + for (int i = 0; i < source.Shops.Count; i++) + { + var shop = source.Shops[i]; + var documents = shop.ShopDocuments; + + for (int j = 0; j < documents.Count; j++) + { + if (documents[j].Item1.Id == document.Id) + { + while (documents[j].Item2 > 0 || count > 0) + { + int tempItem2 = documents[j].Item2; + tempItem2--; + count--; + documents[j] = (documents[j].Item1, tempItem2); + } + } + + if (count < 1) + { + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + MaxCountDocuments = shop.MaxCountDocuments, + ShopDocuments= documents + }); + break; + } + } + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + MaxCountDocuments = shop.MaxCountDocuments, + ShopDocuments = documents + }); + } + + if (count > 0) + { + return false; + } + return true; + } } }