PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantView/FormCreateSupply.cs

160 lines
5.7 KiB
C#
Raw Permalink Normal View History

2024-02-22 01:58:33 +04:00
using AircraftPlantBusinessLogic.BusinessLogics;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
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 FormCreateSupply : Form
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для магазина
/// </summary>
private readonly IShopLogic _logicS;
/// <summary>
/// Бизнес-логика для изделий
/// </summary>
private readonly IPlaneLogic _logicP;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="logicS"></param>
/// <param name="logicP"></param>
public FormCreateSupply(ILogger<FormCreateSupply> logger, IShopLogic logicS, IPlaneLogic logicP)
{
InitializeComponent();
_logger = logger;
_logicS = logicS;
_logicP = logicP;
}
/// <summary>
/// Загрузка списиков магазинов и изделий
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormCreateSupply_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка магазинов");
try
{
var listShops = _logicS.ReadList(null);
if (listShops != null)
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = listShops;
comboBoxShop.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка магазинов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_logger.LogInformation("Загрузка изделий");
try
{
var listPlanes = _logicP.ReadList(null);
if (listPlanes != null)
{
comboBoxPlane.DisplayMember = "PlaneName";
comboBoxPlane.ValueMember = "Id";
comboBoxPlane.DataSource = listPlanes;
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 (comboBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPlane.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Добавление изделия в магазин");
try
{
var plane = _logicP.ReadElement(new()
{
Id = (int)comboBoxPlane.SelectedValue
});
if (plane == null)
{
throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
}
var resultOperation = _logicS.AddPlaneInShop(
new ShopSearchModel
{
Id = (int)comboBoxShop.SelectedValue
},
plane,
(int)numericUpDownCount.Value
);
if (!resultOperation)
{
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();
}
}
}