2024-02-24 21:32:11 +04:00
|
|
|
|
using DinerContracts.BindingModels;
|
|
|
|
|
using DinerContracts.BusinessLogicsContacts;
|
2024-06-15 19:43:34 +04:00
|
|
|
|
using DinerContracts.DI;
|
2024-02-24 21:32:11 +04:00
|
|
|
|
using DinerContracts.SearchModels;
|
|
|
|
|
using DinerDataModels.Models;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography.Xml;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace DinerView
|
|
|
|
|
{
|
|
|
|
|
public partial class FormSnack : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ISnackLogic _logic;
|
|
|
|
|
private int? _ID;
|
|
|
|
|
private Dictionary<int, (IFoodModel, int)> _productComponents;
|
|
|
|
|
public int ID { set { _ID = value; } }
|
|
|
|
|
public FormSnack(ILogger<FormSnack> logger, ISnackLogic logic)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_logic = logic;
|
|
|
|
|
_productComponents = new Dictionary<int, (IFoodModel, int)>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FormSnack_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_ID.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка снэка");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var view = _logic.ReadElement(new SnackSearchModel { ID = _ID.Value });
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
textBoxName.Text = view.ProductName;
|
|
|
|
|
textBoxPrice.Text = view.Price.ToString();
|
|
|
|
|
_productComponents = view.ProductComponents ?? new Dictionary<int, (IFoodModel, int)>();
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки продуктов снэка");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadData()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка продукта снэка");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_productComponents != null)
|
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Clear();
|
|
|
|
|
foreach (var elem in _productComponents)
|
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.ComponentName, elem.Value.Item2 });
|
|
|
|
|
}
|
|
|
|
|
textBoxPrice.Text = CalcPrice().ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки продукта снэка");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double CalcPrice()
|
|
|
|
|
{
|
|
|
|
|
double price = 0;
|
|
|
|
|
foreach (var elem in _productComponents)
|
|
|
|
|
price += ((elem.Value.Item1?.Price ?? 0) * elem.Value.Item2);
|
|
|
|
|
return Math.Round(price * 1.1, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-06-15 19:43:34 +04:00
|
|
|
|
var service = DependencyManager.Instance.Resolve<FormSnackFood>();
|
2024-02-24 21:32:11 +04:00
|
|
|
|
if (service is FormSnackFood form)
|
|
|
|
|
{
|
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (form.foodModel == null) return;
|
2024-04-19 11:14:33 +04:00
|
|
|
|
_logger.LogInformation("Добавление нового снэка: {ProductName} - {Count}", form.foodModel.ComponentName, form.Count);
|
2024-02-24 21:32:11 +04:00
|
|
|
|
if (_productComponents.ContainsKey(form.ID))
|
|
|
|
|
{
|
|
|
|
|
_productComponents[form.ID] = (form.foodModel, form.Count);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_productComponents.Add(form.ID, (form.foodModel, form.Count));
|
|
|
|
|
}
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonChange_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
|
|
|
{
|
2024-06-15 19:43:34 +04:00
|
|
|
|
var service = DependencyManager.Instance.Resolve<FormSnackFood>();
|
2024-02-24 21:32:11 +04:00
|
|
|
|
if (service is FormSnackFood form)
|
|
|
|
|
{
|
|
|
|
|
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
|
|
|
|
form.ID = id;
|
|
|
|
|
form.Count = _productComponents[id].Item2;
|
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (form.foodModel == null) return;
|
2024-04-19 11:14:33 +04:00
|
|
|
|
_logger.LogInformation("Изменение снэка: {ProductName} - {Count}", form.foodModel.ComponentName, form.Count);
|
2024-02-24 21:32:11 +04:00
|
|
|
|
_productComponents[form.ID] = (form.foodModel, form.Count);
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRemove_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-04-19 11:14:33 +04:00
|
|
|
|
_logger.LogInformation("Удаление снэка: {ProductName}", dataGridView.SelectedRows[0].Cells[1].Value);
|
2024-02-24 21:32:11 +04:00
|
|
|
|
_productComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonUpdate_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxPrice.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_productComponents == null || _productComponents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Сохранение изделия");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var model = new SnackBindingModel
|
|
|
|
|
{
|
|
|
|
|
ID = _ID ?? 0,
|
|
|
|
|
ProductName = textBoxName.Text,
|
|
|
|
|
Price = Convert.ToDouble(textBoxPrice.Text),
|
|
|
|
|
ProductComponents = _productComponents
|
|
|
|
|
};
|
|
|
|
|
var operationResult = _ID.HasValue ? _logic.Update(model) : _logic.Create(model);
|
|
|
|
|
if (!operationResult)
|
|
|
|
|
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка сохранения изделия");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|