162 lines
5.9 KiB
C#
162 lines
5.9 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace NeshinaPolina07_11
|
|
{
|
|
public partial class EditOrder : Form
|
|
{
|
|
private readonly int _orderId;
|
|
private readonly MySqlConnection _connection;
|
|
public EditOrder(int orderId)
|
|
{
|
|
InitializeComponent();
|
|
_orderId = orderId;
|
|
_connection = DB.GetInstance().GetConnection();
|
|
LoadComboBoxes();
|
|
LoadOrderData();
|
|
}
|
|
private void LoadComboBoxes()
|
|
{
|
|
LoadComboBox("pickup_points", "point_id", "address", cmbPickupPoint);
|
|
LoadComboBox("users", "user_id", "full_name", cmbUser);
|
|
LoadComboBox("order_statuses", "status_id", "status_name", cmbStatus);
|
|
}
|
|
|
|
private void LoadComboBox(string table, string idCol, string nameCol, ComboBox combo)
|
|
{
|
|
string query = $"SELECT {idCol}, {nameCol} FROM {table} ORDER BY {nameCol}";
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
using (var adapter = new MySqlDataAdapter(cmd))
|
|
{
|
|
var dt = new DataTable();
|
|
adapter.Fill(dt);
|
|
combo.DataSource = dt;
|
|
combo.DisplayMember = nameCol;
|
|
combo.ValueMember = idCol;
|
|
combo.SelectedIndex = -1;
|
|
}
|
|
}
|
|
|
|
private void LoadOrderData()
|
|
{
|
|
string query = @"
|
|
SELECT
|
|
order_date, delivery_date, pickup_point_id, user_id, receive_code, status_id
|
|
FROM orders
|
|
WHERE order_id = @id";
|
|
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@id", _orderId);
|
|
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
dtpOrderDate.Value = Convert.ToDateTime(reader["order_date"]);
|
|
dtpDeliveryDate.Value = Convert.ToDateTime(reader["delivery_date"]);
|
|
txtReceiveCode.Text = reader["receive_code"].ToString();
|
|
|
|
SetComboBoxValue(cmbPickupPoint, reader["pickup_point_id"]);
|
|
SetComboBoxValue(cmbUser, reader["user_id"]);
|
|
SetComboBoxValue(cmbStatus, reader["status_id"]);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Заказ не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetComboBoxValue(ComboBox combo, object value)
|
|
{
|
|
if (value != DBNull.Value && combo.DataSource != null)
|
|
{
|
|
foreach (DataRowView row in combo.Items)
|
|
{
|
|
if (row[combo.ValueMember].Equals(value))
|
|
{
|
|
combo.SelectedItem = row;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void EditOrder_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void label1_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
if (cmbPickupPoint.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите пункт выдачи.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (cmbUser.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите пользователя.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (cmbStatus.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите статус заказа.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(txtReceiveCode.Text))
|
|
{
|
|
MessageBox.Show("Введите код получения.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
string query = @"
|
|
UPDATE orders
|
|
SET
|
|
order_date = @order_date,
|
|
delivery_date = @delivery_date,
|
|
pickup_point_id = @pickup_point_id,
|
|
user_id = @user_id,
|
|
receive_code = @receive_code,
|
|
status_id = @status_id
|
|
WHERE order_id = @id";
|
|
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@order_date", dtpOrderDate.Value);
|
|
cmd.Parameters.AddWithValue("@delivery_date", dtpDeliveryDate.Value);
|
|
cmd.Parameters.AddWithValue("@pickup_point_id", cmbPickupPoint.SelectedValue);
|
|
cmd.Parameters.AddWithValue("@user_id", cmbUser.SelectedValue);
|
|
cmd.Parameters.AddWithValue("@receive_code", txtReceiveCode.Text.Trim());
|
|
cmd.Parameters.AddWithValue("@status_id", cmbStatus.SelectedValue);
|
|
cmd.Parameters.AddWithValue("@id", _orderId);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
MessageBox.Show("Заказ успешно обновлён.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
}
|
|
}
|