сделала отчет для курсов по процедуре в ворд

This commit is contained in:
Елена Бакальская 2024-05-29 01:12:32 +04:00
parent 9d840a764e
commit f1a112715c
12 changed files with 209 additions and 51 deletions

View File

@ -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,35 +17,31 @@ 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<ReportCoursesByProcedureViewModel> GetProcedureCourses(ProcedureSearchModel model)
public List<CourseViewModel> GetProcedureCourses(ProcedureSearchModel model)
{
var procedure = procedureStorage.GetElement(model);
var courses = courseStorage.GetFullList();
var recipes = recipeStorage.GetFullList();
var list = new List<ReportCoursesByProcedureViewModel>();
var record = new ReportCoursesByProcedureViewModel
{
Name = procedure!.Name,
Courses = new List<(int countDays, int pillsPerDay, string comment)>()
};
var list = new List<CourseViewModel>();
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)
{
foreach (var recipeId in recipeProcedureId)
@ -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)

View File

@ -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<CourseViewModel> Courses { get; set; } = new();
}
}

View File

@ -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;
/// <summary>
/// Получение типа выравнивания
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static JustificationValues GetJustificationValues(WordJustificationType type)
{
return type switch
{
WordJustificationType.Both => JustificationValues.Both,
WordJustificationType.Center => JustificationValues.Center,
_ => JustificationValues.Left,
};
}
/// <summary>
/// Настройки страницы
/// </summary>
/// <returns></returns>
private static SectionProperties CreateSectionProperties()
{
var properties = new SectionProperties();
var pageSize = new PageSize
{
Orient = PageOrientationValues.Portrait
};
properties.AppendChild(pageSize);
return properties;
}
/// <summary>
/// Задание форматирования для абзаца
/// </summary>
/// <param name="paragraphProperties"></param>
/// <returns></returns>
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();
}
}
}

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>

View File

@ -15,7 +15,7 @@ namespace PolyclinicContracts.BusinessLogicsContracts
/// Получение данных (списка) курсов по выбранным процедурам
/// </summary>
/// <returns></returns>
List<ReportCoursesByProcedureViewModel> GetProcedureCourses(ProcedureSearchModel model);
List<CourseViewModel> GetProcedureCourses(ProcedureSearchModel model);
/// <summary>
/// Получение списка процедур
@ -28,7 +28,7 @@ namespace PolyclinicContracts.BusinessLogicsContracts
/// Сохранение курсов по процедурам в файл-Word
/// </summary>
/// <param name="model"></param>
void SaveCoursesByProcedureToWordFile(ReportBindingModel model);
void SaveCoursesByProcedureToWordFile(ReportBindingModel model, ProcedureSearchModel procedureSearchMode);
/// <summary>
/// Сохранение курсов по процедурам в файл-Excel

View File

@ -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
{

View File

@ -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<HomeController> 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<ProcedureViewModel> 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()

View File

@ -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<IProcedureStorage, ProcedureStorage>();
builder.Services.AddTransient<IMedicamentStorage, MedicamentStorage>();
builder.Services.AddTransient<IRecipeStorage, RecipeStorage>();
builder.Services.AddTransient<AbstractSaveToWordCoursesByProcedures, SaveToWordCoursesByProcedure>();
builder.Services.AddTransient<ISuretorReportLogic, SuretorReportLogic>();
builder.Services.AddTransient<IDiagnoseLogic, DiagnoseLogic>();
builder.Services.AddTransient<ICourseLogic, CourseLogic>();
builder.Services.AddTransient<ISymptomLogic, SymptomLogic>();

View File

@ -1,5 +1,5 @@
@using PolyclinicContracts.ViewModels
@model List<ProcedureViewModel>
@model ProcedureViewModel
@{
ViewData["Title"] = "ListCoursesByProcedures";
}
@ -13,19 +13,15 @@
<p style="font-size: 3vh">
Выберите процедуру/ы из списка
</p>
<div class="list-procedures list-group overflow-auto">
<ul>
@{
foreach (var item in Model)
{
<li class="d-flex mb-2">
<input class="me-2" name="procedure-@item.Id" type="checkbox" id="procedure-@item.Id" />
<label for="procedure-@item.Id">@item.Name</label>
</li>
}
}
</ul>
</div>
<select id="procedureId" name="procedureId" style="width: 45vh">
<option value="">Выберите процедуру</option>
@foreach (var procedure in ViewBag.Procedures)
{
<option value="@procedure.Id">@procedure.Name</option>
}
</select>
</div>
<div class="d-flex flex-column mb-5 mt-5 justify-content-center align-items-center">
@ -34,20 +30,20 @@
</p>
<div class="d-flex flex-row align-content-center mb-3" style="font-size: 3vh">
<input type="radio" value="docx" class="radio-docx me-1" name="fileFormat" />
<input type="radio" value="docx" class="radio-docx me-1" name="fileFormat" checked/>
<label class="me-5">docx</label>
<input type="radio" value="xlsx" class="radio-xlsx me-1" name="fileFormat" />
<label>xlsx</label>
</div>
<p style="font-size: 3vh" class="mb-3">
Как назовем файл?
Как назовем файл?
</p>
<input type="text" placeholder="Отчет по курсам" />
<input type="text" name="fileName" placeholder="Отчет по курсам" required />
<div class="button-save-list-courses mt-5">
<input type="submit" value="Сохранить" class="button-save-list-courses btn" asp-action="Index" />
<input type="submit" value="Сохранить" class="button-save-list-courses btn" asp-action="CreateWordFile" />
</div>
</div>
</form>

Binary file not shown.

Binary file not shown.

Binary file not shown.