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

216 lines
8.7 KiB
C#
Raw Permalink Normal View History

2024-04-07 17:40:43 +04:00
using Microsoft.Extensions.Logging;
2024-05-19 10:10:11 +04:00
using SoftwareInstallation.Forms;
2024-04-07 17:40:43 +04:00
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationDataModels.Models;
2024-06-17 19:27:42 +04:00
using SoftwareInstallationContracts.DI;
2024-04-07 17:05:30 +04:00
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;
2024-04-07 16:54:02 +04:00
2024-04-07 17:40:43 +04:00
namespace SoftwareInstallationView
2024-04-07 16:54:02 +04:00
{
2024-04-07 17:40:43 +04:00
public partial class FormSoftware : Form
2024-04-07 16:54:02 +04:00
{
private readonly ILogger _logger;
private readonly IPackageLogic _logic;
private int? _id;
2024-04-07 17:40:43 +04:00
private Dictionary<int, (IComponentModel, int)> _SoftwareComponents;
2024-04-07 16:54:02 +04:00
public int Id { set { _id = value; } }
2024-04-07 17:40:43 +04:00
public FormSoftware(ILogger<FormSoftware> logger, IPackageLogic logic)
2024-04-07 16:54:02 +04:00
{
InitializeComponent();
_logger = logger;
_logic = logic;
2024-04-07 17:40:43 +04:00
_SoftwareComponents = new Dictionary<int, (IComponentModel, int)>();
2024-04-07 16:54:02 +04:00
}
2024-04-07 17:40:43 +04:00
private void FormSoftware_Load(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (_id.HasValue)
{
2024-04-07 17:05:30 +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)
{
2024-04-07 17:40:43 +04:00
textBoxName.Text = view.SoftwareName;
2024-04-07 16:54:02 +04:00
textBoxPrice.Text = view.Price.ToString();
2024-04-07 17:40:43 +04:00
_SoftwareComponents = view.SoftwareComponents ?? new
Dictionary<int, (IComponentModel, int)>();
2024-04-07 16:54:02 +04:00
LoadData();
}
}
catch (Exception ex)
{
2024-04-07 17:05:30 +04:00
_logger.LogError(ex, "Ошибка загрузки изделия");
2024-04-07 17:40:43 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
}
private void LoadData()
{
2024-04-07 17:05:30 +04:00
_logger.LogInformation("Загрузка компонент изделия");
2024-04-07 16:54:02 +04:00
try
{
2024-04-07 17:40:43 +04:00
if (_SoftwareComponents != null)
2024-04-07 16:54:02 +04:00
{
dataGridView.Rows.Clear();
2024-04-07 17:40:43 +04:00
foreach (var pc in _SoftwareComponents)
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-04-07 17:05:30 +04:00
_logger.LogError(ex, "Ошибка загрузки компонент изделия");
2024-04-07 17:40:43 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-04-07 17:40:43 +04:00
private void ButtonAdd_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
2024-06-17 19:27:42 +04:00
var service = DependencyManager.Instance.Resolve<FormRepairComponent>();
2024-04-07 16:54:02 +04:00
if (service is FormPackageComponent form)
{
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
{
return;
}
2024-04-07 17:40:43 +04:00
_logger.LogInformation("Добавление нового компонента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count);
if (_SoftwareComponents.ContainsKey(form.Id))
2024-04-07 16:54:02 +04:00
{
2024-04-07 17:40:43 +04:00
_SoftwareComponents[form.Id] = (form.ComponentModel,
form.Count);
2024-04-07 16:54:02 +04:00
}
else
{
2024-04-07 17:40:43 +04:00
_SoftwareComponents.Add(form.Id, (form.ComponentModel,
form.Count));
2024-04-07 16:54:02 +04:00
}
LoadData();
}
}
}
2024-04-07 17:40:43 +04:00
private void ButtonUpd_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (dataGridView.SelectedRows.Count == 1)
{
2024-06-17 19:27:42 +04:00
var service = DependencyManager.Instance.Resolve<FormPackageComponent>();
2024-04-07 16:54:02 +04:00
if (service is FormPackageComponent form)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
2024-04-07 17:40:43 +04:00
form.Count = _SoftwareComponents[id].Item2;
2024-04-07 16:54:02 +04:00
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
{
return;
}
2024-04-07 17:40:43 +04:00
_logger.LogInformation("Изменение компонента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count);
_SoftwareComponents[form.Id] = (form.ComponentModel, form.Count);
2024-04-07 16:54:02 +04:00
LoadData();
}
}
}
}
2024-04-07 17:40:43 +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-04-07 17:40:43 +04:00
_logger.LogInformation("Удаление компонента:{ ComponentName}-{ Count}", dataGridView.SelectedRows[0].Cells[1].Value);
_SoftwareComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
2024-04-07 16:54:02 +04:00
}
catch (Exception ex)
{
2024-04-07 17:40:43 +04:00
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
LoadData();
}
}
}
2024-04-07 17:40:43 +04:00
private void ButtonRef_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
LoadData();
}
2024-04-07 17:40:43 +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-04-07 17:40:43 +04:00
if (_SoftwareComponents == null || _SoftwareComponents.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-04-07 17:05:30 +04:00
_logger.LogInformation("Сохранение изделия");
2024-04-07 16:54:02 +04:00
try
{
var model = new PackageBindingModel
{
Id = _id ?? 0,
2024-04-07 17:40:43 +04:00
SoftwareName = textBoxName.Text,
2024-04-07 16:54:02 +04:00
Price = Convert.ToDouble(textBoxPrice.Text),
2024-04-07 17:40:43 +04:00
SoftwareComponents = _SoftwareComponents
2024-04-07 16:54:02 +04:00
};
2024-04-07 17:40:43 +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-04-07 17:40:43 +04:00
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
2024-04-07 16:54:02 +04:00
Close();
}
catch (Exception ex)
{
2024-04-07 17:05:30 +04:00
_logger.LogError(ex, "Ошибка сохранения изделия");
2024-04-07 16:54:02 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-04-07 17:40:43 +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-04-07 17:40:43 +04:00
foreach (var elem in _SoftwareComponents)
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:40:43 +04:00
}