COP3PLUSSagirov/DeliveryApp/Form1.cs

340 lines
12 KiB
C#
Raw Normal View History

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 = "<22><><EFBFBD>",
Width = 400,
Visible = true,
PropertyObject = "CourierFIO"
});
columnsConfiguratoin.Columns.Add(new ColumnConfig
{
ColumnName = "<22><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
Width = 250,
Visible = true,
PropertyObject = "Type"
});
columnsConfiguratoin.Columns.Add(new ColumnConfig
{
ColumnName = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
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, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!", "<22><> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
form.Id = selectedDelivery.Id;
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void DeleteElement()
{
if (MessageBox.Show("<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?", "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 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, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 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","<22><><EFBFBD>",100),
new ColumnInfo("Phone","<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",100),
new ColumnInfo("Type","<22><><EFBFBD>",75),
};
List<Controls.Models.MergeCells> mergeCells = new List<Controls.Models.MergeCells>()
{
new Controls.Models.MergeCells("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 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, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", mergeCells, columns, dm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>", 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 = $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>:{cell.Id}. <20><><EFBFBD>:{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[]>($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>:{cell.Id}. <20><><EFBFBD>:{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, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 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 = "<22><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>", FilePath = dialog.FileName, Header = "Chart", LegendLocation = NotVisualComponent.Models.Location.Top, Data = data };
excelDiagram1.CreateDoc(conf);
}
}
private void <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddElement();
}
private void <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateElement();
}
private void <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ToolStripMenuItem_Click(object sender, EventArgs e)
{
DeleteElement();
}
}
}