using ComponentLibrary1.pdf_image; using DocumentFormat.OpenXml.Drawing.Charts; using Library_var_4_lab_1; using LibraryContracts.StorageContracts; using LibraryDatabase.Models; using LibraryDatabase.Storages; using LibraryDataModels.AbstractModels; using LibraryDataModels.Dtos; using LibraryDataModels.Views; using LibraryPlugin.Helpers; using LibraryWinFormsApp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using YunusovComponentsLibrary; using YunusovComponentsLibrary.OfficePackage.HelperModels; using static LibraryUtils.ImageConverter.ImageConverter; namespace LibraryPlugin { public class PluginsConvention : IPluginsConvention { private readonly IAuthorStorage _authorStorage = new AuthorStorage(); private readonly IBookStorage _bookStorage = new BookStorage(); private PdfImage pdfImage = new PdfImage(); private ConfigurableTable configurableTable = new ConfigurableTable(); private WordDiagram wordDiagram = new WordDiagram(); private ListOutputComponent bookTable = new ListOutputComponent(); #region interface getters public string PluginName { get; } = "LibraryPlugin"; public UserControl GetControl { get { ReloadData(); return bookTable; } } public PluginsConventionElement GetElement { get { var book = bookTable.GetSelectedObject(); int? id = null; if (book != null) { id = Convert.ToInt32(book.Id); } byte[] bytes = new byte[16]; if (!id.HasValue) { id = -1; } BitConverter.GetBytes(id.Value).CopyTo(bytes, 0); return new() { Id = new Guid(bytes) }; } } public Form GetForm(PluginsConventionElement element) { FormBook form = new FormBook(_bookStorage, _authorStorage); int id = element?.Id.GetHashCode() ?? -1; if ( id > 0) { form._id = id; } return form; } public Form GetThesaurus() { return new FormAuthors(_authorStorage); } #endregion #region interface methods public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument) { try { string fileName = saveDocument.FileName; if (string.IsNullOrWhiteSpace(fileName)) { return false; } var _books = _bookStorage.GetFullList(); List selectedImages = new List(); foreach (var book in _books) { selectedImages.Add(StringToByteArray(book.BookCover)); } var info = new PdfImageInfo { FileName = fileName, Title = "Обложки", Images = selectedImages, }; pdfImage.CreatePdf(info); MessageBox.Show("PDF успешно создан!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument) { try { string fileName = saveDocument.FileName; if (string.IsNullOrWhiteSpace(fileName)) { return false; } List headers = new List { new TreeNode { Text = "Идентификатор", Tag = "Id", Name = "40", }, new TreeNode("Книга", new TreeNode[] { new TreeNode { Text = "Название", Tag = "Name", Name = "40", }, new TreeNode { Text = "Автор", Tag = "Author", Name = "40", }, }), new TreeNode { Text = "Дата", Tag = "ReleaseDate", Name = "40", }, }; var _books = _bookStorage.GetFullList(); configurableTable.CreateTable( fileName, "Книги", headers, _books); MessageBox.Show("Excell успешно создан!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument) { try { string fileName = saveDocument.FileName; if (string.IsNullOrWhiteSpace(fileName)) { return false; } var _books = _bookStorage.GetFullList(); var authors = _authorStorage.GetFullList(); WordDiagramSeries series = new WordDiagramSeries(); series.SeriesName = "Авторы"; foreach (var author in authors) { double sum = 0.0; foreach (var book in _books) { if (string.Equals(book.Author, author.Name)) { sum++; } } series.Data.Add(author.Name, sum); } wordDiagram.CreateDiagram(new WordDiagramInfo() { FileName = fileName, Title = "Авторы", ChartTitle = "Авторы", Series = series, }); MessageBox.Show("Word успешно создан!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } public bool DeleteElement(PluginsConventionElement element) { try { int selectedRow = bookTable.SelectedRow; if (selectedRow == -1) { throw new Exception("Выберите книгу для удаления"); } if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = Convert.ToInt32(bookTable.Rows[selectedRow].Cells[0].Value); _bookStorage.Delete(new BookDto() { Id = id }); ReloadData(); } return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } public void ReloadData() { try { List _books = _bookStorage.GetFullList(); List parameters = new List() { new ColumnInfo("", 0, false, "Id"), new ColumnInfo("Название", 258, true, "Name"), new ColumnInfo("", 0, false, "BookCover"), new ColumnInfo("ФИО автора", 258, true, "Author"), new ColumnInfo("Дата издания", 258, true, "ReleaseDate"), }; bookTable.ConfigColumn(parameters); if (_books == null) { return; } foreach (var book in _books) { bookTable.AddItem(book); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion } }