COP3PLUSSagirov/DeliveryApp/Form1.cs
2024-11-06 15:40:33 +04:00

340 lines
12 KiB
C#

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<DeliveryViewModel>();
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<DeliveryViewModel>();
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<ColumnInfo> columns = new List<ColumnInfo>()
{
new ColumnInfo("Id","Id",50),
new ColumnInfo("CourierFIO","ÔÈÎ",100),
new ColumnInfo("Phone","Òåëåôîí",100),
new ColumnInfo("Type","Òèï",75),
};
List<Controls.Models.MergeCells> mergeCells = new List<Controls.Models.MergeCells>()
{
new Controls.Models.MergeCells("Äîñòàâêà", new int[] {0,3,4}),
};
try
{
var list = _deliveryLogic.ReadList(null);
if (list == null)
{
return;
}
List<Fix> dm = new List<Fix>();
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<Tuple<string, DocImage[]>> prg = new List<Tuple<string, DocImage[]>>();
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<string, DocImage[]>($"Äîñòàâêà íîìåð:{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<string, List<(string Name, double Value)>> data = new Dictionary<string, List<(string Name, double Value)>>();
if (list == null)
{
return;
}
List<(string Name, double Value)> smth = new List<(string Name, double Value)>();
Dictionary<string, double> dt = new Dictionary<string, double>();
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();
}
}
}