ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallation/FormCreateOrder.cs

153 lines
5.8 KiB
C#
Raw Normal View History

2024-05-12 14:45:39 +04:00
using Microsoft.Extensions.Logging;
using SoftwareInstallationContracts.BindingModels;
2024-04-07 16:54:02 +04:00
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
2024-05-12 14:45:39 +04:00
using SoftwareInstallationContracts.ViewModels;
2024-04-07 16:54:02 +04:00
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;
2024-05-12 14:45:39 +04:00
namespace SoftwareInstallationView
2024-04-07 16:54:02 +04:00
{
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
private readonly IPackageLogic _logicP;
private readonly IOrderLogic _logicO;
2024-06-17 20:50:46 +04:00
private readonly IPackageLogic _logicC;
2024-05-12 14:45:39 +04:00
private List<PackageViewModel>? _list;
2024-04-07 16:54:02 +04:00
public FormCreateOrder(ILogger<FormCreateOrder> logger, IPackageLogic logicP, IOrderLogic logicO)
{
InitializeComponent();
_logger = logger;
_logicP = logicP;
_logicO = logicO;
2024-06-17 20:50:46 +04:00
_logicC = logicC;
2024-04-07 16:54:02 +04:00
}
2024-06-17 20:50:46 +04:00
private void FormCreateOrder_Load(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
2024-06-17 20:50:46 +04:00
_logger.LogInformation("Загрузка ремонта для заказа");
try
2024-04-07 16:54:02 +04:00
{
2024-06-17 20:50:46 +04:00
var list = _logicR.ReadList(null);
if (list != null)
{
comboBoxPackage.DisplayMember = "PizzaName";
comboBoxPackage.ValueMember = "Id";
comboBoxPackage.DataSource = list;
comboBoxPackage.SelectedItem = null;
}
_logger.LogInformation("Пицца загружено");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки ремонта");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
_logger.LogInformation("Загрузка клиентов для заказа");
try
{
var list = _logicC.ReadList(null);
if (list != null)
{
comboBoxPackage.DisplayMember = "PackageFIO";
comboBoxPackage.ValueMember = "Id";
comboBoxPackage.DataSource = list;
comboBoxPackage.SelectedItem = null;
}
_logger.LogInformation("Клиенты загружены");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-05-12 14:45:39 +04:00
2024-04-07 16:54:02 +04:00
private void CalcSum()
{
if (comboBoxPackage.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text))
{
try
{
int id = Convert.ToInt32(comboBoxPackage.SelectedValue);
var Package = _logicP.ReadElement(new PackageSearchModel
{
Id = id
});
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (Package?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка расчета суммы заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2024-05-12 14:45:39 +04:00
private void TextBoxCount_TextChanged(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
CalcSum();
}
private void ComboBoxPackage_SelectedIndexChanged(object sender, EventArgs e)
{
CalcSum();
}
2024-05-12 14:45:39 +04:00
private void ButtonSave_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPackage.SelectedValue == null)
{
2024-05-12 14:45:39 +04:00
MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
return;
}
_logger.LogInformation("Создание заказа");
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
PackageId = Convert.ToInt32(comboBoxPackage.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text)
});
if (!operationResult)
{
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
2024-05-12 14:45:39 +04:00
_logger.LogError(ex, "Ошибка создания заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-05-12 14:45:39 +04:00
private void ButtonCancel_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}