From af101ac4d8131e03a417b3e234b18dc148baa7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sun, 19 May 2024 21:56:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=206?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20(=D0=A3=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 19 +++--- .../BusinessLogics/WorkModeling.cs | 63 +++++++++++++++++-- .../Enums/OrderStatus.cs | 4 +- .../FormImplementer.Designer.cs | 3 +- .../IceCreamShopView/FormImplementer.resx | 50 +++++++-------- 5 files changed, 101 insertions(+), 38 deletions(-) diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs index c7b158d..635561e 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -6,6 +6,7 @@ using IceCreamShopContracts.ViewModels; using IceCreamShopDataModels.Enums; using IceCreamShopDataModels.Models; using Microsoft.Extensions.Logging; +using System.Xml.Linq; namespace IceCreamShopBusinessLogic.BusinessLogics { @@ -137,12 +138,15 @@ namespace IceCreamShopBusinessLogic.BusinessLogics _logger.LogWarning("Change status operation failed. Order not found"); return false; } - if (order.Status + 1 != newStatus) + if ((order.Status + 1 != newStatus && order.Status != OrderStatus.Ожидание) || (order.Status == OrderStatus.Ожидание && newStatus != OrderStatus.Выдан)) { - _logger.LogWarning("Change status operation failed. Incorrect new status: {newStatus}. Current status: {currStatus}", - newStatus, order.Status); + _logger.LogWarning("Change status operation failed"); return false; } + if (order.ImplementerId.HasValue) + { + model.ImplementerId = order.ImplementerId; + } if (newStatus == OrderStatus.Выдан) { var iceCream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = order.IceCreamId }); @@ -154,14 +158,15 @@ namespace IceCreamShopBusinessLogic.BusinessLogics if (!DeliverIceCreams(iceCream, order.Count)) { _logger.LogWarning("Change status operation failed. Ice creams delivery operation failed"); + model.Status = OrderStatus.Ожидание; + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + } return false; } } model.Status = newStatus; - if (order.ImplementerId.HasValue) - { - model.ImplementerId = order.ImplementerId; - } if (model.Status == OrderStatus.Готов) { model.DateImplement = DateTime.Now; diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/WorkModeling.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/WorkModeling.cs index 0626f80..e36ea9f 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/WorkModeling.cs @@ -30,12 +30,15 @@ namespace IceCreamShopBusinessLogic.BusinessLogics _logger.LogWarning("DoWork. Implementers is null"); return; } - var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); - if (orders == null || orders.Count == 0) + List? orders = new(); + orders.AddRange(_orderLogic.ReadList(new() { Status = OrderStatus.Принят }) ?? new()); + orders.AddRange(_orderLogic.ReadList(new() { Status = OrderStatus.Выполняется }) ?? new()); + orders.AddRange(_orderLogic.ReadList(new() { Status = OrderStatus.Ожидание }) ?? new()); + if (orders.Count == 0) { - _logger.LogWarning("DoWork. Orders is null or empty"); + _logger.LogWarning("DoWork. Orders is empty"); return; - } + } _logger.LogDebug("DoWork for {Count} orders", orders.Count); foreach (var implementer in implementers) { @@ -54,6 +57,8 @@ namespace IceCreamShopBusinessLogic.BusinessLogics { return; } + + await RunWaitingOrders(implementer); await RunOrderInWork(implementer); await Task.Run(() => @@ -76,6 +81,11 @@ namespace IceCreamShopBusinessLogic.BusinessLogics { Id = order.Id }); + _logger.LogDebug("DoWork. Order {Order} delivery", order.Id); + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = order.Id + }); } // кто-то мог уже перехватить заказ, игнорируем ошибку catch (InvalidOperationException ex) @@ -125,6 +135,13 @@ namespace IceCreamShopBusinessLogic.BusinessLogics { Id = runOrder.Id }); + // доставляем + _logger.LogDebug("DoWork. Order {Order} delivery", runOrder.Id); + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = runOrder.Id + }); + // отдыхаем Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); } @@ -140,5 +157,43 @@ namespace IceCreamShopBusinessLogic.BusinessLogics throw; } } + + private async Task RunWaitingOrders(ImplementerViewModel implementer) + { + if (_orderLogic == null || implementer == null) + { + return; + } + var waitingOrders = await Task.Run(() => _orderLogic.ReadList(new OrderSearchModel + { + ImplementerId = implementer.Id, + Status = OrderStatus.Ожидание + })); + if (waitingOrders == null || waitingOrders.Count == 0) + { + return; + } + _logger.LogDebug("RunWaitingOrders for {Count} orders. Implementer: {id}.", waitingOrders.Count, implementer.Id); + foreach (var waitingOrder in waitingOrders) + { + try + { + _logger.LogInformation("RunWaitingOrders. Order {Order} delivery", waitingOrder.Id); + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = waitingOrder.Id + }); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + } + } } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDataModels/Enums/OrderStatus.cs b/IceCreamShop/IceCreamShopDataModels/Enums/OrderStatus.cs index c1a6e80..5acbebf 100644 --- a/IceCreamShop/IceCreamShopDataModels/Enums/OrderStatus.cs +++ b/IceCreamShop/IceCreamShopDataModels/Enums/OrderStatus.cs @@ -10,6 +10,8 @@ Готов = 2, - Выдан = 3 + Выдан = 3, + + Ожидание = 4 } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/FormImplementer.Designer.cs b/IceCreamShop/IceCreamShopView/FormImplementer.Designer.cs index ee0a8a7..bc5f0f2 100644 --- a/IceCreamShop/IceCreamShopView/FormImplementer.Designer.cs +++ b/IceCreamShop/IceCreamShopView/FormImplementer.Designer.cs @@ -149,8 +149,9 @@ Controls.Add(buttonSave); Controls.Add(textBoxFIO); Controls.Add(labelFIO); - Margin = new Padding(2, 2, 2, 2); + Margin = new Padding(2); Name = "FormImplementer"; + StartPosition = FormStartPosition.CenterScreen; Text = "Исполнитель"; Load += FormImplementer_Load; ResumeLayout(false); diff --git a/IceCreamShop/IceCreamShopView/FormImplementer.resx b/IceCreamShop/IceCreamShopView/FormImplementer.resx index 1af7de1..af32865 100644 --- a/IceCreamShop/IceCreamShopView/FormImplementer.resx +++ b/IceCreamShop/IceCreamShopView/FormImplementer.resx @@ -1,17 +1,17 @@  -