306 lines
12 KiB
C#
306 lines
12 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using RouteGuideBusinessLogics.BusinessLogics;
|
||
using RouteGuideContracts.BindingModels;
|
||
using RouteGuideContracts.BusinessLogicsContracts;
|
||
using RouteGuideContracts.SearchModels;
|
||
using RouteGuideDataModels.Models;
|
||
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 FormRoute : Form
|
||
{
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Бизнес-логика маршрутов
|
||
/// </summary>
|
||
private readonly IRouteLogic _routeLogic;
|
||
|
||
/// <summary>
|
||
/// Бизнес-логика транспорта
|
||
/// </summary>
|
||
private readonly ITransportLogic _transportLogic;
|
||
|
||
/// <summary>
|
||
/// Идентификатор
|
||
/// </summary>
|
||
private object? _id;
|
||
|
||
/// <summary>
|
||
/// Идентификатор
|
||
/// </summary>
|
||
public object Id { set { _id = value; } }
|
||
|
||
/// <summary>
|
||
/// Список остановок в маршруте
|
||
/// </summary>
|
||
private Dictionary<object, (IStopModel<object>, int)> _routeStops;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="logger"></param>
|
||
/// <param name="routeLogic"></param>
|
||
public FormRoute(ILogger<FormRoute> logger, IRouteLogic routeLogic, ITransportLogic transportLogic)
|
||
{
|
||
InitializeComponent();
|
||
_logger = logger;
|
||
_routeLogic = routeLogic;
|
||
_transportLogic = transportLogic;
|
||
_routeStops = new Dictionary<object, (IStopModel<object>, int)>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Загрузка данных
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void FormRoute_Load(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
// Загрузка маршрутов для ComboBoxTransport
|
||
_logger.LogInformation("Загрузка списка транспорта");
|
||
var list = _transportLogic.ReadList(null);
|
||
if (list != null)
|
||
{
|
||
comboBoxTransport.DisplayMember = "License";
|
||
comboBoxTransport.ValueMember = "Id";
|
||
comboBoxTransport.DataSource = list;
|
||
comboBoxTransport.SelectedItem = null;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка загрузки списка транспорта");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
|
||
if (_id == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
_logger.LogInformation("Получение сущности 'Маршрут'");
|
||
var view = _routeLogic.ReadElement(new RouteSearchModel
|
||
{
|
||
Id = _id
|
||
});
|
||
if (view != null)
|
||
{
|
||
textBoxName.Text = view.Name;
|
||
comboBoxTransport.SelectedValue = view.TransportId;
|
||
_routeStops = view.RouteStops ?? new Dictionary<object, (IStopModel<object>, int)>();
|
||
LoadData();
|
||
}
|
||
}
|
||
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 buttonCreate_Click(object sender, EventArgs e)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormRouteStop));
|
||
if (service is FormRouteStop form)
|
||
{
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (form.StopModel == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_logger.LogInformation("Добавление остановки.{Id}: {StopName} в маршрут на позицию {Number}", form.Id, form.StopModel.Name, form.Number);
|
||
if (_routeStops.ContainsKey(form.Id))
|
||
{
|
||
_routeStops[form.Id] = (form.StopModel, form.Number);
|
||
}
|
||
else
|
||
{
|
||
_routeStops.Add(form.Id, (form.StopModel, form.Number));
|
||
}
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Изменить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonUpdate_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count == 1)
|
||
{
|
||
var service = Program.ServiceProvider?.GetService(typeof(FormRouteStop));
|
||
if (service is FormRouteStop form)
|
||
{
|
||
object id = dataGridView.SelectedRows[0].Cells[0].Value;
|
||
form.Id = id;
|
||
form.Number = _routeStops[id].Item2;
|
||
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (form.StopModel == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_logger.LogInformation("Изменение остановки.{Id}: {StopName} в маршруте на позиции {Number}", form.Id, form.StopModel.Name, form.Number);
|
||
_routeStops[form.Id] = (form.StopModel, form.Number);
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Удалить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonDelete_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count == 1)
|
||
{
|
||
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInformation("Удаление остановки.{Id}: {StopName} с позиции в маршруте {Number}", dataGridView.SelectedRows[0].Cells[0].Value, dataGridView.SelectedRows[0].Cells[1].Value, dataGridView.SelectedRows[0].Cells[2].Value);
|
||
_routeStops.Remove(dataGridView.SelectedRows[0].Cells[0].Value);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка удаления остановки.{Id}: {StopName} с позиции в маршруте {Number}", dataGridView.SelectedRows[0].Cells[0].Value, dataGridView.SelectedRows[0].Cells[1].Value, dataGridView.SelectedRows[0].Cells[2].Value);
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
LoadData();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Обновить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonRefresh_Click(object sender, EventArgs e)
|
||
{
|
||
LoadData();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Кнопка "Сохранить"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxName.Text))
|
||
{
|
||
MessageBox.Show("Заполните название маршрута", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if (comboBoxTransport.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите транспорт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
_logger.LogInformation("Сохранение сущности 'Маршрут'");
|
||
try
|
||
{
|
||
var model = new RouteBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
Name = textBoxName.Text,
|
||
TransportId = comboBoxTransport.SelectedValue,
|
||
RouteStops = _routeStops
|
||
};
|
||
|
||
var operationResult = _id != null ? _routeLogic.Update(model) : _routeLogic.Create(model);
|
||
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Загрузка данных
|
||
/// </summary>
|
||
private void LoadData()
|
||
{
|
||
_logger.LogInformation("Загрузка списка остановок в маршруте");
|
||
try
|
||
{
|
||
if (_routeStops != null)
|
||
{
|
||
dataGridView.Rows.Clear();
|
||
foreach (var rs in _routeStops)
|
||
{
|
||
dataGridView.Rows.Add(new object[]
|
||
{
|
||
rs.Key,
|
||
rs.Value.Item1.Name,
|
||
rs.Value.Item2
|
||
});
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка загрузки списка остановок в маршруте");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|