From 28b5ed2cfe8fdf399bc912b3aba6a516a3f6902c Mon Sep 17 00:00:00 2001 From: ujijrujijr Date: Thu, 9 May 2024 17:22:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GarmentFactory/FormAddTextile.cs | 2 +- GarmentFactory/Program.cs | 9 +-- GarmentFactoryBusinessLogic/OrderLogic.cs | 2 + GarmentFactoryBusinessLogic/ShopLogic.cs | 66 ++++++++++++------- .../BusinessLogicsContracts/IShopLogic.cs | 2 +- .../StoragesContracts/IShopStorage.cs | 1 + .../Implements/ShopStorage.cs | 3 +- 7 files changed, 54 insertions(+), 31 deletions(-) diff --git a/GarmentFactory/FormAddTextile.cs b/GarmentFactory/FormAddTextile.cs index d234f54..0b26d2b 100644 --- a/GarmentFactory/FormAddTextile.cs +++ b/GarmentFactory/FormAddTextile.cs @@ -78,7 +78,7 @@ namespace GarmentFactoryView _logger.LogInformation("Пополнение магазина"); try { - var operationResult = _logicS.AddTextile(new SupplyBindingModel + var operationResult = _logicS.MakeSupply(new SupplyBindingModel { ShopId = Convert.ToInt32(comboBoxShop.SelectedValue), TextileId = Convert.ToInt32(comboBoxTextile.SelectedValue), diff --git a/GarmentFactory/Program.cs b/GarmentFactory/Program.cs index 68b4b94..9302db5 100644 --- a/GarmentFactory/Program.cs +++ b/GarmentFactory/Program.cs @@ -38,20 +38,21 @@ namespace GarmentFactory services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/GarmentFactoryBusinessLogic/OrderLogic.cs b/GarmentFactoryBusinessLogic/OrderLogic.cs index f0556fc..9d6d131 100644 --- a/GarmentFactoryBusinessLogic/OrderLogic.cs +++ b/GarmentFactoryBusinessLogic/OrderLogic.cs @@ -140,6 +140,8 @@ namespace GarmentFactoryBusinessLogic { throw new ArgumentNullException(nameof(order)); } + + //Если нельзя пополнить магазины таким кол-вом товаров (не хавтает места), то статус не меняем if (!_shopStorage.RestockShops(new SupplyBindingModel { TextileId = order.TextileId, Count = order.Count })) { throw new ArgumentException("Недостаточно места"); diff --git a/GarmentFactoryBusinessLogic/ShopLogic.cs b/GarmentFactoryBusinessLogic/ShopLogic.cs index 18b47fc..21ddadf 100644 --- a/GarmentFactoryBusinessLogic/ShopLogic.cs +++ b/GarmentFactoryBusinessLogic/ShopLogic.cs @@ -109,7 +109,7 @@ namespace GarmentFactoryBusinessLogic } // Пополнение магазина - public bool AddTextile(SupplyBindingModel model) + public bool MakeSupply(SupplyBindingModel model) { if (model == null) { @@ -121,34 +121,52 @@ namespace GarmentFactoryBusinessLogic throw new ArgumentNullException("Количество добавляемых изделий должно быть больше 0"); } - var shop = _shopStorage.GetElement(new ShopSearchModel{ Id = model.ShopId }); + var textile = _textileStorage.GetElement(new TextileSearchModel { Id = model.TextileId }); + if (textile == null) + { + throw new ArgumentException($"При добавлении товара в магазин товар с id={model.TextileId} не найден"); + } - if (shop == null) + var shop = _shopStorage.GetElement(new ShopSearchModel { Id = model.ShopId }); + if (shop == null) + { + throw new ArgumentException($"При добавлении товара в магазин магазин c id={model.ShopId} не найден"); + } + + int countTextilesInShop = shop.ShopTextiles.Select(x => x.Value.Item2).Sum(); + + //Если в текущий магазин помещается столько товаров + if (countTextilesInShop + model.Count <= shop.TextileMaxCount) { - throw new ArgumentException($"При добавлении товара в магазин магазин c id={model.ShopId} не найден"); - } - - // Если такой товар есть, то прибавление переданного кол-ва - if (shop.ShopTextiles.ContainsKey(model.TextileId)) - { - var oldValue = shop.ShopTextiles[model.TextileId]; - oldValue.Item2 += model.Count; - shop.ShopTextiles[model.TextileId] = oldValue; - } - // Если такого товара нет, то кол-во такого товара равно переданному кол-ву - else - { - var textile = _textileStorage.GetElement(new TextileSearchModel{ Id = model.TextileId }); - if (textile == null) + // Если такой товар есть, то прибавление переданного кол-ва + if (shop.ShopTextiles.ContainsKey(model.TextileId)) + { + var oldValue = shop.ShopTextiles[model.TextileId]; + oldValue.Item2 += model.Count; + shop.ShopTextiles[model.TextileId] = oldValue; + } + // Если такого товара нет, то кол-во такого товара равно переданному кол-ву + else + { + shop.ShopTextiles.Add(model.TextileId, (textile, model.Count)); + } + + _shopStorage.Update(new() { - throw new ArgumentException($"При добавлении товара в магазин товар с id={model.TextileId} не найден"); - } - shop.ShopTextiles.Add(model.TextileId, (textile, model.Count)); - } - return true; + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpen = shop.DateOpen, + ShopTextiles = shop.ShopTextiles, + TextileMaxCount = shop.TextileMaxCount, + }); + return true; + } + //Если не поместилось столько товара + _logger.LogWarning("Required shop is overflowed"); + return false; } - // Проверка данных магазина при добавлении/удалении/обновлении private void CheckModel(ShopBindingModel model, bool withParams = true) { diff --git a/GarmentFactoryContracts/BusinessLogicsContracts/IShopLogic.cs b/GarmentFactoryContracts/BusinessLogicsContracts/IShopLogic.cs index 17b5974..7f43b4d 100644 --- a/GarmentFactoryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/GarmentFactoryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -22,7 +22,7 @@ namespace GarmentFactoryContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); - bool AddTextile(SupplyBindingModel model); + bool MakeSupply(SupplyBindingModel model); bool Sell(SupplySearchModel model); } diff --git a/GarmentFactoryContracts/StoragesContracts/IShopStorage.cs b/GarmentFactoryContracts/StoragesContracts/IShopStorage.cs index 4a57af3..1d8cce9 100644 --- a/GarmentFactoryContracts/StoragesContracts/IShopStorage.cs +++ b/GarmentFactoryContracts/StoragesContracts/IShopStorage.cs @@ -17,6 +17,7 @@ namespace GarmentFactoryContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + //Продажа изделий в нужном кол-ве bool Sell(SupplySearchModel model); //Пополнение магаз-ов bool RestockShops(SupplyBindingModel model); diff --git a/GarmentFactoryListImplement/Implements/ShopStorage.cs b/GarmentFactoryListImplement/Implements/ShopStorage.cs index 4b3395b..d28bf04 100644 --- a/GarmentFactoryListImplement/Implements/ShopStorage.cs +++ b/GarmentFactoryListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using GarmentFactoryContracts.SearchModels; using GarmentFactoryContracts.StoragesContracts; using GarmentFactoryContracts.ViewModels; +using GarmentFactoryDataModels.Models; using GarmentFactoryListImplement.Models; using System; using System.Collections.Generic; @@ -110,7 +111,7 @@ namespace GarmentFactoryListImplement.Implements return null; } - public bool Sell (SupplySearchModel model) + public bool Sell(SupplySearchModel model) { throw new NotImplementedException(); }