SUBD_Aleikin/Restaurant/RestaurantView/FormCreateOrder.cs

213 lines
7.2 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using RestaurantContracts.BindingModels;
using RestaurantContracts.BusinessLogicsContracts;
using RestaurantContracts.SearchModels;
using RestaurantDatabaseImplement.Models;
using RestaurantDataModels.Models;
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 RestaurantView
{
public partial class FormCreateOrder : Form
{
private readonly IProductLogic _logicP;
private readonly IOrderLogic _logicO;
private readonly IClientLogic _logicC;
private int? _id;
private Dictionary<int, (IProductModel, int)> _orderProducts;
public int Id { set { _id = value; } }
public FormCreateOrder(IProductLogic logicP, IOrderLogic logicO, IClientLogic logicC)
{
InitializeComponent();
_logicP = logicP;
_logicO = logicO;
_logicC = logicC;
_orderProducts = new Dictionary<int, (IProductModel, int)>();
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
try
{
var clients = _logicC.ReadList(null);
if (clients != null)
{
comboBoxClients.DisplayMember = "FirstName";
comboBoxClients.ValueMember = "Id";
comboBoxClients.DataSource = clients;
comboBoxClients.SelectedItem = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private double CalcPrice()
{
double price = 0;
foreach (var elem in _orderProducts)
{
price += elem.Value.Item1.Price * elem.Value.Item2;
}
return price;
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxPrice.Text))
{
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
if (_orderProducts == null || _orderProducts.Count == 0)
{
MessageBox.Show("Заполните компоненты", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new OrderBindingModel
{
Id = _id ?? 0,
Price = Convert.ToDouble(textBoxPrice.Text),
ClientId = Convert.ToInt32(comboBoxClients.SelectedValue),
Date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
OrderProducts = _orderProducts
};
var operationResult = _id.HasValue ? _logicO.Update(model) :
_logicO.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LoadData()
{
try
{
if (_orderProducts != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _orderProducts)
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.Type, pc.Value.Item2 });
}
textBoxPrice.Text = CalcPrice().ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormOrderProduct));
if (service is FormOrderProduct form)
{
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ProductModel == null)
{
return;
}
if (_orderProducts.ContainsKey(form.Id))
{
_orderProducts[form.Id] = (form.ProductModel, form.Count);
}
else
{
_orderProducts.Add(form.Id, (form.ProductModel, form.Count));
}
LoadData();
}
}
}
private void ButtonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service =
Program.ServiceProvider?.GetService(typeof(FormOrderProduct));
if (service is FormOrderProduct form)
{
int id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
form.Count = _orderProducts[id].Item2;
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ProductModel == null)
{
return;
}
_orderProducts[form.Id] = (form.ProductModel, form.Count);
LoadData();
}
}
}
}
private void ButtonDel_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
_orderProducts?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
}
private void ButtonRef_Click(object sender, EventArgs e)
{
LoadData();
}
}
}