using COP; using COP.Info; using KOP; using Lab1; using static Lab1.LogicalComponents.TableDocument; using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing; using DocumentFormat.OpenXml.Spreadsheet; using Lab1.LogicalComponents.SupportClasses; using Microsoft.Extensions.Logging; using KOP.PDFComponents.OfficePackage; using System.Windows.Forms; using ShopBusinessLogic.BusinessLogics; using ShopContracts.BindingModels; using ShopContracts.BusinessLogicsContracts; using ShopContracts.SearchModels; using ShopContracts.ViewModels; using static COP.ExcelComponent; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; using LegendPosition = KOP.PDFComponents.Enums.LegendPosition; using NPOI.SS.Formula.Functions; using KOP.Addition; using KOP.PDFComponents; namespace ShopView { public partial class FormMain : Form { private readonly ILogger _logger; private readonly ICustomerLogic _customerLogic; private readonly IProductCategoryLogic _productCategoryLogic; public FormMain(ILogger logger, ICustomerLogic customerLogic, IProductCategoryLogic productCategoryLogic) { InitializeComponent(); _logger = logger; _customerLogic = customerLogic; _productCategoryLogic = productCategoryLogic; Configure(); } private void LoadData() { _logger.LogInformation("Загрузка клиентов"); try { tableOfValues.ClearRows(); var list = _customerLogic.Read(null); if (list != null) { tableOfValues.AddRows(list); } _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) { _logger.LogError(ex, "Ошибка загрузки клиентов"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void FormMain_Load(object sender, EventArgs e) { LoadData(); } private void СоздатьToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCustomer)); if (service is FormCustomer form) { form.ShowDialog(); } LoadData(); } private void Configure() { ColumnInfo ci1 = new ColumnInfo("ID", 100, false, "Id"); ColumnInfo ci2 = new ColumnInfo("FIO", 100, true, "ФИО"); ColumnInfo ci3 = new ColumnInfo("ProductCategoryName", 80, true, "Категория товара"); ColumnInfo ci5 = new ColumnInfo("PhotoFilePath", 80, false, "PhotoFilePath"); ColumnInfo ci4 = new ColumnInfo("Email", 80, true, "Электронная почта"); List lst = new List(); lst.Add(ci1); lst.Add(ci2); lst.Add(ci3); lst.Add(ci4); lst.Add(ci5); tableOfValues.ColumnConfiguration(new TableConfiguration(lst)); } private void ИзменитьToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCustomer)); tableOfValues.IndexProperty = tableOfValues.myDataGridView.CurrentRow.Index; if (service is FormCustomer form) { if (tableOfValues.IndexProperty != -1) { var selectedCustomer = tableOfValues.GetUser(); form.Id = Convert.ToInt32(selectedCustomer.Id); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } else { MessageBox.Show("Выберите клиента для редактирования"); } } } private void УдалитьToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { tableOfValues.IndexProperty = tableOfValues.myDataGridView.CurrentRow.Index; var selectedCustomer = tableOfValues.GetUser(); int id = Convert.ToInt32(selectedCustomer.Id); try { _customerLogic.Delete(new CustomerBindingModel { Id = id }); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadData(); } } private void СоздатьExcelСФотоToolStripMenuItem_Click(object sender, EventArgs e) { var list = _customerLogic.Read(null); List images = new(); using var dialog = new SaveFileDialog { Filter = "xlsx|*.xlsx" }; if (dialog.ShowDialog() == DialogResult.OK) { try { if (list != null) { foreach (var item in list) { images.Add(new ImageInfo() { FilePath = item.PhotoFilePath }); } } ExcelImageInfo info = new(dialog.FileName, "Документ с фотографиями клиентов", images); excelComponent.GenerateExcelWithImages(info); MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void СоздатьДокументСТаблицейToolStripMenuItem_Click(object sender, EventArgs e) { var list = _customerLogic.Read(null); List data = new(); List mergedColumns = new() { new int[] { 1, 2 } }; List columnDefinitions = new() { new ColumnDefinition { Header = "ID", PropertyName = "Id", Weight = 11 }, new ColumnDefinition { Header = "Личные данные", PropertyName = "PersonalData", Weight = 11 }, new ColumnDefinition { Header = "Личные данные", PropertyName = "PersonalData1", Weight = 11 }, new ColumnDefinition { Header = "Категория товаров", PropertyName = "ProductCategoryName", Weight = 21 } }; List columnDefinitions2 = new() { new ColumnDefinition { Header = "ID", PropertyName = "Id", Weight = 11 }, new ColumnDefinition { Header = "ФИО", PropertyName = "FIO", Weight = 11 }, new ColumnDefinition { Header = "Электронная почта", PropertyName = "Email", Weight = 70 }, new ColumnDefinition { Header = "Категория товаров", PropertyName = "ProductCategoryName", Weight = 21 } }; using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; if (dialog.ShowDialog() == DialogResult.OK) { try { if (list != null) { foreach (var item in list) { data.Add(new CustomerBindingModel() { Id = item.Id, FIO = item.FIO, Email = item.Email, ProductCategoryName = item.ProductCategoryName}); } } TableDocumentInfo tableWord = new(dialog.FileName, "Таблица с клиентами", columnDefinitions, columnDefinitions2, data, mergedColumns); Lab1.LogicalComponents.TableDocument.CreateTable(tableWord); MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void СоздатьДокументСДиаграммойToolStripMenuItem_Click(object sender, EventArgs e) { var listCustomers = _customerLogic.Read(null); var listProductCategorys = _productCategoryLogic.Read(null); List<(string, int)> data = new(); var Data = new Dictionary(); using var dialog = new SaveFileDialog { Filter = "pdf|*.pdf" }; if (dialog.ShowDialog() == DialogResult.OK) { try { for (int i = 0; i < listProductCategorys.Count; i++) { int count = 0; for (int j = 0; j < listCustomers.Count; j++) { string[] dirs = listCustomers[j].ProductCategoryName.Split(";"); foreach (var dir in dirs) { if (dir == listProductCategorys[i].Name) count++; } } data.Add((listProductCategorys[i].Name, count)); } if (data != null) { foreach (var item in data) { Data.Add(item.Item1, new[] {item.Item2 }); } } PdfChartComponent pdfChartComponent = new PdfChartComponent(); pdfChartComponent.CreateDocument(dialog.FileName, "Histogram", "Customers-ProductCategorys", LegendPosition.Right, Data); MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void СправочникиToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormHandbooks)); if (service is FormHandbooks form) { form.ShowDialog(); } LoadData(); } } }