using System; using System.Windows.Forms; using Contracts.BindingModels; using Contracts.BusinessLogicContracts; using Contracts.SearchModels; using Contracts.ViewModels; using Library14Petrushin; using Library14Petrushin.Classes; using Library15Gerimovich; using Library15Gerimovich.OfficePackage.HelperModels; using WinFormsLibrary1; using WinFormsLibrary1.Models; using LegendPosition = Library14Petrushin.Classes.LegendPosition; namespace Forms { public partial class MainForm : Form { private readonly IProductLogic _productLogic; private readonly ICategoryLogic _categoryLogic; private ToolStrip toolStrip; public MainForm(IProductLogic productLogic, ICategoryLogic categoryLogic) { InitializeComponent(); _productLogic = productLogic; _categoryLogic = categoryLogic; LoadProducts(); } private void LoadProducts() { var products = _productLogic.ReadList(null); var hierarchy = new List { "Category", "CountOnStorageS", "Id", "Name" }; var alwaysNewBranch = new Dictionary { { "Name", true } }; _productTreeView.SetHierarchy(hierarchy, alwaysNewBranch); foreach (var product in products) { _productTreeView.AddObjectToTree(product, "Name"); } } private void MainForm_Load(object sender, EventArgs e) { // Настройка контекстного меню и горячих клавиш var contextMenu = new ContextMenuStrip(); contextMenu.Items.Add("Добавить", null, (s, ev) => AddProduct()); contextMenu.Items.Add("Редактировать", null, (s, ev) => EditProduct()); contextMenu.Items.Add("Удалить", null, (s, ev) => DeleteProduct()); contextMenu.Items.Add("Создать документ", null, (s, ev) => CreateDocument()); contextMenu.Items.Add("Создать документ с таблицей", null, (s, ev) => CreateTableDocument()); contextMenu.Items.Add("Создать документ с диаграммой", null, (s, ev) => CreateChartDocument()); // Привязка контекстного меню к компоненту вывода списков _productTreeView.ContextMenuStrip = contextMenu; // Настройка горячих клавиш this.KeyDown += MainForm_KeyDown; // Настройка панели инструментов toolStrip = new ToolStrip(); toolStrip.Items.Add("Продукты", null, (s, ev) => OpenProductForm()); toolStrip.Items.Add("Категории", null, (s, ev) => OpenCategoryForm()); this.Controls.Add(toolStrip); } private void MainForm_KeyDown(object sender, KeyEventArgs e) { if (e.Control) { switch (e.KeyCode) { case Keys.A: AddProduct(); break; case Keys.U: EditProduct(); break; case Keys.D: DeleteProduct(); break; case Keys.S: CreateDocument(); break; case Keys.T: CreateTableDocument(); break; case Keys.C: CreateChartDocument(); break; } } } private void OpenProductForm() { var form = new ProductForm(_productLogic, _categoryLogic); form.ShowDialog(); } private void OpenCategoryForm() { var form = new CategoryForm(_categoryLogic); if (form.ShowDialog() == DialogResult.OK) { LoadProducts(); } } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Control | Keys.A)) { AddProduct(); return true; } else if (keyData == (Keys.Control | Keys.U)) { EditProduct(); return true; } else if (keyData == (Keys.Control | Keys.D)) { DeleteProduct(); return true; } else if (keyData == (Keys.Control | Keys.S)) { CreateDocument(); return true; } else if (keyData == (Keys.Control | Keys.T)) { CreateTableDocument(); return true; } else if (keyData == (Keys.Control | Keys.C)) { CreateChartDocument(); return true; } return base.ProcessCmdKey(ref msg, keyData); } private void AddProduct() { var form = new ProductForm(_productLogic, _categoryLogic); if (form.ShowDialog() == DialogResult.OK) { LoadProducts(); } } private void EditProduct() { var selectedProduct = _productTreeView.GetSelectedObject(); selectedProduct = _productLogic.ReadElement(new ProductSearchModel { Id = int.Parse(selectedProduct.Id) }); if (selectedProduct != null) { var form = new ProductForm(_productLogic, _categoryLogic, selectedProduct); if (form.ShowDialog() == DialogResult.OK) { LoadProducts(); } } } private void DeleteProduct() { var selectedProduct = _productTreeView.GetSelectedObject(); selectedProduct = _productLogic.ReadElement(new ProductSearchModel { Id = int.Parse(selectedProduct.Id) }); if (selectedProduct != null) { var result = MessageBox.Show("Вы действительно хотите удалить этот продукт?", "Подтверждение", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { _productLogic.Delete(new ProductBindingModel { Id = int.Parse(selectedProduct.Id) }); LoadProducts(); } } } private void CreateDocument() { 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("Документ успешно создан."); } } } private void CreateTableDocument() { 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; } 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 = "-", }; wordTablesComponent1.CreateTable(config); MessageBox.Show("Документ успешно создан."); } } } private void CreateChartDocument() { 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; } var component = new PdfCirclDiagr(); component.GeneratePdf(saveFileDialog.FileName, "Диаграмма продуктов", "Продукты без наличия", LegendPosition.Right, categories); MessageBox.Show("Документ успешно создан."); } } } } }