using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Plugins; using Contracts.BusinessLogicContracts; using Library14Petrushin; using WinFormsLibrary1; using Library15Gerimovich.OfficePackage.HelperModels; using Contracts.ViewModels; using BusinessLogics.BusinessLogics; using DatabaseImplement.Implements; using Contracts.BindingModels; using DatabaseImplement.Models; using Library15Gerimovich; using Library14Petrushin.Classes; namespace Forms { public class PluginsConvention : IPluginsConvention { private readonly IProductLogic _productLogic; private readonly ICategoryLogic _categoryLogic; private readonly HierarchicalTreeView _myTreeView; private readonly ComponentWithBigText wordLongTextComponent; private readonly WordTablesComponent excelDiagramComponent; private readonly PdfCirclDiagr componentDocumentWithTableMultiHeaderPdf; public string PluginName { get; set; } = "Products"; public UserControl GetControl { get { return _myTreeView; } } public PluginsConvention() { _productLogic = new ProductLogic(new ProductStorage()); _categoryLogic = new CategoryLogic(new CategoryStorage()); wordLongTextComponent = new(); excelDiagramComponent = new(); componentDocumentWithTableMultiHeaderPdf = new(); _myTreeView = new(); } public PluginsConventionElement GetElement { get { int Id = int.Parse(_myTreeView.GetSelectedObject()!.Id); byte[] bytes = new byte[16]; BitConverter.GetBytes(Id).CopyTo(bytes, 0); Guid guid = new Guid(bytes); return new PluginsConventionElement() { Id = guid }; } } public Form GetForm(PluginsConventionElement element) { if (element == null) { return new ProductForm(_productLogic, _categoryLogic); } else { int id = element.Id.GetHashCode(); int? idl = id; ProductViewModel model = _productLogic.ReadElement(new Contracts.SearchModels.ProductSearchModel { Id = idl }); ProductForm form = new ProductForm(_productLogic, _categoryLogic, model); // form.Id = element.Id.GetHashCode(); return form; } } public Form GetThesaurus() { return new CategoryForm(_categoryLogic); } public bool DeleteElement(PluginsConventionElement element) { _productLogic.Delete( new ProductBindingModel { Id = element.Id.GetHashCode() } ); return true; } public void ReloadData() { try { var products = _productLogic.ReadList(null); var hierarchy = new List { "Category", "CountOnStorageS", "Id", "Name" }; var alwaysNewBranch = new Dictionary { { "Name", true } }; _myTreeView.SetHierarchy(hierarchy, alwaysNewBranch); foreach (var product in products) { _myTreeView.AddObjectToTree(product, "Name"); } } catch (Exception ex) { MessageBox.Show( ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument) { using (var saveFileDialog = new SaveFileDialog()) { saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { var products = _productLogic.ReadList(null); var rows = products.Select(p => $"{p.Name} - {p.Description}").ToArray(); var component = new ComponentWithBigText(); component.CreateDocument(saveFileDialog.FileName, "Список продуктов", rows); MessageBox.Show("Документ успешно создан."); } return true; } } public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument) { using (var saveFileDialog = new SaveFileDialog()) { saveFileDialog.Filter = "Word files (*.docx)|*.docx|All files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { var products = _productLogic.ReadList(null); if (products == null || products.Count == 0) { MessageBox.Show("Нет данных для создания документа."); return false; } var headers = new List<(int, int, string, string)> { (0, 0, "Id", "Id"), (1, 0, "Name", "Name"), (2, 0, "Category", "Category"), (3, 0, "CountOnStorage", "CountOnStorage") }; var columnsRowsWidth = new List<(int Column, int Row)> { (100, 0), (200, 1), (150, 2), (150, 3) }; var config = new WordTableWithData { FileName = saveFileDialog.FileName, Title = "Список продуктов", Headers = headers, Data = products, ColumnsRowsWidth = columnsRowsWidth, NullReplace = "-", }; var wordTables = new WordTablesComponent(); wordTables.CreateTable(config); MessageBox.Show("Документ успешно создан."); } return true; } } public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument) { using (var saveFileDialog = new SaveFileDialog()) { saveFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { var products = _productLogic.ReadList(null); var categories = products.Where(p => p.CountOnStorage == 0).GroupBy(p => p.Category) .Select(g => new ChartData { SeriesName = g.Key, Value = g.Count(p => p.CountOnStorage == 0) }).ToList(); if (categories.Count == 0) { MessageBox.Show("Нет данных для создания диаграммы."); return false; } var component = new PdfCirclDiagr(); component.GeneratePdf(saveFileDialog.FileName, "Диаграмма продуктов", "Продукты без наличия", LegendPosition.Right, categories); MessageBox.Show("Документ успешно создан."); } return true; } } } }