2024-10-30 13:34:23 +04:00
|
|
|
|
using Data;
|
|
|
|
|
using Data.Repositories;
|
|
|
|
|
using View;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Data.Models;
|
2024-10-31 00:02:20 +04:00
|
|
|
|
using Library14Petrushin;
|
2024-10-31 01:02:35 +04:00
|
|
|
|
using Library15Gerimovich.OfficePackage.HelperModels;
|
|
|
|
|
using Library15Gerimovich;
|
|
|
|
|
using WinFormsLibrary1;
|
|
|
|
|
using WinFormsLibrary1.Models;
|
2024-10-30 13:34:23 +04:00
|
|
|
|
|
|
|
|
|
namespace Laba3
|
|
|
|
|
{
|
|
|
|
|
public partial class MainForm : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly IProductRepository _productRepository;
|
|
|
|
|
private readonly IManufacturerRepository _manufacturerRepository;
|
2024-10-31 01:02:35 +04:00
|
|
|
|
private SaveFileDialog saveFileDialogPdf;
|
|
|
|
|
private SaveFileDialog saveFileDialogWord;
|
|
|
|
|
private SaveFileDialog saveFileDialogExel;
|
2024-10-30 13:34:23 +04:00
|
|
|
|
|
|
|
|
|
public MainForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_productRepository = productRepository;
|
|
|
|
|
_manufacturerRepository = manufacturerRepository;
|
|
|
|
|
InitializeOutputTableResults();
|
|
|
|
|
LoadProducts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeOutputTableResults()
|
|
|
|
|
{
|
2024-10-31 01:02:35 +04:00
|
|
|
|
outputTableResults.ConfigureColumns(new List<ColumnInfo>
|
2024-10-30 13:34:23 +04:00
|
|
|
|
{
|
2024-10-31 01:02:35 +04:00
|
|
|
|
new ColumnInfo("", 0, false, "Id"),
|
|
|
|
|
new ColumnInfo("Name", 150, true, "Name"),
|
|
|
|
|
new ColumnInfo("ManufacturerNameManufacturerName", 150, true, "ManufacturerName"),
|
|
|
|
|
new ColumnInfo("DeliveryDate", 50, true, "DeliveryDate"),
|
2024-10-30 13:34:23 +04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-10-31 00:02:20 +04:00
|
|
|
|
try
|
2024-10-30 13:34:23 +04:00
|
|
|
|
{
|
2024-10-31 00:02:20 +04:00
|
|
|
|
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());
|
2024-10-30 13:34:23 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2024-10-31 00:02:20 +04:00
|
|
|
|
|
|
|
|
|
private void createSimpleDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Получаем изображения из базы данных
|
|
|
|
|
var images = GetImagesFromDatabase();
|
|
|
|
|
|
|
|
|
|
if (images.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// Показываем диалоговое окно для выбора пути сохранения
|
2024-10-31 01:02:35 +04:00
|
|
|
|
if (saveFileDialogPdf.ShowDialog() == DialogResult.OK)
|
2024-10-31 00:02:20 +04:00
|
|
|
|
{
|
|
|
|
|
// Создаем компонент PdfImg
|
|
|
|
|
var pdfImg = new Library14Petrushin.PdfImg();
|
|
|
|
|
|
|
|
|
|
// Путь для сохранения PDF-документа
|
2024-10-31 01:02:35 +04:00
|
|
|
|
string fileName = saveFileDialogPdf.FileName;
|
2024-10-31 00:02:20 +04:00
|
|
|
|
|
|
|
|
|
// Заголовок документа
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-10-31 01:02:35 +04:00
|
|
|
|
// Получаем все продукты из базы данных
|
|
|
|
|
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.");
|
|
|
|
|
}
|
2024-10-31 00:02:20 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createChartDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-10-31 01:02:35 +04:00
|
|
|
|
// Получаем данные о количестве продукции каждого производителя
|
|
|
|
|
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.");
|
|
|
|
|
}
|
2024-10-31 00:02:20 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-10-31 01:02:35 +04:00
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2024-10-30 13:34:23 +04:00
|
|
|
|
}
|
|
|
|
|
}
|