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(); form.Id = Convert.ToInt32(selectedOrder.Id); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } private void Delete(object sender, EventArgs e) { try { var selectedOrder = listComponent1.GetObjectFromSelectedRow(); 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 tables = new List(); 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 tables = new List(); var deliveryTime = (from order in orders select order.OrderDeliveryTime).Distinct().Order().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, (from delivery in deliveryTime select delivery.ToString()).ToList()); 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(); } } } }