From cb55b7703c26700aa04b2d27b9891cd9ab9ec1e4 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 3 May 2023 19:58:59 +0400 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D0=B7=D0=BD=D0=B5=D1=81=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FishFactoryBusinessLogic/OrderLogic.cs | 58 ++++++++++------- .../FishFactoryBusinessLogic/ShopLogic.cs | 64 ++++++++++++++++++- 2 files changed, 99 insertions(+), 23 deletions(-) diff --git a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs index 0c4879a..cf52fd2 100644 --- a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs @@ -17,10 +17,14 @@ namespace FishFactoryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopLogic _shopLogic; + private readonly ICannedStorage _cannedStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, ICannedStorage cannedStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _cannedStorage = cannedStorage; } public bool CreateOrder(OrderBindingModel model) { @@ -41,31 +45,42 @@ namespace FishFactoryBusinessLogic.BusinessLogics } public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - CheckModel(model, false); - var element = _orderStorage.GetElement(new OrderSearchModel() + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (viewModel == null) { - Id = model.Id - }); - if (element == null) - { - throw new ArgumentNullException(nameof(element)); + throw new ArgumentNullException(nameof(model)); } - model.DateImplement = element.DateImplement; - model.Status = element.Status; - if (newStatus - model.Status == 1) + if (viewModel.Status + 1 != newStatus) { - model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) - model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + model.Status = newStatus; + if (model.Status == OrderStatus.Готов) + { + model.DateImplement = DateTime.Now; + var canned = _cannedStorage.GetElement(new() { Id = viewModel.CannedId }); + if (canned == null) { - _logger.LogWarning("Update operation failed"); - return false; + throw new ArgumentNullException(nameof(canned)); + } + if (!_shopLogic.AddCanneds(canned, viewModel.Count)) + { + throw new Exception($"AddCanneds operation failed. Shop is full."); } - return true; } - _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, newStatus); - throw new ArgumentException($"Невозможно приствоить статус {newStatus} заказу с текущим статусом {model.Status}"); + else + { + model.DateImplement = viewModel.DateImplement; + } + CheckModel(model, false); + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; } public bool DeliveryOrder(OrderBindingModel model) { @@ -83,8 +98,7 @@ namespace FishFactoryBusinessLogic.BusinessLogics public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); - var list = model == null ? _orderStorage.GetFullList() : - _orderStorage.GetFilteredList(model); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); diff --git a/FishFactory/FishFactoryBusinessLogic/ShopLogic.cs b/FishFactory/FishFactoryBusinessLogic/ShopLogic.cs index 2161288..74224f7 100644 --- a/FishFactory/FishFactoryBusinessLogic/ShopLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/ShopLogic.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -39,6 +40,10 @@ namespace FishFactoryBusinessLogic _logger.LogWarning("AddCannedInShop element not found"); return false; } + if (element.MaxCountCanneds - element.ListCanneds.Select(x => x.Value.Item2).Sum() < count) + { + throw new ArgumentNullException("Магазин переполнен", nameof(count)); + } _logger.LogInformation("AddCannedInShop find. Id:{Id}", element.Id); if (element.ListCanneds.TryGetValue(canned.Id, out var pair)) @@ -57,7 +62,8 @@ namespace FishFactoryBusinessLogic Address = element.Address, ShopName = element.ShopName, DateOpening = element.DateOpening, - ListCanneds = element.ListCanneds + ListCanneds = element.ListCanneds, + MaxCountCanneds = element.MaxCountCanneds, }); return true; } @@ -140,6 +146,10 @@ namespace FishFactoryBusinessLogic { throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); } + if (model.MaxCountCanneds < 0) + { + throw new ArgumentException("Максимальное количество консерв в магазине не может быть меньше нуля", nameof(model.MaxCountCanneds)); + } _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.ShopName, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { @@ -150,5 +160,57 @@ namespace FishFactoryBusinessLogic throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + + public bool SellCanneds(ICannedModel model, int count) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (count <= 0) + { + throw new ArgumentException("Количество консерв должно быть больше 0", nameof(count)); + } + var freeCount = _shopStorage.GetFullList() + .Select(x => x.MaxCountCanneds - x.ListCanneds + .Select(p => p.Value.Item2).Sum()).Sum() - count; + if (freeCount < 0) + { + _logger.LogInformation("AddCanned. Не удалось добавить изделия в магазины, они переполнены."); + return false; + } + foreach (var shop in _shopStorage.GetFullList()) + { + int countFree = shop.MaxCountCanneds - shop.ListCanneds.Select(x => x.Value.Item2).Sum(); + if (countFree < count) + { + if (!AddCannedInShop(new() { Id = shop.Id }, model, countFree)) + { + _logger.LogWarning("AddCannedInShop operation failed."); + return false; + } + count -= countFree; + } + else + { + if (!AddCannedInShop(new() { Id = shop.Id }, model, count)) + { + _logger.LogWarning("AddCannedInShop operation failed."); + return false; + } + count = 0; + } + if (count == 0) + { + return true; + } + } + return false; + } + + public bool AddCanneds(ICannedModel model, int count) + { + return _shopStorage.SellCanneds(model, count); + } } }