Zhelovanov_Dmitrii_COP/WinForm/FormMain.cs

433 lines
9.9 KiB
C#
Raw Normal View History

2023-11-30 23:20:29 +04:00
using ComponentsLibraryNet60.Models;
using Contracts.BindingModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using CustomComponents;
using DatabaseImplement.Models;
using DataModels.Models;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Spreadsheet;
using KOP_Labs.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewComponents.NotVisualComponents;
using Book = DatabaseImplement.Models.Book;
namespace WinForm
{
public partial class FormMain : Form
{
private readonly IBookStorage _bookStorage;
private readonly IShapeStorage _shapeStorage;
public FormMain(IBookStorage bookStorage, IShapeStorage shapeStorage)
{
InitializeComponent();
_bookStorage = bookStorage;
_shapeStorage = shapeStorage;
LoadData();
}
public void LoadData()
{
listBoxObjects1.deleteAll();
listBoxObjects2.deleteAll();
var books = _bookStorage.GetFullList();
listBoxObjects1.SetLayoutInfo("Название *Name* Читатели *Readers* Форма *Shape* Аннотация *Annotation* Id *Id*", "*", "*");
listBoxObjects2.SetLayoutInfo("Форма *Shape* Аннотация *Annotation* Id *Id*", "*", "*");
foreach (var book in books)
{
listBoxObjects1.AddInListBox(book);
listBoxObjects2.AddInListBox(book);
}
}
private void CreateToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormCreateBook));
if (service is FormCreateBook form)
{
form.ShowDialog();
LoadData();
}
}
private void изменитьToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
int id = listBoxObjects1.GetObjectFromStr<Book>().Id;
var service = Program.ServiceProvider?.GetService(typeof(FormCreateBook));
if (service is FormCreateBook form)
{
form.Id = id;
form.ShowDialog();
LoadData();
}
}
catch (Exception ex)
{
MessageBox.Show("Ошибка операции", "Необходимо выбрать элемент списка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
{
int id = listBoxObjects1.GetObjectFromStr<Book>().Id;
DialogResult result = MessageBox.Show("Вы уверены, что хотите удалить выбранную запись?" + id, "Подтверждение удаления", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
_bookStorage.Delete(new BookBindingModel
{
Id = id
});
LoadData();
}
}
private void справочникToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormShapes));
if (service is FormShapes form)
{
form.ShowDialog();
LoadData();
}
}
public void CreateSimpleDocItem_Click(object sender, EventArgs e)
{
//фильтрация файлов для диалогового окна
using var dialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
var list = _bookStorage.GetFullList().OrderBy(l => l.Shape).ToList();
var disciplines = _shapeStorage.GetFullList().OrderBy(d => d.Name).ToList();
List<string[,]> totalList = new();
List<BookViewModel> supportList = new();
foreach (var discipline in disciplines)
{
foreach (var elem in list)
{
if (elem.Shape.Equals(discipline.Name))
{
supportList.Add(elem);
}
}
supportList = supportList.OrderBy(sl => sl.Name).ToList();
totalList.Add(new string[,] { { "Форма", discipline.Name } });
foreach (var elem in supportList)
{
var listFCs = elem.Readers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
string[,] newArray = { { elem.Name, listFCs[listFCs.Count - 1] } };
totalList.Add(newArray);
}
supportList.Clear();
}
MyTable table = new(dialog.FileName, "Первое задание", totalList);
wordTableComponent1.CreateDoc(table);
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)
{
CreateSimpleDocItem_Click(sender, e);
}
private void отчёт2ToolStripMenuItem_Click(object sender, EventArgs e)
{
List<BookViewModel>? list = _bookStorage.GetFullList().OrderBy(l => l.Name).ToList();
using var dialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
componentDocumentWithTableHeaderColumnPdf1.CreateDoc(new ComponentDocumentWithTableHeaderDataConfig<BookViewModel>
{
FilePath = dialog.FileName,
Header = "Отчет PDF",
UseUnion = true,
ColumnsRowsWidth = new List<(int, int)> { (0, 25), (0, 25), (0, 25), },
ColumnUnion = new List<(int StartIndex, int Count)> { (1, 2) },
Headers = new List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)>
{
(0, 0, "Название", "Name"),
(1, 0, "Описание", ""),
(1, 1, "Форма", "Shape"),
(2, 1, "Аннотация", "Annotation"),
},
Data = list
});
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void CreateReportExcel(object sender, EventArgs e)
{
using var dialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
var list = _bookStorage.GetFullList();
var shapes = _shapeStorage.GetFullList();
int[,] supportList = new int[4, shapes.Count];
int c = 0;
for (int i = 0; i < shapes.Count; i++)
{
foreach (var elem in list)
{
if (shapes[i].Name.Equals(elem.Shape))
{
c++;
if (elem.Annotation.Length >= 0 && elem.Annotation.Length < 10)
{
supportList[0, i]++;
}
if (elem.Annotation.Length >= 10 && elem.Annotation.Length < 150)
{
supportList[1, i]++;
}
if (elem.Annotation.Length >= 150 && elem.Annotation.Length < 200)
{
supportList[2, i]++;
}
if (elem.Annotation.Length >= 200 && elem.Annotation.Length < 250)
{
supportList[3, i]++;
}
}
}
}
string[] Names = { "50-150", "100-150", "150-200", "200-250" };
var list2D = new Dictionary<string, List<int>>();
for (var i = 0; i < Names.Length; i++)
{
var curlist = new List<int>();
for (int j = 0; j < shapes.Count; j++)
{
curlist.Add(supportList[i, j]);
}
list2D.Add(Names[i], curlist);
}
DiagramComponent diagram = new DiagramComponent();
diagram.CreateExcel(new CustomComponents.MyNonVisualComponents.LineChartConfig
{
ChartTitle = "diagramm",
FilePath = dialog.FileName,
Header = "Diagramm",
Values = list2D
});
MessageBox.Show(" " + c);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void отчёт3ToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateReportExcel(sender, e);
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.A:
CreateToolStripMenuItem_Click(sender, e);
break;
case Keys.U:
изменитьToolStripMenuItem_Click(sender, e);
break;
case Keys.D:
удалитьToolStripMenuItem_Click(sender, e);
break;
case Keys.S:
справочникToolStripMenuItem_Click(sender, e);
break;
case Keys.T:
отчётToolStripMenuItem_Click(sender, e);
break;
case Keys.C:
отчёт2ToolStripMenuItem_Click(sender, e);
break;
case Keys.M:
отчёт3ToolStripMenuItem_Click(sender, e);
break;
}
}
}
private void listBoxObjects1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.A:
CreateToolStripMenuItem_Click(sender, e);
break;
/*case Keys.U:
EditProviderItem_Click(sender, e);
break;
case Keys.D:
RemoveProviderItem_Click(sender, e);
break;
case Keys.S:
GetSimpleDocumentItem_Click(sender, e);
break;
case Keys.T:
GetTableDocumentItem_Click(sender, e);
break;
case Keys.C:
GetDiagramDocumentItem_Click(sender, e);
break;
case Keys.M:
OpenListToolStripMenuItem_Click(sender, e);
break;*/
}
}
}
private void menuStrip1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.A:
CreateToolStripMenuItem_Click(sender, e);
break;
/*case Keys.U:
EditProviderItem_Click(sender, e);
break;
case Keys.D:
RemoveProviderItem_Click(sender, e);
break;
case Keys.S:
GetSimpleDocumentItem_Click(sender, e);
break;
case Keys.T:
GetTableDocumentItem_Click(sender, e);
break;
case Keys.C:
GetDiagramDocumentItem_Click(sender, e);
break;
case Keys.M:
OpenListToolStripMenuItem_Click(sender, e);
break;*/
}
}
}
}
}