PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopView/FormReportGiftComponents.cs

77 lines
2.9 KiB
C#
Raw Normal View History

2024-04-07 10:17:23 +04:00
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
namespace GiftShopView
{
public partial class FormReportGiftComponents : Form
{
private readonly ILogger _logger;
private readonly IReportLogic _logic;
public FormReportGiftComponents(ILogger<FormReportGiftComponents> logger, IReportLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormReportGiftComponents_Load(object sender, EventArgs e)
{
try
{
var dict = _logic.GetGiftComponent();
if (dict != null)
{
dataGridView.Rows.Clear();
foreach (var elem in dict)
{
dataGridView.Rows.Add(new object[] { elem.GiftName, "", "" });
foreach (var listElem in elem.Components)
{
dataGridView.Rows.Add(new object[] { "", listElem.Item1, listElem.Item2 });
}
dataGridView.Rows.Add(new object[] { "Итого", "", elem.TotalCount });
dataGridView.Rows.Add(Array.Empty<object>());
}
}
_logger.LogInformation("Загрузка списка изделий по компонентам");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка изделий по компонентам");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonSaveToExcel_Click(object sender, EventArgs e)
{
using var dialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
_logic.SaveGiftComponentToExcelFile(new ReportBindingModel
{
FileName = dialog.FileName
});
_logger.LogInformation("Сохранение списка изделий по компонентам");
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения списка изделий по компонентам");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
}