From 0a09ce4051bf12f6bd9eed7e5f721fd2fe567ab7 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 19 Mar 2024 20:56:40 +0400 Subject: [PATCH] Fix RepairName --- .../Forms/FormCreateOrder.Designer.cs | 2 +- AutoWorkshop/Forms/FormCreateOrder.cs | 35 +++--- AutoWorkshop/Forms/FormRepair.cs | 9 +- .../Forms/FormRepairComponent.Designer.cs | 1 + AutoWorkshop/Forms/FormRepairs.Designer.cs | 2 +- AutoWorkshop/MainForm.Designer.cs | 10 +- AutoWorkshop/MainForm.cs | 53 ++++----- AutoWorkshop/nlog.config | 15 +++ .../BusinessLogics/ComponentLogic.cs | 2 +- .../BusinessLogics/OrderLogic.cs | 107 ++++++++---------- ...tBindingModel.cs => RepairBindingModel.cs} | 0 .../ViewModels/ComponentViewModel.cs | 6 +- .../ViewModels/OrderViewModel.cs | 2 +- .../ViewModels/RepairViewModel.cs | 8 +- .../Implements/OrderStorage.cs | 25 ++-- 15 files changed, 135 insertions(+), 142 deletions(-) create mode 100644 AutoWorkshop/nlog.config rename AutoWorkshopContracts/BindingModels/{ProductBindingModel.cs => RepairBindingModel.cs} (100%) diff --git a/AutoWorkshop/Forms/FormCreateOrder.Designer.cs b/AutoWorkshop/Forms/FormCreateOrder.Designer.cs index a84b1ea..7ac6681 100644 --- a/AutoWorkshop/Forms/FormCreateOrder.Designer.cs +++ b/AutoWorkshop/Forms/FormCreateOrder.Designer.cs @@ -40,6 +40,7 @@ // // RepairComboBox // + RepairComboBox.DropDownStyle = ComboBoxStyle.DropDownList; RepairComboBox.FormattingEnabled = true; RepairComboBox.Location = new Point(103, 12); RepairComboBox.Name = "RepairComboBox"; @@ -62,7 +63,6 @@ SumTextBox.ReadOnly = true; SumTextBox.Size = new Size(232, 23); SumTextBox.TabIndex = 2; - SumTextBox.TextChanged += SumTextBox_TextChanged; // // SaveButton // diff --git a/AutoWorkshop/Forms/FormCreateOrder.cs b/AutoWorkshop/Forms/FormCreateOrder.cs index 1440327..24ad884 100644 --- a/AutoWorkshop/Forms/FormCreateOrder.cs +++ b/AutoWorkshop/Forms/FormCreateOrder.cs @@ -8,38 +8,38 @@ namespace AutoWorkshopView.Forms public partial class FormCreateOrder : Form { private readonly ILogger _logger; - private readonly IRepairLogic _logicI; - private readonly IOrderLogic _logicO; + private readonly IRepairLogic _repairLogic; + private readonly IOrderLogic _orderLogic; - public FormCreateOrder(ILogger Logger, IRepairLogic LogicI, IOrderLogic LogicO) + public FormCreateOrder(ILogger Logger, IRepairLogic RepairLogic, IOrderLogic OrderLogic) { InitializeComponent(); _logger = Logger; - _logicI = LogicI; - _logicO = LogicO; + _repairLogic = RepairLogic; + _orderLogic = OrderLogic; } private void FormCreateOrder_Load(object sender, EventArgs e) { - _logger.LogInformation("Загрузка ремонта для заказа"); + _logger.LogInformation("Загрузка ремонтов для заказа"); try { - var list = _logicI.ReadList(null); - if (list != null) + var List = _repairLogic.ReadList(null); + if (List != null) { RepairComboBox.DisplayMember = "RepairName"; RepairComboBox.ValueMember = "Id"; - RepairComboBox.DataSource = list; + RepairComboBox.DataSource = List; RepairComboBox.SelectedItem = null; } - _logger.LogInformation("Ремонт загружен"); + _logger.LogInformation("Ремонты загружены"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки ремонта"); + _logger.LogError(ex, "Ошибка загрузки ремонтов"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -51,13 +51,13 @@ namespace AutoWorkshopView.Forms try { int id = Convert.ToInt32(RepairComboBox.SelectedValue); - var Repair = _logicI.ReadElement(new RepairSearchModel + var Repair = _repairLogic.ReadElement(new RepairSearchModel { Id = id }); - int count = Convert.ToInt32(CountTextBox.Text); + int Count = Convert.ToInt32(CountTextBox.Text); - SumTextBox.Text = Math.Round(count * (Repair?.Price ?? 0), 2).ToString(); + SumTextBox.Text = Math.Round(Count * (Repair?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } @@ -74,11 +74,6 @@ namespace AutoWorkshopView.Forms CalcSum(); } - private void SumTextBox_TextChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void SaveButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(CountTextBox.Text)) @@ -97,7 +92,7 @@ namespace AutoWorkshopView.Forms try { - var OperationResult = _logicO.CreateOrder(new OrderBindingModel + var OperationResult = _orderLogic.CreateOrder(new OrderBindingModel { RepairId = Convert.ToInt32(RepairComboBox.SelectedValue), Count = Convert.ToInt32(CountTextBox.Text), diff --git a/AutoWorkshop/Forms/FormRepair.cs b/AutoWorkshop/Forms/FormRepair.cs index 422ec24..8752f0f 100644 --- a/AutoWorkshop/Forms/FormRepair.cs +++ b/AutoWorkshop/Forms/FormRepair.cs @@ -97,13 +97,11 @@ namespace AutoWorkshopView.Forms if (_repairComponents.ContainsKey(Form.Id)) { - _repairComponents[Form.Id] = (Form.ComponentModel, - Form.Count); + _repairComponents[Form.Id] = (Form.ComponentModel, Form.Count); } else { - _repairComponents.Add(Form.Id, (Form.ComponentModel, - Form.Count)); + _repairComponents.Add(Form.Id, (Form.ComponentModel, Form.Count)); } LoadData(); @@ -146,7 +144,8 @@ namespace AutoWorkshopView.Forms { try { - _logger.LogInformation("Удаление компонента: {ComponentName} - {Count}", DataGridView.SelectedRows[0].Cells[1].Value); + _logger.LogInformation("Удаление компонента: {ComponentName} - {Count}", DataGridView.SelectedRows[0].Cells[1].Value, + DataGridView.SelectedRows[0].Cells[2].Value); _repairComponents?.Remove(Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value)); } diff --git a/AutoWorkshop/Forms/FormRepairComponent.Designer.cs b/AutoWorkshop/Forms/FormRepairComponent.Designer.cs index 360d6c1..74a0abf 100644 --- a/AutoWorkshop/Forms/FormRepairComponent.Designer.cs +++ b/AutoWorkshop/Forms/FormRepairComponent.Designer.cs @@ -63,6 +63,7 @@ // // ComboBox // + ComboBox.DropDownStyle = ComboBoxStyle.DropDownList; ComboBox.FormattingEnabled = true; ComboBox.Location = new Point(95, 12); ComboBox.Name = "ComboBox"; diff --git a/AutoWorkshop/Forms/FormRepairs.Designer.cs b/AutoWorkshop/Forms/FormRepairs.Designer.cs index d530879..3d21ff0 100644 --- a/AutoWorkshop/Forms/FormRepairs.Designer.cs +++ b/AutoWorkshop/Forms/FormRepairs.Designer.cs @@ -96,7 +96,7 @@ Controls.Add(AddButton); Controls.Add(DataGridView); Name = "FormRepairs"; - Text = "FormRepairs"; + Text = "Ремонты"; Load += FormRepairs_Load; ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); ResumeLayout(false); diff --git a/AutoWorkshop/MainForm.Designer.cs b/AutoWorkshop/MainForm.Designer.cs index ff58bb9..732938d 100644 --- a/AutoWorkshop/MainForm.Designer.cs +++ b/AutoWorkshop/MainForm.Designer.cs @@ -65,14 +65,14 @@ ComponentsStripMenuItem.Name = "ComponentsStripMenuItem"; ComponentsStripMenuItem.Size = new Size(180, 22); ComponentsStripMenuItem.Text = "Компоненты"; - ComponentsStripMenuItem.Click += КомпонентыStripMenuItem_Click; + ComponentsStripMenuItem.Click += ComponentsStripMenuItem_Click; // // RepairStripMenuItem // RepairStripMenuItem.Name = "RepairStripMenuItem"; RepairStripMenuItem.Size = new Size(180, 22); RepairStripMenuItem.Text = "Ремонты"; - RepairStripMenuItem.Click += МороженноеStripMenuItem_Click; + RepairStripMenuItem.Click += RepairsStripMenuItem_Click; // // DataGridView // @@ -104,7 +104,7 @@ TakeInWorkButton.TabIndex = 3; TakeInWorkButton.Text = "Отдать заказ в работу"; TakeInWorkButton.UseVisualStyleBackColor = true; - TakeInWorkButton.Click += TakeInWorkButton_Click; + TakeInWorkButton.Click += TakeOrderInWorkButton_Click; // // ReadyButton // @@ -115,7 +115,7 @@ ReadyButton.TabIndex = 4; ReadyButton.Text = "Заказ готов"; ReadyButton.UseVisualStyleBackColor = true; - ReadyButton.Click += ReadyButton_Click; + ReadyButton.Click += OrderReadyButton_Click; // // IssuedButton // @@ -126,7 +126,7 @@ IssuedButton.TabIndex = 5; IssuedButton.Text = "Заказ выдан"; IssuedButton.UseVisualStyleBackColor = true; - IssuedButton.Click += IssuedButton_Click; + IssuedButton.Click += OrderDeliveredButton_Click; // // RefreshButton // diff --git a/AutoWorkshop/MainForm.cs b/AutoWorkshop/MainForm.cs index 0df3bc2..7737f4a 100644 --- a/AutoWorkshop/MainForm.cs +++ b/AutoWorkshop/MainForm.cs @@ -18,16 +18,6 @@ namespace AutoWorkshopView _orderLogic = OrderLogic; } - private void КомпонентыStripMenuItem_Click(object sender, EventArgs e) - { - var Service = Program.ServiceProvider?.GetService(typeof(FormComponents)); - - if (Service is FormComponents Form) - { - Form.ShowDialog(); - } - } - private void MainForm_Load(object sender, EventArgs e) { LoadData(); @@ -36,11 +26,11 @@ namespace AutoWorkshopView private void LoadData() { _logger.LogInformation("Загрузка заказов"); - + try { var List = _orderLogic.ReadList(null); - + if (List != null) { DataGridView.DataSource = List; @@ -57,10 +47,20 @@ namespace AutoWorkshopView } } - private void МороженноеStripMenuItem_Click(object sender, EventArgs e) + private void ComponentsStripMenuItem_Click(object sender, EventArgs e) + { + var Service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + + if (Service is FormComponents Form) + { + Form.ShowDialog(); + } + } + + private void RepairsStripMenuItem_Click(object sender, EventArgs e) { var Service = Program.ServiceProvider?.GetService(typeof(FormRepairs)); - + if (Service is FormRepairs Form) { Form.ShowDialog(); @@ -78,20 +78,7 @@ namespace AutoWorkshopView } } - private OrderBindingModel CreateBindingModel(int id, bool isDone = false) - { - return new OrderBindingModel - { - Id = id, - RepairId = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["RepairId"].Value), - Status = Enum.Parse(DataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(DataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(DataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), - }; - } - - private void TakeInWorkButton_Click(object sender, EventArgs e) + private void TakeOrderInWorkButton_Click(object sender, EventArgs e) { if (DataGridView.SelectedRows.Count == 1) { @@ -101,7 +88,7 @@ namespace AutoWorkshopView try { - var OperationResult = _orderLogic.TakeOrderInWork(CreateBindingModel(id)); + var OperationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id }); if (!OperationResult) { @@ -119,7 +106,7 @@ namespace AutoWorkshopView } - private void ReadyButton_Click(object sender, EventArgs e) + private void OrderReadyButton_Click(object sender, EventArgs e) { if (DataGridView.SelectedRows.Count == 1) { @@ -129,7 +116,7 @@ namespace AutoWorkshopView try { - var OperationResult = _orderLogic.FinishOrder(CreateBindingModel(id)); + var OperationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); if (!OperationResult) { @@ -146,7 +133,7 @@ namespace AutoWorkshopView } } - private void IssuedButton_Click(object sender, EventArgs e) + private void OrderDeliveredButton_Click(object sender, EventArgs e) { if (DataGridView.SelectedRows.Count == 1) { @@ -156,7 +143,7 @@ namespace AutoWorkshopView try { - var OperationResult = _orderLogic.DeliveryOrder(CreateBindingModel(id)); + var OperationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); if (!OperationResult) { diff --git a/AutoWorkshop/nlog.config b/AutoWorkshop/nlog.config new file mode 100644 index 0000000..85797a7 --- /dev/null +++ b/AutoWorkshop/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AutoWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs b/AutoWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs index 71539e0..b1736de 100644 --- a/AutoWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/AutoWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -84,7 +84,7 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics public bool Delete(ComponentBindingModel Model) { - CheckModel(Model); + CheckModel(Model, false); _logger.LogInformation("Delete. Id:{Id}", Model.Id); if (_componentStorage.Delete(Model) is null) diff --git a/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 9e5cf5a..246c491 100644 --- a/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AutoWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -31,7 +31,7 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics return null; } - _logger.LogInformation("ReadList. Count:{Count}", List.Count); + _logger.LogInformation("ReadList. Count: {Count}", List.Count); return List; } @@ -39,6 +39,14 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics { CheckModel(Model); + if (Model.Status != OrderStatus.Undefined) + { + _logger.LogWarning("Invalid order status"); + return false; + } + + Model.Status = OrderStatus.Accepted; + if (_orderStorage.Insert(Model) is null) { _logger.LogWarning("Insert operation failed"); @@ -48,81 +56,58 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics return true; } - public bool TakeOrderInWork(OrderBindingModel Model) + private bool ChangeOrderStatus(OrderBindingModel Model, OrderStatus NewStatus) { - CheckModel(Model); + CheckModel(Model, false); - var Element = _orderStorage.GetElement(new OrderSearchModel - { - Id = Model.Id - }); - - if (Element is null) - throw new InvalidOperationException("Заказ с таким идентификатором не существует"); - - Element.Status = OrderStatus.Accepted; - - if (_orderStorage.Update(Model) is null) + var Order = _orderStorage.GetElement(new OrderSearchModel { Id = Model.Id }); + + if (Order == null) { - _logger.LogWarning("Update operation failed"); + _logger.LogWarning("Change status operation failed. Order not found"); + return false; + } + + if (Order.Status + 1 != NewStatus) + { + _logger.LogWarning("Change status operation failed. Incorrect new status: {NewStatus}. Current status: {currStatus}", + NewStatus, Order.Status); + return false; + } + + Model.RepairId = Order.RepairId; + Model.Count = Order.Count; + Model.Sum = Order.Sum; + Model.DateCreate = Order.DateCreate; + Model.Status = NewStatus; + + if (Model.Status == OrderStatus.Ready) + Model.DateImplement = DateTime.Now; + else + Model.DateImplement = Order.DateImplement; + + if (_orderStorage.Update(Model) == null) + { + _logger.LogWarning("Change status operation failed"); return false; } return true; } + public bool TakeOrderInWork(OrderBindingModel Model) + { + return ChangeOrderStatus(Model, OrderStatus.BeingProcessed); + } + public bool FinishOrder(OrderBindingModel Model) { - CheckModel(Model); - - var Element = _orderStorage.GetElement(new OrderSearchModel - { - Id = Model.Id - }); - - if (Element is null) - throw new InvalidOperationException("Заказ с таким идентификатором не существует"); - - if (Element.Status != OrderStatus.BeingProcessed) - { - _logger.LogWarning("FinishOrder. Invalid status"); - return false; - } - - if (_orderStorage.Update(Model) is null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - - return true; + return ChangeOrderStatus(Model, OrderStatus.Ready); } public bool DeliveryOrder(OrderBindingModel Model) { - CheckModel(Model); - - var Element = _orderStorage.GetElement(new OrderSearchModel - { - Id = Model.Id - }); - - if (Element is null) - throw new InvalidOperationException("Заказ с таким идентификатором не существует"); - - if (Element.Status != OrderStatus.Ready) - { - _logger.LogWarning("DeliveryOrder. Invalid status"); - return false; - } - - if (_orderStorage.Update(Model) is null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - - return true; + return ChangeOrderStatus(Model, OrderStatus.Delivered); } private void CheckModel(OrderBindingModel Model, bool WithParams = true) diff --git a/AutoWorkshopContracts/BindingModels/ProductBindingModel.cs b/AutoWorkshopContracts/BindingModels/RepairBindingModel.cs similarity index 100% rename from AutoWorkshopContracts/BindingModels/ProductBindingModel.cs rename to AutoWorkshopContracts/BindingModels/RepairBindingModel.cs diff --git a/AutoWorkshopContracts/ViewModels/ComponentViewModel.cs b/AutoWorkshopContracts/ViewModels/ComponentViewModel.cs index 6efe022..ead047b 100644 --- a/AutoWorkshopContracts/ViewModels/ComponentViewModel.cs +++ b/AutoWorkshopContracts/ViewModels/ComponentViewModel.cs @@ -6,11 +6,11 @@ namespace AutoWorkshopContracts.ViewModels public class ComponentViewModel : IComponentModel { public int Id { get; set; } - [DisplayName("Название компонента")] - + + [DisplayName("Название компонента")] public string ComponentName { get; set; } = string.Empty; - [DisplayName("Цена")] + [DisplayName("Цена")] public double Cost { get; set; } } } diff --git a/AutoWorkshopContracts/ViewModels/OrderViewModel.cs b/AutoWorkshopContracts/ViewModels/OrderViewModel.cs index 1a4c4fe..69c83f0 100644 --- a/AutoWorkshopContracts/ViewModels/OrderViewModel.cs +++ b/AutoWorkshopContracts/ViewModels/OrderViewModel.cs @@ -11,7 +11,7 @@ namespace AutoWorkshopContracts.ViewModels public int RepairId { get; set; } - [DisplayName("Ремонт")] + [DisplayName("Ремонт")] public string RepairName { get; set; } = string.Empty; [DisplayName("Количество")] diff --git a/AutoWorkshopContracts/ViewModels/RepairViewModel.cs b/AutoWorkshopContracts/ViewModels/RepairViewModel.cs index 5551506..d0a5f54 100644 --- a/AutoWorkshopContracts/ViewModels/RepairViewModel.cs +++ b/AutoWorkshopContracts/ViewModels/RepairViewModel.cs @@ -6,11 +6,11 @@ namespace AutoWorkshopContracts.ViewModels public class RepairViewModel : IRepairModel { public int Id { get; set; } - [DisplayName("Название изделия")] - + + [DisplayName("Название ремонта")] public string RepairName { get; set; } = string.Empty; - [DisplayName("Цена")] - + + [DisplayName("Цена")] public double Price { get; set; } public Dictionary RepairComponents diff --git a/AutoWorkshopImplement/Implements/OrderStorage.cs b/AutoWorkshopImplement/Implements/OrderStorage.cs index 838bd47..cafea88 100644 --- a/AutoWorkshopImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using AutoWorkshopContracts.SearchModels; using AutoWorkshopContracts.StoragesContracts; using AutoWorkshopContracts.ViewModels; using AutoWorkshopListImplement.Models; +using System.Reflection; namespace AutoWorkshopListImplement.Implements { @@ -21,7 +22,7 @@ namespace AutoWorkshopListImplement.Implements foreach (var Order in _source.Orders) { - Result.Add(Order.GetViewModel); + Result.Add(JoinRepairName(Order.GetViewModel)); } return Result; @@ -38,7 +39,7 @@ namespace AutoWorkshopListImplement.Implements { if (Order.Id == Model.Id) { - Result.Add(Order.GetViewModel); + Result.Add(JoinRepairName(Order.GetViewModel)); break; } } @@ -55,7 +56,7 @@ namespace AutoWorkshopListImplement.Implements { if (Order.Id == Model.Id) { - return Order.GetViewModel; + return JoinRepairName(Order.GetViewModel); } } @@ -79,7 +80,7 @@ namespace AutoWorkshopListImplement.Implements return null; _source.Orders.Add(NewOrder); - return NewOrder.GetViewModel; + return JoinRepairName(NewOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel Model) @@ -89,7 +90,7 @@ namespace AutoWorkshopListImplement.Implements if (Order.Id == Model.Id) { Order.Update(Model); - return Order.GetViewModel; + return JoinRepairName(Order.GetViewModel); } } @@ -105,12 +106,22 @@ namespace AutoWorkshopListImplement.Implements var Element = _source.Orders[i]; _source.Orders.RemoveAt(i); - return Element.GetViewModel; + return JoinRepairName(Element.GetViewModel); } } return null; } + + private OrderViewModel JoinRepairName(OrderViewModel Model) + { + var SelectedRepair = _source.Repairs.Find(reinforced => reinforced.Id == Model.RepairId); + + if (SelectedRepair != null) + { + Model.RepairName = SelectedRepair.RepairName; + } + return Model; + } } } - \ No newline at end of file