using ComponentsLibraryNet60.Core; using ComponentsLibraryNet60.Models; using Contracts.StoragesContracts; using Contracts.ViewModels; using ControlsLibraryNet60.Models; using DataBaseImplement.Implements; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; 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; using System.Windows.Forms.VisualStyles; using PdfFormsLibrary; using PdfFormsLibrary.Helpers; using WinFormsLibrary.SupportClasses; using WinFormsLibrary.SupportClasses.Enums; using DataBaseImplement.Models; using WinFormsLibrary.not_visual; namespace AppView { public partial class FormMain : Form { private readonly IProviderStorage _providerStorage; private readonly ITypeStorage _typeStorage; public FormMain( IProviderStorage providerStorage, ITypeStorage typeStorage ) { _providerStorage = providerStorage; _typeStorage = typeStorage; InitializeComponent(); KeyDown += new KeyEventHandler(FormMain_KeyDown); } private void FormMain_Load( object sender, EventArgs e ) { LoadData(); } private void LoadData() { try { var providers = _providerStorage.GetFullList(); if (providers != null) { controlDataTreeCell.Clear(); TreeColumnConfiguration(); if (providers.Count > 0) AddTreeData(providers); } } catch (Exception ex) { MessageBox.Show( ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } private void TreeColumnConfiguration() { DataTreeNodeConfig treeConfig = new(); treeConfig.NodeNames = new(); treeConfig.NodeNames.Enqueue("Type"); treeConfig.NodeNames.Enqueue("SupplyDateTime"); treeConfig.NodeNames.Enqueue("Id"); treeConfig.NodeNames.Enqueue("Name"); controlDataTreeCell.LoadConfig(treeConfig); } private void AddTreeData(List providers) { int numOfProperties = typeof(ProviderViewModel).GetProperties().Length; for (int i = 0; i < providers.Count; ++i) { providers[i].SupplyDateTime = providers[i].SupplyDate.ToString(); for (int j = 0; j < numOfProperties; ++j) { controlDataTreeCell.AddCell(j, providers[i]); } } } private void AddProviderItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormProvider)); if (service is FormProvider form) { form.ShowDialog(); LoadData(); } } private void EditProviderItem_Click(object sender, EventArgs e) { if (controlDataTreeCell.GetSelectedObject() == null) { return; } else { var service = Program.ServiceProvider?.GetService(typeof(FormProvider)); if (service is FormProvider form) { form.Id = Convert.ToInt32(controlDataTreeCell.GetSelectedObject()?.Id); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } } private void RemoveProviderItem_Click(object sender, EventArgs e) { if (controlDataTreeCell.GetSelectedObject() == null) return; if (MessageBox.Show( "Вы хотите удалить выбранный элементы?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _providerStorage.Delete(new(controlDataTreeCell.GetSelectedObject())); LoadData(); } } private void GetSimpleDocumentItem_Click(object sender, EventArgs e) { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); SaveFileDialog saveFileDialog = new() { Filter = "PDF Files|*.pdf" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string filePath = saveFileDialog.FileName; string documentTitle = "Поставщики"; List textData = new(); foreach (var provider in _providerStorage.GetFullList()) { textData.Add(provider.Name + ": " + provider.Furniture); } try { pdfGeneratorControl.GeneratePdf(filePath, documentTitle, textData); MessageBox.Show("PDF-документ успешно сохранен.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show( $"Ошибка при создании PDF-документа: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } } private void GetTableDocumentItem_Click(object sender, EventArgs e) { List providersList = _providerStorage.GetFullList(); foreach (var provider in providersList) { if (provider.SupplyDate == null) { provider.SupplyDateTime = "Поставок не было"; provider.SupplyDate = DateTime.MinValue; continue; } provider.SupplyDateTime = provider.SupplyDate.ToString(); } var columnConfigs = new List { new ColumnConfig { Width = 50f, PropertyName = "Id" }, new ColumnConfig { Width = 50f, PropertyName = "Name" }, new ColumnConfig { Width = 50f, PropertyName = "Type" }, new ColumnConfig { Width = 70f, PropertyName = "SupplyDateTime" }, }; SaveFileDialog saveFileDialog = new() { Filter = "Excel Files|*.xlsx" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { componentWithSettings.GenerateExcelDocument(saveFileDialog.FileName, "Отчет по всем поставщикам", columnConfigs, 25f, 35f, providersList); MessageBox.Show( "Excel-документ успешно сохранен.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information ); } catch (Exception ex) { MessageBox.Show( $"Ошибка при создании Excel-документа: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } } private void GetDiagramDocumentItem_Click(object sender, EventArgs e) { var providersList = _providerStorage .GetFullList() .Where(item => item.SupplyDate?.Year == DateTime.Now.Year) .GroupBy(item => item.Type) .Select(group => new { Type = group.Key, Date = group.Select(item => item.SupplyDate), Count = (double)group.Count(), }); List results = new(); List names = new(); foreach (var provider in providersList) { results.Add(provider.Count); names.Add(provider.Type); } SaveFileDialog saveFileDialog = new() { Filter = "Word Files|*.docx" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { SimpleCircleDiagram simpleCircleDiagram = new(saveFileDialog.FileName, "Круговая диаграмма", "Количество поставщиков в разрезе типа организации", EnumAreaLegend.Right, new() { new DataCircleDiagram("Типы организации", results.ToArray()) }); simpleCircleDiagram.NameData = names.ToArray(); try { //componentDocumentWithChartPieWord.CreateDoc(config); circleDiagram.AddCircleDiagram(simpleCircleDiagram); MessageBox.Show( "Word-документ успешно сохранен.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information ); } catch (Exception ex) { MessageBox.Show( $"Ошибка при создании Word-документа: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } } private void FormMain_KeyDown(object sender, KeyEventArgs e) { if (e.Control) { switch (e.KeyCode) { case Keys.A: AddProviderItem_Click(sender, e); break; case Keys.U: EditProviderItem_Click(sender, e); break; case Keys.D: RemoveProviderItem_Click(sender, e); break; case Keys.S: GetSimpleDocumentItem_Click(sender, e); break; case Keys.T: GetTableDocumentItem_Click(sender, e); break; case Keys.C: GetDiagramDocumentItem_Click(sender, e); break; case Keys.M: OpenListToolStripMenuItem_Click(sender, e); break; } } } private void OpenListToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormType)); if (service is FormType form) { form.ShowDialog(); LoadData(); } } } }