Something was created.

This commit is contained in:
ElEgEv 2023-11-02 21:46:57 +04:00
parent e9733d991b
commit e073e19639

View File

@ -1,7 +1,10 @@
using Contracts.ViewModel;
using Contracts.BindingModel;
using Contracts.StorageContracts;
using Contracts.ViewModel;
using DatabaseImplement.Implements;
using DataModels.Models;
using DocumentFormat.OpenXml.EMMA;
using IronPdf;
using NonVisualComponents;
using NonVisualComponents.Classes;
using NonVisualComponents.Enums;
@ -12,17 +15,20 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisualComponentsLib.Components;
using VisualComponentsLib.Components.SupportClasses;
using VisualComponentsLib.CustomListBox;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace VisualComponentsForm
{
public class PluginsConvention : IPluginsConvention
{
private readonly LabWorkStorage _labWorkStorage;
private readonly IDiscipline _discipline;
private readonly IDisciplineStorage _discipline;
private readonly MyListBox listBox;
private SimpleTable wordTable;
private ComponentWord wordTable;
private PdfTable pdfTable;
private ExcelChartInfo excelTable;
@ -32,7 +38,7 @@ namespace VisualComponentsForm
{
_labWorkStorage = new LabWorkStorage();
_discipline = new DisciplineStorage();
wordTable = new SimpleTable();
wordTable = new ComponentWord();
pdfTable = new PdfTable();
//excelTable = new ExcelChartInfo();
listBox = new MyListBox();
@ -47,7 +53,7 @@ namespace VisualComponentsForm
{
get
{
int Id = listBox.GetSelectedRecord<LabWorkViewModel>()!.Id;
int Id = GetSelectedId();
byte[] bytes = new byte[16];
BitConverter.GetBytes(Id).CopyTo(bytes, 0);
@ -58,6 +64,132 @@ namespace VisualComponentsForm
}
}
//запуск основной формы из лабы
public Form GetForm(PluginsConventionElement element)
{
if (element == null)
{
return new FormMain(_labWorkStorage, _discipline);
}
else
{
FormMain form = new FormMain(_labWorkStorage, _discipline);
return form;
}
}
//получаем список дисциплин (мои перечисления по заданию)
public Form GetThesaurus()
{
return new FormCreateDiscipline(_discipline);
}
//создание word табилцы
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
//сразу группируем списки по дисциплинам
var list = _labWorkStorage.GetFullList().OrderBy(l => l.Discipline).ToList();
var disciplines = _discipline.GetFullList().OrderBy(d => d.Name).ToList();
List<string[,]> totalList = new();
List<LabWorkViewModel> supportList = new();
foreach (var discipline in disciplines)
{
foreach (var elem in list)
{
if (elem.Discipline == discipline.Name)
{
supportList.Add(elem);
}
}
supportList = supportList.OrderBy(sl => sl.Theme).ToList();
totalList.Add(new string[,] { { "Дисциплина", discipline.Name } });
foreach (var elem in supportList)
{
var listFCs = elem.FCs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
string[,] newArray = { { elem.Theme, listFCs[listFCs.Count - 1] } };
totalList.Add(newArray);
}
supportList.Clear();
}
SimpleTable table = new(saveDocument.FileName, "Первое задание", totalList);
wordTable.CreateDoc(table);
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
//создание pdf таблицы
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
//сразу группируем списки по дисциплинам
var list = _labWorkStorage.GetFullList().OrderBy(l => l.Discipline).ToList();
var disciplines = _discipline.GetFullList().OrderBy(d => d.Name).ToList();
List<string[,]> totalList = new();
List<LabWorkViewModel> supportList = new();
string[] headers = new[]
{
"Id", "Theme", "FCs",
"Discipline", "Questions"
};
Dictionary<int, (String, int)> merge = new() { [3] = ("Описание", 2) };
PdfTable pdfTable = new();
CreatePDF(new TableData<LabWorkViewModel>
{
FilePath = saveDocument.FileName,
Title = "Второе задание",
Merge = merge,
Headers = headers,
Data = list.ToArray(),
RowHeight = new double[] { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 }
});
MessageBox.Show("Выполнено", "Успех", 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
@ -124,160 +256,89 @@ namespace VisualComponentsForm
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
public bool DeleteElement(PluginsConventionElement element)
{
try
_labWorkStorage.Delete(new LabWorkBindingModel
{
var view = new OrderSearchModel
{
TotalPrice = null
};
var list = _labWorkStorage.GetFilteredList(view);
string[] data = new string[list.Count];
string curStr = "";
for (int i = 0; i < list.Count; i++)
{
curStr = "ФИО Заказчика: " + list[i].UserInfo + ". Информация о товарах: " + list[i].ProductsInfo;
data[i] = curStr;
}
WordData wd = new WordData
{
filePath = saveDocument.FileName,
title = System.IO.Path.GetFileName(saveDocument.FileName),
data = data
};
wordTable.CreateFileWithBigText(wd);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
var list = _labWorkStorage.GetFullList();
var ColumnHeaders = new List<string>() {
"Идентификатор",
"ФИО заказчика",
"Статус заказа",
"Сумма"
};
var listData = new List<OrderTableViewModel>();
int id = 0;
string UserInfo = "";
string TotalPrice = "";
string Status = "";
for (int i = 0; i < list.Count; i++)
{
id = list[i].Id;
UserInfo = list[i].UserInfo;
if (list[i].TotalPrice == null)
{
TotalPrice = "Оплачено скидками";
}
else
{
TotalPrice = list[i].TotalPrice.ToString()!;
}
Status = list[i].Status;
var data = new OrderTableViewModel()
{
Id = id,
UserInfo = UserInfo,
TotalPrice = TotalPrice,
Status = Status
};
listData.Add(data);
}
var props = new List<string>() { "Id", "UserInfo", "Status", "TotalPrice" };
int[] rowHeight = new int[listData.Count + 1];
for (int i = 0; i < rowHeight.Length; i++)
{
rowHeight[i] = 20;
}
pdfTable.SaveToPdfFile(new PdfTableInfo<OrderTableViewModel>
{
FilePath = saveDocument.FileName,
DocumentTitle = "Заголовок",
TableData = listData,
ColumnWidths = new int[] { 10, 10, 10, 30 },
RowHeight = rowHeight,
ColumnHeaders = ColumnHeaders,
Properties = props
Id = element.Id.GetHashCode()
});
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool DeleteElement(PluginsConventionElement element)
{
_labWorkStorage.Delete(new(element.Id.GetHashCode()));
return true;
}
public Form GetForm(PluginsConventionElement element)
{
if (element == null)
{
return new FormMain(_labWorkStorage, _discipline);
}
else
{
FormMain form = new FormMain(_labWorkStorage, _discipline);
return form;
}
}
public Form GetThesaurus()
{
return new FormStatusDirectory(_discipline);
}
public void ReloadData()
{
var labWorks = _labWorkStorage.GetFullList();
myListBox.ClearAll();
listBox.ClearAll();
foreach (var labWork in labWorks)
{
myListBox.AddItem(labWork.Discipline + " | " + labWork.Id.ToString() + " | "
listBox.AddItem(labWork.Discipline + " | " + labWork.Id.ToString() + " | "
+ labWork.Theme + " | " + labWork.Questions);
}
}
//иначе для pdf-ки никак :(((
public void CreatePDF<T>(TableData<T> data)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
StringBuilder htmlContainer = new StringBuilder();
htmlContainer.AppendLine("<link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN\" crossorigin=\"anonymous\">");
htmlContainer.AppendLine($"<h1>{data.Title}</h1>");
htmlContainer.AppendLine("<table class=\"table\">");
int counter = 0;
for (int i = 0; i < data.Headers.Length; i++)
{
htmlContainer.Append($"<tr style=\"height: {data.RowHeight[i]}px\">\r\n");
if (data.Merge.Keys.Contains(i))
{
htmlContainer.Append($"<th rowspan=\"{data.Merge[i].Item2}\">{data.Merge[i].Item1}</th>\r\n");
htmlContainer.Append($"<th>{data.Headers[i]}</th>\r\n");
counter = data.Merge[i].Item2 - 1;
}
else if (counter != 0)
{
htmlContainer.Append($"<th>{data.Headers[i]}</th>\r\n");
counter--;
}
else
{
htmlContainer.Append($"<th colspan=\"2\">{data.Headers[i]}</th>\r\n");
}
foreach (var elem in data.Data)
{
Type type = typeof(T);
string value = type
.GetProperty(data.Headers[i])!
.GetValue(elem, null)!.ToString()!;
htmlContainer.Append($"<td>{value}</th>\r\n");
}
htmlContainer.Append("</tr>\r\n");
}
htmlContainer.AppendLine("</table>");
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContainer.ToString());
Console.WriteLine(htmlContainer.ToString());
pdf.SaveAs(data.FilePath);
}
//получение Id объекта в выделенной строке
public int GetSelectedId()
{
char dilimiterSimbol = '|';
var _id = listBox.SelectedString.SkipWhile(z => z != dilimiterSimbol).Skip(1).ToList()[1];
return int.Parse(_id.ToString());
}
}
}