2024-04-05 02:48:52 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using RouteGuideBusinessLogics.BusinessLogics;
|
|
|
|
|
using RouteGuideContracts.BindingModels;
|
|
|
|
|
using RouteGuideContracts.BusinessLogicsContracts;
|
|
|
|
|
using RouteGuideContracts.SearchModels;
|
|
|
|
|
using RouteGuideDataModels.Enums;
|
|
|
|
|
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 RouteGuideView
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Форма для создания/редактирования транспорта
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class FormTransport : Form
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логгер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Бизнес-логика для транспорта
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ITransportLogic _transportLogic;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Бизнес-логика для водителей
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IDriverLogic _driverLogic;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Идентификатор
|
|
|
|
|
/// </summary>
|
2024-04-23 19:38:25 +04:00
|
|
|
|
private object? _id;
|
2024-04-05 02:48:52 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Идентификатор
|
|
|
|
|
/// </summary>
|
2024-04-23 19:38:25 +04:00
|
|
|
|
public object Id { set { _id = value; } }
|
2024-04-05 02:48:52 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
/// <param name="transportLogic"></param>
|
|
|
|
|
/// <param name="driverLogic"></param>
|
|
|
|
|
public FormTransport(ILogger<FormTransport> logger, ITransportLogic transportLogic, IDriverLogic driverLogic)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_transportLogic = transportLogic;
|
|
|
|
|
_driverLogic = driverLogic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка данных
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void FormTransport_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Загрузка типов транспорта для ComboBoxType
|
|
|
|
|
foreach (TransportType type in Enum.GetValues(typeof(TransportType)))
|
|
|
|
|
{
|
|
|
|
|
comboBoxType.Items.Add(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Загрузка водителей для ComboBoxDriver
|
|
|
|
|
_logger.LogInformation("Загрузка списка водителей");
|
|
|
|
|
var list = _driverLogic.ReadList(null);
|
|
|
|
|
if (list != null)
|
|
|
|
|
{
|
|
|
|
|
comboBoxDriver.DisplayMember = "FullName";
|
|
|
|
|
comboBoxDriver.ValueMember = "Id";
|
|
|
|
|
comboBoxDriver.DataSource = list;
|
|
|
|
|
comboBoxDriver.SelectedItem = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки списка водителей");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 19:38:25 +04:00
|
|
|
|
if (_id == null)
|
2024-04-05 02:48:52 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Заполнение полей
|
|
|
|
|
_logger.LogInformation("Получение сущности 'Транспорт'");
|
|
|
|
|
var view = _transportLogic.ReadElement(new TransportSearchModel
|
|
|
|
|
{
|
2024-04-23 19:38:25 +04:00
|
|
|
|
Id = _id
|
2024-04-05 02:48:52 +04:00
|
|
|
|
});
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
textBoxLicense.Text = view.License;
|
|
|
|
|
textBoxCapacity.Text = view.Capacity.ToString();
|
2024-04-08 21:05:52 +04:00
|
|
|
|
comboBoxDriver.SelectedValue = view.DriverId;
|
2024-04-05 02:48:52 +04:00
|
|
|
|
comboBoxType.SelectedItem = view.Type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex1)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex1, "Ошибка получения сущности 'Водитель'");
|
|
|
|
|
MessageBox.Show(ex1.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Кнопка "Сохранить"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-04-08 21:05:52 +04:00
|
|
|
|
if (comboBoxType.SelectedIndex == -1)
|
2024-04-05 02:48:52 +04:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Выберите тип транспорта", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (comboBoxDriver.SelectedValue == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Выберите водителя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxLicense.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните номер транспорта", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxCapacity.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните вместимость", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Сохранение сущности 'Транспорт'");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var model = new TransportBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = _id ?? 0,
|
|
|
|
|
License = textBoxLicense.Text,
|
2024-04-08 21:05:52 +04:00
|
|
|
|
Type = (TransportType)comboBoxType.SelectedItem,
|
2024-04-05 02:48:52 +04:00
|
|
|
|
Capacity = int.TryParse(textBoxCapacity.Text, out var capacity) ? capacity : 1,
|
2024-04-23 19:38:25 +04:00
|
|
|
|
DriverId = comboBoxDriver.SelectedValue
|
2024-04-05 02:48:52 +04:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-23 19:38:25 +04:00
|
|
|
|
var operationResult = _id != null ? _transportLogic.Update(model) : _transportLogic.Create(model);
|
2024-04-05 02:48:52 +04:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|