Реализовал класс для подключения плагина

This commit is contained in:
Никита Потапов 2024-12-15 01:39:07 +04:00
parent b8f938b6e6
commit 858b0bc76b
3 changed files with 206 additions and 20 deletions

View File

@ -1,48 +1,231 @@
using PluginConventions;
using Components;
using InternetShopContracts.DataSearchModels;
using InternetShopContracts.DataViewModels;
using InternetShopContracts.LogicsContracts;
using InternetShopDatabase.Storages;
using InternetShopForms.Orders;
using InternetShopForms.Products;
using InternetShopLogics.Logics;
using PluginConventions;
using UserComponentsOption19;
using WinFormsLibrary1;
using WinFormsLibrary1.HelperClasses;
namespace InternetShopForms.Plugins
{
public class PluginsConvention : IPluginsConvention
{
public string PluginName => throw new NotImplementedException();
private readonly IOrderLogic _orderLogic;
private readonly IProductLogic _productLogic;
private readonly TableComponent _componentTable = new();
private readonly PDFHistogram _componentPDF = new();
private readonly ComponentExcelWithImage _componentExcel = new();
private readonly TableWordNoVisibleComponent _componentWord = new();
public string PluginName => "PluginLab3";
public UserControl GetControl => throw new NotImplementedException();
public UserControl GetControl => _componentTable;
public PluginsConventionElement GetElement => throw new NotImplementedException();
public PluginsConvention()
{
_orderLogic = new OrderLogic(new OrderStorage());
_productLogic = new ProductLogic(new ProductStorage());
ReloadData();
}
public PluginsConventionElement GetElement
{
get
{
var selected = _componentTable.GetSelectedObject<OrderViewModel>();
if (selected == null) throw new Exception("Не удалось получить выбранный элемент");
byte[] bytes = new byte[16];
BitConverter.GetBytes(selected.Id).CopyTo(bytes, 0);
return new PluginsConventionOrder()
{
Id = new Guid(bytes),
CustomerFIO = selected.CustomerFIO,
CustomerEmail = selected.CustomerEmail,
ProductNames = selected.ProductNames,
};
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
throw new NotImplementedException();
string? exportFileName = saveDocument.FileName;
var orders = _orderLogic.ReadList();
var products = _productLogic.ReadList();
Dictionary<string, double> productsStat = new Dictionary<string, double>();
foreach (var order in orders)
{
foreach (var product in order.ProductNames)
{
if (productsStat.ContainsKey(product))
{
productsStat[product]++;
}
else
{
productsStat.Add(product, 1);
}
}
}
foreach (var product in products)
{
if (!productsStat.ContainsKey(product.Name))
{
productsStat.Add(product.Name, 0);
}
}
List<ChartData> charts = new List<ChartData>();
foreach (var product in productsStat.Keys)
{
ChartData chartData = new ChartData();
Dictionary<string, double> data = new Dictionary<string, double>();
data.Add(product, productsStat[product]);
chartData.SeriesName = product;
chartData.Data = data;
charts.Add(chartData);
}
try
{
_componentPDF.CreateHistogramPdf(
exportFileName,
"Отчет по товарам",
"Количество заказов",
OxyPlot.Legends.LegendPosition.RightTop,
charts
);
//MessageBox.Show("Отчет успешно сформирован", "Создание отчета", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
//MessageBox.Show("Произошла ошибка при создании отчета:\n" + ex.Message, "Создание отчета", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
throw new NotImplementedException();
string? exportFileName = saveDocument.FileName;
var orders = _orderLogic.ReadList();
try
{
string[] headerRow1 = {
"ID",
"Личные данные",
"Личные данные",
"Товары",
};
string[] headerRow2 = {
"ID",
"ФИО",
"Email",
"Товары",
};
List<(float columnWidth, string headerRowCell1, string headerRowCell2, string property)> columnWidths =
[ (3.0f, "ID", "ID", "Id"),
(5.0f, "Личные данные", "ФИО", "CustomerFIO"),
(5.0f, "Личные данные", "Email", "CustomerEmail"),
(5.0f, "Товары", "Товары", "ProductsString"),
];
var mergeColumns = new List<(int StartColumn, int EndColumn)> { (1, 2) };
var columnPropertyMapping = new Dictionary<int, string>
{
{ 0, "Id" },
{ 1, "CustomerFIO" },
{ 2, "CustomerEmail" },
{ 3, "ProductsString" },
};
_componentWord.CreateTableInWordDocument(
exportFileName,
"Отчет по заказам",
mergeColumns,
columnWidths,
orders
);
//MessageBox.Show("Отчет успешно сформирован", "Создание отчета", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
//MessageBox.Show("Произошла ошибка при создании отчета:\n" + ex.Message, "Создание отчета", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
throw new NotImplementedException();
string? exportFileName = saveDocument.FileName;
var orders = _orderLogic.ReadList();
try
{
_componentExcel.CreateExcelWithImages(exportFileName, "Заказы", orders.Select(x => x.ImagePath).ToArray());
//MessageBox.Show("Отчет успешно сформирован", "Создание отчета", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
//MessageBox.Show("Произошла ошибка при создании отчета:\n" + ex.Message, "Создание отчета", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool DeleteElement(PluginsConventionElement element)
{
throw new NotImplementedException();
return _orderLogic.Delete(new OrderSearchModel
{
Id = element.Id.GetHashCode()
});
}
public Form GetForm(PluginsConventionElement element)
{
throw new NotImplementedException();
var form = new FormOrderEdit(_orderLogic, _productLogic);
if (element != null)
{
form.OrderId = element.Id.GetHashCode();
}
return form;
}
public Form GetThesaurus()
{
throw new NotImplementedException();
return new FormProductsList(_productLogic);
}
public void ReloadData()
{
throw new NotImplementedException();
List<(string, string, float)> configureColumns = [
("ID", "Id", 1.0f),
("ФИО заказчика", "CustomerFIO", 1.0f),
("Продукты", "ProductsString", 1.0f),
("Email заказчика", "CustomerEmail", 1.0f),
];
_componentTable.ConfigureColumns(configureColumns);
_componentTable.dataGridView1.AllowUserToDeleteRows = false;
_componentTable.dataGridView1.Columns[0].Visible = false;
try
{
var orders = _orderLogic.ReadList();
_componentTable.FillData(orders);
}
catch (Exception ex)
{
MessageBox.Show("Произошла ошибка при загрузке данных:\n" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@ -0,0 +1,12 @@
using PluginConventions;
namespace InternetShopForms.Plugins
{
public class PluginsConventionOrder : PluginsConventionElement
{
public string CustomerFIO { get; set; } = string.Empty;
public string CustomerEmail { get; set; } = string.Empty;
public string ImagePath { get; set; } = string.Empty;
public List<string> ProductNames { get; set; } = new List<string>();
}
}

View File

@ -1,9 +0,0 @@
using PluginConventions;
namespace InternetShopForms.Plugins
{
public class PluginsConventionProvider : PluginsConventionElement
{
}
}