276 lines
8.6 KiB
C#
Raw Normal View History

2024-10-28 23:08:27 +04:00
using COP;
using COP.Info;
using KOP;
using Lab1;
using static Lab1.LogicalComponents.TableDocument;
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
using DocumentFormat.OpenXml.Spreadsheet;
using Lab1.LogicalComponents.SupportClasses;
using Microsoft.Extensions.Logging;
using KOP.PDFComponents.OfficePackage;
using System.Windows.Forms;
using ShopBusinessLogic.BusinessLogics;
using ShopContracts.BindingModels;
using ShopContracts.BusinessLogicsContracts;
using ShopContracts.SearchModels;
using ShopContracts.ViewModels;
using static COP.ExcelComponent;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using LegendPosition = KOP.PDFComponents.Enums.LegendPosition;
using NPOI.SS.Formula.Functions;
using KOP.Addition;
using KOP.PDFComponents;
namespace ShopView
{
public partial class FormMain : Form
{
private readonly ILogger _logger;
private readonly ICustomerLogic _customerLogic;
private readonly IProductCategoryLogic _productCategoryLogic;
public FormMain(ILogger<FormMain> logger, ICustomerLogic customerLogic, IProductCategoryLogic productCategoryLogic)
{
InitializeComponent();
_logger = logger;
_customerLogic = customerLogic;
_productCategoryLogic = productCategoryLogic;
Configure();
}
private void LoadData()
{
_logger.LogInformation("Загрузка клиентов");
try
{
tableOfValues.ClearRows();
var list = _customerLogic.Read(null);
if (list != null)
{
tableOfValues.AddRows(list);
}
_logger.LogInformation("Загрузка клиентов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormMain_Load(object sender, EventArgs e)
{
2024-11-07 14:19:38 +04:00
2024-10-28 23:08:27 +04:00
LoadData();
}
private void СоздатьToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCustomer));
if (service is FormCustomer form)
{
form.ShowDialog();
}
LoadData();
}
private void Configure()
{
ColumnInfo ci1 = new ColumnInfo("ID", 100, false, "Id");
ColumnInfo ci2 = new ColumnInfo("FIO", 100, true, "ФИО");
ColumnInfo ci3 = new ColumnInfo("ProductCategoryName", 80, true, "Категория товара");
2024-11-07 14:19:38 +04:00
ColumnInfo ci4 = new ColumnInfo("Email", 80, true, "Электронная почта");
2024-10-28 23:08:27 +04:00
List<ColumnInfo> lst = new List<ColumnInfo>();
lst.Add(ci1);
lst.Add(ci2);
lst.Add(ci3);
lst.Add(ci4);
tableOfValues.ColumnConfiguration(new TableConfiguration(lst));
}
private void ИзменитьToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCustomer));
tableOfValues.IndexProperty = tableOfValues.myDataGridView.CurrentRow.Index;
if (service is FormCustomer form)
{
if (tableOfValues.IndexProperty != -1)
{
var selectedCustomer = tableOfValues.GetUser<CustomerSearchModel>();
form.Id = Convert.ToInt32(selectedCustomer.Id);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
else
{
MessageBox.Show("Выберите клиента для редактирования");
}
}
}
private void УдалитьToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
tableOfValues.IndexProperty = tableOfValues.myDataGridView.CurrentRow.Index;
var selectedCustomer = tableOfValues.GetUser<CustomerSearchModel>();
int id = Convert.ToInt32(selectedCustomer.Id);
try
{
_customerLogic.Delete(new CustomerBindingModel { Id = id });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
private void СоздатьExcelСФотоToolStripMenuItem_Click(object sender, EventArgs e)
{
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);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void СоздатьДокументСТаблицейToolStripMenuItem_Click(object sender, EventArgs e)
{
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)
{
2024-11-07 14:19:38 +04:00
data.Add(new CustomerBindingModel() { Id = item.Id, FIO = item.FIO, Email = item.Email, ProductCategoryName = item.ProductCategoryName });
2024-10-28 23:08:27 +04:00
}
}
TableDocumentInfo<CustomerBindingModel> tableWord = new(dialog.FileName, "Таблица с клиентами", columnDefinitions, columnDefinitions2, data, mergedColumns);
Lab1.LogicalComponents.TableDocument.CreateTable(tableWord);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void СоздатьДокументСДиаграммойToolStripMenuItem_Click(object sender, EventArgs e)
{
var listCustomers = _customerLogic.Read(null);
var listProductCategorys = _productCategoryLogic.Read(null);
List<(string, int)> data = 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 < listProductCategorys.Count; i++)
{
int count = 0;
for (int j = 0; j < listCustomers.Count; j++)
{
string[] dirs = listCustomers[j].ProductCategoryName.Split(";");
foreach (var dir in dirs)
{
if (dir == listProductCategorys[i].Name) count++;
}
}
data.Add((listProductCategorys[i].Name, count));
}
if (data != null)
{
foreach (var item in data)
{
2024-11-07 14:19:38 +04:00
Data.Add(item.Item1, new[] { item.Item2 });
2024-10-28 23:08:27 +04:00
}
}
PdfChartComponent pdfChartComponent = new PdfChartComponent();
pdfChartComponent.CreateDocument(dialog.FileName, "Histogram", "Customers-ProductCategorys", LegendPosition.Right, Data);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void СправочникиToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormHandbooks));
if (service is FormHandbooks form)
{
form.ShowDialog();
}
LoadData();
}
}
2024-11-07 14:19:38 +04:00
}