SUBD_TransportCompany/TransportCompany/TransportCompanyView/FormMain.cs

184 lines
7.4 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.BusinessLogicsContracts;
namespace TransportCompanyView
{
public partial class FormMain : Form
{
private readonly ILogger _logger;
private readonly ITransportationLogic _transportationLogic;
public FormMain(ILogger<FormMain> logger, ITransportationLogic transportationLogic)
{
InitializeComponent();
_logger = logger;
_transportationLogic = transportationLogic;
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _transportationLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["DriverId"].Visible = false;
dataGridView.Columns["TransportId"].Visible = false;
dataGridView.Columns["PointToId"].Visible = false;
dataGridView.Columns["PointFromId"].Visible = false;
dataGridView.Columns["CargoId"].Visible = false;
dataGridView.Columns["DriverFio"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataGridView.Columns["Model"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataGridView.Columns["CargoName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}
_logger.LogInformation("Загрузка транспортировок");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки транспортировок");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CargosToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCargos));
if (service is FormCargos form)
{
form.ShowDialog();
}
}
private void DriversToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormDrivers));
if (service is FormDrivers form)
{
form.ShowDialog();
}
}
private void PointsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormPoints));
if (service is FormPoints form)
{
form.ShowDialog();
}
}
private void TransportsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormTransports));
if (service is FormTransports form)
{
form.ShowDialog();
}
}
private void ButtonFormCreateTransportation_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateTransportation));
if (service is FormCreateTransportation form)
{
form.ShowDialog();
LoadData();
}
}
private void ButtonDeliveredTransportation_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Заказ No{id}. Меняется статус на 'В работе'", id);
try
{
var operationResult = _transportationLogic.DeliveredTransportation(new TransportationBindingModel
{
Id = id
});
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка передачи заказа в работу");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonArrivedTransportation_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Заказ No{id}. Меняется статус на 'Готов'", id);
try
{
var operationResult = _transportationLogic.ArrivedTransportation(new TransportationBindingModel
{
Id = id
});
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка отметки о готовности заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonShippedTransportation_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Заказ No{id}. Меняется статус на 'Выдан'", id);
try
{
var operationResult = _transportationLogic.ShippedTransportation(new TransportationBindingModel
{
Id = id
});
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
_logger.LogInformation("Заказ No{id} выдан", id);
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка отметки о выдачи заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonRef_Click(object sender, EventArgs e)
{
LoadData();
}
}
}