ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallation/FormPackage.cs

216 lines
8.4 KiB
C#
Raw Normal View History

2024-05-12 14:45:39 +04:00
using Microsoft.Extensions.Logging;
using SoftwareInstallation;
using SoftwareInstallationContracts.BindingModels;
2024-04-07 16:54:02 +04:00
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationDataModels.Models;
2024-05-12 14:45:39 +04:00
namespace SoftwareInstallationView
2024-04-07 16:54:02 +04:00
{
public partial class FormPackage : Form
{
private readonly ILogger _logger;
private readonly IPackageLogic _logic;
private int? _id;
2024-05-12 14:45:39 +04:00
private Dictionary<int, (IComponentModel, int)> _PackageComponents;
2024-04-07 16:54:02 +04:00
public int Id { set { _id = value; } }
2024-05-12 14:45:39 +04:00
2024-04-07 16:54:02 +04:00
public FormPackage(ILogger<FormPackage> logger, IPackageLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
2024-05-12 14:45:39 +04:00
_PackageComponents = new Dictionary<int, (IComponentModel, int)>();
2024-04-07 16:54:02 +04:00
}
2024-05-12 14:45:39 +04:00
2024-04-07 16:54:02 +04:00
private void FormPackage_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
2024-05-12 14:45:39 +04:00
_logger.LogInformation("Загрузка пиццы");
2024-04-07 16:54:02 +04:00
try
{
var view = _logic.ReadElement(new PackageSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.PackageName;
textBoxPrice.Text = view.Price.ToString();
2024-05-12 14:45:39 +04:00
_PackageComponents = view.PackageComponents ?? new
Dictionary<int, (IComponentModel, int)>();
2024-04-07 16:54:02 +04:00
LoadData();
}
}
catch (Exception ex)
{
2024-05-12 14:45:39 +04:00
_logger.LogError(ex, "Ошибка загрузки пиццы");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
}
2024-05-12 14:45:39 +04:00
2024-04-07 16:54:02 +04:00
private void LoadData()
{
2024-05-12 14:45:39 +04:00
_logger.LogInformation("Загрузка ингредиент пиццы");
2024-04-07 16:54:02 +04:00
try
{
2024-05-12 14:45:39 +04:00
if (_PackageComponents != null)
2024-04-07 16:54:02 +04:00
{
dataGridView.Rows.Clear();
2024-05-12 14:45:39 +04:00
foreach (var pc in _PackageComponents)
2024-04-07 16:54:02 +04:00
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 });
}
textBoxPrice.Text = CalcPrice().ToString();
}
}
catch (Exception ex)
{
2024-05-12 14:45:39 +04:00
_logger.LogError(ex, "Ошибка загрузки ингредиента пиццы");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-05-12 14:45:39 +04:00
private void ButtonAdd_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
var service = Program.ServiceProvider?.GetService(typeof(FormPackageComponent));
if (service is FormPackageComponent form)
{
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
{
return;
}
2024-05-12 14:45:39 +04:00
_logger.LogInformation("Добавление нового ингредиента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count);
if (_PackageComponents.ContainsKey(form.Id))
2024-04-07 16:54:02 +04:00
{
2024-05-12 14:45:39 +04:00
_PackageComponents[form.Id] = (form.ComponentModel,
form.Count);
2024-04-07 16:54:02 +04:00
}
else
{
2024-05-12 14:45:39 +04:00
_PackageComponents.Add(form.Id, (form.ComponentModel,
form.Count));
2024-04-07 16:54:02 +04:00
}
LoadData();
}
}
}
2024-05-12 14:45:39 +04:00
private void ButtonUpd_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormPackageComponent));
if (service is FormPackageComponent form)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
2024-05-12 14:45:39 +04:00
form.Count = _PackageComponents[id].Item2;
2024-04-07 16:54:02 +04:00
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
{
return;
}
2024-05-12 14:45:39 +04:00
_logger.LogInformation("Изменение ингредиента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count);
_PackageComponents[form.Id] = (form.ComponentModel, form.Count);
2024-04-07 16:54:02 +04:00
LoadData();
}
}
}
}
2024-05-12 14:45:39 +04:00
private void ButtonDel_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
2024-05-12 14:45:39 +04:00
_logger.LogInformation("Удаление ингредиента:{ ComponentName}-{ Count}", dataGridView.SelectedRows[0].Cells[1].Value);
_PackageComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
2024-04-07 16:54:02 +04:00
}
catch (Exception ex)
{
2024-05-12 14:45:39 +04:00
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
LoadData();
}
}
}
2024-05-12 14:45:39 +04:00
private void ButtonRef_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
LoadData();
}
2024-05-12 14:45:39 +04:00
private void ButtonSave_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxPrice.Text))
{
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-05-12 14:45:39 +04:00
if (_PackageComponents == null || _PackageComponents.Count == 0)
2024-04-07 16:54:02 +04:00
{
2024-05-12 14:45:39 +04:00
MessageBox.Show("Заполните ингредиенты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
return;
}
2024-05-12 14:45:39 +04:00
_logger.LogInformation("Сохранение пиццы");
2024-04-07 16:54:02 +04:00
try
{
var model = new PackageBindingModel
{
Id = _id ?? 0,
PackageName = textBoxName.Text,
Price = Convert.ToDouble(textBoxPrice.Text),
2024-05-12 14:45:39 +04:00
PackageComponents = _PackageComponents
2024-04-07 16:54:02 +04:00
};
2024-05-12 14:45:39 +04:00
var operationResult = _id.HasValue ? _logic.Update(model) :
_logic.Create(model);
2024-04-07 16:54:02 +04:00
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
2024-05-12 14:45:39 +04:00
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
2024-04-07 16:54:02 +04:00
Close();
}
catch (Exception ex)
{
2024-05-12 14:45:39 +04:00
_logger.LogError(ex, "Ошибка сохранения пиццы");
2024-04-07 16:54:02 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-05-12 14:45:39 +04:00
private void ButtonCancel_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
DialogResult = DialogResult.Cancel;
Close();
}
private double CalcPrice()
{
double price = 0;
2024-05-12 14:45:39 +04:00
foreach (var elem in _PackageComponents)
2024-04-07 16:54:02 +04:00
{
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
}
return Math.Round(price * 1.1, 2);
}
}
2024-05-12 14:45:39 +04:00
}