From da4f49e290a9df2a1ac442b365a9bb6395db97f8 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 16 Apr 2024 23:48:41 +0400 Subject: [PATCH] fix WorkerWorkAsync --- .../BusinessLogics/OrderLogic.cs | 2 +- .../BusinessLogics/WorkModeling.cs | 26 +++++--- .../Implements/OrderStorage.cs | 11 ++-- .../Implements/OrderStorage.cs | 10 +-- .../Implements/OrderStorage.cs | 5 +- AutoWorkshopView/AutoWorkshopView.csproj | 6 ++ .../Forms/FormCreateOrder.Designer.cs | 62 ++++++++++--------- AutoWorkshopView/Forms/FormCreateOrder.cs | 15 +++-- AutoWorkshopView/MainForm.cs | 3 +- 9 files changed, 86 insertions(+), 54 deletions(-) diff --git a/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index dbd5691..4bb3da8 100644 --- a/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -78,7 +78,7 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics Model.RepairId = Order.RepairId; Model.ClientId = Order.ClientId; - Model.ImplementerId = Order.ImplementerId; + if (!Model.ImplementerId.HasValue) Model.ImplementerId = Order.ImplementerId; Model.Count = Order.Count; Model.Sum = Order.Sum; Model.DateCreate = Order.DateCreate; diff --git a/AutoWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs b/AutoWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs index b1876a6..3402718 100644 --- a/AutoWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/AutoWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs @@ -35,15 +35,17 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics } var Orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Accepted }); - - if (Orders == null || Orders.Count == 0) + var BeingProcessedOrders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.BeingProcessed }); + // in case there are BeingProcessed orders but no Accepted orders + + if (Orders == null || BeingProcessedOrders == null || (Orders.Count == 0 && BeingProcessedOrders.Count == 0)) { _logger.LogWarning("DoWork. Orders is null or empty"); return; } _logger.LogDebug("DoWork for {Count} orders", Orders.Count); - + foreach (var Implementer in Implementers) { Task.Run(() => WorkerWorkAsync(Implementer, Orders)); @@ -65,17 +67,25 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics { try { - _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", Implementer.Id, Order.Id); + _logger.LogDebug("WorkerWorkAsync. Worker {Id} try get order {Order}", Implementer.Id, Order.Id); - _orderLogic.TakeOrderInWork(new OrderBindingModel + // TakeOrderInWork will fail when the worker tries to change status to BeingProcessed when the order is already BeingProcessed + // which would happen when other worker already acquired that order + bool AcquireResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = Order.Id, ImplementerId = Implementer.Id }); + if (!AcquireResult) + { + _logger.LogDebug("WorkerWorkAsync. Worker {Id} tried to get order {Order} but it's already acquired by other worker", Implementer.Id, Order.Id); + continue; + } + Thread.Sleep(Implementer.WorkExperience * _rnd.Next(100, 1000) * Order.Count); - _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", Implementer.Id, Order.Id); + _logger.LogDebug("WorkerWorkAsync. Worker {Id} finish order {Order}", Implementer.Id, Order.Id); _orderLogic.FinishOrder(new OrderBindingModel { Id = Order.Id @@ -116,10 +126,10 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics return; } - _logger.LogDebug("DoWork. Worker {Id} back to order {Order}", Implementer.Id, RunOrder.Id); + _logger.LogDebug("RunOrderInWork. Worker {Id} back to order {Order}", Implementer.Id, RunOrder.Id); Thread.Sleep(Implementer.WorkExperience * _rnd.Next(100, 300) * RunOrder.Count); - _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", Implementer.Id, RunOrder.Id); + _logger.LogDebug("RunOrderInWork. Worker {Id} finish order {Order}", Implementer.Id, RunOrder.Id); _orderLogic.FinishOrder(new OrderBindingModel { Id = RunOrder.Id diff --git a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs index 32003d0..a2f116a 100644 --- a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -40,15 +40,17 @@ namespace AutoWorkshopDatabaseImplement.Implements .Include(x => x.Repair) .Include(x => x.Client) .Include (x => x.Implementer) - .Where(x => (Model.Id.HasValue && x.Id == Model.Id) || (Model.ClientId.HasValue && x.ClientId == Model.ClientId) - || (Model.ImplementerId.HasValue && x.ImplementerId == Model.ImplementerId)) + .Where(x => (Model.Id.HasValue && x.Id == Model.Id) + || (Model.ClientId.HasValue && x.ClientId == Model.ClientId) + || (Model.ImplementerId.HasValue && x.ImplementerId == Model.ImplementerId) + || (Model.Status.HasValue && x.Status == Model.Status)) .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel Model) { - if (!Model.Id.HasValue) + if (!Model.Id.HasValue && !Model.ImplementerId.HasValue && !Model.Status.HasValue) return null; using var Context = new AutoWorkshopDatabase(); @@ -57,7 +59,8 @@ namespace AutoWorkshopDatabaseImplement.Implements .Include(x => x.Repair) .Include(x => x.Client) .Include(x => x.Implementer) - .FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)? + .FirstOrDefault(x => (Model.Id.HasValue && x.Id == Model.Id) + || (Model.ImplementerId.HasValue && Model.Status.HasValue && x.ImplementerId == Model.ImplementerId && x.Status == Model.Status) )? .GetViewModel; } diff --git a/AutoWorkshopFileImplement/Implements/OrderStorage.cs b/AutoWorkshopFileImplement/Implements/OrderStorage.cs index 7de8694..efcc9d2 100644 --- a/AutoWorkshopFileImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopFileImplement/Implements/OrderStorage.cs @@ -56,15 +56,17 @@ namespace AutoWorkshopFileImplement.Implements public OrderViewModel? GetElement(OrderSearchModel Model) { - if (!Model.Id.HasValue) + if (!Model.Id.HasValue && !Model.ImplementerId.HasValue && !Model.Status.HasValue) return null; - - var Order = _source.Orders.FirstOrDefault(x => (Model.Id.HasValue && x.Id == Model.Id)); + + var Order = _source.Orders.FirstOrDefault(x => + (Model.Id.HasValue && x.Id == Model.Id) + || (Model.ImplementerId.HasValue && Model.Status.HasValue && x.ImplementerId == Model.ImplementerId && x.Status == Model.Status) ); if (Order == null) return null; - return AddRepairName(Order.GetViewModel); + return AddImplementerFIO(AddClientFIO(AddRepairName(Order.GetViewModel))); } public OrderViewModel? Insert(OrderBindingModel Model) diff --git a/AutoWorkshopImplement/Implements/OrderStorage.cs b/AutoWorkshopImplement/Implements/OrderStorage.cs index 423abff..e83b110 100644 --- a/AutoWorkshopImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopImplement/Implements/OrderStorage.cs @@ -47,12 +47,13 @@ namespace AutoWorkshopListImplement.Implements public OrderViewModel? GetElement(OrderSearchModel Model) { - if (!Model.Id.HasValue) + if (!Model.Id.HasValue && !Model.ImplementerId.HasValue && !Model.Status.HasValue) return null; foreach (var Order in _source.Orders) { - if ((Model.Id.HasValue && Order.Id == Model.Id) || (Model.ImplementerId.HasValue && Order.ImplementerId == Model.ImplementerId)) + if ((Model.Id.HasValue && Order.Id == Model.Id) + || (Model.ImplementerId.HasValue && Model.Status.HasValue && Order.ImplementerId == Model.ImplementerId && Order.Status == Model.Status)) { return JoinImplementerFIO(JoinClientFIO(JoinRepairName(Order.GetViewModel))); } diff --git a/AutoWorkshopView/AutoWorkshopView.csproj b/AutoWorkshopView/AutoWorkshopView.csproj index 8628399..a2417ee 100644 --- a/AutoWorkshopView/AutoWorkshopView.csproj +++ b/AutoWorkshopView/AutoWorkshopView.csproj @@ -28,4 +28,10 @@ + + + Always + + + \ No newline at end of file diff --git a/AutoWorkshopView/Forms/FormCreateOrder.Designer.cs b/AutoWorkshopView/Forms/FormCreateOrder.Designer.cs index 308a3ad..91f816c 100644 --- a/AutoWorkshopView/Forms/FormCreateOrder.Designer.cs +++ b/AutoWorkshopView/Forms/FormCreateOrder.Designer.cs @@ -28,6 +28,8 @@ /// private void InitializeComponent() { + ClientLabel = new Label(); + ClientComboBox = new ComboBox(); RepairComboBox = new ComboBox(); CountTextBox = new TextBox(); SumTextBox = new TextBox(); @@ -38,6 +40,24 @@ SumLabel = new Label(); SuspendLayout(); // + // ClientLabel + // + ClientLabel.AutoSize = true; + ClientLabel.Location = new Point(12, 102); + ClientLabel.Name = "ClientLabel"; + ClientLabel.Size = new Size(49, 15); + ClientLabel.TabIndex = 8; + ClientLabel.Text = "Клиент:"; + // + // ClientComboBox + // + ClientComboBox.DropDownStyle = ComboBoxStyle.DropDownList; + ClientComboBox.FormattingEnabled = true; + ClientComboBox.Location = new Point(103, 99); + ClientComboBox.Name = "ClientComboBox"; + ClientComboBox.Size = new Size(232, 23); + ClientComboBox.TabIndex = 9; + // // RepairComboBox // RepairComboBox.DropDownStyle = ComboBoxStyle.DropDownList; @@ -66,7 +86,7 @@ // // SaveButton // - SaveButton.Location = new Point(173, 107); + SaveButton.Location = new Point(173, 139); SaveButton.Name = "SaveButton"; SaveButton.Size = new Size(81, 23); SaveButton.TabIndex = 3; @@ -76,7 +96,7 @@ // // CancelButton // - CancelButton.Location = new Point(260, 107); + CancelButton.Location = new Point(260, 139); CancelButton.Name = "CancelButton"; CancelButton.Size = new Size(75, 23); CancelButton.TabIndex = 4; @@ -110,33 +130,15 @@ SumLabel.Size = new Size(48, 15); SumLabel.TabIndex = 7; SumLabel.Text = "Сумма:"; - // - // labelClient - // - labelClient.AutoSize = true; - labelClient.Location = new Point(26, 131); - labelClient.Name = "labelClient"; - labelClient.Size = new Size(58, 20); - labelClient.TabIndex = 8; - labelClient.Text = "Клиент"; - // - // comboBoxClient - // - comboBoxClient.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxClient.FormattingEnabled = true; - comboBoxClient.Location = new Point(131, 131); - comboBoxClient.Name = "comboBoxClient"; - comboBoxClient.Size = new Size(260, 28); - comboBoxClient.TabIndex = 9; - // - // FormCreateOrder - // - AutoScaleDimensions = new SizeF(7F, 15F); + // + // FormCreateOrder + // + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(348, 142); - Controls.Add(comboBoxClient); - Controls.Add(labelClient); - Controls.Add(SumLabel); + ClientSize = new Size(348, 183); + Controls.Add(ClientComboBox); + Controls.Add(ClientLabel); + Controls.Add(SumLabel); Controls.Add(CountLabel); Controls.Add(RepairLabel); Controls.Add(CancelButton); @@ -161,7 +163,7 @@ private Label RepairLabel; private Label CountLabel; private Label SumLabel; - private Label labelClient; - private ComboBox comboBoxClient; + private Label ClientLabel; + private ComboBox ClientComboBox; } } \ No newline at end of file diff --git a/AutoWorkshopView/Forms/FormCreateOrder.cs b/AutoWorkshopView/Forms/FormCreateOrder.cs index c8a0816..48d91de 100644 --- a/AutoWorkshopView/Forms/FormCreateOrder.cs +++ b/AutoWorkshopView/Forms/FormCreateOrder.cs @@ -51,10 +51,10 @@ namespace AutoWorkshopView.Forms var list = _clientLogic.ReadList(null); if (list != null) { - comboBoxClient.DisplayMember = "ClientFIO"; - comboBoxClient.ValueMember = "Id"; - comboBoxClient.DataSource = list; - comboBoxClient.SelectedItem = null; + ClientComboBox.DisplayMember = "ClientFIO"; + ClientComboBox.ValueMember = "Id"; + ClientComboBox.DataSource = list; + ClientComboBox.SelectedItem = null; } _logger.LogInformation("Клиенты загружены"); } @@ -110,6 +110,12 @@ namespace AutoWorkshopView.Forms return; } + if (ClientComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите клиента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); try @@ -117,6 +123,7 @@ namespace AutoWorkshopView.Forms var OperationResult = _orderLogic.CreateOrder(new OrderBindingModel { RepairId = Convert.ToInt32(RepairComboBox.SelectedValue), + ClientId = Convert.ToInt32(ClientComboBox.SelectedValue), Count = Convert.ToInt32(CountTextBox.Text), Sum = Convert.ToDouble(SumTextBox.Text) }); diff --git a/AutoWorkshopView/MainForm.cs b/AutoWorkshopView/MainForm.cs index f214854..f4808c4 100644 --- a/AutoWorkshopView/MainForm.cs +++ b/AutoWorkshopView/MainForm.cs @@ -227,7 +227,8 @@ namespace AutoWorkshopView private void StartWorkToolStripMenuItem_Click(object sender, EventArgs e) { - _workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, _orderLogic); + var ImplementerLogic = Program.ServiceProvider?.GetService(typeof(IImplementerLogic)); + _workProcess.DoWork((ImplementerLogic as IImplementerLogic)!, _orderLogic); MessageBox.Show("Процесс обработки запущен", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); }