From f108c6d8f05858aef78a145e65585e6ad7c5ef8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 23 May 2023 20:48:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D0=B8=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ReportCustomerLogic.cs | 33 +++- .../OfficePackage/PdfBuilderCustomer.cs | 185 ++++++++++++++++++ .../IReportCustomerLogic.cs | 2 + .../Views/Home/GetReport.cshtml | 55 ++++++ .../wwwroot/js/Report/reportpdf.js | 1 + .../CaseAccountingDatabase.cs | 4 +- ...roller .cs => ReportCustomerController.cs} | 8 +- .../CaseAccountingRestApi/Program.cs | 6 + 8 files changed, 286 insertions(+), 8 deletions(-) create mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/PdfBuilderCustomer.cs create mode 100644 CaseAccounting/CaseAccountingCustomerView/Views/Home/GetReport.cshtml create mode 100644 CaseAccounting/CaseAccountingCustomerView/wwwroot/js/Report/reportpdf.js rename CaseAccounting/CaseAccountingRestApi/Controllers/{ReportCustomerController .cs => ReportCustomerController.cs} (74%) diff --git a/CaseAccounting/CaseAccountingBusinessLogics/BusinessLogics/ReportCustomerLogic.cs b/CaseAccounting/CaseAccountingBusinessLogics/BusinessLogics/ReportCustomerLogic.cs index 12003e2..77f317c 100644 --- a/CaseAccounting/CaseAccountingBusinessLogics/BusinessLogics/ReportCustomerLogic.cs +++ b/CaseAccounting/CaseAccountingBusinessLogics/BusinessLogics/ReportCustomerLogic.cs @@ -1,4 +1,5 @@ -using CaseAccountingBusinessLogic.OfficePackage; +using CaseAccountingBusinessLogic.BusinessLogic.OfficePackage; +using CaseAccountingBusinessLogic.OfficePackage; using CaseAccountingBusinessLogic.OfficePackage.HelperModels; using CaseAccountingContracts.BindingModels; using CaseAccountingContracts.BusinessLogicContracts; @@ -22,9 +23,11 @@ namespace CaseAccountingBusinessLogic.BusinessLogics private readonly ISpecializationStorage _specializationStorage; private readonly WordBuilderCustomer _wordBuilder; private readonly ExcelBuilderCustomer _excelBuilder; + private readonly PdfBuilderCustomer _pdfBuilder; + private readonly MailSender _mailSender; public ReportCustomerLogic(ICaseStorage caseStorage, IHearingStorage hearingStorage, ILawyerStorage lawyerStorage, ISpecializationStorage specializationStorage, - WordBuilderCustomer wordBuilder, ExcelBuilderCustomer excelBuilder) + WordBuilderCustomer wordBuilder, ExcelBuilderCustomer excelBuilder, PdfBuilderCustomer pdfBuilder, MailSender mailSender) { _caseStorage = caseStorage; _hearingStorage = hearingStorage; @@ -32,6 +35,8 @@ namespace CaseAccountingBusinessLogic.BusinessLogics _specializationStorage = specializationStorage; _wordBuilder = wordBuilder; _excelBuilder = excelBuilder; + _pdfBuilder = pdfBuilder ?? throw new ArgumentNullException(nameof(pdfBuilder)); + _mailSender = mailSender ?? throw new ArgumentNullException(nameof(mailSender)); } public List GetHearingSpecialization(ReportBindingModel model) @@ -98,5 +103,29 @@ namespace CaseAccountingBusinessLogic.BusinessLogics } return file; } + + public void SendByMailStatusReport(ReportBindingModel reportModel) + { + throw new NotImplementedException(); + } + + /*public void SendByMailStatusReport(ReportBindingModel reportModel) + { + byte[] file = _pdfBuilder.GetHearingSpecializationReportFile(new() + { + Title = "Отчет по слушаниям", + DateFrom = reportModel.DateFrom, + DateTo = reportModel.DateTo, + //Records = GetHearingLawyer(reportModel) + }); + _mailSender.SendMailAsync(new() + { + MailAddress = reportModel.UserEmail, + Subject = "Отчет по слушаниям", + Text = $"За период с {reportModel.DateFrom.ToShortDateString()} " + + $"по {reportModel.DateTo.ToShortDateString()}.", + File = file + }); + }*/ } } diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/PdfBuilderCustomer.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/PdfBuilderCustomer.cs new file mode 100644 index 0000000..1e0c2a8 --- /dev/null +++ b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/PdfBuilderCustomer.cs @@ -0,0 +1,185 @@ +using CaseAccountingBusinessLogic.OfficePackage.HelperEnums; +using CaseAccountingBusinessLogic.OfficePackage.HelperModels; +using CaseAccountingContracts.ViewModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.Rendering; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MigraDoc.DocumentObjectModel.Tables; + +namespace CaseAccountingBusinessLogic.OfficePackage +{ + public class PdfBuilderCustomer + { + private readonly string tempFileName = "temp.pdf"; + 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, + _ => ParagraphAlignment.Justify, + }; + } + + private 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; + } + + public void CreateDocument() + { + document = new Document(); + DefineStyles(document); + section = document.AddSection(); + } + + public 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; + } + + public void CreateTable(List columns) + { + if (document == null) + { + return; + } + table = document.LastSection.AddTable(); + foreach (var elem in columns) + { + table.AddColumn(elem); + } + } + + public 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; + } + } + + private void Save() + { + + // Регистрация провайдера кодировки для кодировки 1252, без этого ошибОчка была + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + var renderer = new PdfDocumentRenderer(true) + { + Document = document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(tempFileName); + } + + public byte[] GetFile() + { + Save(); + byte[] file = File.ReadAllBytes(tempFileName); + File.Delete(tempFileName); + return file; + } + + /*public byte[] GetHearingSpecializationReportFile(PdfData data) + { + CreateDocument(); + + CreateParagraph(new PdfParagraph + { + Text = data.Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + CreateParagraph(new PdfParagraph + { + Text = $"за период с {data.DateFrom.ToShortDateString()} " + + $"по {data.DateTo.ToShortDateString()}", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + CreateTable(new List { "4cm", "5cm", "3cm", "3cm" }); + + CreateRow(new PdfRowParameters + { + Texts = new List { "Номер слушания", "Дело", "Дата проведения", "Юрист" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + foreach (var record in data.Records) + { + List<> casesAndLawyes = record.CaseLawyers; + int recordHeight = casesAndLawyes.Count + 1; + for (int i = 0; i < recordHeight; i++) + { + List cellsData = new() { "", "", "", "" }; + if (i == 0) + { + cellsData[0] = record.Hearing; + CreateRow(new PdfRowParameters + { + Texts = cellsData, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + continue; + } + int k = i - 1; + if (k < casesAndLawyes.Count) + { + cellsData[1] = casesAndLawyes[k].Case; + cellsData[2] = casesAndLawyes[k].Date.ToString("yyyy-MM-dd"); + cellsData[3] = casesAndLawyes[k].Lawyer; + } + CreateRow(new PdfRowParameters + { + Texts = cellsData, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + } + + return GetFile(); + }*/ + } +} diff --git a/CaseAccounting/CaseAccountingContracts/BusinessLogicContracts/IReportCustomerLogic.cs b/CaseAccounting/CaseAccountingContracts/BusinessLogicContracts/IReportCustomerLogic.cs index 8fe434e..08bccf1 100644 --- a/CaseAccounting/CaseAccountingContracts/BusinessLogicContracts/IReportCustomerLogic.cs +++ b/CaseAccounting/CaseAccountingContracts/BusinessLogicContracts/IReportCustomerLogic.cs @@ -15,5 +15,7 @@ namespace CaseAccountingContracts.BusinessLogicContracts List GetHearingSpecialization(ReportBindingModel model); byte[] SaveListFile(LawyerHearingListBindingModel model); + + void SendByMailStatusReport(ReportBindingModel reportModel); } } diff --git a/CaseAccounting/CaseAccountingCustomerView/Views/Home/GetReport.cshtml b/CaseAccounting/CaseAccountingCustomerView/Views/Home/GetReport.cshtml new file mode 100644 index 0000000..e0b7210 --- /dev/null +++ b/CaseAccounting/CaseAccountingCustomerView/Views/Home/GetReport.cshtml @@ -0,0 +1,55 @@ +@{ + ViewData["Title"] = "Отчет"; +} + +
+

Отчет по слушаниям

+
+ +
+
+

+
+
+ +
+
+

Дата начала:

+ +
+
+

Дата конца:

+ +
+ + +
+ +

+ За период с  + ... +  по  + ... +

+
+ + + + + + + + + + + +
Номер слушанияДелоДата проведенияСпециализация
+
+ +
+ + \ No newline at end of file diff --git a/CaseAccounting/CaseAccountingCustomerView/wwwroot/js/Report/reportpdf.js b/CaseAccounting/CaseAccountingCustomerView/wwwroot/js/Report/reportpdf.js new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/CaseAccounting/CaseAccountingCustomerView/wwwroot/js/Report/reportpdf.js @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs b/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs index 75477ba..55243a9 100644 --- a/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs +++ b/CaseAccounting/CaseAccountingDataBaseImplement/CaseAccountingDatabase.cs @@ -18,8 +18,8 @@ namespace CaseAccountingDataBaseImplement Host=localhost; Port=5432; Database=CaseAccountingDatabase; - Username=courseuser; - Password=courseuser"); + Username=postgres; + Password=postgres"); } base.OnConfiguring(optionsBuilder); } diff --git a/CaseAccounting/CaseAccountingRestApi/Controllers/ReportCustomerController .cs b/CaseAccounting/CaseAccountingRestApi/Controllers/ReportCustomerController.cs similarity index 74% rename from CaseAccounting/CaseAccountingRestApi/Controllers/ReportCustomerController .cs rename to CaseAccounting/CaseAccountingRestApi/Controllers/ReportCustomerController.cs index 2f77a0a..ad6e11d 100644 --- a/CaseAccounting/CaseAccountingRestApi/Controllers/ReportCustomerController .cs +++ b/CaseAccounting/CaseAccountingRestApi/Controllers/ReportCustomerController.cs @@ -7,18 +7,18 @@ namespace CaseAccountingRestApi.Controllers [Route("api/[controller]/[action]")] [ApiController] public class ReportCustomerController : Controller - { - private readonly IReportCustomerLogic _reportCustomerLogic; + { + private readonly IReportCustomerLogic reportLogic; public ReportCustomerController(IReportCustomerLogic reportLogic) { - _reportCustomerLogic = reportLogic; + this.reportLogic = reportLogic; } [HttpPost] public byte[] LawyerHearinglist(LawyerHearingListBindingModel listModel) { - byte[] file = _reportCustomerLogic.SaveListFile(listModel); + byte[] file = reportLogic.SaveListFile(listModel); return file; } } diff --git a/CaseAccounting/CaseAccountingRestApi/Program.cs b/CaseAccounting/CaseAccountingRestApi/Program.cs index 2f6c3c2..cb61995 100644 --- a/CaseAccounting/CaseAccountingRestApi/Program.cs +++ b/CaseAccounting/CaseAccountingRestApi/Program.cs @@ -1,6 +1,7 @@ using CaseAccountingBusinessLogic.BusinessLogic.OfficePackage; using CaseAccountingBusinessLogic.BusinessLogics; using CaseAccountingBusinessLogic.OfficePackage; +using CaseAccountingBusinessLogic.OfficePackage.HelperModels; using CaseAccountingContracts.BindingModels; using CaseAccountingContracts.BusinessLogicContracts; using CaseAccountingContracts.StoragesContracts; @@ -30,6 +31,11 @@ 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();