273 lines
10 KiB
C#
273 lines
10 KiB
C#
using Data;
|
||
using Data.Repositories;
|
||
using View;
|
||
using System.Windows.Forms;
|
||
using Data.Models;
|
||
using Library14Petrushin;
|
||
using Library15Gerimovich.OfficePackage.HelperModels;
|
||
using Library15Gerimovich;
|
||
using WinFormsLibrary1;
|
||
using WinFormsLibrary1.Models;
|
||
|
||
namespace Laba3
|
||
{
|
||
public partial class MainForm : Form
|
||
{
|
||
private readonly IProductRepository _productRepository;
|
||
private readonly IManufacturerRepository _manufacturerRepository;
|
||
private SaveFileDialog saveFileDialogPdf;
|
||
private SaveFileDialog saveFileDialogWord;
|
||
private SaveFileDialog saveFileDialogExel;
|
||
|
||
public MainForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository)
|
||
{
|
||
InitializeComponent();
|
||
_productRepository = productRepository;
|
||
_manufacturerRepository = manufacturerRepository;
|
||
InitializeOutputTableResults();
|
||
LoadProducts();
|
||
}
|
||
|
||
private void InitializeOutputTableResults()
|
||
{
|
||
outputTableResults.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"),
|
||
});
|
||
|
||
}
|
||
|
||
private void LoadProducts()
|
||
{
|
||
var products = _productRepository.GetAllProducts();
|
||
outputTableResults.ClearGrid();
|
||
foreach (var product in products)
|
||
{
|
||
outputTableResults.InsertValue(product);
|
||
}
|
||
}
|
||
|
||
private void addToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var productForm = new ProductForm(_productRepository, _manufacturerRepository);
|
||
if (productForm.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadProducts();
|
||
}
|
||
}
|
||
|
||
private void editToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
var selectedProductId = outputTableResults.GetSelectedObject<Product>().Id;
|
||
var selectedProduct = _productRepository.GetProductById(selectedProductId);
|
||
var productForm = new ProductForm(_productRepository, _manufacturerRepository, selectedProduct);
|
||
if (productForm.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadProducts();
|
||
}
|
||
}
|
||
catch (Exception ex) {
|
||
MessageBox.Show(ex.ToString());
|
||
}
|
||
}
|
||
|
||
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var selectedProductId = outputTableResults.GetSelectedObject<Product>().Id;
|
||
if (MessageBox.Show("Are you sure you want to delete this product?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
||
{
|
||
_productRepository.DeleteProduct(selectedProductId);
|
||
LoadProducts();
|
||
}
|
||
}
|
||
|
||
private void manufacturersToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var manufacturerForm = new ManufacturerForm(_manufacturerRepository);
|
||
manufacturerForm.ShowDialog();
|
||
}
|
||
|
||
private void createSimpleDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// Получаем изображения из базы данных
|
||
var images = GetImagesFromDatabase();
|
||
|
||
if (images.Count > 0)
|
||
{
|
||
// Показываем диалоговое окно для выбора пути сохранения
|
||
if (saveFileDialogPdf.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// Создаем компонент PdfImg
|
||
var pdfImg = new Library14Petrushin.PdfImg();
|
||
|
||
// Путь для сохранения PDF-документа
|
||
string fileName = saveFileDialogPdf.FileName;
|
||
|
||
// Заголовок документа
|
||
string documentTitle = "Product Images";
|
||
|
||
// Создаем PDF-документ
|
||
pdfImg.CreatePdfDocument(fileName, documentTitle, images);
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("No images found in the database.");
|
||
}
|
||
}
|
||
|
||
private void createCustomTableDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// Получаем все продукты из базы данных
|
||
var products = GetAllProducts();
|
||
|
||
if (products.Count > 0)
|
||
{
|
||
// Показываем диалоговое окно для выбора пути сохранения
|
||
if (saveFileDialogExel.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// Создаем объекты MergeCell и Column
|
||
var mergeCells = new List<MergeCell>
|
||
{
|
||
new MergeCell("Product Information", new int[] { 0, 1, 2, 3 })
|
||
};
|
||
|
||
var columns = new List<Column>
|
||
{
|
||
new Column("ID", "Id", 10),
|
||
new Column("Name", "Name", 20),
|
||
new Column("Manufacturer", "ManufacturerName", 20),
|
||
new Column("Delivery Date", "DeliveryDate", 20)
|
||
};
|
||
|
||
|
||
// Создаем компонент ComponentTable
|
||
var componentTable = new ComponentTable();
|
||
|
||
// Создаем документ Excel
|
||
componentTable.CreateDocument(saveFileDialogExel.FileName, "Product Report", mergeCells, columns, products);
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("No products found in the database.");
|
||
}
|
||
}
|
||
|
||
private void createChartDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// Получаем данные о количестве продукции каждого производителя
|
||
var productCounts = GetProductCountsByManufacturer();
|
||
|
||
if (productCounts.Count > 0)
|
||
{
|
||
// Показываем диалоговое окно для выбора пути сохранения
|
||
if (saveFileDialogWord.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// Создаем объект WordDiagramInfo
|
||
var diagramInfo = new WordDiagramInfo
|
||
{
|
||
FileName = saveFileDialogWord.FileName,
|
||
Title = "Product Count by Manufacturer",
|
||
ChartTitle = "Product Count",
|
||
LegendLocation = Library15Gerimovich.OfficePackage.HelperEnums.WordDiagramLegendLocation.Top,
|
||
Series = new WordDiagramSeries
|
||
{
|
||
SeriesName = "Product Count",
|
||
Data = productCounts
|
||
}
|
||
};
|
||
|
||
// Создаем диаграмму в Word
|
||
wordDiagramComponent.CreateDiagram(diagramInfo);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("No products found in the database.");
|
||
}
|
||
}
|
||
|
||
private void MainForm_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
if (e.Control)
|
||
{
|
||
switch (e.KeyCode)
|
||
{
|
||
case Keys.A:
|
||
addToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.U:
|
||
editToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.D:
|
||
deleteToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.S:
|
||
createSimpleDocumentToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.T:
|
||
createCustomTableDocumentToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.C:
|
||
createChartDocumentToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private List<ImageData> GetImagesFromDatabase()
|
||
{
|
||
var images = new List<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 ImageData { ImagePath = tempFilePath });
|
||
}
|
||
}
|
||
|
||
return images;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
private List<Product> GetAllProducts()
|
||
{
|
||
return _productRepository.GetAllProducts().ToList();
|
||
}
|
||
}
|
||
}
|