From 9e6be7f23c8ca59cf5cebeb42bfe8c14817963de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=A0=D0=BE=D0=B3=D0=B0=D1=88=D0=BE=D0=B2=D0=B0?= Date: Wed, 21 Jun 2023 23:38:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/RecipesLogic.cs | 34 +++++- .../BusinessLogics/ReportLogic.cs | 108 ++++++------------ .../MailWorker/AbstractMailWorker.cs | 64 +++++++++++ .../MailWorker/MailKitWorker.cs | 50 ++++++++ .../OfficePackage/AbstractSaveToPdf.cs | 10 +- .../OfficePackage/HelperModels/PdfInfo.cs | 2 +- .../Controllers/HomeController.cs | 94 ++++++++++++++- .../HospitalClientApp.csproj | 2 + Hospital/HospitalClientApp/Program.cs | 13 +++ .../Views/Home/ReportPdf.cshtml | 78 +++++++++++++ .../Views/Home/ReportWordExel.cshtml | 2 +- .../Views/Home/UpdateProcedure.cshtml | 11 +- .../Views/Home/UpdateRecipe.cshtml | 12 +- .../Views/Shared/_Layout.cshtml | 2 +- .../Controllers/ReportController.cs | 73 ++++++------ Hospital/HospitalRestApi/Program.cs | 16 +++ Hospital/HospitalRestApi/appsettings.json | 9 +- .../BindingModels/MailConfigBindingModel.cs | 18 +++ .../BindingModels/MailSendInfoBindingModel.cs | 15 +++ .../BusinessLogicsContracts/IReportLogic.cs | 4 +- ...ViewModel.cs => ReportRecipesViewModel.cs} | 7 +- .../Implements/RecipesStorage.cs | 19 ++- 22 files changed, 496 insertions(+), 147 deletions(-) create mode 100644 Hospital/HospitalBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 Hospital/HospitalBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 Hospital/HospitalClientApp/Views/Home/ReportPdf.cshtml create mode 100644 HospitalContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 HospitalContracts/BindingModels/MailSendInfoBindingModel.cs rename HospitalContracts/ViewModels/{ReportMedicinesViewModel.cs => ReportRecipesViewModel.cs} (50%) diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs index 356ce02..831c4f2 100644 --- a/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs @@ -1,4 +1,5 @@ -using HospitalContracts.BindingModels; +using HospitalBusinessLogic.MailWorker; +using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; @@ -17,10 +18,14 @@ namespace HospitalBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IRecipesStorage _recipesStorage; - public RecipesLogic(ILogger logger, IRecipesStorage recipesStorage) + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; + public RecipesLogic(ILogger logger, IRecipesStorage recipesStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) { _logger = logger; _recipesStorage = recipesStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; } public bool AddProcedures(RecipesSearchModel model, IProceduresModel procedure) @@ -60,11 +65,13 @@ namespace HospitalBusinessLogic.BusinessLogics public bool Create(RecipesBindingModel model) { CheckModel(model); - if (_recipesStorage.Insert(model) == null) - { + var result = _recipesStorage.Insert(model); + + if (result == null) { _logger.LogWarning("Insert operation failed"); return false; } + SendRecipeMessage(result.ClientId, $"Поликлиника \"Вы больны\", Рецепт №{result.Id}", $"Рецепт №{result.Id} созданный {result.Date} с дозировкой {result.Dose} и способом применения {result.ModeOfApplication} добавлен"); return true; } @@ -145,5 +152,24 @@ namespace HospitalBusinessLogic.BusinessLogics } _logger.LogInformation("Recipes.RecipesId:{Id}.Dose:{ Dose}.ModeOfApplication:{ ModeOfApplication}. MedicineId: { MedicineId}", model.Id, model.Dose, model.ModeOfApplication, model.SymptomsId); } + + private bool SendRecipeMessage(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + + if (client == null) + { + return false; + } + + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + + return true; + } } } diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/ReportLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/ReportLogic.cs index e190306..8673336 100644 --- a/Hospital/HospitalBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/ReportLogic.cs @@ -66,54 +66,27 @@ namespace HospitalBusinessLogic.BusinessLogics return list; } - //public List GetMembers(ReportBindingModel model) - //{ - // var listAll = new List(); - - // var listСonferences = _conferenceStorage.GetFilteredList(new ConferenceSearchModel - // { - // OrganiserId = model.OrganiserId, - // DateFrom = model.DateFrom, - // DateTo = model.DateTo - // }); - - // foreach (var conference in listСonferences) - // { - // foreach (var m in conference.ConferenceMembers.Values) - // { - // listAll.Add(new ReportMembersViewModel - // { - // StartDate = conference.StartDate, - // ConferenceName = conference.ConferenceName, - // MemberFIO = m.MemberFIO - // }); - // } - // } - - // var listMealPlans = _mealPlanStorage.GetFilteredList(new MealPlanSearchModel - // { - // OrganiserId = model.OrganiserId, - // }); - - // foreach (var mealPlan in listMealPlans) - // { - // foreach (var mp in mealPlan.MealPlanMembers.Values) - // { - // listAll.Add(new ReportMembersViewModel - // { - // MemberFIO = mp.MemberFIO, - // MealPlanName = mealPlan.MealPlanName, - // MealPlanPrice = mealPlan.MealPlanPrice - // }); - // } - // } - - // return listAll; - //} - - public List GetRecipeMedicine(ReportBindingModel model) + public List GetRecipes(ReportBindingModel model) { - throw new NotImplementedException(); + var listAll = new List(); + + var listRecipes = _recipeStorage.GetFilteredList(new RecipesSearchModel + { + ClientId = model.ClientId, + DateFrom = model.DateFrom, + DateTo = model.DateTo + }); + + foreach (var recipe in listRecipes) + { + listAll.Add(new ReportRecipesViewModel + { + Date = recipe.Date, + Dose = recipe.Dose, + ModeofApplication = recipe.ModeOfApplication + }); + } + return listAll; } public void SaveMedicinesToExcelFile(ReportBindingModel model) @@ -136,30 +109,25 @@ namespace HospitalBusinessLogic.BusinessLogics }); } - //public void SaveMembersToPdfFile(ReportBindingModel model) - //{ - // if (model.DateFrom == null) - // { - // throw new ArgumentException("Дата начала не задана"); - // } - - // if (model.DateTo == null) - // { - // throw new ArgumentException("Дата окончания не задана"); - // } - // _saveToPdf.CreateDoc(new PdfInfoOrganiser - // { - // FileName = model.FileName, - // Title = "Список участников", - // DateFrom = model.DateFrom!.Value, - // DateTo = model.DateTo!.Value, - // Members = GetMembers(model) - // }); - //} - - public void SaveOrdersToPdfFile(ReportBindingModel model) + public void SaveRecipesToPdfFile(ReportBindingModel model) { - throw new NotImplementedException(); + if (model.DateFrom == null) + { + throw new ArgumentException("Дата начала не задана"); + } + + if (model.DateTo == null) + { + throw new ArgumentException("Дата окончания не задана"); + } + _saveToPdf.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Список рецептов", + DateFrom = model.DateFrom!.Value, + DateTo = model.DateTo!.Value, + Recipes = GetRecipes(model) + }); } } } diff --git a/Hospital/HospitalBusinessLogic/MailWorker/AbstractMailWorker.cs b/Hospital/HospitalBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..9afc804 --- /dev/null +++ b/Hospital/HospitalBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,64 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + protected string _mailPassword = string.Empty; + protected string _smtpClientHost = string.Empty; + protected int _smtpClientPort; + protected string _popHost = string.Empty; + protected int _popPort; + private readonly IClientLogic _clientLogic; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IClientLogic clientLogic) + { + _logger = logger; + _clientLogic = clientLogic; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + { + return; + } + + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + + await SendMailAsync(info); + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + } +} diff --git a/Hospital/HospitalBusinessLogic/MailWorker/MailKitWorker.cs b/Hospital/HospitalBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..eb31c8c --- /dev/null +++ b/Hospital/HospitalBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,50 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net.Mime; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.BindingModels; + +namespace HospitalBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IClientLogic clientLogic) : base(logger, clientLogic) { } + + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + Attachment attachment = new Attachment("E:\\ReportsCourseWork\\pdffile.pdf", new ContentType(MediaTypeNames.Application.Pdf)); + objMailMessage.Attachments.Add(attachment); + + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + + + } +} diff --git a/Hospital/HospitalBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/Hospital/HospitalBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index 7c80749..cdadab6 100644 --- a/Hospital/HospitalBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/Hospital/HospitalBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -25,19 +25,19 @@ namespace HospitalBusinessLogic.OfficePackage Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - CreateTable(new List { "2cm", "6cm"}); + CreateTable(new List { "6cm", "3cm", "6cm"}); CreateRow(new PdfRowParameters { - Texts = new List { "Номер", "Дата создания рецепта" }, + Texts = new List { "Дата создания рецепта", "Доза", "Способ применения"}, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - foreach (var recipe in info.RecipeMedicine) + foreach (var recipe in info.Recipes) { CreateRow(new PdfRowParameters { - Texts = new List { recipe.Id.ToString(), - recipe.DateCreate.ToShortDateString() }, + Texts = new List { + recipe.Date.ToShortDateString(), recipe.Dose, recipe.ModeofApplication }, Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Left }); diff --git a/Hospital/HospitalBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/Hospital/HospitalBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs index 34b4a77..13c747b 100644 --- a/Hospital/HospitalBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs +++ b/Hospital/HospitalBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -13,6 +13,6 @@ namespace HospitalBusinessLogic.OfficePackage.HelperModels public string Title { get; set; } = string.Empty; public DateTime DateFrom { get; set; } public DateTime DateTo { get; set; } - public List RecipeMedicine { get; set; } = new(); + public List Recipes { get; set; } = new(); } } diff --git a/Hospital/HospitalClientApp/Controllers/HomeController.cs b/Hospital/HospitalClientApp/Controllers/HomeController.cs index 91fa145..8574000 100644 --- a/Hospital/HospitalClientApp/Controllers/HomeController.cs +++ b/Hospital/HospitalClientApp/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using HospitalClientApp.Models; using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.ViewModels; using Microsoft.AspNetCore.Mvc; @@ -14,10 +15,13 @@ namespace HospitalClientApp.Controllers public class HomeController : Controller { private readonly ILogger _logger; + private readonly IReportLogic _report; - public HomeController(ILogger logger) + + public HomeController(ILogger logger, IReportLogic report) //IReportLogic report { _logger = logger; + _report = report; } public IActionResult Index() @@ -173,6 +177,94 @@ namespace HospitalClientApp.Controllers { return new PhysicalFileResult("E:\\ReportsCourseWork\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } + + [HttpGet] + public IActionResult ReportPdf() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View("ReportPdf"); + } + + [HttpPost] + public void ReportPdf(DateTime dateFrom, DateTime dateTo, string clientEmail) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(clientEmail)) + { + throw new Exception("Email пуст"); + } + APIClient.PostRequest("api/report/CreateReportToPdfFile", new ReportBindingModel + { + DateFrom = dateFrom, + DateTo = dateTo, + ClientId = APIClient.Client.Id + }); + APIClient.PostRequest("api/report/SendPdfToMail", new MailSendInfoBindingModel + { + MailAddress = clientEmail, + Subject = "Отчет по рецептам (pdf)", + Text = "Отчет по рецептам с " + dateFrom.ToShortDateString() + " до " + dateTo.ToShortDateString() + }); + Response.Redirect("ReportPdf"); + } + + [HttpGet] + public string GetRecipesReport(DateTime dateFrom, DateTime dateTo) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + List result; + try + { + result = _report.GetRecipes(new ReportBindingModel + { + ClientId = APIClient.Client.Id, + DateFrom = dateFrom, + DateTo = dateTo + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания отчета"); + throw; + } + + string table = ""; + table += $"

