From 3bd5643b10bccabe40eb4decad5502c5873ec1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Thu, 28 Mar 2024 22:09:40 +0400 Subject: [PATCH] done hard6 --- .../BusinessLogics/OrderLogic.cs | 87 +++++++++++-------- .../BusinessLogics/WorkModeling.cs | 51 ++++++++++- .../PizzeriaDataModels/Enums/OrderStatus.cs | 3 +- .../PizzeriaDatabase.cs | 3 +- Pizzeria/PizzeriaView/FormMain.Designer.cs | 18 ++-- 5 files changed, 112 insertions(+), 50 deletions(-) diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs index 39b0fc3..7cc5a35 100644 --- a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs @@ -13,7 +13,6 @@ namespace PizzeriaBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; static readonly object _locker = new object(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage) private readonly IShopStorage _shopStorage; public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) @@ -44,7 +43,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", - model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); + model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { @@ -84,24 +83,29 @@ namespace PizzeriaBusinessLogic.BusinessLogics public bool DeliveryOrder(OrderBindingModel model) { - var order = _orderStorage.GetElement(new OrderSearchModel + lock (_locker) { - Id = model.Id, - }); - if (order == null) - { - throw new ArgumentNullException(nameof(order)); + model = FillOrderBindingModel(model); + if (model.Status != OrderStatus.Готов && model.Status != OrderStatus.Ожидает) + { + _logger.LogWarning("Changing status operation faled: Current-{Status}:required-Выдан.", model.Status); + throw new InvalidOperationException($"Невозможно приствоить статус выдан заказу с текущим статусом {model.Status}"); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + PizzaId = model.PizzaId, + Count = model.Count + })) + { + if (model.Status == OrderStatus.Готов || model.Status == OrderStatus.Ожидает) + { + model.Status = OrderStatus.Ожидает; + return UpdateOrder(model); + } + } + model.Status = OrderStatus.Выдан; + return UpdateOrder(model); } - if (!_shopStorage.RestockingShops(new SupplyBindingModel - { - PizzaId = order.PizzaId, - Count = order.Count - })) - { - throw new ArgumentException("Недостаточно места"); - } - - return ChangeStatus(model, OrderStatus.Выдан); } private void CheckModel(OrderBindingModel model, bool withParams = true) @@ -129,8 +133,22 @@ namespace PizzeriaBusinessLogic.BusinessLogics _logger.LogInformation("Pizza. PizzaId:{PizzaId}.Count:{Count}.Sum:{Sum}Id:{Id}", model.PizzaId, model.Count, model.Sum, model.Id); } - private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) + { + model = FillOrderBindingModel(model); + + if (requiredStatus - model.Status == 1) + { + model.Status = requiredStatus; + if (model.Status == OrderStatus.Готов) + model.DateImplement = DateTime.Now; + return UpdateOrder(model); + } + _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); + throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); + } + + private OrderBindingModel FillOrderBindingModel(OrderBindingModel model) { CheckModel(model, false); var element = _orderStorage.GetElement(new OrderSearchModel() @@ -141,33 +159,30 @@ namespace PizzeriaBusinessLogic.BusinessLogics { throw new InvalidOperationException(nameof(element)); } + model.Id = element.Id; model.DateCreate = element.DateCreate; model.PizzaId = element.PizzaId; model.DateImplement = element.DateImplement; model.ClientId = element.ClientId; + model.Status = element.Status; + model.Count = element.Count; + model.Sum = element.Sum; 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) + return model; + } + + private bool UpdateOrder(OrderBindingModel model) + { + if (_orderStorage.Update(model) == null) { - 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("Update operation failed"); + return false; } - _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); - throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); + _logger.LogWarning("Update operation sucsess"); + return true; } } } diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/WorkModeling.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/WorkModeling.cs index 6769202..ae11f2d 100644 --- a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/WorkModeling.cs @@ -30,12 +30,14 @@ namespace PizzeriaBusinessLogic.BusinessLogics _logger.LogWarning("DoWork. Implementers is null"); return; } - var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); - if (orders == null || orders.Count == 0) + var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выдан }); + int count = _orderLogic.ReadList(null).Count; + if (orders == null || count == orders.Count) { _logger.LogWarning("DoWork. Orders is null or empty"); return; } + orders = _orderLogic.ReadList(null); _logger.LogDebug("DoWork for {Count} orders", orders.Count); foreach (var implementer in implementers) { @@ -49,6 +51,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics { return; } + await DeliverWaitingOrder(implementer); await RunOrderInWork(implementer); await Task.Run(() => @@ -71,6 +74,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics { Id = order.Id }); + _orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id }); } // кто-то мог уже перехватить заказ, игнорируем ошибку catch (InvalidOperationException ex) @@ -115,6 +119,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics { Id = runOrder.Id }); + _orderLogic.DeliveryOrder(new OrderBindingModel { Id = runOrder.Id }); // отдыхаем Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); } @@ -130,5 +135,47 @@ namespace PizzeriaBusinessLogic.BusinessLogics throw; } } + + private async Task DeliverWaitingOrder(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.LogInformation("DeliverWaitingOrder. Find some waitig order for implementer:{id}.Count:{count}", implementer.Id, waitingOrders.Count); + foreach (var waitingOrder in waitingOrders) + { + try + { + _logger.LogInformation("DeliverWaitingOrder. Trying to deliver order id:{id}", waitingOrder.Id); + var res = _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = waitingOrder.Id + }); + } + catch (ArgumentException ex) + { + _logger.LogWarning(ex, "DeliverWaitingOrder. Fault"); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try deliver order"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + } + } } } diff --git a/Pizzeria/PizzeriaDataModels/Enums/OrderStatus.cs b/Pizzeria/PizzeriaDataModels/Enums/OrderStatus.cs index 0dfea07..aabc571 100644 --- a/Pizzeria/PizzeriaDataModels/Enums/OrderStatus.cs +++ b/Pizzeria/PizzeriaDataModels/Enums/OrderStatus.cs @@ -12,6 +12,7 @@ namespace PizzeriaDataModels.Enums Принят = 0, Выполняется = 1, Готов = 2, - Выдан = 3 + Ожидает = 3, + Выдан = 4 } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index f808f93..b499199 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using PizzeriaDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace PizzeriaDatabaseImplement { diff --git a/Pizzeria/PizzeriaView/FormMain.Designer.cs b/Pizzeria/PizzeriaView/FormMain.Designer.cs index 86ca413..d40954d 100644 --- a/Pizzeria/PizzeriaView/FormMain.Designer.cs +++ b/Pizzeria/PizzeriaView/FormMain.Designer.cs @@ -39,10 +39,6 @@ this.transactionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.продажаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.отчётыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.componentsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); - this.componentPizzaToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); - this.ordersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.запускРаботToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.списокИзделийToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.пиццаСИнгридиентамиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -52,6 +48,10 @@ this.заказыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.заказыToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.заказыПоГруппамToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.componentsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + this.componentPizzaToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + this.ordersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.запускРаботToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); @@ -67,8 +67,7 @@ this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.bookToolStripMenuItem, - this.отчётыToolStripMenuItem, - this.запускРаботToolStripMenuItem}); + this.запускРаботToolStripMenuItem, this.operationToolStripMenuItem, this.отчётыToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); @@ -83,8 +82,7 @@ this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ingridientsToolStripMenuItem, this.pizzasToolStripMenuItem, - this.клиентToolStripMenuItem, - this.исполнителиToolStripMenuItem}); + this.исполнителиToolStripMenuItem, this.shopsToolStripMenuItem, this.клиентToolStripMenuItem}); this.bookToolStripMenuItem.Name = "bookToolStripMenuItem"; @@ -346,8 +344,6 @@ private ToolStripMenuItem componentPizzaToolStripMenuItem1; private ToolStripMenuItem ordersToolStripMenuItem; private ToolStripMenuItem клиентToolStripMenuItem; - private ToolStripMenuItem исполнителиToolStripMenuItem; - private ToolStripMenuItem запускРаботToolStripMenuItem; private ToolStripMenuItem shopsToolStripMenuItem; private ToolStripMenuItem operationToolStripMenuItem; private ToolStripMenuItem transactionToolStripMenuItem; @@ -361,5 +357,7 @@ private ToolStripMenuItem заказыToolStripMenuItem; private ToolStripMenuItem заказыToolStripMenuItem1; private ToolStripMenuItem заказыПоГруппамToolStripMenuItem; + private ToolStripMenuItem исполнителиToolStripMenuItem; + private ToolStripMenuItem запускРаботToolStripMenuItem; } } \ No newline at end of file