diff --git a/FurnitureAssembly/FurnitureAssembly/FormMain.cs b/FurnitureAssembly/FurnitureAssembly/FormMain.cs index 78005a7..e1304ee 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMain.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMain.cs @@ -107,8 +107,13 @@ namespace FurnitureAssembly if (dataGridView.SelectedRows.Count == 1) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", - id); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); + int furnitureId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["FurnitureId"].Value); + int furnitureCount = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value); + if (!_shopLogic.AddFurnituresAtShops(new FurnitureBindingModel { Id = furnitureId }, furnitureCount)) { + MessageBox.Show("Магазины переполнены. Пополнение невозможно", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } try { var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); @@ -116,6 +121,11 @@ namespace FurnitureAssembly { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } + operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } LoadData(); } catch (Exception ex) diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs index 41094d8..34a90e2 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ShopLogic.cs @@ -109,6 +109,10 @@ namespace FurnitureAssemblyBusinessLogic { throw new ArgumentNullException("Нет даты открытия магазина", nameof(model.DateOpening)); } + if (model.MaxCount <= 0) + { + throw new ArgumentNullException("Нет вместимости магазина", nameof(model.DateOpening)); + } _logger.LogInformation("Shop. ShopName:{ShopName}. Address:{ Address}. DateOpening:{ DateOpening}. Id: { Id}", model.ShopName, model.Address, model.DateOpening, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { ShopName = model.ShopName }); if (element != null && element.Id != model.Id) @@ -126,6 +130,12 @@ namespace FurnitureAssemblyBusinessLogic { return false; } + + if (GetCountFreePlaces(model.Id) < count) + { + return false; + } + if (shop.Furnitures.ContainsKey(furniture.Id)) { int prev_count = shop.Furnitures[furniture.Id].Item2; @@ -137,6 +147,7 @@ namespace FurnitureAssemblyBusinessLogic } model.Furnitures = shop.Furnitures; model.DateOpening = shop.DateOpening; + model.MaxCount = shop.MaxCount; model.Address = shop.Address; model.ShopName = shop.ShopName; if (_shopStorage.Update(model) == null) @@ -146,5 +157,51 @@ namespace FurnitureAssemblyBusinessLogic } return true; } + + private int GetCountFreePlaces(int ShopId) + { + var shop = ReadElement(new ShopSearchModel { Id = ShopId }); + if (shop == null) + return 0; + int count = shop.MaxCount; + foreach (var f in shop.Furnitures) + { + count -= f.Value.Item2; + if (count <= 0) + { + return 0; + } + } + return count; + } + + private int GetCountFreePlaces_All() + { + return _shopStorage.GetFullList().Select(x => GetCountFreePlaces(x.Id)).Sum(); + } + + public bool AddFurnituresAtShops(FurnitureBindingModel furnitureModel, int count) + { + if (GetCountFreePlaces_All() < count) + { + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + int countShop = GetCountFreePlaces(shop.Id); + if (countShop >= count) + { + AddFurniture(new() { Id = shop.Id }, furnitureModel, count); + break; + } + else + { + AddFurniture(new() { Id = shop.Id }, furnitureModel, countShop); + } + count -= countShop; + } + return true; + } } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs index d08b39d..f9eb114 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IShopLogic.cs @@ -17,5 +17,7 @@ namespace FurnitureAssemblyContracts.BusinessLogicsContarcts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddFurniture(ShopBindingModel model, FurnitureBindingModel furnitureModel, int count); + + bool AddFurnituresAtShops(FurnitureBindingModel furnitureModel, int count); } }