Предварительный отчет

"; + table += $""; + table += ""; + table += ""; + table += $""; + table += $""; + table += $""; + table += ""; + table += ""; + foreach (var report in result) + { + bool IsDate = true; + if (report.Date.ToShortDateString() == "01.01.0001") + { + IsDate = false; + } + table += ""; + table += ""; + table += $""; + table += $""; + table += $""; + table += ""; + table += ""; + } + table += "
Дата созданияДозировкаСпособ применения
{(IsDate is true ? report.Date.ToShortDateString() : string.Empty)}{report.Dose}{report.ModeofApplication}
"; + return table; + } /// /// ПРОЦЕДУРЫ /// diff --git a/Hospital/HospitalClientApp/HospitalClientApp.csproj b/Hospital/HospitalClientApp/HospitalClientApp.csproj index 651d9b3..441309b 100644 --- a/Hospital/HospitalClientApp/HospitalClientApp.csproj +++ b/Hospital/HospitalClientApp/HospitalClientApp.csproj @@ -12,6 +12,8 @@ + + diff --git a/Hospital/HospitalClientApp/Program.cs b/Hospital/HospitalClientApp/Program.cs index b06f63e..b072a39 100644 --- a/Hospital/HospitalClientApp/Program.cs +++ b/Hospital/HospitalClientApp/Program.cs @@ -1,6 +1,19 @@ +using HospitalBusinessLogic.BusinessLogics; +using HospitalBusinessLogic.OfficePackage; +using HospitalBusinessLogic.OfficePackage.Implements; using HospitalClientApp; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.StoragesContracts; +using HospitalDataBaseImplements.Implements; var builder = WebApplication.CreateBuilder(args); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); diff --git a/Hospital/HospitalClientApp/Views/Home/ReportPdf.cshtml b/Hospital/HospitalClientApp/Views/Home/ReportPdf.cshtml new file mode 100644 index 0000000..b99960b --- /dev/null +++ b/Hospital/HospitalClientApp/Views/Home/ReportPdf.cshtml @@ -0,0 +1,78 @@ +@using HospitalContracts.ViewModels + +@{ + ViewData["Title"] = "ReportPdf"; +} + +
+

