diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs index ab54224..0fa275f 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -20,10 +20,16 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopLogic _shopLogic; + + private readonly IManufactureStorage _manufactureStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IManufactureStorage manufactureStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _manufactureStorage = manufactureStorage; } //вывод отфильтрованного списка компонентов @@ -122,7 +128,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения"); } - _logger.LogInformation("Order. OrderId:{Id}. Sun:{Sum}. ManufactureId:{Id}", model.Id, model.Sum, model.ManufactureId); + _logger.LogInformation("Order. OrderId:{Id}. Sun:{Sum}. ManufactureId:{Id}. Sum:{Sum}", model.Id, model.Sum, model.ManufactureId, model.Sum); } //обновление статуса заказа @@ -146,9 +152,21 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic model.Status = newOrderStatus; //проверка на выдачу - if (model.Status == OrderStatus.Выдан) + if (model.Status == OrderStatus.Готов) { model.DateImplement = DateTime.Now; + + var manufacture = _manufactureStorage.GetElement(new() { Id = viewModel.ManufactureId }); + + if (manufacture == null) + { + throw new ArgumentNullException(nameof(manufacture)); + } + + if (!_shopLogic.AddManufactures(manufacture, viewModel.Count)) + { + throw new Exception($"AddManufactures operation failed. Shop is full."); + } } else { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs index 313d497..2ac74b1 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs @@ -72,10 +72,10 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic if (count <= 0) { - throw new ArgumentNullException("Количество добавляемого мороженого должно быть больше 0", nameof(count)); + throw new ArgumentNullException("Количество добавляемых изделий должно быть больше 0", nameof(count)); } - _logger.LogInformation("AddIceCream. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id); + _logger.LogInformation("AddManufacture. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id); var shop = _shopStorage.GetElement(model); if (shop == null) @@ -99,6 +99,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic ShopName = shop.ShopName, Address = shop.Address, DateOpen = shop.DateOpen, + MaxCountManufactures = shop.MaxCountManufactures, Manufactures = shop.Manufactures }); @@ -181,5 +182,61 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + + public bool AddManufactures(IManufactureModel model, int count) + { + if (count <= 0) + { + throw new ArgumentNullException("Количество добавляемых изделий должно быть больше 0", nameof(count)); + } + + _logger.LogInformation("AddManufactures. Manufacture: {Manufacture}. Count: {Count}", model?.ManufactureName, count); + + var capacity = _shopStorage.GetFullList().Select(x => x.MaxCountManufactures - x.Manufactures.Select(x => x.Value.Item2).Sum()).Sum() - count; + + if (capacity < 0) + { + _logger.LogWarning("AddManufactures operation failed. Sell {count} Manufactures ", -capacity); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + if (shop.MaxCountManufactures - shop.Manufactures.Select(x => x.Value.Item2).Sum() < count) + { + if (!AddManufacture(new() { Id = shop.Id }, model, shop.MaxCountManufactures - shop.Manufactures.Select(x => x.Value.Item2).Sum())) + { + _logger.LogWarning("AddManufactures operation failed."); + + return false; + } + + count -= shop.MaxCountManufactures - shop.Manufactures.Select(x => x.Value.Item2).Sum(); + } + else + { + if (!AddManufacture(new() { Id = shop.Id }, model, count)) + { + _logger.LogWarning("AddManufactures operation failed."); + + return false; + } + + count -= count; + } + + if (count == 0) + { + return true; + } + } + + return true; + } + + public bool SellManufatures(IManufactureModel model, int count) + { + return _shopStorage.SellManufactures(model, count); + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs index 8807186..b930656 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -24,6 +24,6 @@ namespace BlacksmithWorkshopContracts.StoragesContracts ShopViewModel? Delete(ShopBindingModel model); - bool SellManufactures(IManufactureModel, int count); + bool SellManufactures(IManufactureModel model, int count); } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs index cdfa5b6..e501fc8 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs @@ -26,7 +26,7 @@ namespace BlacksmithWorkshopFileImplement public List Manufactures { get; private set; } - public List Shops { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs index fb812ac..65a67cc 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ShopStorage.cs @@ -96,25 +96,25 @@ namespace BlacksmithWorkshopFileImplement.Implements return null; } - public bool SellIceCreams(IIceCreamModel model, int count) + public bool SellManufactures(IManufactureModel model, int count) { - if (_source.Shops.Select(x => x.IceCreams.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count) + if (_source.Shops.Select(x => x.Manufactures.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count) { return false; } - var list = _source.Shops.Where(x => x.IceCreams.ContainsKey(model.Id)); + var list = _source.Shops.Where(x => x.Manufactures.ContainsKey(model.Id)); foreach (var shop in list) { - if (shop.IceCreams[model.Id].Item2 < count) + if (shop.Manufactures[model.Id].Item2 < count) { - count -= shop.IceCreams[model.Id].Item2; - shop.IceCreams[model.Id] = (shop.IceCreams[model.Id].Item1, 0); + count -= shop.Manufactures[model.Id].Item2; + shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, 0); } else { - shop.IceCreams[model.Id] = (shop.IceCreams[model.Id].Item1, shop.IceCreams[model.Id].Item2 - count); + shop.Manufactures[model.Id] = (shop.Manufactures[model.Id].Item1, shop.Manufactures[model.Id].Item2 - count); count -= count; } @@ -123,7 +123,7 @@ namespace BlacksmithWorkshopFileImplement.Implements ShopName = shop.ShopName, Address = shop.Address, DateOpen = shop.DateOpen, - MaxCountManufactures = shop.MaxCountManufacturess, + MaxCountManufactures = shop.MaxCountManufactures, Manufactures = shop.Manufactures }); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs index 1f578b0..fa00ef2 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Shop.cs @@ -103,7 +103,7 @@ namespace BlacksmithWorkshopFileImplement.Models new XElement("Address", Address), new XElement("DateOpen", DateOpen.ToString()), new XElement("MaxCountManufactures", MaxCountManufactures.ToString()), - new XElement("Manufactures", countManufature.Select(x => new XElement("Manufactures", + new XElement("Manufactures", countManufacture.Select(x => new XElement("Manufactures", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); }