From 00c2a39c41f9ca356d80d5fa05420e7ed722ca1b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Mon, 13 Mar 2023 10:14:57 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A1=D0=B4=D0=B0=D0=BD=D0=B0=202=20=D1=85?= =?UTF-8?q?=D0=B0=D1=80=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 68 ++++++++++++++++++- .../BusinessLogics/ShopLogic.cs | 60 ---------------- .../BusinessLogicContracts/IShopLogic.cs | 1 - .../Implements/ShopStorage.cs | 2 + 4 files changed, 68 insertions(+), 63 deletions(-) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index f6e83eb..e806a16 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -4,6 +4,7 @@ using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -17,16 +18,18 @@ namespace LawFirmBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; private readonly IShopLogic _shopLogic; private readonly IDocumentStorage _documentStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; _shopLogic = shopLogic; _documentStorage = documentStorage; + _shopStorage = shopStorage; } public bool CreateOrder(OrderBindingModel model) @@ -86,7 +89,7 @@ namespace LawFirmBusinessLogic.BusinessLogics _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found."); return false; } - if (_shopLogic.CheckThenSupplyMany(document, model.Count) == false) + if (CheckThenSupplyMany(document, model.Count) == false) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); return false; @@ -104,6 +107,67 @@ namespace LawFirmBusinessLogic.BusinessLogics return true; } + public bool CheckThenSupplyMany(IDocumentModel document, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Document count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountDocuments; + foreach (var doc in shop.ShopDocuments) + { + freeSpace -= doc.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new docs in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountDocuments; + foreach (var doc in shop.ShopDocuments) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyDocuments(new() { Id = shop.Id }, document, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + public bool TakeOrderInWork(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Выполняется); diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs index 2e26e7d..e2092d3 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs @@ -116,66 +116,6 @@ namespace LawFirmBusinessLogic.BusinessLogics } return true; } - public bool CheckThenSupplyMany(IDocumentModel document, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. Document count < 0."); - return false; - } - - int freeSpace = 0; - foreach(var shop in _shopStorage.GetFullList()) - { - freeSpace += shop.MaxCountDocuments; - foreach (var doc in shop.ShopDocuments) - { - freeSpace -= doc.Value.Item2; - } - } - - if (freeSpace - count < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new docs in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.MaxCountDocuments; - foreach (var doc in shop.ShopDocuments) - { - freeSpace -= doc.Value.Item2; - } - if (freeSpace == 0) - { - continue; - } - if (freeSpace - count >= 0) - { - if (SupplyDocuments(new() { Id = shop.Id}, document, count)) count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (freeSpace - count < 0) - { - if (SupplyDocuments(new() { Id = shop.Id }, document, freeSpace)) count-= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (count <= 0) - { - return true; - } - } - return false; - } public bool SellDocument(IDocumentModel document, int count) {; diff --git a/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs b/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs index 48c5760..725111d 100644 --- a/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs +++ b/LawFirm/LawFirmContracts/BusinessLogicContracts/IShopLogic.cs @@ -19,6 +19,5 @@ namespace LawFirmContracts.BusinessLogicContracts bool Delete(ShopBindingModel model); bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count); bool SellDocument(IDocumentModel document, int count); - bool CheckThenSupplyMany(IDocumentModel document, int count); } } diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs index 1aea266..fb006e0 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -83,6 +83,8 @@ namespace LawFirmFileImplement.Implements public bool SellDocument(IDocumentModel model, int count) { + // переделать под linq + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); var countStore = count; From 5327fd4511b1c959886e23e40d0bc6d364874c20 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Mon, 13 Mar 2023 11:15:33 +0400 Subject: [PATCH 2/4] fix --- LawFirm/LawFirm/FormShop.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LawFirm/LawFirm/FormShop.cs b/LawFirm/LawFirm/FormShop.cs index 8091c6a..242ef49 100644 --- a/LawFirm/LawFirm/FormShop.cs +++ b/LawFirm/LawFirm/FormShop.cs @@ -110,7 +110,8 @@ namespace LawFirmView Id = _id ?? 0, Name = textBoxName.Text, Adress = textBoxAddress.Text, - OpeningDate = dateTimePicker.Value.Date + OpeningDate = dateTimePicker.Value.Date, + ShopDocuments = _shopDocuments }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) From 0b3b9b391a014c616e73fba93ecc64c9c65d5fcc Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 7 Apr 2023 16:04:49 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=BF=D0=BE=D0=B4=20LINQ=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20SellDocument?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs index fb006e0..a4128a2 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -81,7 +81,7 @@ namespace LawFirmFileImplement.Implements return shop.GetViewModel; } - public bool SellDocument(IDocumentModel model, int count) +/* public bool SellDocument(IDocumentModel model, int count) { // переделать под linq @@ -151,5 +151,69 @@ namespace LawFirmFileImplement.Implements } return true; } +*/ + public bool SellDocument(IDocumentModel model, int count) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + + if (document == null) + { + return false; + } + + var countStore = count; + + var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id)); + + foreach (var doc in shopDocuments) + { + count -= doc.Value.Item2; + + if (count <= 0) + { + break; + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + foreach (var shop in source.Shops) + { + var documents = shop.ShopDocuments; + + foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id)) + { + var min = Math.Min(doc.Value.Item2, count); + documents[doc.Value.Item1.Id] = (doc.Value.Item1, doc.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + MaxCountDocuments = shop.MaxCountDocuments, + ShopDocuments = documents + }); + + source.SaveShops(); + + if (count <= 0) break; + } + + return count <= 0; + } } } From 71f60ebf7a36756de4656dfbe73503058eace038 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 7 Apr 2023 16:05:54 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 71 ------------------- 1 file changed, 71 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs index a4128a2..09a4599 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -81,77 +81,6 @@ namespace LawFirmFileImplement.Implements return shop.GetViewModel; } -/* public bool SellDocument(IDocumentModel model, int count) - { - // переделать под linq - - var document = source.Documents.FirstOrDefault(x => 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) - { - count -= doc.Value.Item2; - } - if (count <= 0) - { - 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; - - foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id)) - { - var min = Math.Min(doc.Value.Item2, count); - documents[doc.Value.Item1.Id] = (doc.Value.Item1, doc.Value.Item2 - min); - count -= min; - - if (count <= 0) - { - break; - } - } - - shop.Update(new ShopBindingModel - { - Id = shop.Id, - Name = shop.Name, - Adress = shop.Adress, - OpeningDate = shop.OpeningDate, - MaxCountDocuments = shop.MaxCountDocuments, - ShopDocuments = documents - }); - source.SaveShops(); - } - - if (count > 0) - { - return false; - } - return true; - } -*/ public bool SellDocument(IDocumentModel model, int count) { var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);