Отчет по рецептам за период

+
+ +
+ @{ +
+
+
Начало периода:
+
+ +
+
+
+
+
Окончание:
+
+ +
+
+
+
+
Введите почту:
+
+ +
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+ } +
+ +@section Scripts { + +} diff --git a/Hospital/HospitalClientApp/Views/Home/ReportWordExel.cshtml b/Hospital/HospitalClientApp/Views/Home/ReportWordExel.cshtml index cc37bbb..1bce6b7 100644 --- a/Hospital/HospitalClientApp/Views/Home/ReportWordExel.cshtml +++ b/Hospital/HospitalClientApp/Views/Home/ReportWordExel.cshtml @@ -71,7 +71,7 @@
-
+
diff --git a/Hospital/HospitalClientApp/Views/Home/UpdateProcedure.cshtml b/Hospital/HospitalClientApp/Views/Home/UpdateProcedure.cshtml index ef734f4..890671d 100644 --- a/Hospital/HospitalClientApp/Views/Home/UpdateProcedure.cshtml +++ b/Hospital/HospitalClientApp/Views/Home/UpdateProcedure.cshtml @@ -28,16 +28,7 @@ placeholder="Тип процедуры" name="type" /> -
-

Лекарства

- - - - - - -
Название
-
+
diff --git a/Hospital/HospitalClientApp/Views/Home/UpdateRecipe.cshtml b/Hospital/HospitalClientApp/Views/Home/UpdateRecipe.cshtml index 71e37fe..f480eb1 100644 --- a/Hospital/HospitalClientApp/Views/Home/UpdateRecipe.cshtml +++ b/Hospital/HospitalClientApp/Views/Home/UpdateRecipe.cshtml @@ -33,17 +33,7 @@
-
-

