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

209 lines
8.1 KiB
C#
Raw Normal View History

2024-05-12 14:38:32 +04:00
using SoftwareInstallationContracts.BindingModels;
2024-04-07 16:54:02 +04:00
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationDataModels.Models;
2024-05-12 14:38:32 +04:00
using Microsoft.Extensions.Logging;
2024-04-07 16:54:02 +04:00
2024-05-12 14:38:32 +04:00
namespace SoftwareInstallationView
2024-04-07 16:54:02 +04:00
{
public partial class FormPackage : Form
{
private readonly ILogger _logger;
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
private readonly IPackageLogic _logic;
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
private int? _id;
2024-05-12 14:38:32 +04:00
private Dictionary<int, (IComponentModel, int)> _iceCreamComponents;
2024-04-07 16:54:02 +04:00
public int Id { set { _id = value; } }
2024-05-12 14:38:32 +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:38:32 +04:00
_iceCreamComponents = new Dictionary<int, (IComponentModel, int)>();
2024-04-07 16:54:02 +04:00
}
2024-05-12 14:38:32 +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:38:32 +04:00
_logger.LogInformation("Ice cream loading");
2024-04-07 16:54:02 +04:00
try
{
2024-05-12 14:38:32 +04:00
var view = _logic.ReadElement(new PackageSearchModel { Id = _id.Value });
2024-04-07 16:54:02 +04:00
if (view != null)
{
textBoxName.Text = view.PackageName;
textBoxPrice.Text = view.Price.ToString();
2024-05-12 14:38:32 +04:00
_iceCreamComponents = view.PackageComponents ?? new Dictionary<int, (IComponentModel, int)>();
2024-04-07 16:54:02 +04:00
LoadData();
}
}
catch (Exception ex)
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Ice cream loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
}
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
private void LoadData()
{
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Ice cream components loading");
2024-04-07 16:54:02 +04:00
try
{
2024-05-12 14:38:32 +04:00
if (_iceCreamComponents != null)
2024-04-07 16:54:02 +04:00
{
dataGridView.Rows.Clear();
2024-05-12 14:38:32 +04:00
foreach (var iceCreamC in _iceCreamComponents)
2024-04-07 16:54:02 +04:00
{
2024-05-12 14:38:32 +04:00
dataGridView.Rows.Add(new object[] { iceCreamC.Key, iceCreamC.Value.Item1.ComponentName, iceCreamC.Value.Item2 });
2024-04-07 16:54:02 +04:00
}
textBoxPrice.Text = CalcPrice().ToString();
}
}
catch (Exception ex)
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Ice cream components loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-05-12 14:38:32 +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:38:32 +04:00
_logger.LogInformation("Adding new component: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count);
if (_iceCreamComponents.ContainsKey(form.Id))
2024-04-07 16:54:02 +04:00
{
2024-05-12 14:38:32 +04:00
_iceCreamComponents[form.Id] = (form.ComponentModel, form.Count);
2024-04-07 16:54:02 +04:00
}
else
{
2024-05-12 14:38:32 +04:00
_iceCreamComponents.Add(form.Id, (form.ComponentModel, form.Count));
2024-04-07 16:54:02 +04:00
}
LoadData();
}
}
}
2024-05-12 14:38:32 +04:00
private void ButtonEdit_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:38:32 +04:00
form.Count = _iceCreamComponents[id].Item2;
2024-04-07 16:54:02 +04:00
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
{
return;
}
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Component editing: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count);
_iceCreamComponents[form.Id] = (form.ComponentModel, form.Count);
2024-04-07 16:54:02 +04:00
LoadData();
}
}
}
}
2024-05-12 14:38:32 +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:38:32 +04:00
_logger.LogInformation("Deletion of component: {ComponentName} - {Count}", dataGridView.SelectedRows[0].Cells[1].Value,
dataGridView.SelectedRows[0].Cells[2].Value);
_iceCreamComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
2024-04-07 16:54:02 +04:00
}
catch (Exception ex)
{
2024-04-07 17:05:30 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
LoadData();
}
}
}
2024-05-12 14:38:32 +04:00
private void ButtonUpd_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
LoadData();
}
2024-05-12 14:38:32 +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:38:32 +04:00
if (_iceCreamComponents == null || _iceCreamComponents.Count == 0)
2024-04-07 16:54:02 +04:00
{
2024-04-07 17:05:30 +04:00
MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
return;
}
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Ice cream saving");
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:38:32 +04:00
PackageComponents = _iceCreamComponents
2024-04-07 16:54:02 +04:00
};
2024-04-07 17:05:30 +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:38:32 +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:38:32 +04:00
_logger.LogError(ex, "Ice cream saving error");
2024-04-07 16:54:02 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-05-12 14:38:32 +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:38:32 +04:00
foreach (var elem in _iceCreamComponents)
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-04-07 17:05:30 +04:00
}