From 3da62fe2494f2bc29e4690b819e5732fb571f89e Mon Sep 17 00:00:00 2001 From: GokaPek Date: Wed, 29 May 2024 23:23:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=20=D0=BD=D0=B5=D1=80=D0=B2=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81?= =?UTF-8?q?=D1=80=D1=8B=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ReportLogic.cs | 19 +-- .../AbstractSaveToPdfStorekeeper.cs | 71 +++++++++++ .../HelperModels/PdfInfoStorekeeper.cs | 16 +++ .../HelperModels/WordInfoStorekeeper.cs | 3 +- .../Implements/SaveToPdfStorekeeper.cs | 115 ++++++++++++++++++ .../Controllers/HomeController.cs | 25 ++++ .../Views/Home/Index.cshtml | 1 + .../Views/Home/ReportDisciplines.cshtml | 42 +++++++ .../BusinessLogicsContracts/IReportLogic.cs | 3 +- .../ViewModels/ReportDisciplineViewModel.cs | 2 +- .../Implements/StatementStorage.cs | 35 +++--- .../Controllers/DisciplineController .cs | 32 ++++- .../Controllers/TeacherController.cs | 1 - University/UniversityRestApi/Program.cs | 2 +- 14 files changed, 336 insertions(+), 31 deletions(-) create mode 100644 University/UniversityBusinessLogic/OfficePackage/AbstractSaveToPdfStorekeeper.cs create mode 100644 University/UniversityBusinessLogic/OfficePackage/HelperModels/PdfInfoStorekeeper.cs create mode 100644 University/UniversityBusinessLogic/OfficePackage/Implements/SaveToPdfStorekeeper.cs create mode 100644 University/UniversityClientApp/Views/Home/ReportDisciplines.cshtml diff --git a/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs b/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs index 8ffd687..ff35281 100644 --- a/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs @@ -1,5 +1,4 @@ using UniversityBusinessLogic.OfficePackage; -using University.ViewModels; using UniversityContracts.BindingModels; using UniversityContracts.BusinessLogicContracts; using UniversityContracts.SearchModels; @@ -12,10 +11,6 @@ namespace UniversityBusinessLogics.BusinessLogics; public class ReportLogic : IReportLogic { - public List GetDisciplines(ReportBindingModel model) - { - throw new NotImplementedException(); - } /*private readonly AbstractSaveToWord _saveToWord; private readonly AbstractSaveToExcel _saveToExcel; @@ -32,13 +27,13 @@ public class ReportLogic : IReportLogic private readonly AbstractSaveToPdfWorker _saveToPdfWorker; private readonly AbstractSaveToExcelStorekeeper _saveToExcelStorekeeper; - //private readonly AbstractSaveToPdfWorker _saveToPdfWorker; + private readonly AbstractSaveToPdfStorekeeper _saveToPdfStorekeeper; private readonly AbstractSaveToWordStorekeeper _saveToWordStorekeeper; public ReportLogic (ITeacherStorage teacherStorage, IDisciplineStorage disciplineStorage, IStudentStorage studentStorage, IStatementStorage statementStorage, IPlanOfStudyStorage planOfStudyStorage, AbstractSaveToExcelWorker saveToExcelWorker, AbstractSaveToWordWorker saveToWordWorker ,AbstractSaveToPdfWorker saveToPdfWorker, AbstractSaveToWordStorekeeper saveToWordStorekeeper, - AbstractSaveToExcelStorekeeper saveToExcelStorekeeper) + AbstractSaveToExcelStorekeeper saveToExcelStorekeeper, AbstractSaveToPdfStorekeeper saveToPdfStorekeeper) { _teacherStorage = teacherStorage; _disciplineStorage = disciplineStorage; @@ -52,6 +47,7 @@ public class ReportLogic : IReportLogic _saveToWordStorekeeper = saveToWordStorekeeper; _saveToExcelStorekeeper = saveToExcelStorekeeper; + _saveToPdfStorekeeper = saveToPdfStorekeeper; } public List GetTeachers(int userId) { @@ -106,7 +102,7 @@ public class ReportLogic : IReportLogic public List GetDisciplines(ReportDateRangeBindingModel model) { - var disciplines = _disciplineStorage.GetFullList(); + var disciplines = _disciplineStorage.GetFilteredList(new DisciplineSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo}); var reportDisciplineViewModels = new List(); @@ -258,7 +254,12 @@ public class ReportLogic : IReportLogic public void SendDisciplinesToEmail(ReportDateRangeBindingModel option, string email) { - throw new NotImplementedException(); + _saveToPdfWorker.CreateDoc(new PdfInfoWorker + { + FileName = option.FileName, + Title = "Отчёт по дисциплинам", + PlanOfStudyAndStudent = GetPlanOfStudyAndStudents() + }); } public void SendPlanOfStudyToEmail(ReportBindingModel option) diff --git a/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToPdfStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToPdfStorekeeper.cs new file mode 100644 index 0000000..b0f252c --- /dev/null +++ b/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToPdfStorekeeper.cs @@ -0,0 +1,71 @@ +using UniversityBusinessLogic.OfficePackage.HelperEnums; +using UniversityBusinessLogic.OfficePackage.HelperModels; + +namespace UniversityBusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToPdfStorekeeper + { + public void CreateDoc(PdfInfoStorekeeper info) + { + CreatePdf(info); + CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); + + CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "4 cm" }); + + CreateRow(new PdfRowParameters + { + Texts = new List { "Дисциплина", "План обучения", "Ведомость" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + foreach (var item in info.Disciplines) + { + foreach (var plOfSt in item.PlanOfStudys) { + foreach (var statement in item.Statements) + { + CreateRow(new PdfRowParameters + { + Texts = new List { item.DisciplineName, plOfSt, statement}, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + } + } + SavePdf(info); + } + + /// + /// Создание doc-файла + /// + /// + protected abstract void CreatePdf(PdfInfoStorekeeper info); + + /// + /// Создание параграфа с текстом + /// + /// + /// + protected abstract void CreateParagraph(PdfParagraph paragraph); + + /// + /// Создание таблицы + /// + /// + /// + protected abstract void CreateTable(List columns); + + /// + /// Создание и заполнение строки + /// + /// + protected abstract void CreateRow(PdfRowParameters rowParameters); + + /// + /// Сохранение файла + /// + /// + protected abstract void SavePdf(PdfInfoStorekeeper info); + } +} diff --git a/University/UniversityBusinessLogic/OfficePackage/HelperModels/PdfInfoStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/HelperModels/PdfInfoStorekeeper.cs new file mode 100644 index 0000000..5366c89 --- /dev/null +++ b/University/UniversityBusinessLogic/OfficePackage/HelperModels/PdfInfoStorekeeper.cs @@ -0,0 +1,16 @@ +using UniversityContracts.ViewModels; + +namespace UniversityBusinessLogic.OfficePackage.HelperModels +{ + public class PdfInfoStorekeeper + { + public string? FileName { get; set; } + public Stream? Stream { get; set; } + + public string Title { get; set; } = string.Empty; + public DateOnly DateFrom { get; set; } + public DateOnly DateTo { get; set; } + public List ReportObjects { get; set; } = new(); + public List Disciplines { get; set; } = new(); + } +} diff --git a/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs index ead12cd..03a6d7f 100644 --- a/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs +++ b/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs @@ -1,5 +1,4 @@ -using University.ViewModels; -using UniversityContracts.ViewModels; +using UniversityContracts.ViewModels; namespace UniversityBusinessLogic.OfficePackage.HelperModels { diff --git a/University/UniversityBusinessLogic/OfficePackage/Implements/SaveToPdfStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/Implements/SaveToPdfStorekeeper.cs new file mode 100644 index 0000000..88ab317 --- /dev/null +++ b/University/UniversityBusinessLogic/OfficePackage/Implements/SaveToPdfStorekeeper.cs @@ -0,0 +1,115 @@ +using UniversityBusinessLogic.OfficePackage.HelperEnums; +using UniversityBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; + +namespace UniversityBusinessLogic.OfficePackage.Implements +{ + public class SaveToPdfStorekeeper : AbstractSaveToPdfStorekeeper + { + private Document? _document; + + private Section? _section; + + private Table? _table; + + private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type) + { + return type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Right => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + + /// + /// Создание стилей для документа + /// + /// + private static void DefineStyles(Document document) + { + var style = document.Styles["Normal"]; + style.Font.Name = "Times New Roman"; + style.Font.Size = 14; + + style = document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + } + + protected override void CreatePdf(PdfInfoStorekeeper info) + { + _document = new Document(); + DefineStyles(_document); + _document.DefaultPageSetup.Orientation = Orientation.Landscape; + _section = _document.AddSection(); + } + + protected override void CreateParagraph(PdfParagraph pdfParagraph) + { + if (_section == null) + { + return; + } + var paragraph = _section.AddParagraph(pdfParagraph.Text); + paragraph.Format.SpaceAfter = "1cm"; + paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment); + paragraph.Style = pdfParagraph.Style; + } + + protected override void CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + + protected override void CreateRow(PdfRowParameters rowParameters) + { + if (_table == null) + { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Texts.Count; ++i) + { + row.Cells[i].AddParagraph(rowParameters.Texts[i]); + + if (!string.IsNullOrEmpty(rowParameters.Style)) + { + row.Cells[i].Style = rowParameters.Style; + } + + Unit borderWidth = 0.5; + + row.Cells[i].Borders.Left.Width = borderWidth; + row.Cells[i].Borders.Right.Width = borderWidth; + row.Cells[i].Borders.Top.Width = borderWidth; + row.Cells[i].Borders.Bottom.Width = borderWidth; + + row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment); + row.Cells[i].VerticalAlignment = VerticalAlignment.Center; + } + } + + protected override void SavePdf(PdfInfoStorekeeper info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } +} \ No newline at end of file diff --git a/University/UniversityClientApp/Controllers/HomeController.cs b/University/UniversityClientApp/Controllers/HomeController.cs index 9928c54..ec8dea0 100644 --- a/University/UniversityClientApp/Controllers/HomeController.cs +++ b/University/UniversityClientApp/Controllers/HomeController.cs @@ -217,6 +217,31 @@ namespace UniversityClientApp.Controllers } } + [HttpGet] + public IActionResult ReportDisciplines() + { + if (APIStorekeeper.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + + [HttpGet] + public IActionResult GetReportDisciplines(DateOnly dateFrom, DateOnly dateTo) + { + if (APIStorekeeper.Client == null) + { + return Redirect("~/Home/Enter"); + } + + // + //var reportData = APIStorekeeper.GetRequest>($"api/disciplines/GetReportDisciplines?datefrom={dateFrom}&dateto={dateTo}"); + var reportData = APIStorekeeper.GetRequest>($"api/discipline/getreportdisciplines"); + // + return PartialView("ReportDisciplines", reportData); + } + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/University/UniversityClientApp/Views/Home/Index.cshtml b/University/UniversityClientApp/Views/Home/Index.cshtml index 25e967a..b5c64a9 100644 --- a/University/UniversityClientApp/Views/Home/Index.cshtml +++ b/University/UniversityClientApp/Views/Home/Index.cshtml @@ -8,6 +8,7 @@ Teachers Statements Report + ReportDisciplines
diff --git a/University/UniversityClientApp/Views/Home/ReportDisciplines.cshtml b/University/UniversityClientApp/Views/Home/ReportDisciplines.cshtml new file mode 100644 index 0000000..1e90a9f --- /dev/null +++ b/University/UniversityClientApp/Views/Home/ReportDisciplines.cshtml @@ -0,0 +1,42 @@ +@using UniversityContracts.ViewModels +@model List + +
+
+

Отчёт за период по дисциплинам

+
+
+ + +
+
+ + +
+
+
+
+
+ +@if (Model != null && Model.Any()) +{ + + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
Название дисциплиныПланы обученияЗаявления
@item.DisciplineName@string.Join(", ", item.PlanOfStudys)@string.Join(", ", item.Statements)
+} \ No newline at end of file diff --git a/University/UniversityContracts/BusinessLogicsContracts/IReportLogic.cs b/University/UniversityContracts/BusinessLogicsContracts/IReportLogic.cs index 9be881f..85a40d8 100644 --- a/University/UniversityContracts/BusinessLogicsContracts/IReportLogic.cs +++ b/University/UniversityContracts/BusinessLogicsContracts/IReportLogic.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using University.ViewModels; using UniversityContracts.BindingModels; using UniversityContracts.ViewModels; @@ -17,7 +16,7 @@ namespace UniversityContracts.BusinessLogicContracts /// /// List GetTeachers(int userId); - List GetDisciplines(ReportBindingModel model); + List GetDisciplines(ReportDateRangeBindingModel model); List GetPlanOfStudyAndDisciplines(int userId); List GetPlanOfStudyAndStudents(); diff --git a/University/UniversityContracts/ViewModels/ReportDisciplineViewModel.cs b/University/UniversityContracts/ViewModels/ReportDisciplineViewModel.cs index cfd4fcf..35c1e0b 100644 --- a/University/UniversityContracts/ViewModels/ReportDisciplineViewModel.cs +++ b/University/UniversityContracts/ViewModels/ReportDisciplineViewModel.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace University.ViewModels +namespace UniversityContracts.ViewModels { public class ReportDisciplineViewModel { diff --git a/University/UniversityDatabaseImplement/Implements/StatementStorage.cs b/University/UniversityDatabaseImplement/Implements/StatementStorage.cs index b888ac9..a886fdd 100644 --- a/University/UniversityDatabaseImplement/Implements/StatementStorage.cs +++ b/University/UniversityDatabaseImplement/Implements/StatementStorage.cs @@ -24,21 +24,28 @@ namespace UniversityDatabaseImplement.Implements return context.Statements.Include(x => x.Teacher).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - public List GetFilteredList(StatementSearchModel model) - { - if (!model.Id.HasValue && !model.Date.HasValue) - { - return new(); - } - using var context = new UniversityDatabase(); - return context.Statements - .Where(x => x.Id == model.Id || model.Date <= x.Date) - .Include(x => x.Teacher) - .Select(x => x.GetViewModel) - .ToList(); - } + public List GetFilteredList(StatementSearchModel model) + { + using var context = new UniversityDatabase(); - public List GetFullList() + // Фильтр по Id + IQueryable query = context.Statements; + if (model.Id.HasValue) + { + query = query.Where(x => x.Id == model.Id.Value); + } + + // Фильтр по Date + if (model.Date.HasValue) + { + query = query.Where(x => x.Date <= model.Date.Value); + } + + // Загрузка связанных данных + return query.Include(x => x.Teacher).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() { using var context = new UniversityDatabase(); return context.Statements.Include(x => x.Teacher).Select(x => x.GetViewModel).ToList(); diff --git a/University/UniversityRestApi/Controllers/DisciplineController .cs b/University/UniversityRestApi/Controllers/DisciplineController .cs index 1a3e3f9..05e592c 100644 --- a/University/UniversityRestApi/Controllers/DisciplineController .cs +++ b/University/UniversityRestApi/Controllers/DisciplineController .cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicContracts; using UniversityContracts.BusinessLogicsContracts; using UniversityContracts.SearchModels; using UniversityContracts.ViewModels; @@ -12,10 +13,12 @@ namespace UniversityRestApi.Controllers { private readonly ILogger _logger; private readonly IDisciplineLogic _logic; - public DisciplineController(IDisciplineLogic logic, ILogger logger) + private readonly IReportLogic _reportLogic; + public DisciplineController(IDisciplineLogic logic, ILogger logger, IReportLogic reportLogic) { _logic = logic; _logger = logger; + _reportLogic = reportLogic; } [HttpGet] public List? GetDisciplines(int userId) @@ -29,6 +32,33 @@ namespace UniversityRestApi.Controllers _logger.LogError(ex, "Ошибка получения списка дисциплин"); throw; } + } + /*[HttpPost] + public List GetReportDisciplines(DateOnly dateFrom, DateOnly dateTo) + { + try + { + return _reportLogic.GetDisciplines(new ReportDateRangeBindingModel { DateFrom = dateFrom, DateTo = dateTo }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка дисциплин"); + throw; + } + }*/ + + [HttpGet] + public List GetReportDisciplines() + { + try + { + return _reportLogic.GetDisciplines(new ReportDateRangeBindingModel { DateFrom = DateOnly.MinValue, DateTo = DateOnly.MaxValue }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка дисциплин"); + throw; + } } [HttpPost] public void CreateDiscipline(DisciplineBindingModel model) diff --git a/University/UniversityRestApi/Controllers/TeacherController.cs b/University/UniversityRestApi/Controllers/TeacherController.cs index 90beb0f..ba235ef 100644 --- a/University/UniversityRestApi/Controllers/TeacherController.cs +++ b/University/UniversityRestApi/Controllers/TeacherController.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Mvc; -using University.ViewModels; using UniversityContracts.BindingModels; using UniversityContracts.BusinessLogicContracts; using UniversityContracts.BusinessLogicsContracts; diff --git a/University/UniversityRestApi/Program.cs b/University/UniversityRestApi/Program.cs index 72184a7..9e59da3 100644 --- a/University/UniversityRestApi/Program.cs +++ b/University/UniversityRestApi/Program.cs @@ -30,7 +30,7 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); -// builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient();