From 98f1f4df671f09a7a43ae1142db9c0b7893cd34b Mon Sep 17 00:00:00 2001 From: Artyom_Yashin Date: Tue, 7 May 2024 22:46:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=20=D0=B2?= =?UTF-8?q?=D1=80=D0=BE=D0=B4=D0=B5=20=D0=BA=D0=B0=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputersShop/FormMain.Designer.cs | 29 ++++++++- .../BusinessLogics/OrderLogic.cs | 25 ++++---- .../BusinessLogics/WorkModeling.cs | 64 +++++++++++++++++-- .../ComputersShopDataModels/OrderStatus.cs | 3 +- 4 files changed, 101 insertions(+), 20 deletions(-) diff --git a/ComputersShop/ComputersShop/FormMain.Designer.cs b/ComputersShop/ComputersShop/FormMain.Designer.cs index 56fab26..ecc96fa 100644 --- a/ComputersShop/ComputersShop/FormMain.Designer.cs +++ b/ComputersShop/ComputersShop/FormMain.Designer.cs @@ -48,6 +48,9 @@ списокМагазиновToolStripMenuItem = new ToolStripMenuItem(); компьютерыПоМагазинамToolStripMenuItem = new ToolStripMenuItem(); заказыПоДатамToolStripMenuItem = new ToolStripMenuItem(); + поставкиToolStripMenuItem = new ToolStripMenuItem(); + продажиToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -64,7 +67,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, компьютерыToolStripMenuItem, клиентыToolStripMenuItem, исполнителиToolStripMenuItem, запускРаботToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, компьютерыToolStripMenuItem, клиентыToolStripMenuItem, исполнителиToolStripMenuItem, запускРаботToolStripMenuItem, поставкиToolStripMenuItem, продажиToolStripMenuItem, магазиныToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(117, 24); справочникиToolStripMenuItem.Text = "Справочники"; @@ -213,6 +216,27 @@ заказыПоДатамToolStripMenuItem.Text = "Заказы по датам"; заказыПоДатамToolStripMenuItem.Click += заказыПоДатамToolStripMenuItem_Click; // + // поставкиToolStripMenuItem + // + поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem"; + поставкиToolStripMenuItem.Size = new Size(224, 26); + поставкиToolStripMenuItem.Text = "Поставки"; + поставкиToolStripMenuItem.Click += SupplyToolStripMenuItem_Click; + // + // продажиToolStripMenuItem + // + продажиToolStripMenuItem.Name = "продажиToolStripMenuItem"; + продажиToolStripMenuItem.Size = new Size(224, 26); + продажиToolStripMenuItem.Text = "Продажи"; + продажиToolStripMenuItem.Click += SellToolStripMenuItem_Click; + // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(224, 26); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += ShopsToolStripMenuItem_Click; + // // FormMain // AutoScaleDimensions = new SizeF(8F, 20F); @@ -258,5 +282,8 @@ private ToolStripMenuItem списокМагазиновToolStripMenuItem; private ToolStripMenuItem компьютерыПоМагазинамToolStripMenuItem; private ToolStripMenuItem заказыПоДатамToolStripMenuItem; + private ToolStripMenuItem поставкиToolStripMenuItem; + private ToolStripMenuItem продажиToolStripMenuItem; + private ToolStripMenuItem магазиныToolStripMenuItem; } } \ No newline at end of file diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs index 36b4ae8..03abf4b 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -19,15 +19,14 @@ namespace ComputersShopBusinessLogic.BusinessLogics static readonly object locker = new object(); public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IComputerStorage computerStorage, ILogger logger) - public OrderLogic(ILogger logger, IOrderStorage orderStorage) { - _logger = logger; - _orderStorage = orderStorage; - _shopStorage = shopStorage; - _logger = logger; - _shopLogic = shopLogic; - _computerStorage = computerStorage; - } + _logger = logger; + _orderStorage = orderStorage; + _shopStorage = shopStorage; + _logger = logger; + _shopLogic = shopLogic; + _computerStorage = computerStorage; + } public List? ReadList(OrderSearchModel? model) { @@ -140,23 +139,23 @@ namespace ComputersShopBusinessLogic.BusinessLogics _logger.LogWarning("Read operation failed"); return false; } - if (element.Status != status - 1) + if (element.Status != status - 1 || element.Status == OrderStatus.Готов) { _logger.LogWarning("Status change operation failed"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); } - if (element.Status == OrderStatus.Готов) + if (element.Status == OrderStatus.Готов || element.Status == OrderStatus.Ожидает) { - var computer = _computerStorage.GetElement(new ComputerSearchModel() { Id = model.ComputerId }); + var computer = _computerStorage.GetElement(new ComputerSearchModel() { Id = element.ComputerId }); if (computer == null) { _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Document not found."); return false; } - if (CheckSupply(computer, model.Count) == false) + if (CheckSupply(computer, element.Count) == false) { _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Shop supply error."); - return false; + status = OrderStatus.Ожидает; } } model.Status = status; diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/WorkModeling.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/WorkModeling.cs index d16a148..dbacafd 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/WorkModeling.cs @@ -32,19 +32,21 @@ namespace ComputersShopBusinessLogic.BusinessLogics _logger.LogWarning("DoWork. Implementers is null"); return; } - var orders = _orderLogic.ReadList(new OrderSearchModel + var deliveredOrders = _orderLogic.ReadList(new OrderSearchModel { - Status = OrderStatus.Принят + Status = OrderStatus.Выдан }); - if (orders == null || orders.Count == 0) + var allOrders = _orderLogic.ReadList(null); + if (allOrders == null || deliveredOrders == null || deliveredOrders.Count == allOrders.Count) { _logger.LogWarning("DoWork. Orders is null or empty"); return; } - _logger.LogDebug("DoWork for {Count} orders", orders.Count); + deliveredOrders.ForEach(x => allOrders.Remove(x)); + _logger.LogDebug("DoWork for {Count} orders", allOrders.Count); foreach (var implementer in implementers) { - Task.Run(() => WorkerWorkAsync(implementer, orders)); + Task.Run(() => WorkerWorkAsync(implementer, allOrders)); } } /// Иммитация работы исполнителя @@ -55,6 +57,7 @@ namespace ComputersShopBusinessLogic.BusinessLogics { return; } + await DeliverWaitingOrder(implementer); await RunOrderInWork(implementer); await Task.Run(() => { @@ -77,6 +80,10 @@ namespace ComputersShopBusinessLogic.BusinessLogics { Id = order.Id }); + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = order.Id + }); // отдыхаем Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); } @@ -120,6 +127,10 @@ namespace ComputersShopBusinessLogic.BusinessLogics { Id = runOrder.Id }); + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = runOrder.Id + }); // отдыхаем Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); } @@ -135,5 +146,48 @@ namespace ComputersShopBusinessLogic.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/ComputersShop/ComputersShopDataModels/OrderStatus.cs b/ComputersShop/ComputersShopDataModels/OrderStatus.cs index 348af9b..255e98b 100644 --- a/ComputersShop/ComputersShopDataModels/OrderStatus.cs +++ b/ComputersShop/ComputersShopDataModels/OrderStatus.cs @@ -6,6 +6,7 @@ Принят = 0, Выполняется = 1, Готов = 2, - Выдан = 3 + Ожидает = 3, + Выдан = 4 } } \ No newline at end of file