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..09a4599 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -85,25 +85,22 @@ namespace LawFirmFileImplement.Implements { var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); - var countStore = count; - if (document == null) { return false; } - foreach (var shop in source.Shops) + var countStore = count; + + var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id)); + + foreach (var doc in shopDocuments) { - foreach (var doc in shop.ShopDocuments) + count -= doc.Value.Item2; + + if (count <= 0) { - if (doc.Value.Item1.Id == document.Id) - { - count -= doc.Value.Item2; - } - if (count <= 0) - { - break; - } + break; } } @@ -114,9 +111,8 @@ namespace LawFirmFileImplement.Implements count = countStore; - for (int i = 0; i < source.Shops.Count; i++) + foreach (var shop in source.Shops) { - var shop = source.Shops[i]; var documents = shop.ShopDocuments; foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id)) @@ -124,7 +120,7 @@ namespace LawFirmFileImplement.Implements 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; @@ -140,14 +136,13 @@ namespace LawFirmFileImplement.Implements MaxCountDocuments = shop.MaxCountDocuments, ShopDocuments = documents }); + source.SaveShops(); + + if (count <= 0) break; } - if (count > 0) - { - return false; - } - return true; + return count <= 0; } } }