PIbd-31_Pyatakov_Kirill_COP/COP3_/PluginsConvention.cs
2024-12-13 05:34:43 +03:00

232 lines
8.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Plugins;
using Contracts.BusinessLogicContracts;
using WinFormsLibrary1;
using Contracts.ViewModels;
using Contracts.BindingModels;
using UserComponentsOption19;
using Components;
using DataBaseImplements.Implements;
using BusinessLogic;
using WinFormsLibrary1.HelperClasses;
namespace COP3_
{
public class PluginsConvention : IPluginsConvention
{
private readonly IOrderLogic _orderLogic;
private readonly ICityStatusLogic _cityLogic;
private readonly ListComponent _list;
private readonly PDFTable _pdfTable;
private readonly ComponentExcelTableWithColumnHeader _excelTable;
private readonly DiagramWordNoVisibleComponent _wordDiagram;
public string PluginName { get; set; } = "Orders";
public UserControl GetControl
{
get { return _list; }
}
public PluginsConvention()
{
_orderLogic = new OrderLogic(new OrderStorage());
_cityLogic = new CityLogic(new CityStorage());
_pdfTable = new();
_excelTable = new();
_wordDiagram = new();
_list = new();
}
public PluginsConventionElement GetElement
{
get
{
int Id = _list.GetObjectFromSelectedRow<OrderViewModel>()!.Id;
byte[] bytes = new byte[16];
BitConverter.GetBytes(Id).CopyTo(bytes, 0);
Guid guid = new Guid(bytes);
return new PluginsConventionElement() { Id = guid };
}
}
public Form GetForm(PluginsConventionElement element)
{
if (element == null)
{
return new FormMain(_orderLogic, _cityLogic);
}
else
{
int id = element.Id.GetHashCode();
int? idl = id;
OrderViewModel model = _orderLogic.ReadElement(new Contracts.SearchModels.OrderSearchModel { Id = idl });
FormEdit form = new FormEdit(_orderLogic, _cityLogic);
form.Id = element.Id.GetHashCode();
return form;
}
}
public Form GetThesaurus()
{
return new FormGuide(_cityLogic);
}
public bool DeleteElement(PluginsConventionElement element)
{
_orderLogic.Delete(
new OrderBindingModel { Id = element.Id.GetHashCode() }
);
return true;
}
public void ReloadData()
{
try
{
var orders = _orderLogic.ReadList(null);
_list.ClearRows();
_list.FillTemplateString("Заказ: (OrderDestination), Индентфикатор: (Id), ФИО: (FIO), Дата доставки: (OrderDeliveryTime)", "(", ")");
foreach (var order in orders)
{
_list.AddObjectToListBox(order);
}
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
string filePath = saveDocument.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);
_pdfTable.GeneratePdf(pdfdata);
MessageBox.Show("Файл успешно создан.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
else return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
string filePath = saveDocument.FileName;
try
{
var orders = _orderLogic.ReadList(null);
if (orders == null || !orders.Any())
{
MessageBox.Show("Нет данных");
return false;
}
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, "Заказ")
};
_excelTable.GenerateExcelFile(filePath, "Excel", mergeCellsInfo, orders, headers);
MessageBox.Show("Файл успешно создан.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при создании файла: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
string filePath = saveDocument.FileName;
string fileTitle = "Информация о заказах";
try
{
var orders = _orderLogic.ReadList(null);
if (orders == null || !orders.Any())
{
MessageBox.Show("Нет данных");
return false;
}
List<UserComponentsOption19.DiagramWordNoVisibleComponent.ChartSeries> tables = new List<DiagramWordNoVisibleComponent.ChartSeries>();
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(),
}
);
}
_wordDiagram.CreateDocumentWithChart(filePath, fileTitle, "Заказы", UserComponentsOption19.DiagramWordNoVisibleComponent.LegendPosition.Bottom, tables, (from delivery in deliveryTime select delivery.ToString()).ToList());
MessageBox.Show("Файл успешно создан.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при создании файла: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
}