SUBD_TransportCompany/TransportCompany/TransportCompanyView/FormCreateTransportation.cs

158 lines
7.0 KiB
C#

using Microsoft.Extensions.Logging;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.BusinessLogicsContracts;
using TransportCompanyContracts.ViewModels;
namespace TransportCompanyView
{
public partial class FormCreateTransportation : Form
{
private readonly ILogger _logger;
private readonly ICargoLogic _logicC;
private readonly IDriverLogic _logicD;
private readonly ITransportLogic _logicT;
private readonly IPointLogic _logicP;
private readonly ITransportationLogic _logicTt;
private List<CargoViewModel>? _cargoList;
private List<DriverViewModel>? _driverList;
private List<TransportViewModel>? _transportList;
private List<PointViewModel>? _pointFromList;
private List<PointViewModel>? _pointToList;
public FormCreateTransportation(ILogger<FormCreateTransportation> logger, ICargoLogic logicC, IDriverLogic logicD, ITransportLogic logicT, IPointLogic logicP, ITransportationLogic logicTt)
{
InitializeComponent();
_logger = logger;
_logicC = logicC;
_logicD = logicD;
_logicT = logicT;
_logicP = logicP;
_logicTt = logicTt;
}
private void FormCreateTransportation_Load(object sender, EventArgs e)
{
_cargoList = _logicC.ReadList(null);
_driverList = _logicD.ReadList(null);
_transportList = _logicT.ReadList(null);
_pointFromList = _logicP.ReadList(null);
_pointToList = _logicP.ReadList(null);
if (_cargoList != null)
{
comboBoxCargo.DisplayMember = "CargoName";
comboBoxCargo.ValueMember = "Id";
comboBoxCargo.DataSource = _cargoList;
comboBoxCargo.SelectedItem = null;
_logger.LogInformation("Загрузка грузов для транспортировки");
}
if (_driverList != null)
{
comboBoxDriver.DisplayMember = "DriverFio";
comboBoxDriver.ValueMember = "Id";
comboBoxDriver.DataSource = _driverList;
comboBoxDriver.SelectedItem = null;
_logger.LogInformation("Загрузка водителей для транспортировки");
}
if (_transportList != null)
{
comboBoxTransport.DisplayMember = "Model";
comboBoxTransport.ValueMember = "Id";
comboBoxTransport.DataSource = _transportList;
comboBoxTransport.SelectedItem = null;
_logger.LogInformation("Загрузка машин для транспортировки");
}
if (_pointFromList != null)
{
comboBoxPointFrom.DisplayMember = "PointName";
comboBoxPointFrom.ValueMember = "Id";
comboBoxPointFrom.DataSource = _pointFromList;
comboBoxPointFrom.SelectedItem = null;
_logger.LogInformation("Загрузка пунктов отправления для транспортировки");
}
if (_pointToList != null)
{
comboBoxPointTo.DisplayMember = "PointName";
comboBoxPointTo.ValueMember = "Id";
comboBoxPointTo.DataSource = _pointToList;
comboBoxPointTo.SelectedItem = null;
_logger.LogInformation("Загрузка пунктов прибытия для транспортировки");
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxCargo.SelectedValue == null)
{
MessageBox.Show("Выберите груз", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните количество груза", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxDriver.SelectedValue == null)
{
MessageBox.Show("Выберите водителя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxTransport.SelectedValue == null)
{
MessageBox.Show("Выберите машину", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPointFrom.SelectedValue == null)
{
MessageBox.Show("Выберите пункт отправки", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPointTo.SelectedValue == null)
{
MessageBox.Show("Выберите пункт прибытия", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Создание транспортировки");
try
{
var operationResult = _logicTt.CreateTransportation(new TransportationBindingModel
{
DriverId = Convert.ToInt32(comboBoxDriver.SelectedValue),
TransportId = Convert.ToInt32(comboBoxTransport.SelectedValue),
CargoId = Convert.ToInt32(comboBoxCargo.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text),
PointToId = Convert.ToInt32(comboBoxPointTo.SelectedValue),
PointFromId = Convert.ToInt32(comboBoxPointFrom.SelectedValue),
});
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();
}
}
}