From 64b59948b7717914077b22d6dfb1cedfc027e900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 18:21:15 +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=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=20?= =?UTF-8?q?=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D0=BA=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B8=20=D0=BF=D0=BE=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B0=D0=B3=D0=B0?= =?UTF-8?q?=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D1=8F=D0=BC=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D0=BE=D0=B4=D0=B5=20=D0=B7=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D0=B0=20=D0=B2=20=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=20?= =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/OrderLogic.cs | 15 +++++++- ConfectionaryBusinessLogic/ShopLogic.cs | 36 +++++++++++++++++++ ConfectionaryFileImplement/Shop.cs | 1 - .../BusinessLogicsContracts/IShopLogic.cs | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index 1471185..3a57520 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -12,10 +12,14 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IPastryStorage _pastryStorage; + private readonly IShopLogic _shopLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic) { _logger = logger; + _shopLogic = shopLogic; + _pastryStorage = pastryStorage; _orderStorage = orderStorage; } @@ -96,6 +100,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics $"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}", nameof(vmodel)); } + if (orderStatus == OrderStatus.Готов) + { + var vpastry = _pastryStorage.GetElement(new() { Id = vmodel.PastryId }); + + if (vpastry == null || !_shopLogic.AddPastriesInShops(vpastry, vmodel.Count)) + { + throw new Exception($"Не удалось заполнить магазины изделием '{vpastry?.PastryName ?? string.Empty}' из заказа {vmodel.Id}"); + } + } model.Status = orderStatus; model.DateCreate = vmodel.DateCreate; if (model.DateImplement == null) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 3974880..5956da2 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -168,5 +168,41 @@ namespace ConfectioneryBusinessLogic }); return true; } + + + public int GetFreePlacesWithPastriesInShops(int countPastries) + { + // Сумма разностей между максимальный кол-вом изделий и суммой всех изделий в магазине + return _shopStorage.GetFullList() + .Select(x => x.MaxCountPastries - x.Pastries + .Select(p => p.Value.Item2).Sum()) + .Sum() - countPastries; + } + + public bool AddPastriesInShops(IPastryModel pastry, int count) + { + var freePlaces = GetFreePlacesWithPastriesInShops(count); + if (freePlaces < 0) + { + _logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." + + $"Освободите магазины на {-freePlaces} изделий"); + return false; + } + foreach (var shop in _shopStorage.GetFullList()) + { + var cnt = Math.Min(count, shop.MaxCountPastries - shop.Pastries.Select(x => x.Value.Item2).Sum()); + if (!AddPastry(new() { Id = shop.Id }, pastry, cnt)) + { + _logger.LogWarning("При добавления изделий во все магазины произошла ошибка"); + return false; + } + count -= cnt; + if (count == 0) + { + return true; + } + } + return true; + } } } diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index a873e7d..928faf6 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -81,7 +81,6 @@ namespace ConfectioneryFileImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - MaxCountPastries = model.MaxCountPastries; CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); _cachedPastries = null; } diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index 3ba0749..cd2e634 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -13,5 +13,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); + int GetFreePlacesWithPastriesInShops(int countPastries); + bool AddPastriesInShops(IPastryModel pastry, int count); } }