From 4665f4a4093f8bb98041364e08949a5ef24ce1e3 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Sat, 25 May 2024 20:56:44 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D1=82=D1=8B=20=D0=B2=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=B0=D0=B5=D1=88=D1=8C=20=D0=BC=D0=BD=D0=B5=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BA=D1=83=D1=8E=20=D1=82=D0=BE=20=D0=B4=D0=B8=D1=87=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MailWorker/AbstractMailWorker.cs | 64 +++++++++++++ .../MailWorker/MailWorker.cs | 50 +++++++++++ .../BindingModels/MailConfigBindingModel.cs | 18 ++++ .../BindingModels/MailSendInfoBindingModel.cs | 15 ++++ .../Controllers/DrugController.cs | 4 +- .../Controllers/ReportController.cs | 89 +++++++++++++++++++ VeterinaryView/VeterinaryRestApi/Program.cs | 26 +++++- .../VeterinaryRestApi/appsettings.json | 8 +- .../Controllers/HomeController.cs | 49 ++++++++++ .../Home/PurchaseMedicationReport.cshtml | 17 +++- 10 files changed, 330 insertions(+), 10 deletions(-) create mode 100644 VeterinaryView/VeterinaryBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 VeterinaryView/VeterinaryBusinessLogic/MailWorker/MailWorker.cs create mode 100644 VeterinaryView/VeterinaryContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 VeterinaryView/VeterinaryContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs diff --git a/VeterinaryView/VeterinaryBusinessLogic/MailWorker/AbstractMailWorker.cs b/VeterinaryView/VeterinaryBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..0373655 --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,64 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; + +namespace VeterinaryBusinessLogic.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 IDoctorLogic _doctorLogic; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IDoctorLogic doctorLogic) + { + _logger = logger; + _doctorLogic = doctorLogic; + } + + 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/VeterinaryView/VeterinaryBusinessLogic/MailWorker/MailWorker.cs b/VeterinaryView/VeterinaryBusinessLogic/MailWorker/MailWorker.cs new file mode 100644 index 0000000..4b752ad --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/MailWorker/MailWorker.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 VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; + +namespace VeterinaryBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IDoctorLogic doctorLogic) : base(logger, doctorLogic) { } + + 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("C:\\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/VeterinaryView/VeterinaryContracts/BindingModels/MailConfigBindingModel.cs b/VeterinaryView/VeterinaryContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..c44d152 --- /dev/null +++ b/VeterinaryView/VeterinaryContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VeterinaryContracts.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/VeterinaryView/VeterinaryContracts/BindingModels/MailSendInfoBindingModel.cs b/VeterinaryView/VeterinaryContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..6e6e149 --- /dev/null +++ b/VeterinaryView/VeterinaryContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VeterinaryContracts.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/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs index 2b373ef..472f6c7 100644 --- a/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs +++ b/VeterinaryView/VeterinaryRestApi/Controllers/DrugController.cs @@ -26,9 +26,7 @@ namespace VeterinaryRestApi.Controllers var elem = _drug.ReadElement(new DrugSearchModel { Id = drugId }); if (elem == null) return null; - var huinya = Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList()); - return huinya; - //return Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList()); + return Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList()); } catch (Exception ex) { diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs new file mode 100644 index 0000000..081f01c --- /dev/null +++ b/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs @@ -0,0 +1,89 @@ +using Microsoft.AspNetCore.Mvc; +using VeterinaryBusinessLogic.BusinessLogic; +using VeterinaryBusinessLogic.MailWorker; +//using VeterinaryBusinessLogic.MailWorker; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryDatabaseImplement.Implements; + +namespace VeterinaryRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ReportController : Controller + { + private readonly IReportLogicDoctor _reportDoctor; + private readonly AbstractMailWorker _mailWorker; + public ReportController(ILogger logger, IReportLogicDoctor reportDoctor, AbstractMailWorker mailWorker) + { + _reportDoctor = reportDoctor; + _mailWorker = mailWorker; + } + [Microsoft.AspNetCore.Mvc.HttpGet] + public IActionResult Index(ReportLogicDoctor reportDoctor) + { + return View(); + } + [HttpPost] + public void CreatePurchaseListWordFile(ReportPurchaseMedicationBindingModel model) + { + try + { + _reportDoctor.SavePurchasesToWordFile(model); + } + catch (Exception ex) + { + throw; + } + } + [HttpPost] + public void CreatePurchaseListExcelFile(ReportPurchaseMedicationBindingModel model) + { + try + { + _reportDoctor.SavePurchasesToExcelFile(model); + } + catch (Exception ex) + { + throw; + } + } + [HttpGet] + public List GetVisitsGuidesReport(string dateFrom, string dateTo, int pharmacistId) + { + try + { + DateTime DateFrom = DateTime.Parse(dateFrom); + DateTime DateTo = DateTime.Parse(dateTo); + VisitsGuidesBindingModel model = new(); + model.DateFrom = DateFrom; + model.DateTo = DateTo; + model.DoctorId = pharmacistId; + return _reportDoctor.GetMedicineVisitsAndGuidances(model); + } + catch (Exception ex) + { + throw; + } + } + + [HttpPost] + public void SendVisitsGuidesReportToEmail(VisitsGuidesBindingModel model) + { + try + { + _reportDoctor.SaveMedicinesToPdfFile(model); + _mailWorker.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = model.Email!, + Subject = "Отчет по медикаментам", + Text = "Лови" + }); + } + catch (Exception ex) + { + throw; + } + } + } +} diff --git a/VeterinaryView/VeterinaryRestApi/Program.cs b/VeterinaryView/VeterinaryRestApi/Program.cs index 9dc04af..48b2afc 100644 --- a/VeterinaryView/VeterinaryRestApi/Program.cs +++ b/VeterinaryView/VeterinaryRestApi/Program.cs @@ -1,8 +1,10 @@ using VeterinaryBusinessLogic.BusinessLogic; +using VeterinaryContracts.BindingModels; using VeterinaryContracts.BusinessLogicContracts; using VeterinaryContracts.StorageContracts; using VeterinaryDatabaseImplement.Implements; using Microsoft.OpenApi.Models; +using VeterinaryBusinessLogic.MailWorker; var builder = WebApplication.CreateBuilder(args); @@ -29,9 +31,29 @@ builder.Services.AddTransient(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); - +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo { Title = "VeterinaryViewRestApi", Version = "v1" }); +}); var app = builder.Build(); +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/VeterinaryView/VeterinaryRestApi/appsettings.json b/VeterinaryView/VeterinaryRestApi/appsettings.json index 10f68b8..29d63fa 100644 --- a/VeterinaryView/VeterinaryRestApi/appsettings.json +++ b/VeterinaryView/VeterinaryRestApi/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "anasirov48@gmail.com", + "MailPassword": "ozxp vjof uinv fcmj" } diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs index 6acbfe4..58ed78e 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs +++ b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs @@ -474,6 +474,55 @@ namespace VeterinaryShowDoctorApp.Controllers ViewBag.Medications = APIDoctor.GetRequest>($"api/medication/getmedications?doctorid={APIDoctor.Doctor.Id}"); return View(); } + + [HttpPost] + public void PurchaseMedicationReport(List medications, string type) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + + if (medications.Count <= 0) + { + throw new Exception("Количество должно быть больше 0"); + } + + if (string.IsNullOrEmpty(type)) + { + throw new Exception("Неверный тип отчета"); + } + + if (type == "docx") + { + APIDoctor.PostRequest("api/report/createpurchaselistwordfile", new ReportPurchaseMedicationBindingModel + { + Medications = medications, + FileName = "C:\\ReportsCourseWork\\wordfile.docx" + }); + Response.Redirect("GetWordFile"); + } + else + { + APIDoctor.PostRequest("api/report/createpurchaselistexcelfile", new ReportPurchaseMedicationBindingModel + { + Medications = medications, + FileName = "C:\\ReportsCourseWork\\excelfile.xlsx" + }); + Response.Redirect("GetExcelFile"); + } + } + + [HttpGet] + public IActionResult GetWordFile() + { + return new PhysicalFileResult("C:\\ReportsCourseWork\\wordfile.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + } + + public IActionResult GetExcelFile() + { + return new PhysicalFileResult("C:\\ReportsCourseWork\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + } [HttpGet] public IActionResult Report() { diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/PurchaseMedicationReport.cshtml b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/PurchaseMedicationReport.cshtml index b9daf6d..693fc0e 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/PurchaseMedicationReport.cshtml +++ b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/PurchaseMedicationReport.cshtml @@ -19,9 +19,18 @@ -
-
-
-
+
+ +
+ + +
+
+ + +
+
+
+
From d9dd87f489ff916536ba09c330f39108566252f9 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Sat, 25 May 2024 22:45:25 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=B4=D0=B8=D0=BD=D1=8C=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=BD=20=D0=B2=20=D1=82=D0=B2=D0=BE=D0=B9=20=D0=B4=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/DoctorLogic.cs | 5 + .../BusinessLogic/ReportLogicDoctor.cs | 18 ++- .../OfficePackage/AbstractSaveToPdfDoctor.cs | 72 +++++++++++ .../OfficePackage/HelperModels/PdfInfo.cs | 2 +- .../Implements/SaveToPdfDoctor.cs | 108 +++++++++++++++++ .../VeterinaryBusinessLogic.csproj | 1 + .../ReportDrugsVisitsBindingModel.cs | 10 +- .../Implements/MedicationStorage.cs | 3 +- .../Controllers/ReportController.cs | 13 +- .../VeterinaryRestApi/appsettings.json | 2 +- .../Controllers/HomeController.cs | 93 +++++++++++++- .../Views/Home/Drugs.cshtml | 6 + .../Views/Home/Report.cshtml | 113 ++++++++++-------- 13 files changed, 380 insertions(+), 66 deletions(-) create mode 100644 VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs create mode 100644 VeterinaryView/VeterinaryBusinessLogic/OfficePackage/Implements/SaveToPdfDoctor.cs diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DoctorLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DoctorLogic.cs index 4b4a106..4491de9 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DoctorLogic.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/DoctorLogic.cs @@ -9,6 +9,7 @@ using VeterinaryContracts.SearchModels; using VeterinaryContracts.StorageContracts; using VeterinaryContracts.ViewModels; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; namespace VeterinaryBusinessLogic.BusinessLogic { @@ -101,6 +102,10 @@ namespace VeterinaryBusinessLogic.BusinessLogic throw new ArgumentNullException("Нет Login доктора", nameof(model.Login)); } + if (!Regex.IsMatch(model.Login, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) + { + throw new ArgumentException("Некорретно введен email клиента", nameof(model.Login)); + } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля доктора", diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs index 4772aef..521c561 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/ReportLogicDoctor.cs @@ -19,12 +19,15 @@ namespace VeterinaryBusinessLogic.BusinessLogic private readonly IMedicationStorage _medicationStorage; private readonly AbstractSaveToExcelDoctor _saveToExcel; private readonly AbstractSaveToWordDoctor _saveToWord; + private readonly AbstractSaveToPdfDoctor _saveToPdf; + public ReportLogicDoctor( IMedicationStorage medicationStorage, - AbstractSaveToExcelDoctor saveToExcel, AbstractSaveToWordDoctor saveToWord) + AbstractSaveToExcelDoctor saveToExcel, AbstractSaveToWordDoctor saveToWord, AbstractSaveToPdfDoctor saveToPdf) { _medicationStorage = medicationStorage; _saveToExcel = saveToExcel; _saveToWord = saveToWord; + _saveToPdf = saveToPdf; } public void SavePurchasesToExcelFile(ReportPurchaseMedicationBindingModel model) { @@ -54,5 +57,18 @@ namespace VeterinaryBusinessLogic.BusinessLogic { return _medicationStorage.GetReportDrugsVisits(new() { DateFrom = model.DateFrom, DateTo = model.DateTo }); } + + public void SaveMedicationsToPdfFile(ReportDrugsVisitsBindingModel model) + { + _saveToPdf.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Список медикаментов", + DateFrom = model.DateFrom!, + DateTo = model.DateTo!, + ReportDrugsVisits = GetVisitsDrugs(model) + }); + + } } } diff --git a/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs new file mode 100644 index 0000000..a5188d7 --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/AbstractSaveToPdfDoctor.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryBusinessLogic.OfficePackage.HelperEnums; +using VeterinaryBusinessLogic.OfficePackage.HelperModels; +using VeterinaryDatabaseImplement.Implements; + +namespace VeterinaryBusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToPdfDoctor + { + public void CreateDoc(PdfInfo info) + { + CreatePdf(info); + CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = + "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateParagraph(new PdfParagraph + { + Text = $"с { info.DateFrom.ToShortDateString() } по { info.DateTo.ToShortDateString() }", Style + = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateTable(new List { "4cm", "4cm", "4cm", "4cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "Дата", "Название медикамента", "Услуга рекомендации","Название визита" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var medication in info.ReportDrugsVisits) + { + CreateRow(new PdfRowParameters + { + Texts = new List { "", medication.MedicationName, "", "" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach(var visit in medication.Visits) + { + CreateRow(new PdfRowParameters + { + Texts = new List { visit.DateVisit.ToString(), "", "", visit.VisitName }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + } + foreach (var guidance in medication.Drugs) + { + CreateRow(new PdfRowParameters + { + Texts = new List { guidance.DateCreate.ToString(), "", guidance.DrugName, "" }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + } + } + SavePdf(info); + } + protected abstract void CreatePdf(PdfInfo info); + protected abstract void CreateParagraph(PdfParagraph paragraph); + protected abstract void CreateTable(List columns); + protected abstract void CreateRow(PdfRowParameters rowParameters); + protected abstract void SavePdf(PdfInfo info); + } +} diff --git a/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs index befb5d4..c7dbbd8 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -10,6 +10,6 @@ namespace VeterinaryBusinessLogic.OfficePackage.HelperModels public DateTime DateFrom { get; set; } public DateTime DateTo { get; set; } public List ReportDrugsVisits { get; set; } = new(); - public List ReportVisitsDrugs{ get; set; } = new(); + public List ReportVisitsDrugs{ get; set; } = new();// возможно надо убрать } } diff --git a/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/Implements/SaveToPdfDoctor.cs b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/Implements/SaveToPdfDoctor.cs new file mode 100644 index 0000000..a24f1bf --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/OfficePackage/Implements/SaveToPdfDoctor.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VeterinaryBusinessLogic.OfficePackage.HelperEnums; +using VeterinaryBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using VeterinaryBusinessLogic.OfficePackage; + +namespace VeterinaryBusinessLogic.OfficePackage.Implements +{ + public class SaveToPdfDoctor : AbstractSaveToPdfDoctor + { + private Document? _document; + private Section? _section; + private Table? _table; + private static ParagraphAlignment + GetParagraphAlignment(PdfParagraphAlignmentType type) + { + return type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Right => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + /// + /// Создание стилей для документа + /// + /// + private static void DefineStyles(Document document) + { + var style = document.Styles["Normal"]; + style.Font.Name = "Times New Roman"; + style.Font.Size = 14; + style = document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + } + protected override void CreatePdf(PdfInfo info) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + } + protected override void CreateParagraph(PdfParagraph pdfParagraph) + { + if (_section == null) + { + return; + } + var paragraph = _section.AddParagraph(pdfParagraph.Text); + paragraph.Format.SpaceAfter = "1cm"; + paragraph.Format.Alignment = + GetParagraphAlignment(pdfParagraph.ParagraphAlignment); + paragraph.Style = pdfParagraph.Style; + } + protected override void CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + protected override void CreateRow(PdfRowParameters rowParameters) + { + if (_table == null) + { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Texts.Count; ++i) + { + row.Cells[i].AddParagraph(rowParameters.Texts[i]); + if (!string.IsNullOrEmpty(rowParameters.Style)) + { + row.Cells[i].Style = rowParameters.Style; + } + Unit borderWidth = 0.5; + row.Cells[i].Borders.Left.Width = borderWidth; + row.Cells[i].Borders.Right.Width = borderWidth; + row.Cells[i].Borders.Top.Width = borderWidth; + row.Cells[i].Borders.Bottom.Width = borderWidth; + row.Cells[i].Format.Alignment = + GetParagraphAlignment(rowParameters.ParagraphAlignment); + row.Cells[i].VerticalAlignment = VerticalAlignment.Center; + } + } + protected override void SavePdf(PdfInfo info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } + +} diff --git a/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj b/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj index 8af590c..1411e9c 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj +++ b/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj @@ -14,6 +14,7 @@ + diff --git a/VeterinaryView/VeterinaryContracts/BindingModels/ReportDrugsVisitsBindingModel.cs b/VeterinaryView/VeterinaryContracts/BindingModels/ReportDrugsVisitsBindingModel.cs index bd80480..89e7acf 100644 --- a/VeterinaryView/VeterinaryContracts/BindingModels/ReportDrugsVisitsBindingModel.cs +++ b/VeterinaryView/VeterinaryContracts/BindingModels/ReportDrugsVisitsBindingModel.cs @@ -9,8 +9,12 @@ namespace VeterinaryContracts.BindingModels public class ReportDrugsVisitsBindingModel { public string FileName { get; set; } = string.Empty; - public List Medications { get; set; } = new(); - public DateTime? DateFrom { get; set; } - public DateTime? DateTo { get; set; } + //public List Medications { get; set; } = new(); + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + public int? DoctorId { get; set; } + public string? Email { get; set; } + + } } diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs b/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs index 7464fda..9a9e1bb 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Implements/MedicationStorage.cs @@ -110,7 +110,8 @@ namespace VeterinaryDatabaseImplement.Implements { MedicationName = pet.MedicationName, Drugs = context.Drugs - .Where(drug => drug.Medications + .Where(drug => drug.DateCreate <= model.DateTo && + drug.DateCreate >= model.DateFrom && drug.Medications .Select(x => x.MedicationId) .ToList() .Contains(pet.Id)) diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs index 081f01c..6123d33 100644 --- a/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs +++ b/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs @@ -4,6 +4,7 @@ using VeterinaryBusinessLogic.MailWorker; //using VeterinaryBusinessLogic.MailWorker; using VeterinaryContracts.BindingModels; using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.ViewModels; using VeterinaryDatabaseImplement.Implements; namespace VeterinaryRestApi.Controllers @@ -49,17 +50,17 @@ namespace VeterinaryRestApi.Controllers } } [HttpGet] - public List GetVisitsGuidesReport(string dateFrom, string dateTo, int pharmacistId) + public List GetVisitsGuidesReport(string dateFrom, string dateTo, int doctorId)// переименовать { try { DateTime DateFrom = DateTime.Parse(dateFrom); DateTime DateTo = DateTime.Parse(dateTo); - VisitsGuidesBindingModel model = new(); + ReportDrugsVisitsBindingModel model = new(); model.DateFrom = DateFrom; model.DateTo = DateTo; - model.DoctorId = pharmacistId; - return _reportDoctor.GetMedicineVisitsAndGuidances(model); + model.DoctorId = doctorId; + return _reportDoctor.GetVisitsDrugs(model); } catch (Exception ex) { @@ -68,11 +69,11 @@ namespace VeterinaryRestApi.Controllers } [HttpPost] - public void SendVisitsGuidesReportToEmail(VisitsGuidesBindingModel model) + public void SendVisitsGuidesReportToEmail(ReportDrugsVisitsBindingModel model)// переименовать { try { - _reportDoctor.SaveMedicinesToPdfFile(model); + _reportDoctor.SaveMedicationsToPdfFile(model); _mailWorker.MailSendAsync(new MailSendInfoBindingModel { MailAddress = model.Email!, diff --git a/VeterinaryView/VeterinaryRestApi/appsettings.json b/VeterinaryView/VeterinaryRestApi/appsettings.json index 29d63fa..5645fc4 100644 --- a/VeterinaryView/VeterinaryRestApi/appsettings.json +++ b/VeterinaryView/VeterinaryRestApi/appsettings.json @@ -11,5 +11,5 @@ "PopHost": "pop.gmail.com", "PopPort": "995", "MailLogin": "anasirov48@gmail.com", - "MailPassword": "ozxp vjof uinv fcmj" + "MailPassword": "xoac ehyi tnar fiho" } diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs index 58ed78e..dce67d2 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs +++ b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs @@ -7,6 +7,7 @@ using System.Text; using VeterinaryDataModels.Models; using VeterinaryContracts.SearchModels; using VeterinaryContracts.StorageContracts; +using System.Globalization; namespace VeterinaryShowDoctorApp.Controllers { @@ -526,11 +527,95 @@ namespace VeterinaryShowDoctorApp.Controllers [HttpGet] public IActionResult Report() { - if (APIDoctor.Doctor == null) - { - return Redirect("~/Home/Enter"); - } + ViewBag.Report = new List(); return View(); } + [HttpGet] + public string GetMedicationsReport(DateTime dateFrom, DateTime dateTo) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + List result; + try + { + string dateFromS = dateFrom.ToString("s", CultureInfo.InvariantCulture); + string dateToS = dateTo.ToString("s", CultureInfo.InvariantCulture); + result = APIDoctor.GetRequest> + ($"api/report/getvisitsguidesreport?datefrom={dateFromS}&dateto={dateToS}&doctorid={APIDoctor.Doctor.Id}")!; // переделать + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания отчета"); + throw; + } + string table = ""; + table += "

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

"; + table += "
"; + table += ""; + table += ""; + table += ""; + table += ""; + table += ""; + table += ""; + table += ""; + table += ""; + table += ""; + foreach (var medication in result) + { + table += ""; + table += ""; + table += $""; + table += $""; + table += $""; + table += $""; + table += ""; + foreach (var drug in medication.Drugs) + { + table += ""; + table += $""; + table += $""; + table += $""; + table += $""; + table += ""; + } + foreach (var visit in medication.Visits) + { + table += ""; + table += $""; + table += $""; + table += $""; + table += $""; + table += ""; + } + table += ""; + } + table += "
ДатаНазвание медикаментаЛекарствоНазвание визита
{medication.MedicationName}
{drug.DateCreate}{drug.DrugName}
{visit.DateVisit}{visit.VisitName}
"; + table += "
"; + return table; + } + + [HttpPost] + public void Report(DateTime dateFrom, DateTime dateTo) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIDoctor.PostRequest("api/report/sendvisitsguidesreporttoemail", new ReportDrugsVisitsBindingModel // поменять + { + FileName = "C:\\ReportsCourseWork\\pdffile.pdf", + DoctorId = APIDoctor.Doctor.Id, + DateFrom = dateFrom, + DateTo = dateTo, + Email = APIDoctor.Doctor.Login + + }); + Response.Redirect("Report"); + + } + } } \ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Drugs.cshtml b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Drugs.cshtml index 0a7503d..3ce223c 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Drugs.cshtml +++ b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Drugs.cshtml @@ -33,6 +33,9 @@ Цена + + Дата создания + @@ -51,6 +54,9 @@ @Html.DisplayFor(modelItem =>item.Price) + + @Html.DisplayFor(modelItem =>item.DateCreate) + } diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Report.cshtml b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Report.cshtml index 912d627..c30a9f6 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Report.cshtml +++ b/VeterinaryView/VeterinaryShowDoctorApp/Views/Home/Report.cshtml @@ -1,51 +1,66 @@ -@{ - ViewData["Title"] = "Report"; + +@{ + ViewData["Title"] = "Report"; } -
-

Список медикаментов с расшифровкой по посещениям и лекарствам

+ +
+
+

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

+
+ +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+ +
+
+ +
+
+
+ +
+
+ +
+
-
- @{ -
-
Начальная дата:
-
- -
-
-
-
Конечная дата:
-
- -
-
- - - - - - - - - - - - будет заполняться вьюшками отчета - -
- Номер - - Дата - - Медикамент - - Посещение - - Лекарство -
-
-
-
-
-
- } -
\ No newline at end of file + +@section Scripts { + +} \ No newline at end of file From 27ca6a94ea525922d3eeca52953b836868db21b8 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Sat, 25 May 2024 22:56:36 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=B4=D0=B0=D0=B4=D0=B0=D0=B4=D0=B0=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogicContracts/IReportLogicDoctor.cs | 1 + VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs index 67aef9c..5f9fec9 100644 --- a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs +++ b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IReportLogicDoctor.cs @@ -15,5 +15,6 @@ namespace VeterinaryContracts.BusinessLogicContracts List GetPurchaseMedications(ReportPurchaseMedicationBindingModel model); void SavePurchasesToWordFile(ReportPurchaseMedicationBindingModel model); void SavePurchasesToExcelFile(ReportPurchaseMedicationBindingModel model); + void SaveMedicationsToPdfFile(ReportDrugsVisitsBindingModel model); } } diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs index 6123d33..5327569 100644 --- a/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs +++ b/VeterinaryView/VeterinaryRestApi/Controllers/ReportController.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Mvc; using VeterinaryBusinessLogic.BusinessLogic; using VeterinaryBusinessLogic.MailWorker; -//using VeterinaryBusinessLogic.MailWorker; using VeterinaryContracts.BindingModels; using VeterinaryContracts.BusinessLogicContracts; using VeterinaryContracts.ViewModels; From 4e2a7dd1a2edaf709079cbd09360507717b69041 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Sat, 25 May 2024 23:15:47 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=B0=D1=85=D0=B0=D1=85=D0=B0=D1=85=D1=85?= =?UTF-8?q?=D0=B0=D0=B0=D1=85=D0=B0=D1=85=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/PurchaseLogic.cs | 8 ++++---- ...signer.cs => 20240525190933_InitialCreate.Designer.cs} | 2 +- ...7_InitialCreate.cs => 20240525190933_InitialCreate.cs} | 0 3 files changed, 5 insertions(+), 5 deletions(-) rename VeterinaryView/VeterinaryDatabaseImplement/Migrations/{20240525104527_InitialCreate.Designer.cs => 20240525190933_InitialCreate.Designer.cs} (99%) rename VeterinaryView/VeterinaryDatabaseImplement/Migrations/{20240525104527_InitialCreate.cs => 20240525190933_InitialCreate.cs} (100%) diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs index 345aefe..14fc58b 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -72,10 +72,10 @@ namespace VeterinaryBusinessLogic.BusinessLogic { throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); } - if (model.DateCreate < DateTime.Now) - { - throw new ArgumentNullException("Дата покупки не должна быть в прошлом", nameof(model.DateCreate)); - } + //if (model.DateCreate < DateTime.Now) + //{ + // throw new ArgumentNullException("Дата покупки не должна быть в прошлом", nameof(model.DateCreate)); + //} if (model.Count <= 0) { throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525104527_InitialCreate.Designer.cs b/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525190933_InitialCreate.Designer.cs similarity index 99% rename from VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525104527_InitialCreate.Designer.cs rename to VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525190933_InitialCreate.Designer.cs index 2a42754..dfbd99b 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525104527_InitialCreate.Designer.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525190933_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using VeterinaryDatabaseImplement; namespace VeterinaryDatabaseImplement.Migrations { [DbContext(typeof(VeterinaryDatabase))] - [Migration("20240525104527_InitialCreate")] + [Migration("20240525190933_InitialCreate")] partial class InitialCreate { /// diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525104527_InitialCreate.cs b/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525190933_InitialCreate.cs similarity index 100% rename from VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525104527_InitialCreate.cs rename to VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240525190933_InitialCreate.cs