using AircraftPlantBusinessLogic.BusinessLogics;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
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 AircraftPlantView
{
///
/// Главная форма
///
public partial class FormMain : Form
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для заказов
///
private readonly IOrderLogic _orderLogic;
///
/// Взаимодействие с отчетами
///
private readonly IReportLogic _reportLogic;
///
/// Имитация деятельности исполнителей
///
private readonly IWorkProcess _workProcess;
///
/// Конструктор
///
///
///
public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkProcess workProcess)
{
InitializeComponent();
_logger = logger;
_orderLogic = orderLogic;
_reportLogic = reportLogic;
_workProcess = workProcess;
}
///
/// Загрузка списка заказов
///
///
///
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
///
/// Показать список всех компонентов
///
///
///
private void компонентыToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormComponents));
if (service is FormComponents form)
{
form.ShowDialog();
}
}
///
/// Показать список всех изделий
///
///
///
private void изделияToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormPlanes));
if (service is FormPlanes form)
{
form.ShowDialog();
}
}
///
/// Показать список всех магазинов
///
///
///
private void магазиныToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormShops));
if (service is FormShops form)
{
form.ShowDialog();
}
}
///
/// Вывести список всех клиентов
///
///
///
private void клиентыToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormClients));
if (service is FormClients form)
{
form.ShowDialog();
}
}
///
/// Вывести список всех исполнителей
///
///
///
private void исполнителиToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormImplementers));
if (service is FormImplementers form)
{
form.ShowDialog();
}
}
///
/// Вывести отчет по изделиям
///
///
///
private void изделияToolStripMenuItem1_Click(object sender, EventArgs e)
{
using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };
if (dialog.ShowDialog() == DialogResult.OK)
{
_reportLogic.SavePlanesToWordFile(new ReportBindingModel { FileName = dialog.FileName });
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
///
/// Получить отчет по изделиям с расшифровкой по компонентам
///
///
///
private void изделияСКомпонентамиToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormReportPlaneComponents));
if (service is FormReportPlaneComponents form)
{
form.ShowDialog();
}
}
///
/// Получить отчет по заказам
///
///
///
private void списокЗаказовToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders));
if (service is FormReportOrders form)
{
form.ShowDialog();
}
}
///
/// Получить отчет по заказам с группировкой по датам
///
///
///
private void заказыСГруппировкойToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormReportGroupOrders));
if (service is FormReportGroupOrders form)
{
form.ShowDialog();
}
}
///
/// Получить отчет со списком магазинов
///
///
///
private void списокМагазиновToolStripMenuItem_Click(object sender, EventArgs e)
{
using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };
if (dialog.ShowDialog() == DialogResult.OK)
{
_reportLogic.SaveShopsToWordFile(new ReportBindingModel { FileName = dialog.FileName });
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
///
/// Получить отчет по ассортименту магазинов
///
///
///
private void ассортиментМагазиновToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormReportShopPlanes));
if (service is FormReportShopPlanes form)
{
form.ShowDialog();
}
}
///
/// Кнопка "Создать заказ"
///
///
///
private void запускРаботToolStripMenuItem_Click(object sender, EventArgs e)
{
_workProcess.DoWork((
Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!,
_orderLogic);
MessageBox.Show("Процесс обработки запущен", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
///
/// Посмотреть письма
///
///
///
private void почтаToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormMail));
if (service is FormMail form)
{
form.ShowDialog();
}
}
///
/// Кнопка "Создать заказ"
///
///
///
private void buttonCreateOrder_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder));
if (service is FormCreateOrder form)
{
form.ShowDialog();
LoadData();
}
}
///
/// Кнопка "Заказ выдан"
///
///
///
private void buttonIssuedOrder_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id);
try
{
var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id });
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
_logger.LogInformation("Заказ №{id} выдан", id);
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка отметки о выдаче заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
///
/// Кнопка "Добавить изделие в магазин"
///
///
///
private void buttonAddPlaneInShop_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateSupply));
if (service is FormCreateSupply form)
{
form.ShowDialog();
}
}
///
/// Кнопка "Продать изделия"
///
///
///
private void buttonSellPlanes_Click(object sender, EventArgs e)
{
var services = Program.ServiceProvider?.GetService(typeof(FormSell));
if (services is FormSell form)
{
form.ShowDialog();
}
}
///
/// Кнопка "Обновить список"
///
///
///
private void buttonRefresh_Click(object sender, EventArgs e)
{
LoadData();
}
///
/// Метод загрузки списка заказов
///
private void LoadData()
{
_logger.LogInformation("Загрузка заказов");
try
{
var list = _orderLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["PlaneId"].Visible = false;
dataGridView.Columns["ClientId"].Visible = false;
dataGridView.Columns["ImplementerId"].Visible = false;
}
_logger.LogInformation("Загрузка заказов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки заказов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}