From 6b1ad9142c748409d55f0e32d076c1e02d7e098e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 May 2024 02:40:05 +0400 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D0=B7=D0=BD=D0=B5=D1=81=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=98?= =?UTF-8?q?=D1=81=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ExecutorLogic.cs | 2 +- .../BusinessLogics/ReportLogicExecutor.cs | 145 +++++++- .../BusinessLogics/ReportLogicGuarantor.cs | 32 +- .../AbstractSaveToExcelExecutor.cs | 72 +++- .../AbstractSaveToPdfExecutor.cs | 77 ++++- .../AbstractSaveToWordExecutor.cs | 54 ++- .../AbstractSaveToWordGuarantor.cs | 72 +++- .../HelperModels/ExcelInfoExecutor.cs | 7 +- .../HelperModels/PdfInfoExecutor.cs | 10 +- .../HelperModels/WordInfoExecutor.cs | 8 +- .../Implements/SaveToExcelExecutor.cs | 323 +++++++++++++++++- .../Implements/SaveToExcelGuarantor.cs | 4 +- .../Implements/SaveToPdfExecutor.cs | 102 +++++- .../Implements/SaveToWordExecutor.cs | 125 ++++++- 14 files changed, 993 insertions(+), 40 deletions(-) diff --git a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ExecutorLogic.cs b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ExecutorLogic.cs index 4300395..ee76d7e 100644 --- a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ExecutorLogic.cs +++ b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ExecutorLogic.cs @@ -14,7 +14,7 @@ namespace LawCompanyBusinessLogic.BusinessLogics { public class ExecutorLogic : IExecutorLogic { - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly IExecutorStorage _executorStorage; public ExecutorLogic(ILogger logger, IExecutorStorage executorStorage) diff --git a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicExecutor.cs b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicExecutor.cs index 7efe614..3b40b0e 100644 --- a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicExecutor.cs @@ -6,6 +6,7 @@ using LawCompanyContracts.SearchModels; using LawCompanyContracts.StoragesContracts; using LawCompanyContracts.ViewModels; using LawCompanyDatabaseImplement.Models; +using LawCompanyDatabaseImplement.Implements; namespace HotelBusinessLogic.BusinessLogics { @@ -16,24 +17,158 @@ namespace HotelBusinessLogic.BusinessLogics private readonly IVisitStorage _visitStorage; private readonly IConsultationStorage _consultationStorage; private readonly ICaseStorage _caseStorage; + private readonly IClientStorage _clientStorage; private readonly AbstractSaveToExcelExecutor _saveToExcel; private readonly AbstractSaveToWordExecutor _saveToWord; private readonly AbstractSaveToPdfExecutor _saveToPdf; - public ReportLogicGuarantor(IHearingStorage hearingStorage, ILawyerStorage lawyerStorage, - IVisitStorage visitStorage, IConsultationStorage consultationStorage, ICaseStorage caseStorage, - AbstractSaveToExcelExecutor saveToExcel, AbstractSaveToWordExecutor saveToWord, AbstractSaveToPdfExecutor saveToPdf) + public ReportLogicExecutor(IHearingStorage hearingStorage, ILawyerStorage lawyerStorage, + IVisitStorage visitStorage, IConsultationStorage consultationStorage, ICaseStorage caseStorage, IClientStorage clientStorage, + AbstractSaveToExcelExecutor saveToExcel, AbstractSaveToWordExecutor saveToWord, AbstractSaveToPdfExecutor saveToPdf) { _hearingStorage = hearingStorage; _lawyerStorage = lawyerStorage; _visitStorage = visitStorage; _consultationStorage = consultationStorage; - _caseStorage = caseStorage; + _clientStorage = clientStorage; + _caseStorage = caseStorage; _saveToExcel = saveToExcel; _saveToWord = saveToWord; _saveToPdf = saveToPdf; } - + public List GetClientHearing(List Ids) + { + if (Ids == null || !Ids.Any()) + { + return new List(); + } + + var clients = _clientStorage.GetFullList(); + var hearings = _hearingStorage.GetFullList(); + var visits = _visitStorage.GetFullList(); + + var filteredClients = clients.Where(c => Ids.Contains(c.Id)).ToList(); + var result = new List(); + + foreach (var client in filteredClients) + { + var record = new ReportClientHearingViewModel + { + FIO = client.FIO, + Hearing = new List>() + }; + + var clientVisits = visits.Where(v => v.VisitClients.ContainsKey(client.Id)).ToList(); + foreach (var visit in clientVisits) + { + if (visit.HearingId.HasValue) + { + var hearing = hearings.FirstOrDefault(h => h.Id == visit.HearingId.Value); + if (hearing != null) + { + record.Hearing.Add(new Tuple(hearing.Judge, hearing.HearingDate)); + } + } + } + + result.Add(record); + } + + return result; + } + + public List GetClients(ReportExecutorBindingModel model) + { + var listAll = new List(); + + // Получаем список всех дел для указанного исполнителя и за указанный период + var cases = _caseStorage.GetFilteredList(new CaseSearchModel + { + ExecutorId = model.ExecutorId, + DateFrom = model.DateFrom, + DateTo = model.DateTo + }); + + // Добавляем информацию о клиентах из дел + foreach (var _case in cases) + { + foreach (var cc in _case.CaseClients.Values) + { + listAll.Add(new ReportClientsViewModel + { + FIO = cc.FIO, + Name = _case.Name, + Status = _case.Status + }); + } + + } + + // Получаем список всех консультаций для указанного исполнителя и за указанный период + var consultations = _consultationStorage.GetFilteredList(new ConsultationSearchModel + { + GuarantorId = model.ExecutorId, + DateFrom = model.DateFrom, + DateTo = model.DateTo + }); + + // Добавляем информацию о клиентах из консультаций + foreach (var consultation in consultations) + { + foreach (var cc in consultation.Case.CaseClients.Values) + { + listAll.Add(new ReportClientsViewModel + { + FIO = cc.FIO, + ConsultationDate = consultation.ConsultationDate, + Cost = consultation.Cost + }); + } + } + + return listAll; + } + + public void SaveClientHearingToExcelFile(ReportExecutorBindingModel model) + { + _saveToExcel.CreateReport(new ExcelInfoExecutor + { + FileName = model.FileName, + Title = "Список слушаний", + ClientHearings = GetClientHearing(model.Ids) + }); + } + + public void SaveClientHearingToWordFile(ReportExecutorBindingModel model) + { + _saveToWord.CreateDoc(new WordInfoExecutor + { + FileName = model.FileName, + Title = "Список слушаний", + ClientHearings = GetClientHearing(model.Ids) + }); + } + + public void SaveClientsToPdfFile(ReportExecutorBindingModel model) + { + if (model.DateFrom == null) + { + throw new ArgumentException("Дата начала не задана"); + } + + if (model.DateTo == null) + { + throw new ArgumentException("Дата окончания не задана"); + } + _saveToPdf.CreateDoc(new PdfInfoExecutor + { + FileName = model.FileName, + Title = "Список клиентов", + DateFrom = model.DateFrom!.Value, + DateTo = model.DateTo!.Value, + Clients = GetClients(model) + }); + } } } diff --git a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicGuarantor.cs b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicGuarantor.cs index 526d7e1..0866279 100644 --- a/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicGuarantor.cs +++ b/LawCompany/LawCompanyBusinessLogic/BusinessLogics/ReportLogicGuarantor.cs @@ -9,9 +9,9 @@ using LawCompanyDatabaseImplement.Models; namespace LawCompanyBusinessLogic.BusinessLogics { - public class ReportLogicGuarantor : IReportGuarantorLogic - { - private readonly IVisitStorage _visitStorage; + public class ReportLogicGuarantor : IReportGuarantorLogic + { + /*private readonly IVisitStorage _visitStorage; private readonly IClientStorage _clientStorage; private readonly ICaseStorage _caseStorage; private readonly IConsultationStorage _consultationStorage; @@ -32,8 +32,30 @@ namespace LawCompanyBusinessLogic.BusinessLogics _saveToExcel = saveToExcel; _saveToWord = saveToWord; _saveToPdf = saveToPdf; - } + }*/ + public List GetLawyerHearing(List Ids) + { + throw new NotImplementedException(); + } + public List GetLawyers(ReportGuarantorBindingModel model) + { + throw new NotImplementedException(); + } - } + public void SaveLawyerHearingToExcelFile(ReportGuarantorBindingModel model) + { + throw new NotImplementedException(); + } + + public void SaveLawyerHearingToWordFile(ReportGuarantorBindingModel model) + { + throw new NotImplementedException(); + } + + public void SaveLawyersToPdfFile(ReportGuarantorBindingModel model) + { + throw new NotImplementedException(); + } + } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToExcelExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToExcelExecutor.cs index 974b44c..2cf3e7e 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToExcelExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToExcelExecutor.cs @@ -1,4 +1,6 @@ -using System; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,73 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage { - internal class AbstractSaveToExcelExecutor + public abstract class AbstractSaveToExcelExecutor { + public void CreateReport(ExcelInfoExecutor info) + { + CreateExcel(info); + + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = 1, + Text = info.Title, + StyleInfo = ExcelStyleInfoType.Title + }); + + MergeCells(new ExcelMergeParameters + { + CellFromName = "A1", + CellToName = "C1" + }); + + uint rowIndex = 2; + + foreach (var mc in info.ClientHearings) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = rowIndex, + Text = mc.FIO, + StyleInfo = ExcelStyleInfoType.Text + }); + + rowIndex++; + + foreach (var conference in mc.Hearing) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "B", + RowIndex = rowIndex, + Text = conference.Item1, + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "C", + RowIndex = rowIndex, + Text = conference.Item2.ToString("d"), + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + + rowIndex++; + } + + rowIndex++; + } + + SaveExcel(info); + } + + protected abstract void CreateExcel(ExcelInfoExecutor info); + + protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams); + + protected abstract void MergeCells(ExcelMergeParameters excelParams); + + protected abstract void SaveExcel(ExcelInfoExecutor info); } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToPdfExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToPdfExecutor.cs index 7e96ab8..0204484 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToPdfExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToPdfExecutor.cs @@ -1,4 +1,6 @@ -using System; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,78 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage { - internal class AbstractSaveToPdfExecutor + public abstract class AbstractSaveToPdfExecutor { + public void CreateDoc(PdfInfoExecutor info) + { + CreatePdf(info); + + CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + CreateParagraph(new PdfParagraph + { + Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + CreateTable(new List { "4cm", "5cm", "3cm", "4cm", "2cm" }); + + CreateRow(new PdfRowParameters + { + Texts = new List { "ФИО клиента", "Дата консультации", "Стоимость консультации", "Название дела", "Статус дела" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + foreach (var member in info.Clients) + { + bool IsDate = true; + if (member.ConsultationDate.ToShortDateString() == "01.01.0001") + { + IsDate = false; + } + + bool IsCost = true; + if (member.Cost.ToString() == "0") + { + IsCost = false; + } + + CreateRow(new PdfRowParameters + { + Texts = new List + { + member.FIO, + IsDate is true ? member.ConsultationDate.ToShortDateString() : string.Empty, + IsCost is true ? member.Cost.ToString() : string.Empty, + member.Name, + member.Status.ToString(), + }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + + CreateParagraph(new PdfParagraph + { + Text = $"Итого: {info.Clients.Sum(x => x.Cost)}\t", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Rigth + }); + + SavePdf(info); + } + + protected abstract void CreatePdf(PdfInfoExecutor info); + protected abstract void CreateParagraph(PdfParagraph paragraph); + protected abstract void CreateTable(List columns); + protected abstract void CreateRow(PdfRowParameters rowParameters); + protected abstract void SavePdf(PdfInfoExecutor info); } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordExecutor.cs index b50b987..94893cd 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordExecutor.cs @@ -1,4 +1,6 @@ -using System; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,55 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage { - internal class AbstractSaveToWordExecutor + public abstract class AbstractSaveToWordExecutor { + public void CreateDoc(WordInfoExecutor info) + { + CreateWord(info); + + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> { (info.Title, new WordTextProperties { Bold = true, Size = "24", }) }, + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Center + } + }); + + foreach (var mc in info.ClientHearings) + { + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> + { ( mc.FIO, new WordTextProperties { Size = "20", Bold=true})}, + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Both + } + }); + + foreach (var conference in mc.Hearing) + { + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> + { (conference.Item1 + " - ", new WordTextProperties { Size = "16", Bold=false}), + (conference.Item2.ToShortDateString(), new WordTextProperties { Size = "16", Bold=false})}, + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Both + } + }); + } + } + SaveWord(info); + } + + protected abstract void CreateWord(WordInfoExecutor info); + protected abstract void CreateParagraph(WordParagraph paragraph); + protected abstract void SaveWord(WordInfoExecutor info); } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordGuarantor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordGuarantor.cs index d452793..5ef250f 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordGuarantor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/AbstractSaveToWordGuarantor.cs @@ -1,4 +1,6 @@ -using System; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,73 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage { - internal class AbstractSaveToWordGuarantor + public class AbstractSaveToWordGuarantor { + /*public void CreateReport(ExcelInfoGuarantor info) + { + CreateExcel(info); + z + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = 1, + Text = info.Title, + StyleInfo = ExcelStyleInfoType.Title + }); + + MergeCells(new ExcelMergeParameters + { + CellFromName = "A1", + CellToName = "C1" + }); + + uint rowIndex = 2; + + foreach (var mc in info.MemberConferences) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = rowIndex, + Text = $"{mc.MemberSurname} {mc.MemberName} {mc.MemberPatronymic}", + StyleInfo = ExcelStyleInfoType.Text + }); + + rowIndex++; + + foreach (var conference in mc.ConferenceBookings) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "B", + RowIndex = rowIndex, + Text = conference.Item1, + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "C", + RowIndex = rowIndex, + Text = conference.Item2.ToString("d"), + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + + rowIndex++; + } + + rowIndex++; + } + + SaveExcel(info); + } + + protected abstract void CreateExcel(ExcelInfoOrganiser info); + + protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams); + + protected abstract void MergeCells(ExcelMergeParameters excelParams); + + protected abstract void SaveExcel(ExcelInfoOrganiser info);*/ } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/ExcelInfoExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/ExcelInfoExecutor.cs index 9aa7e03..66b6866 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/ExcelInfoExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/ExcelInfoExecutor.cs @@ -1,4 +1,5 @@ -using System; +using LawCompanyContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,6 +9,8 @@ namespace LawCompanyBusinessLogic.OfficePackage.HelperModels { public class ExcelInfoExecutor { - + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public List ClientHearings { get; set; } = new(); } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/PdfInfoExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/PdfInfoExecutor.cs index 1b5abf2..24bc69a 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/PdfInfoExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/PdfInfoExecutor.cs @@ -1,4 +1,5 @@ -using System; +using LawCompanyContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,12 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage.HelperModels { - internal class PdfInfoExecutor + public class PdfInfoExecutor { + public string FileName { get; set; } = "C:\\Reports\\pdffile.pdf"; + public string Title { get; set; } = string.Empty; + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + public List Clients { get; set; } = new(); } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/WordInfoExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/WordInfoExecutor.cs index ad4af98..4caa96a 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/WordInfoExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/HelperModels/WordInfoExecutor.cs @@ -1,4 +1,5 @@ -using System; +using LawCompanyContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,10 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage.HelperModels { - internal class WordInfoExecutor + public class WordInfoExecutor { + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public List ClientHearings { get; set; } = new(); } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelExecutor.cs index 0ba587c..02fc268 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelExecutor.cs @@ -1,4 +1,11 @@  +using DocumentFormat.OpenXml.Office2010.Excel; +using DocumentFormat.OpenXml.Office2013.Excel; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using DocumentFormat.OpenXml; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; using System; using System.Collections.Generic; using System.Linq; @@ -7,7 +14,321 @@ using System.Threading.Tasks; namespace LawCompanyBusinessLogic.OfficePackage.Implements { - internal class SaveToExcelExecutor + public class SaveToExcelExecutor : AbstractSaveToExcelExecutor { + private SpreadsheetDocument? _spreadsheetDocument; + private SharedStringTablePart? _shareStringPart; + private Worksheet? _worksheet; + + private static void CreateStyles(WorkbookPart workbookpart) + { + var sp = workbookpart.AddNewPart(); + sp.Stylesheet = new Stylesheet(); + + var fonts = new Fonts() { Count = 2U, KnownFonts = true }; + + var fontUsual = new Font(); + fontUsual.Append(new FontSize() { Val = 12D }); + fontUsual.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Theme = 1U }); + fontUsual.Append(new FontName() { Val = "Times New Roman" }); + fontUsual.Append(new FontFamilyNumbering() { Val = 2 }); + fontUsual.Append(new FontScheme() { Val = FontSchemeValues.Minor }); + + var fontTitle = new Font(); + fontTitle.Append(new Bold()); + fontTitle.Append(new FontSize() { Val = 14D }); + fontTitle.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Theme = 1U }); + fontTitle.Append(new FontName() { Val = "Times New Roman" }); + fontTitle.Append(new FontFamilyNumbering() { Val = 2 }); + fontTitle.Append(new FontScheme() { Val = FontSchemeValues.Minor }); + + fonts.Append(fontUsual); + fonts.Append(fontTitle); + + var fills = new Fills() { Count = 2U }; + + var fill1 = new Fill(); + fill1.Append(new PatternFill() { PatternType = PatternValues.None }); + + var fill2 = new Fill(); + fill2.Append(new PatternFill() { PatternType = PatternValues.Gray125 }); + + fills.Append(fill1); + fills.Append(fill2); + + var borders = new Borders() { Count = 2U }; + + var borderNoBorder = new Border(); + borderNoBorder.Append(new LeftBorder()); + borderNoBorder.Append(new RightBorder()); + borderNoBorder.Append(new TopBorder()); + borderNoBorder.Append(new BottomBorder()); + borderNoBorder.Append(new DiagonalBorder()); + + var borderThin = new Border(); + + var leftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }; + leftBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + + var rightBorder = new RightBorder() { Style = BorderStyleValues.Thin }; + rightBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + + var topBorder = new TopBorder() { Style = BorderStyleValues.Thin }; + topBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + + var bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }; + bottomBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + + borderThin.Append(leftBorder); + borderThin.Append(rightBorder); + borderThin.Append(topBorder); + borderThin.Append(bottomBorder); + borderThin.Append(new DiagonalBorder()); + + borders.Append(borderNoBorder); + borders.Append(borderThin); + + var cellStyleFormats = new CellStyleFormats() + { + Count = 1U + }; + var cellFormatStyle = new CellFormat() + { + NumberFormatId = 0U, + FontId = 0U, + FillId = 0U, + BorderId = 0U + }; + + cellStyleFormats.Append(cellFormatStyle); + + var cellFormats = new CellFormats() + { + Count = 3U + }; + var cellFormatFont = new CellFormat() + { + NumberFormatId = 0U, + FontId = 0U, + FillId = 0U, + BorderId = 0U, + FormatId = 0U, + ApplyFont = true + }; + var cellFormatFontAndBorder = new CellFormat() + { + NumberFormatId = 0U, + FontId = 0U, + FillId = 0U, + BorderId = 1U, + FormatId = 0U, + ApplyFont = true, + ApplyBorder = true + }; + var cellFormatTitle = new CellFormat() + { + NumberFormatId = 0U, + FontId = 1U, + FillId = 0U, + BorderId = 0U, + FormatId = 0U, + Alignment = new Alignment() + { + Vertical = VerticalAlignmentValues.Center, + WrapText = true, + Horizontal = HorizontalAlignmentValues.Center + }, + ApplyFont = true + }; + cellFormats.Append(cellFormatFont); + cellFormats.Append(cellFormatFontAndBorder); + cellFormats.Append(cellFormatTitle); + var cellStyles = new CellStyles() { Count = 1U }; + cellStyles.Append(new CellStyle() + { + Name = "Normal", + FormatId = 0U, + BuiltinId = 0U + }); + var differentialFormats = new DocumentFormat.OpenXml.Office2013.Excel.DifferentialFormats() + { + Count = 0U + }; + + var tableStyles = new TableStyles() + { + Count = 0U, + DefaultTableStyle = "TableStyleMedium2", + DefaultPivotStyle = "PivotStyleLight16" + }; + var stylesheetExtensionList = new StylesheetExtensionList(); + var stylesheetExtension1 = new StylesheetExtension() + { + Uri = "{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" + }; + stylesheetExtension1.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"); + stylesheetExtension1.Append(new SlicerStyles() + { + DefaultSlicerStyle = "SlicerStyleLight1" + }); + var stylesheetExtension2 = new StylesheetExtension() + { + Uri = "{9260A510-F301-46a8-8635-F512D64BE5F5}" + }; + stylesheetExtension2.AddNamespaceDeclaration("x15", "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"); + stylesheetExtension2.Append(new TimelineStyles() + { + DefaultTimelineStyle = "TimeSlicerStyleLight1" + }); + + stylesheetExtensionList.Append(stylesheetExtension1); + stylesheetExtensionList.Append(stylesheetExtension2); + + sp.Stylesheet.Append(fonts); + sp.Stylesheet.Append(fills); + sp.Stylesheet.Append(borders); + sp.Stylesheet.Append(cellStyleFormats); + sp.Stylesheet.Append(cellFormats); + sp.Stylesheet.Append(cellStyles); + sp.Stylesheet.Append(differentialFormats); + sp.Stylesheet.Append(tableStyles); + sp.Stylesheet.Append(stylesheetExtensionList); + } + + private static uint GetStyleValue(ExcelStyleInfoType styleInfo) + { + return styleInfo switch + { + ExcelStyleInfoType.Title => 2U, + ExcelStyleInfoType.TextWithBroder => 1U, + ExcelStyleInfoType.Text => 0U, + _ => 0U, + }; + } + + protected override void CreateExcel(ExcelInfoExecutor info) + { + _spreadsheetDocument = SpreadsheetDocument.Create(info.FileName, SpreadsheetDocumentType.Workbook); + var workbookpart = _spreadsheetDocument.AddWorkbookPart(); + workbookpart.Workbook = new Workbook(); + CreateStyles(workbookpart); + _shareStringPart = _spreadsheetDocument.WorkbookPart!.GetPartsOfType().Any() ? _spreadsheetDocument.WorkbookPart.GetPartsOfType().First() : _spreadsheetDocument.WorkbookPart.AddNewPart(); + + if (_shareStringPart.SharedStringTable == null) + { + _shareStringPart.SharedStringTable = new SharedStringTable(); + } + + var worksheetPart = workbookpart.AddNewPart(); + worksheetPart.Worksheet = new Worksheet(new SheetData()); + + var sheets = _spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets()); + var sheet = new Sheet() + { + Id = _spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), + SheetId = 1, + Name = "Лист" + }; + sheets.Append(sheet); + _worksheet = worksheetPart.Worksheet; + } + + protected override void InsertCellInWorksheet(ExcelCellParameters excelParams) + { + if (_worksheet == null || _shareStringPart == null) + { + return; + } + + var sheetData = _worksheet.GetFirstChild(); + + if (sheetData == null) + { + return; + } + + Row row; + + if (sheetData.Elements().Where(r => r.RowIndex! == excelParams.RowIndex).Any()) + { + row = sheetData.Elements().Where(r => r.RowIndex! == excelParams.RowIndex).First(); + } + else + { + row = new Row() { RowIndex = excelParams.RowIndex }; + sheetData.Append(row); + } + + Cell cell; + + if (row.Elements().Where(c => c.CellReference!.Value == excelParams.CellReference).Any()) + { + cell = row.Elements().Where(c => c.CellReference!.Value == excelParams.CellReference).First(); + } + else + { + Cell? refCell = null; + foreach (Cell rowCell in row.Elements()) + { + if (string.Compare(rowCell.CellReference!.Value, excelParams.CellReference, true) > 0) + { + refCell = rowCell; + break; + } + } + var newCell = new Cell() + { + CellReference = excelParams.CellReference + }; + row.InsertBefore(newCell, refCell); + cell = newCell; + } + + _shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new Text(excelParams.Text))); + _shareStringPart.SharedStringTable.Save(); + cell.CellValue = new CellValue((_shareStringPart.SharedStringTable.Elements().Count() - 1).ToString()); + cell.DataType = new EnumValue(CellValues.SharedString); + cell.StyleIndex = GetStyleValue(excelParams.StyleInfo); + } + + protected override void MergeCells(ExcelMergeParameters excelParams) + { + if (_worksheet == null) + { + return; + } + MergeCells mergeCells; + if (_worksheet.Elements().Any()) + { + mergeCells = _worksheet.Elements().First(); + } + else + { + mergeCells = new MergeCells(); + if (_worksheet.Elements().Any()) + { + _worksheet.InsertAfter(mergeCells, _worksheet.Elements().First()); + } + else + { + _worksheet.InsertAfter(mergeCells, _worksheet.Elements().First()); + } + } + var mergeCell = new MergeCell() + { + Reference = new StringValue(excelParams.Merge) + }; + mergeCells.Append(mergeCell); + } + + protected override void SaveExcel(ExcelInfoExecutor info) + { + if (_spreadsheetDocument == null) + { + return; + } + _spreadsheetDocument.WorkbookPart!.Workbook.Save(); + _spreadsheetDocument.Close(); + } } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelGuarantor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelGuarantor.cs index 522187d..303d51f 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelGuarantor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToExcelGuarantor.cs @@ -8,7 +8,7 @@ using DocumentFormat.OpenXml; namespace LawCompanyBusinessLogic.OfficePackage.Implements { - public class SaveToExcelGuarantor : AbstractSaveToExcelGuarantor + /*public class SaveToExcelGuarantor : AbstractSaveToExcelGuarantor { private SpreadsheetDocument? _spreadsheetDocument; private SharedStringTablePart? _shareStringPart; @@ -324,5 +324,5 @@ namespace LawCompanyBusinessLogic.OfficePackage.Implements _spreadsheetDocument.WorkbookPart!.Workbook.Save(); _spreadsheetDocument.Close(); } - } + }*/ } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToPdfExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToPdfExecutor.cs index 2464fac..556db0f 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToPdfExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToPdfExecutor.cs @@ -1,12 +1,102 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.Rendering; +using MigraDoc.DocumentObjectModel.Tables; namespace LawCompanyBusinessLogic.OfficePackage.Implements { - internal class SaveToPdfExecutor + public class SaveToPdfExecutor : AbstractSaveToPdfExecutor { + 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.Rigth => 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 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 CreatePdf(PdfInfoExecutor info) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + } + + 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 CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + + protected override void SavePdf(PdfInfoExecutor info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } } } diff --git a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToWordExecutor.cs b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToWordExecutor.cs index 7b292a8..bd99ec2 100644 --- a/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToWordExecutor.cs +++ b/LawCompany/LawCompanyBusinessLogic/OfficePackage/Implements/SaveToWordExecutor.cs @@ -1,12 +1,125 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using LawCompanyBusinessLogic.OfficePackage.HelperEnums; +using LawCompanyBusinessLogic.OfficePackage.HelperModels; namespace LawCompanyBusinessLogic.OfficePackage.Implements { - internal class SaveToWordExecutor + public class SaveToWordExecutor : AbstractSaveToWordExecutor { + private WordprocessingDocument? _wordDocument; + private Body? _docBody; + + private static JustificationValues GetJustificationValues(WordJustificationType type) + { + return type switch + { + WordJustificationType.Both => JustificationValues.Both, + WordJustificationType.Center => JustificationValues.Center, + _ => JustificationValues.Left, + }; + } + + private static SectionProperties CreateSectionProperties() + { + var properties = new SectionProperties(); + var pageSize = new PageSize + { + Orient = PageOrientationValues.Portrait + }; + properties.AppendChild(pageSize); + return properties; + } + + private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties) + { + if (paragraphProperties == null) + { + return null; + } + + var properties = new ParagraphProperties(); + + properties.AppendChild(new Justification() + { + Val = GetJustificationValues(paragraphProperties.JustificationType) + }); + + properties.AppendChild(new SpacingBetweenLines + { + LineRule = LineSpacingRuleValues.Auto + }); + + properties.AppendChild(new Indentation()); + var paragraphMarkRunProperties = new ParagraphMarkRunProperties(); + + if (!string.IsNullOrEmpty(paragraphProperties.Size)) + { + paragraphMarkRunProperties.AppendChild(new FontSize + { + Val = paragraphProperties.Size + }); + } + + properties.AppendChild(paragraphMarkRunProperties); + + return properties; + } + + protected override void CreateParagraph(WordParagraph paragraph) + { + if (_docBody == null || paragraph == null) + { + return; + } + + var docParagraph = new Paragraph(); + + docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties)); + + foreach (var run in paragraph.Texts) + { + var docRun = new Run(); + var properties = new RunProperties(); + properties.AppendChild(new FontSize { Val = run.Item2.Size }); + + if (run.Item2.Bold) + { + properties.AppendChild(new Bold()); + } + + docRun.AppendChild(properties); + + docRun.AppendChild(new Text + { + Text = run.Item1, + Space = SpaceProcessingModeValues.Preserve + }); + + docParagraph.AppendChild(docRun); + } + _docBody.AppendChild(docParagraph); + } + + protected override void CreateWord(WordInfoExecutor info) + { + _wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document); + MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); + mainPart.Document = new Document(); + _docBody = mainPart.Document.AppendChild(new Body()); + } + + protected override void SaveWord(WordInfoExecutor info) + { + if (_docBody == null || _wordDocument == null) + { + return; + } + + _docBody.AppendChild(CreateSectionProperties()); + _wordDocument.MainDocumentPart!.Document.Save(); + _wordDocument.Close(); + } } }