Реализация интерфейса плагина

This commit is contained in:
bekodeg 2024-11-24 15:22:18 +04:00
parent 2513ebcc8e
commit 5ee30cdde5
8 changed files with 253 additions and 14 deletions

View File

@ -41,7 +41,7 @@ namespace Cop.Borovkov.Var3.Components
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
} }
outDataGridView.Columns.Add(column); _ = outDataGridView.Columns.Add(column);
} }
} }
@ -108,10 +108,10 @@ namespace Cop.Borovkov.Var3.Components
Type type = typeof(TType); Type type = typeof(TType);
for (int i = 0; i < insertValues.Count(); ++i) for (int i = 0; i < insertValues.Count; ++i)
{ {
var row = insertValues[i]; var row = insertValues[i];
outDataGridView.Rows.Add(); _ = outDataGridView.Rows.Add();
for (int j = 0; j < outDataGridView.ColumnCount; ++j) for (int j = 0; j < outDataGridView.ColumnCount; ++j)
{ {

View File

@ -14,7 +14,7 @@ namespace Lab3.Database.Extensions
this IServiceCollection services, this IServiceCollection services,
IConfiguration configuration) IConfiguration configuration)
{ {
services.AddDbContextPool<COPContext>( _ = services.AddDbContextPool<COPContext>(
dbContextOptions => dbContextOptions =>
{ {
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

View File

@ -19,7 +19,7 @@ namespace Lab3.Forms
var values = (await _repository.Get()).ToList(); var values = (await _repository.Get()).ToList();
for (int i = 0; i < values.Count; i++) for (int i = 0; i < values.Count; i++)
{ {
Catalog.Rows.Add(); _ = Catalog.Rows.Add();
Catalog.Rows[i].Cells[0].Value = values[i]; Catalog.Rows[i].Cells[0].Value = values[i];
} }
} }
@ -40,7 +40,7 @@ namespace Lab3.Forms
string? val = (string?)Catalog.Rows[i].Cells[0].Value; string? val = (string?)Catalog.Rows[i].Cells[0].Value;
if (string.IsNullOrEmpty(val)) if (string.IsNullOrEmpty(val))
{ {
MessageBox.Show( _ = MessageBox.Show(
"Неверные данные", "Неверные данные",
"Ошибка", "Ошибка",
MessageBoxButtons.OK, MessageBoxButtons.OK,
@ -54,7 +54,7 @@ namespace Lab3.Forms
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show( _ = MessageBox.Show(
ex.Message, ex.Message,
"Ошибка", "Ошибка",
MessageBoxButtons.OK, MessageBoxButtons.OK,
@ -66,7 +66,7 @@ namespace Lab3.Forms
{ {
if (e.KeyCode == Keys.Insert) if (e.KeyCode == Keys.Insert)
{ {
Catalog.Rows.Add(); _ = Catalog.Rows.Add();
} }
if (e.KeyCode == Keys.Delete && Catalog.SelectedRows.Count == 1) if (e.KeyCode == Keys.Delete && Catalog.SelectedRows.Count == 1)
@ -84,7 +84,7 @@ namespace Lab3.Forms
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show( _ = MessageBox.Show(
ex.Message, ex.Message,
"Ошибка", "Ошибка",
MessageBoxButtons.OK, MessageBoxButtons.OK,

View File

@ -37,7 +37,7 @@ namespace Lab3.Forms
} }
var student = await _studentRepository.GetAsync(_updatedStudentGuid.Value); var student = await _studentRepository.GetAsync(_updatedStudentGuid.Value);
NameTextBox.Text = student.Name; NameTextBox.Text = student!.Name;
StartEducationDataPicker.Value = student.StartEducation; StartEducationDataPicker.Value = student.StartEducation;
FormSelector.ComboBoxSelectedValue = student.EducationForm; FormSelector.ComboBoxSelectedValue = student.EducationForm;
@ -101,11 +101,11 @@ namespace Lab3.Forms
if (_updatedStudentGuid != null) if (_updatedStudentGuid != null)
{ {
await _studentRepository.UpdateAsync(student); _ = await _studentRepository.UpdateAsync(student);
} }
else else
{ {
await _studentRepository.CreateAsync(student); _ = await _studentRepository.CreateAsync(student);
} }
Close(); Close();
} }

View File

@ -0,0 +1,233 @@
using Cop.Borovkov.Var3.Components;
using Lab4.Interfaces;
using Lab4.Models;
using Lab3.Database.Repository.Interfaces;
using ComponentsLibrary.entities;
using ComponentsLibrary;
using ComponentsLibrary.entities.enums;
using Lab3.Database.DTO;
using CustomComponentsVar2;
using Lab3.Forms;
using Lab3.Models;
using AutoMapper;
namespace Lab4.Implementations
{
public class PluginsConvention : IPluginsConvention
{
private readonly IMapper _mapper;
private readonly IStudentRepository _studentRepository;
private readonly IEducationFormRepository _educationFormRepository;
private readonly CustomListBox _control;
private readonly CustomPdfTable _simpleDocumentCreator;
private readonly CustomExcelTable _tableCreator;
private readonly ComponentDiagram _chartCreator;
public PluginsConvention(
IMapper mapper,
IStudentRepository studentRepository,
IEducationFormRepository educationFormRepository)
{
_mapper = mapper;
_studentRepository = studentRepository;
_educationFormRepository = educationFormRepository;
_control = new();
_chartCreator = new();
_tableCreator = new();
_simpleDocumentCreator = new();
}
public string PluginName => throw new NotImplementedException();
public UserControl GetControl => _control;
public PluginsConventionElement GetElement {
get
{
var filds = _control.Selected.Split();
Guid id = Guid.Parse(filds[0]);
return new()
{
Id = id,
};
}
}
public bool CreateChartDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
using var saveFileDialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return false;
}
var data = _studentRepository.GetAsync().Result
.GroupBy(s => s.EducationForm)
.Select(s => new DataLine(
s.Key,
s.GroupBy(x => x.StartEducation.Year)
.Select(x => (year: x.Key.ToString(), count: (double)x.Count()))
.ToArray()))
.ToList();
_chartCreator.AddDiagram(
new(
saveFileDialog.FileName,
"Поступления",
"Диаграмма",
EnumAreaLegend.Bottom,
data
)
);
return true;
}
catch
{
return false;
}
}
public bool CreateSimpleDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
using var saveFileDialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return false;
}
var values = (_studentRepository.GetAsync()).Result
.Select(s => s.StudentSessions
.OrderBy(x => x.Number)
.Select(x => x.Score.ToString())
.ToArray())
.ToArray();
var tables = new string[values.Length, 6];
for (int i = 0; i < values.Length; ++i)
{
for (int j = 0; j < 6; ++j)
{
tables[i, j] = values[i][j];
}
}
_simpleDocumentCreator.SaveToPdf(new()
{
FilePath = saveFileDialog.FileName,
Title = "Сессии",
Tables = [tables]
});
return true;
}
catch
{
return false;
}
}
public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument)
{
try
{
using var saveFileDialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return false;
}
_tableCreator.SaveToExcel<StudentDTO>(new()
{
FilePath = saveFileDialog.FileName,
Title = "Студенты",
Headers =
[
("Id", nameof(StudentDTO.Id)),
("ФИО", nameof(StudentDTO.Name)),
("Форма обучения", nameof(StudentDTO.EducationForm)),
("Дата поступления", nameof(StudentDTO.StartEducation)),
],
HeaderGroups = [new() {
GroupHeader = "Образование",
FirstHeader = 2,
LastHeader = 3,
}],
Values = _studentRepository.GetAsync().Result
});
return true;
}
catch
{
return false;
}
}
public bool DeleteElement(PluginsConventionElement element)
{
try
{
_studentRepository
.DeleteAsync(element.Id).Wait();
return true;
}
catch
{
return false;
}
}
public Form GetForm(PluginsConventionElement? element = null)
=> new CreateForm(_studentRepository, _educationFormRepository, element?.Id);
public Form GetThesaurus()
=> new CatalogForm(_educationFormRepository);
public void ReloadData()
{
try
{
var students = _mapper.Map<List<StudentViewModel>>(_studentRepository.GetAsync().Result);
_control.FillValues(
students.Select(s => string.Join(" ",
[
s.Id,
s.Name,
s.EducationForm,
s.StartEducation.ToLongDateString(),
s.SessionMarks,
]
))
);
}
catch
{
}
}
}
}

View File

@ -8,4 +8,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Lab3\Lab3.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -1,3 +1,5 @@
using Lab4.Forms;
namespace Lab4 namespace Lab4
{ {
internal static class Program internal static class Program
@ -11,7 +13,7 @@ namespace Lab4
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new Form1()); Application.Run(new FormMain());
} }
} }
} }