PIbd-21_RazubaevSM_SUBD_Labs/SYBDView/FormCreateOrder.cs

172 lines
7.0 KiB
C#
Raw Normal View History

using SYBDContracts.BindingModels;
using SYBDContracts.BusinessLogicsContracts;
using SYBDContracts.SearchModels;
using Microsoft.Extensions.Logging;
using System.Windows.Forms;
namespace SYBDView
{
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
private readonly IWorkerLogic _logicWorker;
private readonly IAutoLogic _logicAuto;
private readonly IInsuranceLogic _logicInsurance;
private readonly IOrderLogic _logicO;
private readonly IClientLogic _logicClient;
private int? _id;
public int Id { set { _id = value; } }
public FormCreateOrder(ILogger<FormCreateOrder> logger, IWorkerLogic logicW, IOrderLogic logicO, IClientLogic logicClient, IInsuranceLogic logicI, IAutoLogic autoLogic)
{
InitializeComponent();
_logger = logger;
_logicWorker = logicW;
_logicO = logicO;
_logicClient = logicClient;
_logicAuto = autoLogic;
_logicInsurance = logicI;
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
try
{
var list = _logicWorker.ReadList(null);
if (list != null)
{
comboBoxWorker.DataSource = list;
comboBoxWorker.DisplayMember = "Fullname";
comboBoxWorker.ValueMember = "Id";
comboBoxWorker.SelectedItem = null;
}
_logger.LogInformation("Загрузка работ");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки работ");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
var list = _logicClient.ReadList(null);
if (list != null)
{
comboBoxClient.DataSource = list;
comboBoxClient.DisplayMember = "Fullname";
comboBoxClient.ValueMember = "Id";
comboBoxClient.SelectedItem = null;
}
_logger.LogInformation("Загрузка клиентов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
var list = _logicAuto.ReadList(null);
if (list != null)
{
comboBoxAuto.DataSource = list;
comboBoxAuto.DisplayMember = "Model";
comboBoxAuto.ValueMember = "Id";
comboBoxAuto.SelectedItem = null;
}
_logger.LogInformation("Загрузка клиентов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
var list = _logicInsurance.ReadList(null);
if (list != null)
{
comboBoxInsurance.DataSource = list;
comboBoxInsurance.DisplayMember = "Name";
comboBoxInsurance.ValueMember = "Id";
comboBoxInsurance.SelectedItem = null;
}
_logger.LogInformation("Загрузка клиентов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxDescription.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxClient.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxInsurance.SelectedValue == null)
{
MessageBox.Show("Выберите клиента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxWorker.SelectedValue == null)
{
MessageBox.Show("Выберите клиента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxAuto.SelectedValue == null)
{
MessageBox.Show("Выберите клиента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Создание заказа");
try
{
var operationResult = _logicO.Create(new OrderBindingModel
{
WorkerId = Convert.ToInt32(comboBoxWorker.SelectedValue),
AutoId = Convert.ToInt32(comboBoxAuto.SelectedValue),
InsuranceId = Convert.ToInt32(comboBoxInsurance.SelectedValue),
ClientId = Convert.ToInt32(comboBoxClient.SelectedValue),
Description = textBoxDescription.Text,
Price = Convert.ToDouble(textBoxPrice.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);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}