220 lines
7.8 KiB
C#
220 lines
7.8 KiB
C#
using FlowerShopContracts.BindingModels;
|
|
using FlowerShopContracts.BusinessLogicsContracts;
|
|
using FlowerShopContracts.DI;
|
|
using FlowerShopContracts.SearchModels;
|
|
using FlowerShopDataModels.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace FlowerShop
|
|
{
|
|
public partial class FormBouquet : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IBouquetLogic _logic;
|
|
private int? _id;
|
|
private Dictionary<int, (IComponentModel, int)> _bouquetComponents;
|
|
public int Id { set { _id = value; } }
|
|
|
|
public FormBouquet(ILogger<FormBouquet> logger, IBouquetLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
_bouquetComponents = new Dictionary<int, (IComponentModel, int)>();
|
|
}
|
|
|
|
private void BouquetForm_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
_logger.LogInformation("Receving bouquet");
|
|
try
|
|
{
|
|
var view = _logic.ReadElement(new BouquetSearchModel { Id = _id.Value });
|
|
if (view != null)
|
|
{
|
|
textBoxName.Text = view.BouquetName;
|
|
textBoxPrice.Text = view.Price.ToString();
|
|
_bouquetComponents = view.BouquetComponents ?? new Dictionary<int, (IComponentModel, int)>();
|
|
LoadData();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error during receiving bouquet");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
}
|
|
private void LoadData()
|
|
{
|
|
_logger.LogInformation("Загрузка компонент изделия");
|
|
try
|
|
{
|
|
if (_bouquetComponents != null)
|
|
{
|
|
dataGridView.Rows.Clear();
|
|
foreach (var pc in _bouquetComponents)
|
|
{
|
|
dataGridView.Rows.Add(new object[] { pc.Value.Item1.ComponentName, pc.Value.Item2 });
|
|
}
|
|
textBoxPrice.Text = CalcPrice().ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка загрузки компонент изделия");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
var form = DependencyManager.Instance.Resolve<FormBouquetComponent>();
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (form.ComponentModel == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("Добавление нового компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count);
|
|
|
|
if (_bouquetComponents.ContainsKey(form.Id))
|
|
{
|
|
_bouquetComponents[form.Id] = (form.ComponentModel, form.Count);
|
|
}
|
|
|
|
else
|
|
{
|
|
_bouquetComponents.Add(form.Id, (form.ComponentModel, form.Count));
|
|
}
|
|
|
|
LoadData();
|
|
}
|
|
}
|
|
|
|
private void buttonChange_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
{
|
|
var form = DependencyManager.Instance.Resolve<FormBouquetComponent>();
|
|
|
|
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
|
|
|
form.Id = id;
|
|
form.Count = _bouquetComponents[id].Item2;
|
|
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (form.ComponentModel == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("Изменение компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count);
|
|
_bouquetComponents[form.Id] = (form.ComponentModel, form.Count);
|
|
LoadData();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void buttonDelete_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
{
|
|
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value);
|
|
_bouquetComponents?.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 (_bouquetComponents == null || _bouquetComponents.Count == 0)
|
|
{
|
|
MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("Saving bouquet");
|
|
try
|
|
{
|
|
var model = new BouquetBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
BouquetName = textBoxName.Text,
|
|
Price = Convert.ToDouble(textBoxPrice.Text),
|
|
BouquetComponents = _bouquetComponents
|
|
};
|
|
|
|
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
|
|
|
if (!operationResult)
|
|
{
|
|
throw new Exception("Error during saving");
|
|
}
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error during saving bouquet");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
private double CalcPrice()
|
|
{
|
|
double price = 0;
|
|
|
|
foreach (var elem in _bouquetComponents)
|
|
{
|
|
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
|
|
}
|
|
|
|
return Math.Round(price * 1.1, 2);
|
|
}
|
|
}
|
|
}
|