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); } }