187 lines
7.4 KiB
C#
Raw Normal View History

2024-05-01 21:20:46 +04:00
using Microsoft.Extensions.Logging;
2024-03-23 18:12:12 +04:00
using MotorPlantContracts.BindingModels;
using MotorPlantContracts.BusinessLogicsContracts;
2024-04-25 23:20:52 +04:00
using MotorPlantBusinessLogic;
namespace MotorPlantView.Forms
2024-03-23 18:12:12 +04:00
{
public partial class FormMain : Form
{
private readonly ILogger _logger;
private readonly IOrderLogic _orderLogic;
2024-04-25 23:20:52 +04:00
private readonly IReportLogic _reportLogic;
public FormMain(ILogger<FormMain> logger, IOrderLogic orderLogic, IReportLogic reportLogic)
2024-03-23 18:12:12 +04:00
{
InitializeComponent();
_logger = logger;
_orderLogic = orderLogic;
2024-04-25 23:20:52 +04:00
_reportLogic = reportLogic;
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
2024-05-01 21:20:46 +04:00
_logger.LogInformation("Загрузка заказов");
2024-04-25 23:20:52 +04:00
try
{
var list = _orderLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["EngineId"].Visible = false;
dataGridView.Columns["EngineName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
2024-05-01 21:20:46 +04:00
_logger.LogInformation("Загрузка заказов");
2024-04-25 23:20:52 +04:00
}
catch (Exception ex)
{
2024-05-01 21:20:46 +04:00
_logger.LogError(ex, "Ошибка загрузки заказов");
2024-04-25 23:20:52 +04:00
}
}
2024-05-01 21:20:46 +04:00
private void КомпонентыToolStripMenuItem_Click(object sender, EventArgs e)
2024-04-25 23:20:52 +04:00
{
var service = Program.ServiceProvider?.GetService(typeof(FormComponents));
if (service is FormComponents form)
{
form.ShowDialog();
}
}
2024-05-01 21:20:46 +04:00
private void ИзделияToolStripMenuItem_Click(object sender, EventArgs e)
2024-04-25 23:20:52 +04:00
{
var service = Program.ServiceProvider?.GetService(typeof(FormEngines));
if (service is FormEngines form)
{
form.ShowDialog();
}
2024-03-23 18:12:12 +04:00
}
2024-04-25 23:20:52 +04:00
private void buttonCreateOrder_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder));
if (service is FormCreateOrder form)
{
form.ShowDialog();
LoadData();
}
}
2024-04-25 23:20:52 +04:00
private void buttonTakeOrderInWork_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
2024-05-01 21:20:46 +04:00
_logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id);
2024-03-23 18:12:12 +04:00
try
{
var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel
{
Id = id,
});
if (!operationResult)
{
2024-05-01 21:20:46 +04:00
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
2024-03-23 18:12:12 +04:00
}
LoadData();
}
catch (Exception ex)
{
2024-05-01 21:20:46 +04:00
_logger.LogError(ex, "Ошибка передачи заказа в работу");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-03-23 18:12:12 +04:00
}
}
}
2024-04-25 23:20:52 +04:00
private void buttonOrderReady_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
if (dataGridView.SelectedRows.Count == 1)
{
2024-04-25 23:20:52 +04:00
int id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
2024-05-01 21:20:46 +04:00
_logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id);
2024-03-23 18:12:12 +04:00
try
{
2024-04-25 23:20:52 +04:00
var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id });
2024-03-23 18:12:12 +04:00
if (!operationResult)
{
2024-05-01 21:20:46 +04:00
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
2024-03-23 18:12:12 +04:00
}
LoadData();
}
catch (Exception ex)
{
2024-05-01 21:20:46 +04:00
_logger.LogError(ex, "Ошибка отметки о готовности заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-03-23 18:12:12 +04:00
}
}
}
2024-04-25 23:20:52 +04:00
private void buttonIssuedOrder_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
2024-05-01 21:20:46 +04:00
_logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id);
2024-03-23 18:12:12 +04:00
try
{
2024-04-25 23:20:52 +04:00
var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel
2024-03-23 18:12:12 +04:00
{
2024-04-25 23:20:52 +04:00
Id = id
2024-03-23 18:12:12 +04:00
});
if (!operationResult)
{
2024-05-01 21:20:46 +04:00
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
2024-03-23 18:12:12 +04:00
}
2024-05-01 21:20:46 +04:00
_logger.LogInformation("Заказ №{id} выдан", id);
2024-03-23 18:12:12 +04:00
LoadData();
}
catch (Exception ex)
{
2024-05-01 21:20:46 +04:00
_logger.LogError(ex, "Ошибка отметки о выдачи заказа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-03-23 18:12:12 +04:00
}
}
}
2024-04-25 23:20:52 +04:00
private void buttonRef_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
LoadData();
}
2024-05-01 21:20:46 +04:00
private void списокКомпонентовToolStripMenuItem_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
2024-04-25 23:20:52 +04:00
using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };
if (dialog.ShowDialog() == DialogResult.OK)
2024-03-23 18:12:12 +04:00
{
2024-04-25 23:20:52 +04:00
_reportLogic.SaveEnginesToWordFile(new ReportBindingModel
2024-03-23 18:12:12 +04:00
{
2024-04-25 23:20:52 +04:00
FileName = dialog.FileName
});
2024-05-01 21:20:46 +04:00
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK,
2024-04-25 23:20:52 +04:00
MessageBoxIcon.Information);
2024-03-23 18:12:12 +04:00
}
}
2024-05-01 21:20:46 +04:00
private void компонентыПоДвигателямToolStripMenuItem_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
2024-04-25 23:20:52 +04:00
var service = Program.ServiceProvider?.GetService(typeof(FormReportEngineComponents));
if (service is FormReportEngineComponents form)
2024-03-23 18:12:12 +04:00
{
form.ShowDialog();
}
}
2024-05-01 21:20:46 +04:00
private void списокЗаказовToolStripMenuItem_Click(object sender, EventArgs e)
2024-03-23 18:12:12 +04:00
{
2024-04-25 23:20:52 +04:00
var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders));
if (service is FormReportOrders form)
2024-03-23 18:12:12 +04:00
{
form.ShowDialog();
}
}
2024-05-01 21:20:46 +04:00
private void клиентыToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormClients));
if (service is FormClients form)
{
form.ShowDialog();
}
}
}
2024-04-25 23:20:52 +04:00
}