вроде финал
This commit is contained in:
parent
262360562e
commit
9f6e51abb3
@ -24,33 +24,7 @@ namespace View
|
||||
_selectedPlugin = string.Empty;
|
||||
}
|
||||
|
||||
/*private Dictionary<string, IPluginsConvention> LoadPlugins()
|
||||
{
|
||||
PluginsManager manager = new PluginsManager();
|
||||
var plugins = manager.plugins_dictionary;
|
||||
|
||||
ToolStripItem[] toolStripItems = new ToolStripItem[plugins.Count];
|
||||
int i = 0;
|
||||
if (plugins.Count > 0)
|
||||
{
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
ToolStripMenuItem itemMenu = new ToolStripMenuItem();
|
||||
itemMenu.Text = plugin.Value.PluginName;
|
||||
itemMenu.Click += (sender, e) =>
|
||||
{
|
||||
_selectedPlugin = plugin.Value.PluginName;
|
||||
panelControl.Controls.Clear();
|
||||
panelControl.Controls.Add(_plugins[_selectedPlugin].GetControl);
|
||||
panelControl.Controls[0].Dock = DockStyle.Fill;
|
||||
};
|
||||
toolStripItems[i] = itemMenu;
|
||||
i++;
|
||||
}
|
||||
ControlsStripMenuItem.DropDownItems.AddRange(toolStripItems);
|
||||
}
|
||||
return plugins;
|
||||
}*/
|
||||
|
||||
|
||||
private Dictionary<string, IPluginsConvention> LoadPlugins()
|
||||
{
|
||||
|
@ -1,304 +0,0 @@
|
||||
using Data;
|
||||
using Data.Models;
|
||||
using Data.Repositories;
|
||||
using Library15Gerimovich;
|
||||
using Library15Gerimovich.OfficePackage.HelperModels;
|
||||
using PluginsConventionLibrary.Plugins;
|
||||
using PluginsConvention14;
|
||||
using PluginsConvention14.MyPlugin;
|
||||
using System.Composition;
|
||||
using WinFormsLibrary1;
|
||||
|
||||
namespace View
|
||||
{
|
||||
[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;
|
||||
ReloadData();
|
||||
}
|
||||
|
||||
/// Название плагина
|
||||
/*string IPluginsConvention.PluginName => PluginName();
|
||||
public string PluginName()
|
||||
{
|
||||
return "Products";
|
||||
}*/
|
||||
|
||||
public UserControl GetControl => оutputTableResults;
|
||||
|
||||
PluginsConventionElement IPluginsConvention.GetElement => GetElement();
|
||||
|
||||
public PluginsConventionElement GetElement()
|
||||
{
|
||||
var product = оutputTableResults.GetSelectedObject<MainPluginConventionElement>(); ;
|
||||
MainPluginConventionElement element = null;
|
||||
if (оutputTableResults != null)
|
||||
{
|
||||
element = new MainPluginConventionElement
|
||||
{
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
ManufacturerName = product.ManufacturerName,
|
||||
DeliveryDate = product.DeliveryDate,
|
||||
};
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
using PluginsConventionLibrary.Plugins;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.ComponentModel.Composition.Hosting;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace View
|
||||
{
|
||||
public class PluginsManager
|
||||
{
|
||||
//Тег, указывающий, что plugins должны быть заполнены CompositionContainer
|
||||
[ImportMany(typeof(IPluginsConvention))]
|
||||
IEnumerable<IPluginsConvention> Plugins { get; set; }
|
||||
|
||||
public readonly Dictionary<string, IPluginsConvention> plugins_dictionary = new Dictionary<string, IPluginsConvention>();
|
||||
|
||||
public PluginsManager()
|
||||
{
|
||||
AggregateCatalog catalog = new AggregateCatalog();
|
||||
catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
|
||||
catalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins")));
|
||||
|
||||
|
||||
//Контейнер композиции
|
||||
|
||||
CompositionContainer container = new CompositionContainer(catalog);
|
||||
try
|
||||
{
|
||||
container.ComposeParts(this);
|
||||
}
|
||||
catch (CompositionException compositionException)
|
||||
{
|
||||
MessageBox.Show(compositionException.ToString());
|
||||
}
|
||||
if (Plugins.Any())
|
||||
{
|
||||
Plugins
|
||||
.ToList()
|
||||
.ForEach(p =>
|
||||
{
|
||||
if (!plugins_dictionary.Keys.Contains(p.PluginName))
|
||||
plugins_dictionary.Add(p.PluginName, p);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user