PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesView/MainForm.cs

193 lines
5.3 KiB
C#
Raw Normal View History

2024-05-04 19:41:17 +04:00
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
using System.Windows.Forms;
2024-05-04 19:46:22 +04:00
using SewingDressesBusinessLogic.BusinessLogic;
2024-05-04 19:41:17 +04:00
namespace SewingDressesView
{
2024-05-26 13:34:14 +04:00
public partial class MainForm : Form
{
private readonly ILogger _logger;
private readonly IOrderLogic _orderLogic;
private readonly IReportLogic _reportLogic;
public MainForm(ILogger<MainForm> logger, IOrderLogic orderLogic, IReportLogic reportLogic)
{
InitializeComponent();
_logger = logger;
_orderLogic = orderLogic;
_reportLogic = reportLogic;
}
private void LoadData()
{
try
{
var list = _orderLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["DressId"].Visible = false;
dataGridView.Columns["ClientId"].Visible = false;
}
_logger.LogInformation("Load orders");
}
catch (Exception ex)
{
_logger.LogError(ex, "Load orders error");
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(ComponentsForm));
if (service is ComponentsForm form)
{
form.ShowDialog();
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void DressesToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(DressesForm));
if (service is DressesForm form)
{
form.ShowDialog();
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(OrderForm));
if (service is OrderForm form)
{
form.ShowDialog();
LoadData();
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void buttonDoOrder_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Order №{id}. Change Status on 'In work'", id);
try
{
var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id });
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Change Status on 'In work' error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void buttonOrderReady_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Order №{id}. Change status on 'Ready'",
id);
try
{
var operationResult = _orderLogic.FinishOrder(new
OrderBindingModel
{ Id = id });
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Change status on 'Ready' error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void buttonOrderGive_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Order №{id}. Status change on 'Given'", id);
try
{
var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id });
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
_logger.LogInformation("Order №{id} given", id);
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Change status on 'Give' error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2024-05-04 19:41:17 +04:00
2024-05-26 13:34:14 +04:00
private void buttonOrderUpdate_Click(object sender, EventArgs e)
{
LoadData();
}
2024-05-04 19:42:41 +04:00
2024-05-26 13:34:14 +04:00
private void MainForm_Load(object sender, EventArgs e)
{
LoadData();
}
private void ComponentProductsToolStripMenuItemReport_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(ReportDressComponentsForm));
if (service is ReportDressComponentsForm form)
{
form.ShowDialog();
}
}
private void OrdersToolStripMenuItemReport_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(ReportOrdersForm));
if (service is ReportOrdersForm form)
{
form.ShowDialog();
}
}
2024-05-04 19:46:22 +04:00
2024-05-26 13:34:14 +04:00
private void DressesToolStripMenuItemReport_Click(object sender, EventArgs e)
{
using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };
if (dialog.ShowDialog() == DialogResult.OK)
{
_reportLogic.SaveComponentsToWordFile(new ReportBindingModel
{
FileName = dialog.FileName
});
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void clientsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(ClientsForm));
if (service is ClientsForm form)
{
form.ShowDialog();
}
}
}
2024-05-04 19:41:17 +04:00
}