255 lines
10 KiB
C#
Raw Normal View History

using COP;
using COP.Info;
using COPWinForms;
using PluginsConventionLibrary.Forms;
using PluginsConventionLibrary.Plugins;
using System.ComponentModel.Composition;
using UniversityBusinessLogic.BusinessLogics;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
using WinFormsControlLibrary;
using static COP.ExcelComponent;
using static COPWinForms.ComponentWord2;
using LegendPosition = WinFormsControlLibrary.LegendPosition;
namespace PluginsConventionLibrary
{
[Export(typeof(IPluginsConvention))]
public class MainPluginConvention : IPluginsConvention
{
private TableOfValues tableOfValues;
private readonly IStudentLogic _studentLogic;
private readonly IDirectionLogic _directionLogic;
public MainPluginConvention()
{
_studentLogic = new StudentLogic();
_directionLogic = new DirectionLogic();
tableOfValues = new TableOfValues();
var menu = new ContextMenuStrip();
var directionMenuItem = new ToolStripMenuItem("Формы");
menu.Items.Add(directionMenuItem);
directionMenuItem.Click += (sender, e) =>
{
var formDirection = new FormDirection(_directionLogic);
formDirection.ShowDialog();
};
tableOfValues.ContextMenuStrip = menu;
ReloadData();
}
/// Название плагина
string IPluginsConvention.PluginName => PluginName();
public string PluginName()
{
return "Студенты";
}
public UserControl GetControl => tableOfValues;
PluginsConventionElement IPluginsConvention.GetElement => GetElement();
public PluginsConventionElement GetElement()
{
var student = tableOfValues.GetSelectedObject<MainPluginConventionElement>(); ;
MainPluginConventionElement element = null;
if (tableOfValues != null)
{
element = new MainPluginConventionElement
{
Id = student.Id,
FIO = student.FIO,
PhotoFilePath = student.PhotoFilePath,
Email = student.Email,
DirectionName = student.DirectionName,
};
}
return (new PluginsConventionElement { Id = element.Id });
}
public Form GetForm(PluginsConventionElement element)
{
var formOrder = new FormStudent(_studentLogic, _directionLogic);
if (element != null)
{
formOrder.Id = element.Id;
}
return formOrder;
}
public bool DeleteElement(PluginsConventionElement element)
{
try
{
_studentLogic.Delete(new StudentBindingModel { Id = element.Id });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public void ReloadData()
{
var columnConfigs = new List<GridColumnConfig>
{
new GridColumnConfig { HeaderText = "Id", Width = 100, Visible = false, PropertyName = "Id" },
new GridColumnConfig { HeaderText = "ФИО", Width = 100, Visible = true, PropertyName = "FIO" },
new GridColumnConfig { HeaderText = "Направление", Width = 80, Visible = true, PropertyName = "DirectionName" },
new GridColumnConfig { HeaderText = "Электронная почта", Width = 80, Visible = true, PropertyName = "Email" },
};
tableOfValues.ConfigureColumns(columnConfigs);
}
private byte[] StringToImage(string bytes)
{
byte[] arrayimg = Convert.FromBase64String(bytes);
return arrayimg;
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
ExcelComponent excelComponent = new();
var list = _studentLogic.Read(null);
List<ImageInfo> images = new();
using var dialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
if (list != null)
{
foreach (var item in list)
{
images.Add(new ImageInfo() { FilePath = item.PhotoFilePath });
}
}
ExcelImageInfo info = new(dialog.FileName, "Документ с фотографиями студентов", images);
excelComponent.GenerateExcelWithImages(info);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return true;
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
ComponentWord2 componentWord = new();
var list = _studentLogic.Read(null);
List<StudentBindingModel> data = new();
List<int[]> mergedColumns = new()
{
new int[] { 1, 2 }
};
List<ColumnDefinition> columnDefinitions = new()
{
new ColumnDefinition { Header = "ID", PropertyName = "Id", Width = 11 },
new ColumnDefinition { Header = "Личные данные", PropertyName = "PersonalData", Width = 11 },
new ColumnDefinition { Header = "Личные данные", PropertyName = "PersonalData1", Width = 11 },
new ColumnDefinition { Header = "Направление", PropertyName = "DirectionName", Width = 21 }
};
List<ColumnDefinition> columnDefinitions2 = new()
{
new ColumnDefinition { Header = "ID", PropertyName = "Id", Width = 11 },
new ColumnDefinition { Header = "ФИО", PropertyName = "FIO", Width = 11 },
new ColumnDefinition { Header = "Электронная почта", PropertyName = "Email", Width = 70 },
new ColumnDefinition { Header = "Направление", PropertyName = "DirectionName", Width = 21 }
};
using var dialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
if (list != null)
{
foreach (var item in list)
{
data.Add(new StudentBindingModel() { Id = item.Id, FIO = item.FIO, Email = item.Email, DirectionName = item.DirectionName });
}
}
TableWord<StudentBindingModel> tableWord = new(dialog.FileName, "Таблица со студентами", columnDefinitions, columnDefinitions2, data, mergedColumns);
componentWord.CreateTable(tableWord);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return true;
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
GistogramPdfComponent3 gistogramPdfComponent = new();
var listStudents = _studentLogic.Read(null);
var listDirections = _directionLogic.Read(null);
List<(string, int)> data = new();
List<HistogramData> gistData = new();
using var dialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
for (int i = 0; i < listDirections.Count; i++)
{
int count = 0;
for (int j = 0; j < listStudents.Count; j++)
{
string[] dirs = listStudents[j].DirectionName.Split(";");
foreach (var dir in dirs)
{
if (dir == listDirections[i].Name) count++;
}
}
data.Add((listDirections[i].Name, count));
}
if (data != null)
{
foreach (var item in data)
{
gistData.Add(new HistogramData
{
SeriesName = item.Item1,
Data = new double[] { item.Item2 }
});
}
}
gistogramPdfComponent.GenerateHistogramDocument(dialog.FileName, "Histogram", "Students-Directions", LegendPosition.TopRight, gistData);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return true;
}
}
}