using Contracts.BindingModels; using Contracts.BuisnessLogicsContracts; using Contracts.ViewModels; using Controls.Models; using CreateVisualComponent; using CustomComponents.NonViewComponents.SupportClasses; using DatabaseImplements.Models; using DataModels.Models; using DocumentFormat.OpenXml.Spreadsheet; using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; using NotVisualComponent.Models; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word; namespace DeliveryApp { public partial class Form1 : Form { private readonly IDeliveryLogic _deliveryLogic; private readonly ITypeLogic _typeLogic; public Form1(IDeliveryLogic deliveryLogic, ITypeLogic typeLogic) { _deliveryLogic = deliveryLogic; _typeLogic = typeLogic; InitializeComponent(); ColumnsConfiguratoin columnsConfiguratoin = new ColumnsConfiguratoin(); columnsConfiguratoin.Columns.Add(new ColumnConfig { ColumnName = "Id", Width = 10, Visible = false, PropertyObject = "Id" }); columnsConfiguratoin.Columns.Add(new ColumnConfig { ColumnName = "ФИО", Width = 400, Visible = true, PropertyObject = "CourierFIO" }); columnsConfiguratoin.Columns.Add(new ColumnConfig { ColumnName = "Тип посылки", Width = 250, Visible = true, PropertyObject = "Type" }); columnsConfiguratoin.Columns.Add(new ColumnConfig { ColumnName = "Телефон", Width = 200, Visible = true, PropertyObject = "Phone" }); listOutputComponent1.ConfigColumn(columnsConfiguratoin); } private void Form1_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { listOutputComponent1.ClearDataGrid(); try { var deliverys = _deliveryLogic.ReadList(null); if (deliverys == null) { return; } int k = -1; foreach (var delivery in deliverys) { k++; for (int i = 0; i < 4; i++) { listOutputComponent1.AddItem(delivery, k, i); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void AddElement() { var service = Program.ServiceProvider?.GetService(typeof(FormDelivery)); if (!(service is FormDelivery form)) { return; } if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } private void UpdateElement() { var service = Program.ServiceProvider?.GetService(typeof(FormDelivery)); if (service is FormDelivery form) { var selectedDelivery = listOutputComponent1.GetSelectedObjectInRow(); if (selectedDelivery == null) { MessageBox.Show("Ошибка при редактировании!", "Вы не выбрали элемент", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } form.Id = selectedDelivery.Id; if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } private void DeleteElement() { if (MessageBox.Show("Вы уверенны?", "Удалить", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } var selectedDelivery = listOutputComponent1.GetSelectedObjectInRow(); int id = Convert.ToInt32(selectedDelivery.Id); try { _deliveryLogic.Delete(new DeliveryBindingModel { Id = id }); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadData(); } private void toolStripType_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormType)); if (!(service is FormType form)) { return; } if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } private void toolStripDelivery_Click(object sender, EventArgs e) { AddElement(); } private void toolStripPdf_Click(object sender, EventArgs e) { CreatePDF(); } private void CreatePDF() { Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); using var dialog = new SaveFileDialog { Filter = "pdf|*.pdf" }; if (dialog.ShowDialog() == DialogResult.OK) { pdfRenderer.PdfDocument.Save(dialog.FileName); List columns = new List() { new ColumnInfo("Id","Id",50), new ColumnInfo("CourierFIO","ФИО",100), new ColumnInfo("Phone","Телефон",100), new ColumnInfo("Type","Тип",75), }; List mergeCells = new List() { new Controls.Models.MergeCells("Доставка", new int[] {0,3,4}), }; try { var list = _deliveryLogic.ReadList(null); if (list == null) { return; } List dm = new List(); foreach (var cell in list) { dm.Add( new Fix() { Phone = cell.Phone, Id = cell.Id, CourierFIO = cell.CourierFIO, Type = cell.Type, }); } tableComponent1.CreateTable(dialog.FileName, "Таблица", mergeCells, columns, dm); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при формировании пдф", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void OnKeyPressed(object sender, KeyEventArgs e) { if (e.Control) { switch (e.KeyCode) { case Keys.A: AddElement(); break; case Keys.U: UpdateElement(); break; case Keys.D: DeleteElement(); break; case Keys.S: CreateWord(); break; case Keys.T: CreatePDF(); break; case Keys.C: CreateExcel(); break; default: break; } } } private void toolStripWord_Click(object sender, EventArgs e) { CreateWord(); } private void CreateWord() { var wordApp = new Word.Application(); var document = wordApp.Documents.Add(); using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; List> prg = new List>(); if (dialog.ShowDialog() == DialogResult.OK) { try { var list = _deliveryLogic.ReadList(null); if (list == null) { return; } foreach (var cell in list) { var paragraph = document.Paragraphs.Add(); paragraph.Range.Text = $"Доставка номер:{cell.Id}. Тип:{cell.Type}"; paragraph.Range.Font.Size = 24; paragraph.Range.Font.Bold = 1; paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph.Range.InsertParagraphAfter(); prg.Add(new Tuple($"Доставка номер:{cell.Id}. Тип:{cell.Type}", new DocImage[] { new DocImage { Path = cell.Image, Height = 200, Width = 200 } })); } document.SaveAs2(dialog.FileName); wordApp.Quit(); foreach (var cell in prg) { imageWord1.AddImages(dialog.FileName, cell.Item1, cell.Item2); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при формировании docx", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void toolStripExcel_Click(object sender, EventArgs e) { CreateExcel(); } private void CreateExcel() { using var dialog = new SaveFileDialog { Filter = "xlsx|*.xlsx" }; if (dialog.ShowDialog() == DialogResult.OK) { var list = _deliveryLogic.ReadList(null); Dictionary> data = new Dictionary>(); if (list == null) { return; } List<(string Name, double Value)> smth = new List<(string Name, double Value)>(); Dictionary dt = new Dictionary(); foreach (var cell in list) { if (dt.ContainsKey(cell.Type)) { dt[cell.Type]++; continue; } dt.Add(cell.Type, 1); } foreach (var f in dt) { smth.Add(new(f.Key, f.Value)); } data.Add("Series 1", smth); ChartConfig conf = new ChartConfig { ChartTitle = "Отчет по типам", FilePath = dialog.FileName, Header = "Chart", LegendLocation = NotVisualComponent.Models.Location.Top, Data = data }; excelDiagram1.CreateDoc(conf); } } private void добавитьToolStripMenuItem_Click(object sender, EventArgs e) { AddElement(); } private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e) { UpdateElement(); } private void удалитьToolStripMenuItem_Click(object sender, EventArgs e) { DeleteElement(); } } }