265 lines
10 KiB
C#
Raw Normal View History

2024-11-07 14:19:38 +04:00
using COP;
using COP.Info;
using PluginsConventionLibrary.Forms;
using PluginsConventionLibrary.Plugins;
using System.ComponentModel.Composition;
using ShopBusinessLogic.BusinessLogics;
using ShopContracts.BindingModels;
using ShopContracts.BusinessLogicsContracts;
using static COP.ExcelComponent;
using Lab1.LogicalComponents.SupportClasses;
using LegendPosition = KOP.PDFComponents.Enums.LegendPosition;
using KOP.Addition;
using static KOP.PDFComponents.OfficePackage.PdfRowParameters;
using Aspose.Words.XAttr;
using KOP.PDFComponents.OfficePackage;
using KOP.PDFComponents;
namespace PluginsConventionLibrary
{
[Export(typeof(IPluginsConvention))]
public class MainPluginConvention : IPluginsConvention
{
private KOP.MyTable tableOfValues;
private readonly ICustomerLogic _customerLogic;
private readonly IProductCategoryLogic _productCategoryLogic;
public MainPluginConvention()
{
_customerLogic = new CustomerLogic();
_productCategoryLogic = new ProductCategoryLogic();
tableOfValues = new KOP.MyTable();
var menu = new ContextMenuStrip();
var directionMenuItem = new ToolStripMenuItem("Категория товаров");
menu.Items.Add(directionMenuItem);
directionMenuItem.Click += (sender, e) =>
{
var formDirection = new FormProductCategory(_productCategoryLogic);
formDirection.ShowDialog();
};
tableOfValues.ContextMenuStrip = menu;
ReloadData();
}
public void ReloadData()
{
try
{
var lst = new List<ColumnInfo>();
ColumnInfo ci1 = new ColumnInfo("ID", 100, false, "Id");
ColumnInfo ci2 = new ColumnInfo("FIO", 100, true, "ФИО");
ColumnInfo ci3 = new ColumnInfo("ProductCategoryName", 80, true, "Категория товара");
ColumnInfo ci5 = new ColumnInfo("PhotoFilePath", 80, false, "PhotoFilePath");
ColumnInfo ci4 = new ColumnInfo("Email", 80, true, "Электронная почта");
lst.Add(ci1);
lst.Add(ci2);
lst.Add(ci3);
lst.Add(ci4);
lst.Add(ci5);
tableOfValues.ColumnConfiguration(new TableConfiguration(lst));
tableOfValues.ClearRows();
var list = _customerLogic.Read(null);
if (list != null)
{
tableOfValues.AddRows(list);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// Название плагина
string IPluginsConvention.PluginName => PluginName();
public string PluginName()
{
return "Клиенты";
}
public UserControl GetControl => tableOfValues;
PluginsConventionElement IPluginsConvention.GetElement => GetElement();
public PluginsConventionElement GetElement()
{
tableOfValues.IndexProperty = tableOfValues.myDataGridView.CurrentRow.Index;
var student = tableOfValues.GetUser<MainPluginConventionElement>(); ;
MainPluginConventionElement element = null;
if (tableOfValues != null)
{
element = new MainPluginConventionElement
{
Id = student.Id,
FIO = student.FIO,
PhotoFilePath = student.PhotoFilePath,
Email = student.Email,
ProductCategoryName = student.ProductCategoryName,
};
}
return (new PluginsConventionElement { Id = element.Id });
}
public Form GetForm(PluginsConventionElement element)
{
var formOrder = new FormCustomer(_customerLogic, _productCategoryLogic);
if (element != null)
{
formOrder.Id = element.Id;
}
return formOrder;
}
public bool DeleteElement(PluginsConventionElement element)
{
try
{
_customerLogic.Delete(new CustomerBindingModel { Id = element.Id });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
ExcelComponent excelComponent = new();
var list = _customerLogic.Read(null);
List<ImageInfo> images = new();
using var dialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
if (list != null)
{
foreach (var item in list)
{
images.Add(new ImageInfo() { FilePath = item.PhotoFilePath });
}
}
ExcelImageInfo info = new(dialog.FileName, "Документ с фотографиями студентов", images);
excelComponent.GenerateExcelWithImages(info);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return true;
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
Lab1.LogicalComponents.TableDocument componentWord = new();
var list = _customerLogic.Read(null);
List<CustomerBindingModel> data = new();
List<int[]> mergedColumns = new()
{
new int[] { 1, 2 }
};
List<ColumnDefinition> columnDefinitions = new()
{
new ColumnDefinition { Header = "ID", PropertyName = "Id", Weight = 11 },
new ColumnDefinition { Header = "Личные данные", PropertyName = "PersonalData", Weight = 11 },
new ColumnDefinition { Header = "Личные данные", PropertyName = "PersonalData1", Weight = 11 },
new ColumnDefinition { Header = "Категория товаров", PropertyName = "ProductCategoryName", Weight = 21 }
};
List<ColumnDefinition> columnDefinitions2 = new()
{
new ColumnDefinition { Header = "ID", PropertyName = "Id", Weight = 11 },
new ColumnDefinition { Header = "ФИО", PropertyName = "FIO", Weight = 11 },
new ColumnDefinition { Header = "Электронная почта", PropertyName = "Email", Weight = 70 },
new ColumnDefinition { Header = "Категория товаров", PropertyName = "ProductCategoryName", Weight = 21 }
};
using var dialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
if (list != null)
{
foreach (var item in list)
{
data.Add(new CustomerBindingModel() { Id = item.Id, FIO = item.FIO, Email = item.Email, ProductCategoryName = item.ProductCategoryName });
}
}
TableDocumentInfo<CustomerBindingModel> tableWord = new(dialog.FileName, "Таблица с клиентами", columnDefinitions, columnDefinitions2, data, mergedColumns);
Lab1.LogicalComponents.TableDocument.CreateTable(tableWord);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return true;
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
KOP.PDFComponents.PdfChartComponent gistogramPdfComponent = new();
var listStudents = _customerLogic.Read(null);
var listDirections = _productCategoryLogic.Read(null);
List<(string, int)> data = new();
List<PdfChartComponent> gistData = new();
var Data = new Dictionary<string, int[]>();
using var dialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
for (int i = 0; i < listDirections.Count; i++)
{
int count = 0;
for (int j = 0; j < listStudents.Count; j++)
{
string[] dirs = listStudents[j].ProductCategoryName.Split(";");
foreach (var dir in dirs)
{
if (dir == listDirections[i].Name) count++;
}
}
data.Add((listDirections[i].Name, count));
}
if (data != null)
{
foreach (var item in data)
{
Data.Add(item.Item1, new[] {item.Item2 });
}
}
gistogramPdfComponent.CreateDocument(dialog.FileName, "Histogram", "Customers-ProductCategorys", LegendPosition.Right, Data);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return true;
}
}
}