270 lines
10 KiB
C#

using Components;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using DocumentFormat.OpenXml.Drawing.Diagrams;
using UserComponentsOption19;
using WinFormsLibrary1.HelperClasses;
namespace COP3_
{
public partial class FormMain : Form
{
private readonly IOrderLogic _orderLogic;
private readonly ICityStatusLogic _cityLogic;
public FormMain(IOrderLogic orderLogic, ICityStatusLogic cityStatusLogic)
{
InitializeComponent();
_orderLogic = orderLogic;
_cityLogic = cityStatusLogic;
this.KeyPreview = true;
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var orders = _orderLogic.ReadList(null);
listComponent1.ClearRows();
listComponent1.FillTemplateString("Çàêàç: (OrderDestination), Èíäåíòôèêàòîð: (Id), ÔÈÎ: (FIO), Äàòà äîñòàâêè: (OrderDeliveryTime)", "(", ")");
foreach (var order in orders)
{
listComponent1.AddObjectToListBox(order);
}
}
catch (Exception ex)
{
MessageBox.Show($"{ex.Message}");
}
}
private void Create(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormEdit));
if (!(service is FormEdit form))
{
return;
}
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void Open(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormEdit));
if (!(service is FormEdit form))
{
return;
}
var selectedOrder = listComponent1.GetObjectFromSelectedRow<OrderBindingModel>();
form.Id = Convert.ToInt32(selectedOrder.Id);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void Delete(object sender, EventArgs e)
{
try
{
var selectedOrder = listComponent1.GetObjectFromSelectedRow<OrderBindingModel>();
if (selectedOrder != null)
{
var result = MessageBox.Show(
"Ïîäòâåðäèòå óäàëåíèå çàïèñè",
"Ïîäòâåðæäåíèå",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
);
if (result == DialogResult.Yes)
{
_orderLogic.Delete(new OrderBindingModel { Id = selectedOrder.Id });
MessageBox.Show("Óäàëåíî");
LoadData();
}
else if (result == DialogResult.No)
{
MessageBox.Show("Óäàëåíèå îòìåíåíî", "Èíôîðìàöèÿ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
LoadData();
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CreateDoc(object sender, EventArgs e)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "PDF Documents (*.pdf)|*.pdf";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName;
string title = "Ïðîäâèæåíèå çàêàçà";
try
{
var orders = _orderLogic.ReadList(null);
var cities = _cityLogic.ReadList(null);
if (orders != null)
{
List<string[,]> tables = new List<string[,]>();
string[,] var = new string[7, orders.Count];
for (int i = 0; i < orders.Count; i++)
{
var[0,i] = orders[i].Id.ToString();
for(int j = 1; j < 7; j++)
{
if(j <= orders[i].OrderPath.Length)
var[j,i] = orders[i].OrderPath[j-1];
else var[j,i] = string.Empty;
}
}
tables.Add(var);
PdfDocumentData pdfdata = new PdfDocumentData(filePath, title, tables);
pdfTable1.GeneratePdf(pdfdata);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
private void CreateTableDoc(object sender, EventArgs e)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "Excel Files (*.xlsx)|*.xlsx";
saveFileDialog.Title = "Ñîõðàíèòü òàáëèöó â Excel";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName;
try
{
var orders = _orderLogic.ReadList(null);
if (orders == null || !orders.Any())
{
MessageBox.Show("Íåò äàííûõ");
return;
}
List<(string title, string propertyName, float height)> headers = new List<(string title, string propertyName, float height)>
{
("ID","Id",20),
("ÔÈÎ", "FIO",40),
("Ïóíêò íàçíà÷åíèÿ", "OrderDestination",40),
("Äàòà äîñòàâêè", "OrderDeliveryTime",40)
};
List<(int StartRow, int EndRow, int StartCol, int EndCol, string title)> mergeCellsInfo = new List<(int StartRow, int EndRow, int StartCol, int EndCol, string title)>
{
(3, 4, 1, 1, "Çàêàç")
};
componentExcelTableWithColumnHeader1.GenerateExcelFile(filePath,"Excel",mergeCellsInfo,orders,headers);
MessageBox.Show("Ôàéë óñïåøíî ñîçäàí.", "Èíôîðìàöèÿ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà ïðè ñîçäàíèè ôàéëà: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void CreateDiagramDoc(object sender, EventArgs e)
{
using (SaveFileDialog fileDialog = new SaveFileDialog())
{
fileDialog.Filter = "Word Files (*.docx)|*.docx";
fileDialog.Title = "Ñîõðàíèòü äèàãðàììó â Word";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = fileDialog.FileName;
string fileTitle = "Èíôîðìàöèÿ î çàêàçàõ";
try
{
var orders = _orderLogic.ReadList(null);
if (orders == null || !orders.Any())
{
MessageBox.Show("Íåò äàííûõ");
return;
}
List<UserComponentsOption19.DiagramWordNoVisibleComponent.ChartSeries> tables = new List<DiagramWordNoVisibleComponent.ChartSeries>();
//var destinations = (from order in orders group order.OrderDeliveryTime by order.OrderDestination).Distinct().ToList() ;
var deliveryTime = (from order in orders select order.OrderDeliveryTime).Distinct().ToArray();
var destinations = (from order in orders select order.OrderDestination).Distinct().ToList();
foreach(var item in destinations)
{
double[] ints = new double[deliveryTime.Length];
for (int i = 0; i < deliveryTime.Length; i++)
{
ints[i] = (from order in orders where order.OrderDeliveryTime == deliveryTime[i] && order.OrderDestination == item select order).Count();
}
tables.Add(
new DiagramWordNoVisibleComponent.ChartSeries
{
Name = item,
Data = ints.ToList(),
}
);
}
diagramWordNoVisibleComponent1.CreateDocumentWithChart(filePath, fileTitle, "Çàêàçû", UserComponentsOption19.DiagramWordNoVisibleComponent.LegendPosition.Bottom, tables);
MessageBox.Show("Ôàéë óñïåøíî ñîçäàí.", "Èíôîðìàöèÿ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà ïðè ñîçäàíèè ôàéëà: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void ñïðàâî÷íèêToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormGuide));
if (!(service is FormGuide form))
{
return;
}
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}