Процедуры

- - - - - - - -
НазваниеТип
-
+
diff --git a/Hospital/HospitalClientApp/Views/Shared/_Layout.cshtml b/Hospital/HospitalClientApp/Views/Shared/_Layout.cshtml index ec4b4c0..933744e 100644 --- a/Hospital/HospitalClientApp/Views/Shared/_Layout.cshtml +++ b/Hospital/HospitalClientApp/Views/Shared/_Layout.cshtml @@ -61,7 +61,7 @@
  • Список лекарств (pdf) + asp-area="dropdown-item" asp-controller="Home" asp-action="ReportPdf">Список рецептов (pdf)
  • diff --git a/Hospital/HospitalRestApi/Controllers/ReportController.cs b/Hospital/HospitalRestApi/Controllers/ReportController.cs index b89ef90..20454f4 100644 --- a/Hospital/HospitalRestApi/Controllers/ReportController.cs +++ b/Hospital/HospitalRestApi/Controllers/ReportController.cs @@ -1,4 +1,5 @@ -using HospitalContracts.BindingModels; +using HospitalBusinessLogic.MailWorker; +using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using Microsoft.AspNetCore.Mvc; @@ -10,47 +11,47 @@ namespace HospitalRestApi.Controllers { private readonly ILogger _logger; private readonly IReportLogic _reportLogic; - //private readonly AbstractMailWorker _mailWorker; - public ReportController(ILogger logger, IReportLogic reportLogic) + private readonly AbstractMailWorker _mailWorker; + public ReportController(ILogger logger, IReportLogic reportLogic, AbstractMailWorker mailWorker) { _logger = logger; _reportLogic = reportLogic; - //_mailWorker = mailWorker; + _mailWorker = mailWorker; } - //[HttpPost] - //public void CreateReportToPdfFile(ReportBindingModel model) - //{ - // try - // { - // _reportLogic.SaveMedicinesToPdfFile(new ReportBindingModel - // { - // DateFrom = model.DateFrom, - // DateTo = model.DateTo, - // OrganiserId = model.OrganiserId, - // FileName = "C:\\ReportsCourseWork\\pdffile.pdf", - // }); - // } - // catch (Exception ex) - // { - // _logger.LogError(ex, "Ошибка создания отчета"); - // throw; - // } - //} + [HttpPost] + public void CreateReportToPdfFile(ReportBindingModel model) + { + try + { + _reportLogic.SaveRecipesToPdfFile(new ReportBindingModel + { + DateFrom = model.DateFrom, + DateTo = model.DateTo, + ClientId = model.ClientId, + FileName = "E:\\ReportsCourseWork\\pdffile.pdf", + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания отчета"); + throw; + } + } - //[HttpPost] - //public void SendPdfToMail(MailSendInfoBindingModel model) - //{ - // try - // { - // _mailWorker.MailSendAsync(model); - // } - // catch (Exception ex) - // { - // _logger.LogError(ex, "Ошибка отправки письма"); - // throw; - // } - //} + [HttpPost] + public void SendPdfToMail(MailSendInfoBindingModel model) + { + try + { + _mailWorker.MailSendAsync(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отправки письма"); + throw; + } + } [HttpPost] public void CreateReportToWordFile(ReportBindingModel model) diff --git a/Hospital/HospitalRestApi/Program.cs b/Hospital/HospitalRestApi/Program.cs index fe63820..9b7c677 100644 --- a/Hospital/HospitalRestApi/Program.cs +++ b/Hospital/HospitalRestApi/Program.cs @@ -1,6 +1,8 @@ using HospitalBusinessLogic.BusinessLogics; +using HospitalBusinessLogic.MailWorker; using HospitalBusinessLogic.OfficePackage; using HospitalBusinessLogic.OfficePackage.Implements; +using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.StoragesContracts; using HospitalDataBaseImplements; @@ -35,6 +37,8 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddSingleton(); + builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -53,6 +57,18 @@ var app = builder.Build(); LoaderFromXML.LoadSymptoms(); LoaderFromXML.LoadKurses(); +var mailSender = app.Services.GetService(); + +mailSender?.MailConfig(new MailConfigBindingModel +{ + MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty, + MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty, + SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty, + SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()), + PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty, + PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString()) +}); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/Hospital/HospitalRestApi/appsettings.json b/Hospital/HospitalRestApi/appsettings.json index 10f68b8..17020f2 100644 --- a/Hospital/HospitalRestApi/appsettings.json +++ b/Hospital/HospitalRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "rea22works@gmail.com", + "MailPassword": "paww pnyu fpho bgts" } diff --git a/HospitalContracts/BindingModels/MailConfigBindingModel.cs b/HospitalContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..0f7df55 --- /dev/null +++ b/HospitalContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + public string MailPassword { get; set; } = string.Empty; + public string SmtpClientHost { get; set; } = string.Empty; + public int SmtpClientPort { get; set; } + public string PopHost { get; set; } = string.Empty; + public int PopPort { get; set; } + } +} diff --git a/HospitalContracts/BindingModels/MailSendInfoBindingModel.cs b/HospitalContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..333f25f --- /dev/null +++ b/HospitalContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} diff --git a/HospitalContracts/BusinessLogicsContracts/IReportLogic.cs b/HospitalContracts/BusinessLogicsContracts/IReportLogic.cs index f4c4781..3d1fdf5 100644 --- a/HospitalContracts/BusinessLogicsContracts/IReportLogic.cs +++ b/HospitalContracts/BusinessLogicsContracts/IReportLogic.cs @@ -20,7 +20,7 @@ namespace HospitalContracts.BusinessLogicsContracts /// /// /// - List GetRecipeMedicine(ReportBindingModel model); + List GetRecipes(ReportBindingModel model); /// /// Сохранение компонент в файл-Word /// @@ -35,6 +35,6 @@ namespace HospitalContracts.BusinessLogicsContracts /// Сохранение заказов в файл-Pdf /// /// - void SaveOrdersToPdfFile(ReportBindingModel model); + void SaveRecipesToPdfFile(ReportBindingModel model); } } diff --git a/HospitalContracts/ViewModels/ReportMedicinesViewModel.cs b/HospitalContracts/ViewModels/ReportRecipesViewModel.cs similarity index 50% rename from HospitalContracts/ViewModels/ReportMedicinesViewModel.cs rename to HospitalContracts/ViewModels/ReportRecipesViewModel.cs index 75c12fd..550e19e 100644 --- a/HospitalContracts/ViewModels/ReportMedicinesViewModel.cs +++ b/HospitalContracts/ViewModels/ReportRecipesViewModel.cs @@ -6,10 +6,11 @@ using System.Threading.Tasks; namespace HospitalContracts.ViewModels { - public class ReportMedicinesViewModel + public class ReportRecipesViewModel { public int Id { get; set; } - public DateTime DateCreate { get; set; } - public string MedicinesName { get; set; } = string.Empty; + public DateTime Date { get; set; } + public string Dose { get; set; } = string.Empty; + public string ModeofApplication { get; set; } = string.Empty; } } diff --git a/HospitalDataBaseImplements/Implements/RecipesStorage.cs b/HospitalDataBaseImplements/Implements/RecipesStorage.cs index 47abaff..ec22510 100644 --- a/HospitalDataBaseImplements/Implements/RecipesStorage.cs +++ b/HospitalDataBaseImplements/Implements/RecipesStorage.cs @@ -47,12 +47,28 @@ namespace HospitalDataBaseImplements.Implements //НУЖЕН ТУТ ЕЩЕ ОДИН ТУ ЛИСТ??? public List GetFilteredList(RecipesSearchModel model) { + if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue) + { + return new(); + } if (model is null) { return new(); } using var context = new HospitalDatabase(); - + if (model.DateFrom.HasValue) + { + return context.Recipes. + Include(x => x.Procedures). + ThenInclude(x => x.Procedure) + .Include(x => x.Symptoms) + .Include(x => x.Client) + .Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo && x.ClientId == model.ClientId) + .Where(x => x.ClientId == model.ClientId).ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + else{ return context.Recipes. Include(x => x.Procedures). ThenInclude(x => x.Procedure) @@ -61,6 +77,7 @@ namespace HospitalDataBaseImplements.Implements .Where(x => x.ClientId == model.ClientId).ToList() .Select(x => x.GetViewModel) .ToList(); + } } public List GetFullList()