diff --git a/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs b/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs index 158565f..3b1eb59 100644 --- a/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/University/UniversityBusinessLogic/BusinessLogics/ReportLogic.cs @@ -5,6 +5,8 @@ using UniversityContracts.BusinessLogicContracts; using UniversityContracts.SearchModels; using UniversityContracts.StorageContracts; using UniversityContracts.ViewModels; +using DocumentFormat.OpenXml.EMMA; +using UniversityBusinessLogic.OfficePackage.HelperModels; namespace UniversityBusinessLogics.BusinessLogics; @@ -28,7 +30,9 @@ public class ReportLogic : IReportLogic private readonly AbstractSaveToExcelWorker _saveToExcelWorker; private readonly AbstractSaveToWordWorker _saveToWordWorker; private readonly AbstractSaveToPdfWorker _saveToPdfWorker; - public ReportLogic (ITeacherStorage teacherStorage, IDisciplineStorage + + private readonly AbstractSaveToWordStorekeeper _saveToWordStorekeeper; + public ReportLogic (ITeacherStorage teacherStorage, IDisciplineStorage disciplineStorage, IStudentStorage studentStorage, IStatementStorage statementStorage, IPlanOfStudyStorage planOfStudyStorage, AbstractSaveToExcelWorker saveToExcelWorker, AbstractSaveToWordWorker saveToWordWorker, AbstractSaveToPdfWorker saveToPdfWorker) @@ -216,7 +220,12 @@ public class ReportLogic : IReportLogic public void SaveTeachersToWord(ReportBindingModel option) { - throw new NotImplementedException(); + _saveToWordStorekeeper.CreateDoc(new WordInfoStorekeeper + { + FileName = option.FileName, + Title = "Список пакетов документов", + TeacherInfo = GetTeachers() + }); } public void SendDisciplinesToEmail(ReportDateRangeBindingModel option, string email) diff --git a/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToWordStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToWordStorekeeper.cs index d3d74f0..2b99827 100644 --- a/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToWordStorekeeper.cs +++ b/University/UniversityBusinessLogic/OfficePackage/AbstractSaveToWordStorekeeper.cs @@ -24,7 +24,7 @@ namespace UniversityBusinessLogic.OfficePackage } }); - foreach (var discipline in info.Disciplines) + foreach (var discipline in info.TeacherInfo) { CreateParagraph(new WordParagraph { @@ -38,16 +38,15 @@ namespace UniversityBusinessLogic.OfficePackage JustificationType = WordJustificationType.Both } }); - foreach (var assembly in discipline.StudentDisciplines) + foreach (var student in discipline.Students) { - /*if (!string.IsNullOrEmpty(assembly.AssemblyName) && !string.IsNullOrEmpty(assembly.AssemblyCategory) && assembly.AssemblyPrice != 0) + if (!string.IsNullOrEmpty(student.Name) && !string.IsNullOrEmpty(student.PhoneNumber)) { CreateParagraph(new WordParagraph { Texts = new List<(string, WordTextProperties)> { - (assembly.AssemblyName + " - ", new WordTextProperties { Size = "24" }), - (assembly.AssemblyCategory + " - ", new WordTextProperties { Size = "24" }), - (assembly.AssemblyPrice.ToString(), new WordTextProperties { Size = "24" }) + (student.Name + " - ", new WordTextProperties { Size = "24" }), + (student.PhoneNumber + " - ", new WordTextProperties { Size = "24" }) }, TextProperties = new WordTextProperties { @@ -55,7 +54,7 @@ namespace UniversityBusinessLogic.OfficePackage JustificationType = WordJustificationType.Both } }); - }*/ + } } } diff --git a/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs index f40ff67..ead12cd 100644 --- a/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs +++ b/University/UniversityBusinessLogic/OfficePackage/HelperModels/WordInfoStorekeeper.cs @@ -1,4 +1,5 @@ -using UniversityContracts.ViewModels; +using University.ViewModels; +using UniversityContracts.ViewModels; namespace UniversityBusinessLogic.OfficePackage.HelperModels { @@ -8,6 +9,6 @@ namespace UniversityBusinessLogic.OfficePackage.HelperModels public Stream? Stream { get; set; } public string Title { get; set; } = string.Empty; public List ReportObjects { get; set; } = new(); - public List Disciplines{ get; set; } = new(); + public List TeacherInfo{ get; set; } = new(); } } diff --git a/University/UniversityBusinessLogic/OfficePackage/Implements/SaveToWordStorekeeper.cs b/University/UniversityBusinessLogic/OfficePackage/Implements/SaveToWordStorekeeper.cs new file mode 100644 index 0000000..e4afbcc --- /dev/null +++ b/University/UniversityBusinessLogic/OfficePackage/Implements/SaveToWordStorekeeper.cs @@ -0,0 +1,136 @@ +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using UniversityBusinessLogic.OfficePackage.HelperModels; +using UniversityBusinessLogic.OfficePackage; +using UniversityBusinessLogics.OfficePackage.HelperEnums; + +namespace UniversityBusinessLogic.OfficePackage.Implements +{ + public class SaveToWordStorekeeper : AbstractSaveToWordStorekeeper + { + 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(WordInfoStorekeeper 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(WordInfoStorekeeper info) + { + if (_docBody == null || _wordDocument == null) + { + return; + } + _docBody.AppendChild(CreateSectionProperties()); + + _wordDocument.MainDocumentPart!.Document.Save(); + + _wordDocument.Dispose(); + } + } +} \ No newline at end of file diff --git a/University/UniversityContracts/ViewModels/ReportTeacherViewModel.cs b/University/UniversityContracts/ViewModels/ReportTeacherViewModel.cs index abc3141..270bdcc 100644 --- a/University/UniversityContracts/ViewModels/ReportTeacherViewModel.cs +++ b/University/UniversityContracts/ViewModels/ReportTeacherViewModel.cs @@ -8,7 +8,8 @@ namespace University.ViewModels { public class ReportTeacherViewModel { + public int TeacherId { get; set; } public string TeacherName { get; set; } = string.Empty; - public List<(string Student, string PhoneNumber)> Students { get; set; } = new(); + public List<(string Name, string PhoneNumber)> Students { get; set; } = new(); } } \ No newline at end of file