226 lines
8.5 KiB
C#
226 lines
8.5 KiB
C#
using MySql.Data.MySqlClient;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Drawing.Imaging;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using System.Xml.Linq;
|
||
using static NeshinaPolina2111.AddProduction;
|
||
|
||
namespace NeshinaPolina2111
|
||
{
|
||
public partial class EditProduction : Form
|
||
{
|
||
public EditProduction()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
public int ProductId { get; set; }
|
||
|
||
|
||
private void btnEdit_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
//проверки все
|
||
if (ProductId <= 0)
|
||
{
|
||
MessageBox.Show("ID материала не задан.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
this.Close();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbArticle.Text))
|
||
{
|
||
MessageBox.Show("Введите Артикул", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbArticle.Focus();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbMinPrice.Text))
|
||
{
|
||
MessageBox.Show("Введите Минимальную цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbMinPrice.Focus();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbName.Text))
|
||
{
|
||
MessageBox.Show("Введите Название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbName.Focus();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbWidth.Text))
|
||
{
|
||
MessageBox.Show("Введите Ширину", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbWidth.Focus();
|
||
return;
|
||
}
|
||
|
||
if (cmbProduct.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Выберите тип продукта.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
cmbProduct.Focus();
|
||
return;
|
||
}
|
||
|
||
if (!decimal.TryParse(tbMinPrice.Text, out decimal price) || price < 0)
|
||
{
|
||
MessageBox.Show("Введите корректную цену (неотрицательное число)", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbMinPrice.Focus();
|
||
return;
|
||
}
|
||
|
||
|
||
var typeItem = (ComboItem)cmbProduct.SelectedItem;
|
||
string upquery = @"
|
||
update Products_import
|
||
set
|
||
Name = @Name,
|
||
Article = @Article,
|
||
MinPrice = @MinPrice,
|
||
Width = @Width,
|
||
idProduct_type_import = @idProduct_type_import
|
||
where idProducts_import = @idProducts_import";
|
||
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if (conn.State == ConnectionState.Closed)
|
||
conn.Open();
|
||
using (var cmd = new MySqlCommand(upquery, conn))
|
||
{
|
||
cmd.Parameters.AddWithValue("@idProduct_type_import", typeItem.Id);
|
||
cmd.Parameters.AddWithValue("@Name", tbName.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@Article", tbArticle.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@MinPrice", price);
|
||
cmd.Parameters.AddWithValue("@Width", tbWidth.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@idProducts_import", ProductId);
|
||
try
|
||
{
|
||
int rowsAffected = cmd.ExecuteNonQuery();
|
||
if (rowsAffected > 0)
|
||
{
|
||
MessageBox.Show("Материал успешно обновлён!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
this.DialogResult = DialogResult.OK;
|
||
this.Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось обновить материал. Возможно, он был удалён.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
this.DialogResult = DialogResult.Cancel;
|
||
this.Close();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка при обновлении:\n{ex.ToString()}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
private void LoadProduct() //загрузка продуктов
|
||
{
|
||
if (ProductId <= 0)
|
||
{
|
||
MessageBox.Show("Некорректный ID материала.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
this.DialogResult = DialogResult.Cancel;
|
||
this.Close();
|
||
return;
|
||
}
|
||
|
||
LoadTypes();
|
||
string query = @"
|
||
select
|
||
p.idProduct_type_import,
|
||
p.Name,
|
||
p.Article,
|
||
p.MinPrice,
|
||
p.Width
|
||
from Products_import p
|
||
WHERE p.idProducts_import = @id";
|
||
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if(conn.State == ConnectionState.Closed)
|
||
conn.Open();
|
||
|
||
using (var cmd = new MySqlCommand(query, conn))
|
||
{
|
||
cmd.Parameters.AddWithValue("@id", ProductId);
|
||
|
||
using (var reader = cmd.ExecuteReader())
|
||
{
|
||
if (reader.Read())
|
||
{
|
||
tbName.Text = reader.GetString("Name");
|
||
tbArticle.Text = reader.GetString("Article");
|
||
decimal price = reader.GetDecimal("MinPrice");
|
||
tbMinPrice.Text = price.ToString();
|
||
tbWidth.Text = reader.GetString("Width");
|
||
int productTypeId = reader.GetInt32("idProduct_type_import");
|
||
|
||
SelectComboBoxItem(cmbProduct, productTypeId);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Материал не найден. Возможно, он был удалён.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
this.DialogResult = DialogResult.Cancel;
|
||
this.Close();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void SelectComboBoxItem(ComboBox comboBox, int id)
|
||
{
|
||
foreach (ComboItem item in comboBox.Items)
|
||
{
|
||
if (item.Id == id)
|
||
{
|
||
comboBox.SelectedItem = item;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadTypes() //ЗАГРУЗКА ТИПОВ
|
||
{
|
||
string query = "select idProduct_type_import, TypeProduction from Product_type_import";
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if (conn.State == ConnectionState.Closed)
|
||
{
|
||
conn.Open();
|
||
}
|
||
using (var cmd = new MySqlCommand(query, conn))
|
||
{
|
||
using (var reader = cmd.ExecuteReader())
|
||
{
|
||
while (reader.Read())
|
||
{
|
||
cmbProduct.Items.Add(new ComboItem
|
||
{
|
||
Id = reader.GetInt32("idProduct_type_import"),
|
||
Name = reader.GetString("TypeProduction")
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void btnExit_Click(object sender, EventArgs e)
|
||
{
|
||
this.Hide();
|
||
}
|
||
|
||
private void EditProduction_Load(object sender, EventArgs e)
|
||
{
|
||
LoadProduct();
|
||
}
|
||
}
|
||
}
|