using ComponentsLibraryNet60.Models; using Contracts.BindingModels; using Contracts.BusinessLogicContracts; using Contracts.SearchModels; using Contracts.ViewModels; using CustomComponents; using KOP_Labs.Classes; using System.Collections.Generic; using Unity; using ViewComponents.NotVisualComponents; namespace Library { public partial class FormMain : Form { private readonly IBookLogic _bookLogic; private readonly IAuthorLogic _authorLogic; public FormMain(IBookLogic bookLogic, IAuthorLogic authorLogic) { _bookLogic = bookLogic; _authorLogic = authorLogic; InitializeComponent(); tableComponent.TableConfiguration(5, new List { new TableParameters("Id", 80, false, "Id"), new TableParameters("Название", 100, true, "Name"), new TableParameters("Автор", 180, true, "Author"), new TableParameters("Обложка", 80, false, "PicturePath"), new TableParameters("Дата издания", 150, true, "PublicationDate") }); tableComponent.ContextMenuStrip = contextMenuStrip; } private void FormMain_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { tableComponent.ClearRows(); try { var list = _bookLogic.Read(null); foreach (var row in list) { tableComponent.AddRow(row); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void AddNewElement() { var form = Program.Container.Resolve(); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } private void UpdateElement() { var form = Program.Container.Resolve(); var selectedBook = tableComponent.GetSelectedObject(); if (selectedBook != null) { form.Id = Convert.ToInt32(selectedBook.Id); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } else { MessageBox.Show("Выберите книгу для редактирования"); } } private void DeleteElement() { if (MessageBox.Show("Удалить книгу", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { var selectedBook = tableComponent.GetSelectedObject(); int id = Convert.ToInt32(selectedBook.Id); try { _bookLogic.Delete(new BookBindingModel { Id = id }); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadData(); } } private void CreatePdf() { string fileName = ""; using (var dialog = new SaveFileDialog { Filter = "pdf|*.pdf" }) { if (dialog.ShowDialog() == DialogResult.OK) { fileName = dialog.FileName.ToString(); MessageBox.Show("Файл выбран", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } else return; } List images = new List(); var list = _bookLogic.Read(null); try { if (list != null) { foreach (var item in list) { images.Add(item.PicturePath); } string[] imagesArray = images.ToArray(); pdfImages.CreatePdfDoc(new ImagesForPDF(fileName, "Иллюстрации книг", imagesArray)); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка"); } } private void CreateExcel() { string fileName = ""; using (var dialog = new SaveFileDialog { Filter = "xlsx|*.xlsx" }) { if (dialog.ShowDialog() == DialogResult.OK) { fileName = dialog.FileName.ToString(); MessageBox.Show("Файл выбран", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } else return; } List<(int Column, int Row)> width = new List<(int, int)>() { (30, 0), (30, 0), (30, 0), (30, 0) }; List<(int StartIndex, int Count)> column_union = new List<(int, int)>() { (1, 2) }; List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)> headers = new List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)> { (0,0, "Идентификатор", "Id"), (1,0,"Книга", ""), (1,1,"Название", "Name"), (2,1,"Автор","Author"), (3,0,"Дата публикации", "PublicationDate") }; var list = _bookLogic.Read(null); ComponentDocumentWithTableHeaderDataConfig config = new(); try { excelTable1.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig() { Data = list, UseUnion = true, ColumnsRowsWidth = width, ColumnUnion = column_union, Headers = headers, FilePath = fileName, Header = "Отчёт по книгам" }); } catch(Exception ex) { MessageBox.Show(ex.Message, "Ошибка"); } } private void CreateWord() { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); string fileName = ""; using (var dialog = new SaveFileDialog { Filter = "docx|*.docx" }) { if (dialog.ShowDialog() == DialogResult.OK) { fileName = dialog.FileName.ToString(); MessageBox.Show("Файл выбран", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } else return; } List dataHistogramms = new List(); var list_author = _authorLogic.Read(null); var list_book = _bookLogic.Read(null); foreach (var nm in list_author) { int bk_count = 0; foreach (var bk in list_book) if (bk.Author.Contains(nm.FIO)) bk_count++; dataHistogramms.Add(new DataHistogramm(nm.FIO, bk_count)); } try { wordHistogramm.CreateHistogramm(new MyHistogramm(fileName, "Гистограмма", "Авторы и их книги", EnumLegends.Top, dataHistogramms)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка"); } } private void добавитьToolStripMenuItem_Click(object sender, EventArgs e) { AddNewElement(); } private void удалитьToolStripMenuItem_Click(object sender, EventArgs e) { DeleteElement(); } private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e) { UpdateElement(); } private void сохранитьВPDFToolStripMenuItem_Click(object sender, EventArgs e) { CreatePdf(); } private void сохранитьВExcelToolStripMenuItem_Click(object sender, EventArgs e) { CreateExcel(); } private void сохранитьВWordToolStripMenuItem_Click(object sender, EventArgs e) { CreateWord(); } private void авторыToolStripMenuItem_Click(object sender, EventArgs e) { var form = Program.Container.Resolve(); form.ShowDialog(); } } }