225 lines
8.2 KiB
C#
225 lines
8.2 KiB
C#
|
using AbstractSoftwareInstallationContracts.BindingModels;
|
|||
|
using AbstractSoftwareInstallationContracts.BusinessLogicsContracts;
|
|||
|
using AbstractSoftwareInstallationContracts.SearchModels;
|
|||
|
using AbstractSoftwareInstallationDataModels.Models;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using SoftwareInstallation;
|
|||
|
|
|||
|
namespace SoftwareInstallationView
|
|||
|
{
|
|||
|
public partial class FormPackage : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IPackageLogic _logic;
|
|||
|
private int? _id;
|
|||
|
private Dictionary<int, (ISoftwareModel, int)> _packageSoftwares;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
|
|||
|
public FormPackage(ILogger<FormPackage> logger, IPackageLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_logic = logic;
|
|||
|
_packageSoftwares = new Dictionary<int, (ISoftwareModel, int)>();
|
|||
|
}
|
|||
|
private void FormPackage_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (_id.HasValue)
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка ПО");
|
|||
|
try
|
|||
|
{
|
|||
|
var view = _logic.ReadElement(new PackageSearchModel
|
|||
|
{
|
|||
|
Id = _id.Value
|
|||
|
});
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxName.Text = view.PackageName;
|
|||
|
textBoxPrice.Text = view.Price.ToString();
|
|||
|
_packageSoftwares = view.PackageSoftware ?? new Dictionary<int, (ISoftwareModel, int)>();
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки ПО");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|||
|
MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private void LoadData()
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка ПО пакета");
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (_packageSoftwares != null)
|
|||
|
{
|
|||
|
dataGridView.Rows.Clear();
|
|||
|
foreach (var pc in _packageSoftwares)
|
|||
|
{
|
|||
|
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.SoftwareName, 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 service = Program.ServiceProvider?.GetService(typeof(FormPackageSoftware));
|
|||
|
|
|||
|
if (service is FormPackageSoftware form)
|
|||
|
{
|
|||
|
if (form.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
if (form.SoftwareModel == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Добавление нового ПО: { SoftwareName} - { Count}", form.SoftwareModel.SoftwareName, form.Count);
|
|||
|
|
|||
|
if (_packageSoftwares.ContainsKey(form.Id))
|
|||
|
{
|
|||
|
_packageSoftwares[form.Id] = (form.SoftwareModel, form.Count);
|
|||
|
}
|
|||
|
|
|||
|
else
|
|||
|
{
|
|||
|
_packageSoftwares.Add(form.Id, (form.SoftwareModel, form.Count));
|
|||
|
}
|
|||
|
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void buttonEdit_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (dataGridView.SelectedRows.Count == 1)
|
|||
|
{
|
|||
|
var service = Program.ServiceProvider?.GetService(typeof(FormPackageSoftware));
|
|||
|
|
|||
|
if (service is FormPackageSoftware form)
|
|||
|
{
|
|||
|
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
|||
|
form.Id = id;
|
|||
|
form.Count = _packageSoftwares[id].Item2;
|
|||
|
|
|||
|
if (form.ShowDialog() == DialogResult.OK)
|
|||
|
{
|
|||
|
if (form.SoftwareModel == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Изменение ПО: { SoftwareName} - { Count}", form.SoftwareModel.SoftwareName, form.Count);
|
|||
|
_packageSoftwares[form.Id] = (form.SoftwareModel, 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("Удаление ПО: { SoftwareName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value);
|
|||
|
_packageSoftwares?.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 (_packageSoftwares == null || _packageSoftwares.Count == 0)
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните ПО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Сохранение пакета");
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new PackageBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
PackageName = textBoxName.Text,
|
|||
|
Price = Convert.ToDouble(textBoxPrice.Text),
|
|||
|
PackageSoftware = _packageSoftwares
|
|||
|
};
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
private double CalcPrice()
|
|||
|
{
|
|||
|
double price = 0;
|
|||
|
|
|||
|
foreach (var elem in _packageSoftwares)
|
|||
|
{
|
|||
|
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
|
|||
|
}
|
|||
|
|
|||
|
return Math.Round(price * 1.1, 2);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|