diff --git a/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SuretorReportLogic.cs b/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SuretorReportLogic.cs index e33f432..a7d132b 100644 --- a/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SuretorReportLogic.cs +++ b/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SuretorReportLogic.cs @@ -1,4 +1,6 @@ -using PolyclinicContracts.BindingModels; +using PolyclinicBusinessLogic.OfficePackage; +using PolyclinicBusinessLogic.OfficePackage.HelperModels.Word; +using PolyclinicContracts.BindingModels; using PolyclinicContracts.BusinessLogicsContracts; using PolyclinicContracts.SearchModels; using PolyclinicContracts.StoragesContracts; @@ -15,34 +17,30 @@ namespace PolyclinicBusinessLogic.BusinessLogics private readonly ICourseStorage courseStorage; private readonly ISymptomStorage symptomStorage; private readonly IRecipeStorage recipeStorage; + + private readonly AbstractSaveToWordCoursesByProcedures saveToWord; public SuretorReportLogic(IProcedureStorage procedureStorage, IMedicamentStorage medicamentStorage, - ICourseStorage courseStorage, ISymptomStorage symptomStorage, IRecipeStorage recipeStorage) + ICourseStorage courseStorage, ISymptomStorage symptomStorage, IRecipeStorage recipeStorage, AbstractSaveToWordCoursesByProcedures saveToWord) { this.procedureStorage = procedureStorage; this.medicamentStorage = medicamentStorage; this.courseStorage = courseStorage; this.symptomStorage = symptomStorage; this.recipeStorage = recipeStorage; + this.saveToWord = saveToWord; } - public List GetProcedureCourses(ProcedureSearchModel model) + public List GetProcedureCourses(ProcedureSearchModel model) { var procedure = procedureStorage.GetElement(model); var courses = courseStorage.GetFullList(); var recipes = recipeStorage.GetFullList(); - var list = new List(); - - var record = new ReportCoursesByProcedureViewModel - { - Name = procedure!.Name, - Courses = new List<(int countDays, int pillsPerDay, string comment)>() - }; + var list = new List(); using var context = new PolyclinicDatabase(); var recipeProcedureId = context.RecipeProcedures .Where(x => x.ProcedureId == model.Id).Select(x => x.RecipeId).ToList(); - foreach (var recipe in recipes) { @@ -54,13 +52,12 @@ namespace PolyclinicBusinessLogic.BusinessLogics { if(recipe.CourseId == course.Id) { - record.Courses.Add((course.DaysCount, course.PillsPerDay, course.Comment)); + list.Add(course); } } } } } - list.Add(record); return list; } @@ -103,9 +100,14 @@ namespace PolyclinicBusinessLogic.BusinessLogics throw new NotImplementedException(); } - public void SaveCoursesByProcedureToWordFile(ReportBindingModel model) + public void SaveCoursesByProcedureToWordFile(ReportBindingModel model, ProcedureSearchModel procedureSearchMode) { - throw new NotImplementedException(); + saveToWord.CreateDoc(new WordCoursesByProceduresInfo + { + FileName = model.FileName, + Title = "Курсы по процедурам", + Courses = GetProcedureCourses(procedureSearchMode) + }); } public void SaveProceduresToPdfFile(ReportBindingModel model) diff --git a/Polyclinic/PolyclinicBusinessLogic/OfficePackage/HelperModels/Word/WordCoursesByProceduresInfo.cs b/Polyclinic/PolyclinicBusinessLogic/OfficePackage/HelperModels/Word/WordCoursesByProceduresInfo.cs index a503fbd..8f35c8a 100644 --- a/Polyclinic/PolyclinicBusinessLogic/OfficePackage/HelperModels/Word/WordCoursesByProceduresInfo.cs +++ b/Polyclinic/PolyclinicBusinessLogic/OfficePackage/HelperModels/Word/WordCoursesByProceduresInfo.cs @@ -6,6 +6,7 @@ namespace PolyclinicBusinessLogic.OfficePackage.HelperModels.Word { public string FileName { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; + public string ProcedureName { get; set; } = string.Empty; public List Courses { get; set; } = new(); } } diff --git a/Polyclinic/PolyclinicBusinessLogic/OfficePackage/Implements/SaveToWordCoursesByProcedure.cs b/Polyclinic/PolyclinicBusinessLogic/OfficePackage/Implements/SaveToWordCoursesByProcedure.cs new file mode 100644 index 0000000..1cfcb72 --- /dev/null +++ b/Polyclinic/PolyclinicBusinessLogic/OfficePackage/Implements/SaveToWordCoursesByProcedure.cs @@ -0,0 +1,122 @@ +using PolyclinicBusinessLogic.OfficePackage.HelperEnums; +using PolyclinicBusinessLogic.OfficePackage.HelperModels.Word; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; + +namespace PolyclinicBusinessLogic.OfficePackage.Implements +{ + public class SaveToWordCoursesByProcedure : AbstractSaveToWordCoursesByProcedures + { + 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(WordCoursesByProceduresInfo 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(WordCoursesByProceduresInfo info) + { + if (_docBody == null || _wordDocument == null) + { + return; + } + _docBody.AppendChild(CreateSectionProperties()); + _wordDocument.MainDocumentPart!.Document.Save(); + _wordDocument.Dispose(); + } + } +} + + + diff --git a/Polyclinic/PolyclinicBusinessLogic/PolyclinicBusinessLogic.csproj b/Polyclinic/PolyclinicBusinessLogic/PolyclinicBusinessLogic.csproj index f5e1d5c..ee32ea1 100644 --- a/Polyclinic/PolyclinicBusinessLogic/PolyclinicBusinessLogic.csproj +++ b/Polyclinic/PolyclinicBusinessLogic/PolyclinicBusinessLogic.csproj @@ -7,6 +7,7 @@ + diff --git a/Polyclinic/PolyclinicContracts/BusinessLogicsContracts/ISuretorReportLogic.cs b/Polyclinic/PolyclinicContracts/BusinessLogicsContracts/ISuretorReportLogic.cs index ccfba9d..789b597 100644 --- a/Polyclinic/PolyclinicContracts/BusinessLogicsContracts/ISuretorReportLogic.cs +++ b/Polyclinic/PolyclinicContracts/BusinessLogicsContracts/ISuretorReportLogic.cs @@ -15,7 +15,7 @@ namespace PolyclinicContracts.BusinessLogicsContracts /// Получение данных (списка) курсов по выбранным процедурам /// /// - List GetProcedureCourses(ProcedureSearchModel model); + List GetProcedureCourses(ProcedureSearchModel model); /// /// Получение списка процедур @@ -28,7 +28,7 @@ namespace PolyclinicContracts.BusinessLogicsContracts /// Сохранение курсов по процедурам в файл-Word /// /// - void SaveCoursesByProcedureToWordFile(ReportBindingModel model); + void SaveCoursesByProcedureToWordFile(ReportBindingModel model, ProcedureSearchModel procedureSearchMode); /// /// Сохранение курсов по процедурам в файл-Excel diff --git a/Polyclinic/PolyclinicContracts/ViewModels/ReportCoursesByProcedureViewModel.cs b/Polyclinic/PolyclinicContracts/ViewModels/ReportCoursesByProcedureViewModel.cs index 7362ded..ba3df1f 100644 --- a/Polyclinic/PolyclinicContracts/ViewModels/ReportCoursesByProcedureViewModel.cs +++ b/Polyclinic/PolyclinicContracts/ViewModels/ReportCoursesByProcedureViewModel.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PolyclinicContracts.ViewModels +namespace PolyclinicContracts.ViewModels { public class ReportCoursesByProcedureViewModel { diff --git a/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs b/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs index 680ed67..f7c7b88 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs +++ b/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using PolyclinicBusinessLogic.BusinessLogics; +using PolyclinicBusinessLogic.OfficePackage; using PolyclinicContracts.BindingModels; using PolyclinicContracts.BusinessLogicsContracts; using PolyclinicContracts.SearchModels; @@ -18,13 +19,15 @@ namespace PolyclinicWebAppSuretor.Controllers private readonly IRecipeLogic _recipeLogic; private readonly ISymptomLogic _symptomLogic; private readonly ICourseLogic _courseLogic; + private readonly ISuretorReportLogic _suretorReportLogic; public HomeController(ILogger logger, IProcedureLogic procedureLogic, IMedicamentLogic medicamentLogic, IRecipeLogic recipeLogic, ISymptomLogic symptomLogic, - ICourseLogic courseLogic) + ICourseLogic courseLogic, + ISuretorReportLogic suretorReportLogic) { _logger = logger; _procedureLogic = procedureLogic; @@ -32,6 +35,7 @@ namespace PolyclinicWebAppSuretor.Controllers _recipeLogic = recipeLogic; _symptomLogic = symptomLogic; _courseLogic = courseLogic; + _suretorReportLogic = suretorReportLogic; } public IActionResult Index() @@ -74,9 +78,9 @@ namespace PolyclinicWebAppSuretor.Controllers RecipeBindingModel recipe = new RecipeBindingModel { - ProceduresCount = model.RecipeViewModel.ProceduresCount, - Comment = model.RecipeViewModel.Comment, - RecipeProcedures = selectedProcedures + ProceduresCount = model.RecipeViewModel.ProceduresCount, + Comment = model.RecipeViewModel.Comment, + RecipeProcedures = selectedProcedures .ToDictionary( x => x, x => allProcedures.Where(y => y.Id == x) as IProcedureModel @@ -276,11 +280,7 @@ namespace PolyclinicWebAppSuretor.Controllers return RedirectToAction("Procedures"); } - public IActionResult ListCoursesByProcedures() - { - List procedures = _procedureLogic.ReadList(null); - return View(procedures); - } + public IActionResult AddSymptomToMedicament(AddSymptomToMedicamentModel model) { @@ -292,6 +292,43 @@ namespace PolyclinicWebAppSuretor.Controllers return View(model); } + public IActionResult ListCoursesByProcedures() + { + ViewBag.Procedures = _procedureLogic.ReadList(null); + return View(); + } + + [HttpPost] + public IActionResult CreateWordFile(int procedureId, string fileName, string fileFormat) + { + var procedure = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId}); + ViewBag.Procedures = procedure; + + fileName = fileName + $".{fileFormat}"; + + if(procedure == null) + { + return NotFound(" "); + } + + var report = new ReportBindingModel + { + FileName = fileName, + }; + + var procedureSearch = new ProcedureSearchModel { Id = procedureId}; + + if (fileFormat == "docx") + { + _suretorReportLogic.SaveCoursesByProcedureToWordFile(report,procedureSearch); + } + + var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); + byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); + string mimeType = fileFormat.ToLower() == "docx" ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + return File(fileBytes, mimeType, fileName); + } + [HttpGet] [HttpPost] public IActionResult ProceduresReport() diff --git a/Polyclinic/PolyclinicWebAppSuretor/Program.cs b/Polyclinic/PolyclinicWebAppSuretor/Program.cs index d66e48c..86ff5bf 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Program.cs +++ b/Polyclinic/PolyclinicWebAppSuretor/Program.cs @@ -1,4 +1,6 @@ using PolyclinicBusinessLogic.BusinessLogics; +using PolyclinicBusinessLogic.OfficePackage; +using PolyclinicBusinessLogic.OfficePackage.Implements; using PolyclinicContracts.BusinessLogicsContracts; using PolyclinicContracts.StoragesContracts; using PolyclinicDatabaseImplement.Implements; @@ -17,6 +19,9 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/Polyclinic/PolyclinicWebAppSuretor/Views/Home/ListCoursesByProcedures.cshtml b/Polyclinic/PolyclinicWebAppSuretor/Views/Home/ListCoursesByProcedures.cshtml index bffad41..0e77b27 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Views/Home/ListCoursesByProcedures.cshtml +++ b/Polyclinic/PolyclinicWebAppSuretor/Views/Home/ListCoursesByProcedures.cshtml @@ -1,5 +1,5 @@ @using PolyclinicContracts.ViewModels -@model List +@model ProcedureViewModel @{ ViewData["Title"] = "ListCoursesByProcedures"; } @@ -13,19 +13,15 @@

Выберите процедуру/ы из списка

-
-
    - @{ - foreach (var item in Model) - { -
  • - - -
  • - } - } -
-
+ + +
@@ -34,20 +30,20 @@

- +

- Как назовем файл? + Как назовем файл?

- +
- +
diff --git a/Polyclinic/PolyclinicWebAppSuretor/jopa.docx b/Polyclinic/PolyclinicWebAppSuretor/jopa.docx new file mode 100644 index 0000000..104bfb0 Binary files /dev/null and b/Polyclinic/PolyclinicWebAppSuretor/jopa.docx differ diff --git a/Polyclinic/PolyclinicWebAppSuretor/qwerty b/Polyclinic/PolyclinicWebAppSuretor/qwerty new file mode 100644 index 0000000..9cb6b99 Binary files /dev/null and b/Polyclinic/PolyclinicWebAppSuretor/qwerty differ diff --git a/Polyclinic/PolyclinicWebAppSuretor/qwerty.docx b/Polyclinic/PolyclinicWebAppSuretor/qwerty.docx new file mode 100644 index 0000000..317db0a Binary files /dev/null and b/Polyclinic/PolyclinicWebAppSuretor/qwerty.docx differ