diff --git a/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs b/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs index b23867e..141da97 100644 --- a/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs @@ -1,4 +1,7 @@ -using CarServiceContracts.BindingModels; +using BlacksmithWorkshopBusinessLogic.OfficePackage; +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels; +using BlacksmithWorkshopBusinessLogic.OfficePackage.Implements; +using CarServiceContracts.BindingModels; using CarServiceContracts.BusinessLogicsContracts; using CarServiceContracts.StorageContracts; using CarServiceContracts.ViewModels; @@ -11,11 +14,14 @@ namespace CarServiceBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IWorkStorage _workStorage; private readonly IWorkPaymentStorage _workPaymentStorage; - public ReportLogic(ILogger logger, IWorkStorage workStorage, IWorkPaymentStorage workPaymentStorage) + private readonly AbstractSaveToWord _saveToWord; + + public ReportLogic(ILogger logger, IWorkStorage workStorage, IWorkPaymentStorage workPaymentStorage, AbstractSaveToWord saveToWord) { _logger = logger; _workStorage = workStorage; _workPaymentStorage = workPaymentStorage; + _saveToWord = saveToWord; } public List GetRequestsByWorks(ReportBindingModel model) { @@ -29,7 +35,12 @@ namespace CarServiceBusinessLogic.BusinessLogics } public void SaveComponentsToWordFile(ReportBindingModel model) { - throw new NotImplementedException(); + _saveToWord.CreateDoc(new WordInfo + { + FileName = model.FileName, + Title = "Список заявок", + WorksWithRequests = GetRequestsByWorks(model) + }); } public void SaveManufactureComponentToExcelFile(ReportBindingModel model) { diff --git a/CarService/CarServiceBusinessLogic/CarServiceBusinessLogic.csproj b/CarService/CarServiceBusinessLogic/CarServiceBusinessLogic.csproj index 0378213..a54394f 100644 --- a/CarService/CarServiceBusinessLogic/CarServiceBusinessLogic.csproj +++ b/CarService/CarServiceBusinessLogic/CarServiceBusinessLogic.csproj @@ -7,7 +7,9 @@ + + diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/AbstractSaveToWord.cs b/CarService/CarServiceBusinessLogic/OfficePackage/AbstractSaveToWord.cs new file mode 100644 index 0000000..e7a1b96 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/AbstractSaveToWord.cs @@ -0,0 +1,89 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToWord + { + public void CreateDoc(WordInfo 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 WWR in info.WorksWithRequests) + { + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> + { + (WWR.WorkName, new WordTextProperties { Bold = true, Size = "24", }) + }, + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Both + } + }); + if (WWR.RepairRequests.Count == 0) + { + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> + { + ("Заявок по работе нет", new WordTextProperties { Size = "24", }) + }, + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Both + } + }); + } + foreach (var RR in WWR.RepairRequests) + { + + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> + { + ($"Заявка № {RR.RepairRequestId} от {RR.RepairRequestDateCreated}. Заказчик - {RR.CustomerName}. Транспортное средство - {RR.VehicleName}, гос. номер {RR.Plate}. Количество работ: {RR.WorksCount}", new WordTextProperties { Size = "24", }) + }, + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Both + } + }); + } + } + SaveWord(info); + } + /// + /// Создание doc-файла + /// + /// + protected abstract void CreateWord(WordInfo info); + /// + /// Создание абзаца с текстом + /// + /// + /// + protected abstract void CreateParagraph(WordParagraph paragraph); + /// + /// Сохранение файла + /// + /// + protected abstract void SaveWord(WordInfo info); + + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/ExcelStyleInfoType.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/ExcelStyleInfoType.cs new file mode 100644 index 0000000..d5cecc1 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/ExcelStyleInfoType.cs @@ -0,0 +1,9 @@ +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums +{ + public enum ExcelStyleInfoType + { + Title, + Text, + TextWithBorder + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..51202ca --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs @@ -0,0 +1,9 @@ +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums +{ + public enum PdfParagraphAlignmentType + { + Center, + Left, + Right + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/WordJustificationType.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/WordJustificationType.cs new file mode 100644 index 0000000..d05b578 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperEnums/WordJustificationType.cs @@ -0,0 +1,8 @@ +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums +{ + public enum WordJustificationType + { + Center, + Both + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelCellParameters.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelCellParameters.cs new file mode 100644 index 0000000..6ce7979 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelCellParameters.cs @@ -0,0 +1,12 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class ExcelCellParameters + { + public string ColumnName { get; set; } = string.Empty; + public uint RowIndex { get; set; } + public string Text { get; set; } = string.Empty; + public string CellReference => $"{ColumnName}{RowIndex}"; + public ExcelStyleInfoType StyleInfo { get; set; } + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs new file mode 100644 index 0000000..a5d9dfc --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs @@ -0,0 +1,12 @@ + +using CarServiceContracts.ViewModels; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class ExcelInfo + { + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public List WorksWithRequests { get; set; } = new(); + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelMergeParameters.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelMergeParameters.cs new file mode 100644 index 0000000..2db633b --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/ExcelMergeParameters.cs @@ -0,0 +1,9 @@ +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class ExcelMergeParameters + { + public string CellFromName { get; set; } = string.Empty; + public string CellToName { get; set; } = string.Empty; + public string Merge => $"{CellFromName}:{CellToName}"; + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs new file mode 100644 index 0000000..1505445 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -0,0 +1,13 @@ +using CarServiceContracts.ViewModels; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class PdfInfo + { + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + public List Payments { get; set; } = new(); + } +} \ No newline at end of file diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs new file mode 100644 index 0000000..c7b8302 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs @@ -0,0 +1,11 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class PdfParagraph + { + public string Text { get; set; } = string.Empty; + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} \ No newline at end of file diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs new file mode 100644 index 0000000..7d2afec --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs @@ -0,0 +1,11 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class PdfRowParameters + { + public List Texts { get; set; } = new(); + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} \ No newline at end of file diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordInfo.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordInfo.cs new file mode 100644 index 0000000..c8c1f20 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordInfo.cs @@ -0,0 +1,11 @@ +using CarServiceContracts.ViewModels; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class WordInfo + { + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public List WorksWithRequests { get; set; } = new(); + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs new file mode 100644 index 0000000..3111dfd --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs @@ -0,0 +1,8 @@ +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class WordParagraph + { + public List<(string, WordTextProperties)> Texts { get; set; } = new(); + public WordTextProperties? TextProperties { get; set; } + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs new file mode 100644 index 0000000..703cc05 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs @@ -0,0 +1,11 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels +{ + public class WordTextProperties + { + public string Size { get; set; } = string.Empty; + public bool Bold { get; set; } + public WordJustificationType JustificationType { get; set; } + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/Implements/SaveToWord.cs b/CarService/CarServiceBusinessLogic/OfficePackage/Implements/SaveToWord.cs new file mode 100644 index 0000000..5cd3963 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/Implements/SaveToWord.cs @@ -0,0 +1,121 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using DocumentFormat.OpenXml; + +namespace BlacksmithWorkshopBusinessLogic.OfficePackage.Implements +{ + public class SaveToWord : AbstractSaveToWord + { + 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 CreateWord(WordInfo 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 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 SaveWord(WordInfo info) + { + if (_docBody == null || _wordDocument == null) + { + return; + } + _docBody.AppendChild(CreateSectionProperties()); + _wordDocument.MainDocumentPart!.Document.Save(); + _wordDocument.Dispose(); + } + } +} diff --git a/CarService/CarServiceWebApp/CarServiceWebApp.csproj b/CarService/CarServiceWebApp/CarServiceWebApp.csproj index b6cf70f..8b7382d 100644 --- a/CarService/CarServiceWebApp/CarServiceWebApp.csproj +++ b/CarService/CarServiceWebApp/CarServiceWebApp.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/CarService/CarServiceWebApp/Controllers/ReportController.cs b/CarService/CarServiceWebApp/Controllers/ReportController.cs index fb4b341..8374e1e 100644 --- a/CarService/CarServiceWebApp/Controllers/ReportController.cs +++ b/CarService/CarServiceWebApp/Controllers/ReportController.cs @@ -1,6 +1,7 @@ using CarServiceContracts.BindingModels; using CarServiceContracts.BusinessLogicsContracts; using CarServiceWebApp.Models; +using log4net; using Microsoft.AspNetCore.Mvc; namespace CarServiceWebApp.Controllers @@ -75,5 +76,19 @@ namespace CarServiceWebApp.Controllers ViewBag.Payments = payments; return View(); } + public IActionResult SaveToWord() + { + _reportLogic.SaveComponentsToWordFile(new ReportBindingModel { SelectedWorks = SelectedWorks, FileName = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportWord.docx" }); + return Redirect("~/Report/DownLoadWord"); + } + public IActionResult DownLoadWord() + { + string filePath = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportWord.docx"; + string fileName = "Отчет.docx"; + + byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); + + return File(fileBytes, "application/force-download", fileName); + } } } diff --git a/CarService/CarServiceWebApp/Files/ReportWord.docx b/CarService/CarServiceWebApp/Files/ReportWord.docx new file mode 100644 index 0000000..33209be Binary files /dev/null and b/CarService/CarServiceWebApp/Files/ReportWord.docx differ diff --git a/CarService/CarServiceWebApp/Program.cs b/CarService/CarServiceWebApp/Program.cs index 25cdde4..3c65ace 100644 --- a/CarService/CarServiceWebApp/Program.cs +++ b/CarService/CarServiceWebApp/Program.cs @@ -1,3 +1,5 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage; +using BlacksmithWorkshopBusinessLogic.OfficePackage.Implements; using CarServiceBusinessLogic.BusinessLogics; using CarServiceContracts.BusinessLogicsContracts; using CarServiceContracts.StorageContracts; @@ -25,6 +27,8 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); + var app = builder.Build(); diff --git a/CarService/CarServiceWebApp/Views/Report/ReportRequestsByWorks.cshtml b/CarService/CarServiceWebApp/Views/Report/ReportRequestsByWorks.cshtml index bfbaed7..6291a78 100644 --- a/CarService/CarServiceWebApp/Views/Report/ReportRequestsByWorks.cshtml +++ b/CarService/CarServiceWebApp/Views/Report/ReportRequestsByWorks.cshtml @@ -43,4 +43,6 @@

Заявок нет

} } + + \ No newline at end of file