245 lines
6.1 KiB
C#
245 lines
6.1 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using TransportGuideContracts.BusinessLogicsContracts;
|
|
using TransportGuideContracts.SearchModels;
|
|
using TransportGuideDataModels.Models;
|
|
using TransportGuideContracts.BindingModels;
|
|
|
|
namespace TransportGuide
|
|
{
|
|
public partial class FormRoute : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IRouteLogic _logic;
|
|
private readonly ITransportTypeLogic _logicTT;
|
|
private int? _id;
|
|
private Dictionary<int, (IStopModel, int)> _RouteStops;
|
|
public int Id { set { _id = value; } }
|
|
public FormRoute(ILogger<FormRoute> logger, IRouteLogic logic, ITransportTypeLogic logicTT)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
_RouteStops = new Dictionary<int, (IStopModel, int)>();
|
|
_logicTT = logicTT;
|
|
}
|
|
|
|
private void FormRoute_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
var list = _logicTT.ReadList(null);
|
|
if (list != null)
|
|
{
|
|
comboBoxTransportType.DisplayMember = "Type";
|
|
comboBoxTransportType.ValueMember = "Id";
|
|
comboBoxTransportType.DataSource = list.Select(c => c.Name).ToList();
|
|
comboBoxTransportType.SelectedItem = null;
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка загрузки списка");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
var view = _logic.ReadElement(new RouteSearchModel
|
|
{
|
|
Id = _id.Value
|
|
});
|
|
if (view != null)
|
|
{
|
|
textBoxName.Text = view.Name;
|
|
textBoxIP.Text = view.IP.ToString();
|
|
comboBoxTransportType.Text = view.TransportTypeName;
|
|
_RouteStops = view.StopRoutes ?? new Dictionary<int, (IStopModel, int)>();
|
|
LoadData();
|
|
//foreach (var el in view.StopRoutes.Values)
|
|
//{
|
|
// dataGridView.Rows.Add(new object[] {el.Item1.Name, el.Item2});
|
|
//}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
private void LoadData()
|
|
{
|
|
try
|
|
{
|
|
if (_RouteStops != null)
|
|
{
|
|
dataGridView.Rows.Clear();
|
|
foreach (var pc in _RouteStops)
|
|
{
|
|
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.Name, pc.Value.Item2 });
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
var service = Program.ServiceProvider?.GetService(typeof(FormStopRoute));
|
|
if (service is FormStopRoute form)
|
|
{
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (form.StopModel == null)
|
|
{
|
|
return;
|
|
}
|
|
if (_RouteStops.ContainsKey(form.Id))
|
|
{
|
|
_RouteStops[form.Id] = (form.StopModel, form.Nomer);
|
|
}
|
|
else
|
|
{
|
|
_RouteStops.Add(form.Id, (form.StopModel, form.Nomer));
|
|
}
|
|
LoadData();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void buttonChange_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
{
|
|
var service = Program.ServiceProvider?.GetService(typeof(FormStopRoute));
|
|
if (service is FormStopRoute form)
|
|
{
|
|
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
|
|
form.Id = id;
|
|
form.Nomer = _RouteStops[id].Item2;
|
|
if (form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (form.StopModel == null)
|
|
{
|
|
return;
|
|
}
|
|
_RouteStops[form.Id] = (form.StopModel, form.Nomer);
|
|
LoadData();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void buttonDelete_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.SelectedRows.Count == 1)
|
|
{
|
|
if (MessageBox.Show("Удалить запись?", "Вопрос",
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
try
|
|
{
|
|
_RouteStops?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
LoadData();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void buttonUpdate_Click(object sender, EventArgs e)
|
|
{
|
|
LoadData();
|
|
}
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|
{
|
|
MessageBox.Show("Fill in Name", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(textBoxIP.Text))
|
|
{
|
|
MessageBox.Show("Fill in IP", "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (_RouteStops == null || _RouteStops.Count == 0)
|
|
{
|
|
MessageBox.Show("Fill in Stops", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
if (comboBoxTransportType.SelectedItem == null)
|
|
{
|
|
MessageBox.Show("Fill in Type", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var model = new RouteBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
Name = textBoxName.Text,
|
|
IP = textBoxIP.Text,
|
|
TransportTypeId = comboBoxTransportType.SelectedIndex + 1,
|
|
StopRoutes = _RouteStops
|
|
};
|
|
var operationResult = _id.HasValue ? _logic.Update(model) :
|
|
_logic.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);
|
|
}
|
|
|
|
}
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
}
|
|
}
|