diff --git a/DatabaseImplement/Models/Supplier.cs b/DatabaseImplement/Models/Supplier.cs index 86b2da0..90f461d 100644 --- a/DatabaseImplement/Models/Supplier.cs +++ b/DatabaseImplement/Models/Supplier.cs @@ -44,6 +44,7 @@ namespace DatabaseImplement.Models { Id = model.Id, Name = model.Name, + Deals = model.Deals, Products = model.AvailibleProducts.Select(x => new SupplierProduct { @@ -55,10 +56,12 @@ namespace DatabaseImplement.Models public void Update(Database context, SupplierBindingModel model) { Name = model.Name; + + Debug.WriteLine(model.AvailibleProducts.Keys); Products = model.AvailibleProducts.Select(x => new SupplierProduct { - Product = context.Products.First(y => y.Id == x.Key), + Product = context.Products.First(y => y.Id == x.Value.Item1.Id), Count = x.Value.Item2 }).ToList(); } @@ -78,28 +81,55 @@ namespace DatabaseImplement.Models } public void UpdateProducts(Database context, SupplierBindingModel model) { - var supplierProducts = context.SupplierProducts.Where(rec => - rec.Id == model.Id).ToList(); + var supplierProducts = context.SupplierProducts.Where(rec => rec.SupplierId == model.Id).ToList(); + if (supplierProducts != null && supplierProducts.Count > model.AvailibleProducts.Count) + { + var newList = new List(); + var trueCount = model.AvailibleProducts.Count; + for (int i = 0; i < trueCount; i++) + { + newList.Add(supplierProducts[i]); + } + supplierProducts = newList; + } if (supplierProducts != null && supplierProducts.Count > 0) { // удалили те, которых нет в модели - context.SupplierProducts.RemoveRange(supplierProducts.Where(rec - => !model.AvailibleProducts.ContainsKey(rec.ProductId))); + foreach (var item in supplierProducts) + { + bool flag = false; + foreach(var product in model.AvailibleProducts) + { + if (product.Value.Item1.Id == item.ProductId) + { + flag = true; + } + } + if (!flag) + { + context.SupplierProducts.Remove(item); + } + } context.SaveChanges(); - // обновили количество у существующих записей + //обновили количество у существующих записей, затем удалили foreach (var updateProduct in supplierProducts) { - updateProduct.Count = model.AvailibleProducts[updateProduct.ProductId].Item2; - model.AvailibleProducts.Remove(updateProduct.ProductId); + try + { + updateProduct.Count = model.AvailibleProducts[updateProduct.Id].Item2; + model.AvailibleProducts.Remove(updateProduct.Id); + } + catch (Exception ex) { } } context.SaveChanges(); } var supplier = context.Suppliers.First(x => x.Id == Id); + // тут всё дюпается, тут должны добавляться те которые остались в модели foreach (var pc in model.AvailibleProducts) { context.SupplierProducts.Add(new SupplierProduct { Supplier = supplier, - Product = context.Products.First(x => x.Id == pc.Key), + Product = context.Products.First(x => x.Id == pc.Value.Item1.Id), Count = pc.Value.Item2 }); context.SaveChanges(); diff --git a/WinFormsApp/FormSuppliers.Designer.cs b/WinFormsApp/FormSuppliers.Designer.cs index 97b4c89..c890be7 100644 --- a/WinFormsApp/FormSuppliers.Designer.cs +++ b/WinFormsApp/FormSuppliers.Designer.cs @@ -29,7 +29,11 @@ private void InitializeComponent() { groupBoxCreateSupplier = new GroupBox(); + label4 = new Label(); + label3 = new Label(); + numericUpDownDeals = new NumericUpDown(); groupBoxSupplierProducts = new GroupBox(); + buttonRefresh = new Button(); label2 = new Label(); buttonCancel = new Button(); label1 = new Label(); @@ -46,16 +50,13 @@ buttonUpdateSupplier = new Button(); buttonCreateSupplier = new Button(); dataGridView = new DataGridView(); - numericUpDownDeals = new NumericUpDown(); - label3 = new Label(); - label4 = new Label(); groupBoxCreateSupplier.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)numericUpDownDeals).BeginInit(); groupBoxSupplierProducts.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)numericUpDownCount).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridViewProducts).BeginInit(); groupBoxControls.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); - ((System.ComponentModel.ISupportInitialize)numericUpDownDeals).BeginInit(); SuspendLayout(); // // groupBoxCreateSupplier @@ -74,8 +75,35 @@ groupBoxCreateSupplier.TabStop = false; groupBoxCreateSupplier.Text = "Создание/изменение поставщика"; // + // label4 + // + label4.AutoSize = true; + label4.Location = new Point(17, 93); + label4.Name = "label4"; + label4.Size = new Size(87, 15); + label4.TabIndex = 15; + label4.Text = "Кол-во сделок"; + // + // label3 + // + label3.AutoSize = true; + label3.Location = new Point(6, 54); + label3.Name = "label3"; + label3.Size = new Size(101, 15); + label3.TabIndex = 14; + label3.Text = "Имя поставщика"; + // + // numericUpDownDeals + // + numericUpDownDeals.Location = new Point(110, 91); + numericUpDownDeals.Maximum = new decimal(new int[] { 100000, 0, 0, 0 }); + numericUpDownDeals.Name = "numericUpDownDeals"; + numericUpDownDeals.Size = new Size(245, 23); + numericUpDownDeals.TabIndex = 14; + // // groupBoxSupplierProducts // + groupBoxSupplierProducts.Controls.Add(buttonRefresh); groupBoxSupplierProducts.Controls.Add(label2); groupBoxSupplierProducts.Controls.Add(buttonCancel); groupBoxSupplierProducts.Controls.Add(label1); @@ -91,6 +119,16 @@ groupBoxSupplierProducts.TabStop = false; groupBoxSupplierProducts.Text = "Доступные товары поставщика"; // + // buttonRefresh + // + buttonRefresh.Location = new Point(58, 307); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(81, 23); + buttonRefresh.TabIndex = 14; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // // label2 // label2.AutoSize = true; @@ -108,6 +146,7 @@ buttonCancel.TabIndex = 5; buttonCancel.Text = "Отмена"; buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; // // label1 // @@ -237,32 +276,6 @@ dataGridView.Size = new Size(392, 637); dataGridView.TabIndex = 4; // - // numericUpDownDeals - // - numericUpDownDeals.Location = new Point(110, 91); - numericUpDownDeals.Maximum = new decimal(new int[] { 100000, 0, 0, 0 }); - numericUpDownDeals.Name = "numericUpDownDeals"; - numericUpDownDeals.Size = new Size(245, 23); - numericUpDownDeals.TabIndex = 14; - // - // label3 - // - label3.AutoSize = true; - label3.Location = new Point(6, 54); - label3.Name = "label3"; - label3.Size = new Size(101, 15); - label3.TabIndex = 14; - label3.Text = "Имя поставщика"; - // - // label4 - // - label4.AutoSize = true; - label4.Location = new Point(17, 93); - label4.Name = "label4"; - label4.Size = new Size(87, 15); - label4.TabIndex = 15; - label4.Text = "Кол-во сделок"; - // // FormSuppliers // AutoScaleDimensions = new SizeF(7F, 15F); @@ -276,13 +289,13 @@ Load += FormSuppliers_Load; groupBoxCreateSupplier.ResumeLayout(false); groupBoxCreateSupplier.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)numericUpDownDeals).EndInit(); groupBoxSupplierProducts.ResumeLayout(false); groupBoxSupplierProducts.PerformLayout(); ((System.ComponentModel.ISupportInitialize)numericUpDownCount).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridViewProducts).EndInit(); groupBoxControls.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); - ((System.ComponentModel.ISupportInitialize)numericUpDownDeals).EndInit(); ResumeLayout(false); } @@ -309,5 +322,6 @@ private Label label4; private Label label3; private NumericUpDown numericUpDownDeals; + private Button buttonRefresh; } } \ No newline at end of file diff --git a/WinFormsApp/FormSuppliers.cs b/WinFormsApp/FormSuppliers.cs index 14ebd30..74347e2 100644 --- a/WinFormsApp/FormSuppliers.cs +++ b/WinFormsApp/FormSuppliers.cs @@ -54,16 +54,6 @@ namespace WinFormsApp private void LoadSupplierData() { - if (_id.HasValue) - { - var view = _supplierLogic.ReadElement(new SupplierSearchModel - { - Id = _id.Value - }); - _supplierProducts = view.AvailibleProducts; - textBoxName.Text = view.Name; - numericUpDownDeals.Value = view.Deals; - } dataGridViewProducts.Rows.Clear(); foreach (var pc in _supplierProducts) { @@ -110,6 +100,9 @@ namespace WinFormsApp { Id = _id.Value }); + _supplierProducts = view.AvailibleProducts; + textBoxName.Text = view.Name; + numericUpDownDeals.Value = view.Deals; LoadSupplierData(); } @@ -219,7 +212,7 @@ namespace WinFormsApp }; var operationResult = _id.HasValue ? _supplierLogic.Update(model) : _supplierLogic.Create(model); - + if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -235,17 +228,22 @@ namespace WinFormsApp } finally { - LoadData(); + LoadData(); _id = null; - _supplierProducts.Clear(); + _supplierProducts = new Dictionary; textBoxName.Text = string.Empty; numericUpDownDeals.Value = 0; - groupBoxControls.Enabled = true; + //groupBoxControls.Enabled = true; groupBoxControls.Show(); - groupBoxCreateSupplier.Enabled = false; + //groupBoxCreateSupplier.Enabled = false; groupBoxCreateSupplier.Hide(); textBoxName.Text = string.Empty; } } + + private void buttonRefresh_Click(object sender, EventArgs e) + { + LoadSupplierData(); + } } }