KOP_Library_v5/Library/FormMain.cs

256 lines
6.4 KiB
C#

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<TableParameters>
{
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<FormBook>();
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void UpdateElement()
{
var form = Program.Container.Resolve<FormBook>();
var selectedBook = tableComponent.GetSelectedObject<BookSearchModel>();
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<BookSearchModel>();
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<string> images = new List<string>();
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<BookViewModel> config = new();
try
{
excelTable1.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig<BookViewModel>()
{
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<DataHistogramm> dataHistogramms = new List<DataHistogramm>();
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<FormAuthor>();
form.ShowDialog();
}
}
}