From 643cfa5b0772264f3aaa766d7033b60c6e11f4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Mon, 15 May 2023 11:23:37 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BD=D1=8F=D1=82=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0=20=E2=84=966?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 205 +++++++++--------- .../BusinessLogics/WorkModeling.cs | 14 +- 2 files changed, 112 insertions(+), 107 deletions(-) diff --git a/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs index d828339..46e97a6 100644 --- a/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs @@ -15,33 +15,35 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics { public class OrderLogic : IOrderLogic { - private readonly ILogger _logger; - private readonly IOrderStorage _orderStorage; + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; static readonly object _locker = new object(); public OrderLogic(ILogger logger, IOrderStorage orderStorage) - { - _logger = logger; - _orderStorage = orderStorage; - } - public List? ReadList(OrderSearchModel? model) - { - _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); - var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogInformation("ReadList. Count:{Count}", list.Count); - return list; - } - public OrderViewModel? ReadElement(OrderSearchModel? model) + { + _logger = logger; + _orderStorage = orderStorage; + } + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model?.ClientId, model?.OrderStatus, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public OrderViewModel? ReadElement(OrderSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } - _logger.LogInformation("ReadElement. ImplementerId:{ImplementerId}. OrderStatus:{OrderStatus}. Id:{Id}", model.ImplementerId, model.OrderStatus, model.Id); + _logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model.ClientId, model.OrderStatus, model.ImplementerId, model.DateFrom, model.DateTo, model.Id); var element = _orderStorage.GetElement(model); if (element == null) { @@ -52,89 +54,92 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics return element; } public bool CreateOrder(OrderBindingModel model) - { - CheckModel(model); - if (model.Status != OrderStatus.Неизвестен) - return false; - model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; - } - public bool TakeOrderInWork(OrderBindingModel model) - { + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) + return false; + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool TakeOrderInWork(OrderBindingModel model) + { lock (_locker) { return ChangeStatus(model, OrderStatus.Выполняется); } } - public bool FinishOrder(OrderBindingModel model) - { - return ChangeStatus(model, OrderStatus.Готов); - } - public bool DeliveryOrder(OrderBindingModel model) - { - return ChangeStatus(model, OrderStatus.Выдан); - } - private void CheckModel(OrderBindingModel model, bool withParams = true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { - return; - } - if (model.Count <= 0) - { - throw new ArgumentException("Колличество изделий в заказе не может быть меньше 1", nameof(model.Count)); - } - if (model.Sum <= 0) - { - throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum)); - } - if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) - { - throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}"); - } - _logger.LogInformation("Reinforced. ReinforcedId:{ReinforcedId}.Count:{Count}.Sum:{Sum}Id:{Id}", - model.DishId, model.Count, model.Sum, model.Id); - } - private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) - { - CheckModel(model, false); - var element = _orderStorage.GetElement(new OrderSearchModel() - { - Id = model.Id - }); - if (element == null) - { - throw new ArgumentNullException(nameof(element)); - } - model.DateCreate = element.DateCreate; - model.DishId = element.DishId; - model.DateImplement = element.DateImplement; - model.Status = element.Status; - model.Count = element.Count; - model.Sum = element.Sum; - if (requiredStatus - model.Status == 1) - { - model.Status = requiredStatus; - if (model.Status == OrderStatus.Готов) - model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } - _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); - throw new ArgumentException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); - } - } + public bool FinishOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Готов); + } + public bool DeliveryOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выдан); + } + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.Count <= 0) + { + throw new ArgumentException("Колличество изделий в заказе не может быть меньше 1", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum)); + } + if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) + { + throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}"); + } + _logger.LogInformation("Reinforced. ReinforcedId:{ReinforcedId}.Count:{Count}.Sum:{Sum}Id:{Id}", + model.DishId, model.Count, model.Sum, model.Id); + } + private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel() + { + Id = model.Id + }); + if (element == null) + { + throw new InvalidOperationException(nameof(element)); + } + model.DateCreate = element.DateCreate; + model.DishId = element.DishId; + model.DateImplement = element.DateImplement; + model.ClientId = element.ClientId; + if (!model.ImplementerId.HasValue) + model.ImplementerId = element.ImplementerId; + model.Status = element.Status; + model.Count = element.Count; + model.Sum = element.Sum; + if (requiredStatus - model.Status == 1) + { + model.Status = requiredStatus; + if (model.Status == OrderStatus.Готов) + model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); + throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); + } + } } diff --git a/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/WorkModeling.cs b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/WorkModeling.cs index d815c3f..6841cc3 100644 --- a/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/WorkModeling.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace AbstractFoodOrdersBusinessLogic.BusinessLogics { - public class WorkModeling: IWorkProcess + public class WorkModeling : IWorkProcess { private readonly ILogger _logger; @@ -39,9 +39,8 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics if (orders == null || orders.Count == 0) { _logger.LogWarning("DoWork. Orders is null or empty"); - return; } - _logger.LogDebug("DoWork for {Count} orders", orders.Count); + _logger.LogDebug("DoWork for {Count} orders", orders?.Count); foreach (var implementer in implementers) { Task.Run(() => WorkerWorkAsync(implementer, orders)); @@ -55,11 +54,12 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics /// private async Task WorkerWorkAsync(ImplementerViewModel implementer, List? orders) { - if (_orderLogic == null || implementer == null || orders == null) + if (_orderLogic == null || implementer == null) { return; } await RunOrderInWork(implementer); + if (orders == null || orders.Count == 0) return; @@ -83,6 +83,8 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics { Id = order.Id }); + // отдыхаем + await Task.Delay(implementer.Qualification * _rnd.Next(10, 100)); } // кто-то мог уже перехватить заказ, игнорируем ошибку catch (InvalidOperationException ex) @@ -95,8 +97,6 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics _logger.LogError(ex, "Error while do work"); throw; } - // отдыхаем - await Task.Delay(implementer.Qualification * _rnd.Next(10, 100)); } }); } @@ -133,7 +133,7 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics Id = runOrder.Id }); // отдыхаем - await Task.Delay(implementer.Qualification * _rnd.Next(10, 100)); + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); } // заказа может не быть, просто игнорируем ошибку catch (InvalidOperationException ex)