PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantView/FormSell.cs
2024-03-11 03:16:54 +04:00

128 lines
4.5 KiB
C#

using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
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;
namespace AircraftPlantView
{
/// <summary>
/// Форма продажи изделий
/// </summary>
public partial class FormSell : Form
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для изделий
/// </summary>
private readonly IPlaneLogic _logicP;
/// <summary>
/// Бизнес-логика для магазинов
/// </summary>
private readonly IShopLogic _logicS;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="planeLogic"></param>
/// <param name="shopLogic"></param>
public FormSell(ILogger<FormSell> logger, IPlaneLogic planeLogic, IShopLogic shopLogic)
{
InitializeComponent();
_logger = logger;
_logicP = planeLogic;
_logicS = shopLogic;
}
/// <summary>
/// Загрузка списка изделий
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormSell_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка изделий для продажи");
try
{
var list = _logicP.ReadList(null);
if (list != null)
{
comboBoxPlane.DisplayMember = "PlaneName";
comboBoxPlane.ValueMember = "Id";
comboBoxPlane.DataSource = list;
comboBoxPlane.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка изделий");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Кнопка "Сохранить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPlane.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Продажа изделий");
_logger.LogInformation("Создание заказа");
try
{
var operationResult = _logicS.SellPlanes(
_logicP.ReadElement(new() { Id = Convert.ToInt32(comboBoxPlane.SelectedValue) }),
Convert.ToInt32(textBoxCount.Text)
);
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);
}
}
/// <summary>
/// Кнопка "Отмена"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}