317 lines
11 KiB
C#
317 lines
11 KiB
C#
using Data;
|
||
using Data.Models;
|
||
using Data.Repositories;
|
||
using Library15Gerimovich;
|
||
using Library15Gerimovich.OfficePackage.HelperModels;
|
||
using PluginsConventionLibrary.Plugins;
|
||
using System.Composition;
|
||
using WinFormsLibrary1;
|
||
|
||
namespace PluginsConvention14.MyPlugin
|
||
{
|
||
[Export(typeof(IPluginsConvention))]
|
||
public class MainPluginConvention : IPluginsConvention
|
||
{
|
||
|
||
private OutputTableResults оutputTableResults;
|
||
private readonly IProductRepository _productRepository;
|
||
private readonly IManufacturerRepository _manufacturerRepository;
|
||
public string PluginName { get; set; } = "Products";
|
||
|
||
public MainPluginConvention()
|
||
{
|
||
var dbContext = new ApplicationDbContext();
|
||
_productRepository = new ProductRepository(dbContext);
|
||
_manufacturerRepository = new ManufacturerRepository(dbContext);
|
||
|
||
оutputTableResults = new OutputTableResults();
|
||
var menu = new ContextMenuStrip();
|
||
var skillMenuItem = new ToolStripMenuItem("Forms");
|
||
menu.Items.Add(skillMenuItem);
|
||
skillMenuItem.Click += (sender, e) =>
|
||
{
|
||
var formSkill = new ManufacturerForm(_manufacturerRepository);
|
||
formSkill.ShowDialog();
|
||
};
|
||
оutputTableResults.ContextMenuStrip = menu;
|
||
|
||
InitializeOutputTableResults();
|
||
ReloadData();
|
||
}
|
||
|
||
private void InitializeOutputTableResults()
|
||
{
|
||
оutputTableResults.ConfigureColumns(new List<ColumnInfo>
|
||
{
|
||
new ColumnInfo("", 0, false, "Id"),
|
||
new ColumnInfo("Name", 150, true, "Name"),
|
||
new ColumnInfo("ManufacturerNameManufacturerName", 150, true, "ManufacturerName"),
|
||
new ColumnInfo("DeliveryDate", 50, true, "DeliveryDate"),
|
||
});
|
||
|
||
}
|
||
|
||
/// Название плагина
|
||
/*string IPluginsConvention.PluginName => PluginName();
|
||
public string PluginName()
|
||
{
|
||
return "Products";
|
||
}*/
|
||
|
||
public UserControl GetControl => оutputTableResults;
|
||
|
||
PluginsConventionElement IPluginsConvention.GetElement => GetElement();
|
||
|
||
public PluginsConventionElement GetElement()
|
||
{
|
||
var product = оutputTableResults.GetSelectedObject<Product>();
|
||
MainPluginConventionElement element = null;
|
||
if (оutputTableResults != null)
|
||
{
|
||
element = new MainPluginConventionElement
|
||
{
|
||
Id = product.Id,
|
||
Name = product.Name,
|
||
ManufacturerName = product.ManufacturerName,
|
||
DeliveryDate = product.DeliveryDate.ToString(),
|
||
};
|
||
}
|
||
return (new PluginsConventionElement { Id = element.Id });
|
||
}
|
||
|
||
public Form GetForm(PluginsConventionElement element)
|
||
{
|
||
if (element != null)
|
||
{
|
||
var formOrder = new ProductForm(_productRepository, _manufacturerRepository, _productRepository.GetProductById(Convert.ToInt32(element.Id)));
|
||
return formOrder;
|
||
}
|
||
else
|
||
{
|
||
var formOrder = new ProductForm(_productRepository, _manufacturerRepository);
|
||
return formOrder;
|
||
}
|
||
}
|
||
|
||
public bool DeleteElement(PluginsConventionElement element)
|
||
{
|
||
try
|
||
{
|
||
_productRepository.DeleteProduct(Convert.ToInt32(element.Id));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public void ReloadData()
|
||
{
|
||
var products = _productRepository.GetAllProducts();
|
||
|
||
оutputTableResults.ClearGrid();
|
||
foreach (var product in products)
|
||
{
|
||
оutputTableResults.InsertValue(product);
|
||
}
|
||
}
|
||
public bool CreatePdf(PluginsConventionSaveDocument saveDocument)
|
||
{
|
||
try
|
||
{
|
||
// Получаем изображения из базы данных
|
||
var images = GetImagesFromDatabase();
|
||
|
||
if (images.Count > 0)
|
||
{
|
||
string fileName = "";
|
||
using (var dialog = new SaveFileDialog { Filter = "pdf|*.pdf" })
|
||
{
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
fileName = dialog.FileName.ToString();
|
||
}
|
||
}
|
||
// Создаем компонент PdfImg
|
||
var pdfImg = new Library14Petrushin.PdfImg();
|
||
|
||
// Заголовок документа
|
||
string documentTitle = "Product Images";
|
||
|
||
// Создаем PDF-документ
|
||
pdfImg.CreatePdfDocument(fileName, documentTitle, images);
|
||
|
||
// Уведомление об успешной загрузке
|
||
MessageBox.Show("PDF document created successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private List<Library14Petrushin.ImageData> GetImagesFromDatabase()
|
||
{
|
||
var images = new List<Library14Petrushin.ImageData>();
|
||
var products = _productRepository.GetAllProducts();
|
||
|
||
foreach (var product in products)
|
||
{
|
||
if (product.Image != null)
|
||
{
|
||
// Создаем временный файл для сохранения изображения
|
||
string tempFilePath = Path.GetTempFileName();
|
||
File.WriteAllBytes(tempFilePath, product.Image);
|
||
|
||
// Добавляем путь к временному файлу в список
|
||
images.Add(new Library14Petrushin.ImageData { ImagePath = tempFilePath });
|
||
}
|
||
}
|
||
|
||
return images;
|
||
}
|
||
|
||
public bool CreateExcel(PluginsConventionSaveDocument saveDocument)
|
||
{
|
||
try
|
||
{
|
||
|
||
|
||
// Получаем все продукты из базы данных
|
||
var products = GetAllProducts();
|
||
|
||
if (products.Count > 0)
|
||
{
|
||
string fileName = "";
|
||
using (var dialog = new SaveFileDialog { Filter = "xlsx|*.xlsx" })
|
||
{
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
fileName = dialog.FileName.ToString();
|
||
}
|
||
}
|
||
// Создаем объекты MergeCell и Column
|
||
var mergeCells = new List<WinFormsLibrary1.Models.MergeCell>
|
||
{
|
||
new WinFormsLibrary1.Models.MergeCell("Product", new int[] {1, 2})
|
||
};
|
||
|
||
var columns = new List<WinFormsLibrary1.Models.Column>
|
||
{
|
||
new WinFormsLibrary1.Models.Column("Id", "Id", 10),
|
||
new WinFormsLibrary1.Models.Column("Name", "Name", 20),
|
||
new WinFormsLibrary1.Models.Column("ManufacturerName", "ManufacturerName", 20),
|
||
new WinFormsLibrary1.Models.Column("DeliveryDate", "DeliveryDate", 20)
|
||
};
|
||
|
||
// Создаем компонент ComponentTable
|
||
var componentTable = new ComponentTable();
|
||
|
||
// Конвертируем продукты в представление для таблицы
|
||
var tableItems = MainPluginConventionElement.ConvertProductsToTableItems(products);
|
||
|
||
// Создаем документ Excel
|
||
componentTable.CreateDocument(fileName, "Product Report", mergeCells, columns, tableItems);
|
||
|
||
// Уведомление об успешной загрузке
|
||
MessageBox.Show("Excel document created successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("No products found in the database.");
|
||
}
|
||
}
|
||
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private List<Product> GetAllProducts()
|
||
{
|
||
return _productRepository.GetAllProducts().ToList();
|
||
}
|
||
|
||
public bool CreateWord(PluginsConventionSaveDocument saveDocument)
|
||
{
|
||
try
|
||
{
|
||
|
||
string fileName = "";
|
||
using (var dialog = new SaveFileDialog { Filter = "docx|*.docx" })
|
||
{
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
fileName = dialog.FileName.ToString();
|
||
}
|
||
}
|
||
|
||
// Получаем данные о количестве продукции каждого производителя
|
||
var productCounts = GetProductCountsByManufacturer();
|
||
|
||
if (productCounts.Count > 0)
|
||
{
|
||
// Создаем объект WordDiagramInfo
|
||
var diagramInfo = new WordDiagramInfo
|
||
{
|
||
FileName = fileName,
|
||
Title = "Product Count by Manufacturer",
|
||
ChartTitle = "Product Count",
|
||
LegendLocation = Library15Gerimovich.OfficePackage.HelperEnums.WordDiagramLegendLocation.Top,
|
||
Series = new WordDiagramSeries
|
||
{
|
||
SeriesName = "Product Count",
|
||
Data = productCounts
|
||
}
|
||
};
|
||
|
||
var wordDiagramComponent = new WordDiagramComponent();
|
||
|
||
// Создаем диаграмму в Word
|
||
wordDiagramComponent.CreateDiagram(diagramInfo);
|
||
|
||
// Уведомление об успешной загрузке
|
||
MessageBox.Show("Word document with chart created successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("No products found in the database.");
|
||
}
|
||
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private Dictionary<string, double> GetProductCountsByManufacturer()
|
||
{
|
||
var productCounts = new Dictionary<string, double>();
|
||
var products = _productRepository.GetAllProducts();
|
||
|
||
foreach (var product in products)
|
||
{
|
||
if (productCounts.ContainsKey(product.ManufacturerName))
|
||
{
|
||
productCounts[product.ManufacturerName]++;
|
||
}
|
||
else
|
||
{
|
||
productCounts[product.ManufacturerName] = 1;
|
||
}
|
||
}
|
||
|
||
return productCounts;
|
||
}
|
||
}
|
||
}
|