diff --git a/VetClinic/PharmacistApp/Controllers/HomeController.cs b/VetClinic/PharmacistApp/Controllers/HomeController.cs index d7c6935..fb3c334 100644 --- a/VetClinic/PharmacistApp/Controllers/HomeController.cs +++ b/VetClinic/PharmacistApp/Controllers/HomeController.cs @@ -665,7 +665,7 @@ View(res); string dateFromS = dateFrom.ToString("s", CultureInfo.InvariantCulture); string dateToS = dateTo.ToString("s", CultureInfo.InvariantCulture); result = APIPharmacist.GetRequest> - ($"api/report/getvisitsguidesreport?datefrom={dateFromS}&dateto={dateToS}")!; + ($"api/report/getvisitsguidesreport?datefrom={dateFromS}&dateto={dateToS}&pharmacistid={APIPharmacist.Pharmacist.Id}")!; } catch (Exception ex) @@ -718,6 +718,26 @@ View(res); table += ""; return table; } - } + + [HttpPost] + public void Report(DateTime dateFrom, DateTime dateTo) + { + if (APIPharmacist.Pharmacist == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIPharmacist.PostRequest("api/report/sendvisitsguidesreporttoemail", new VisitsGuidesBindingModel + { + FileName = "C:\\ReportsCourseWork\\pdffile.pdf", + PharmacistId = APIPharmacist.Pharmacist.Id, + DateFrom = dateFrom, + DateTo = dateTo, + Email = APIPharmacist.Pharmacist.Email + + }); + Response.Redirect("Report"); + + } + } } diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/ReportLogicPharmacist.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/ReportLogicPharmacist.cs index d449088..9ec9b53 100644 --- a/VetClinic/VetClinicBusinessLogic/BusinessLogics/ReportLogicPharmacist.cs +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/ReportLogicPharmacist.cs @@ -94,9 +94,9 @@ namespace VetClinicBusinessLogic.BusinessLogics { List ans = new(); List>>>> responseGuides = - _medicineStorage.GetGuidancesInfo(new VisitGuidesSearchModel { DateFrom = model.DateFrom!, DateTo = model.DateTo!}); + _medicineStorage.GetGuidancesInfo(new VisitGuidesSearchModel { DateFrom = model.DateFrom!, DateTo = model.DateTo!, PharmacistId = model.PharmacistId!}); List>>>> responseVisits = - _medicineStorage.GetVisitsInfo(new VisitGuidesSearchModel { DateFrom = model.DateFrom!, DateTo = model.DateTo! }); + _medicineStorage.GetVisitsInfo(new VisitGuidesSearchModel { DateFrom = model.DateFrom!, DateTo = model.DateTo!, PharmacistId = model.PharmacistId! }); Dictionary dict = new(); foreach(var medicine in responseGuides) diff --git a/VetClinic/VetClinicBusinessLogic/MailWorker/AbstractMailWorker.cs b/VetClinic/VetClinicBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..e484fa6 --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/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 VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; + +namespace VetClinicBusinessLogic.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 IPharmacistLogic _pharmacistLogic; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IPharmacistLogic pharmacistLogic) + { + _logger = logger; + _pharmacistLogic = pharmacistLogic; + } + + 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/VetClinic/VetClinicBusinessLogic/MailWorker/MailWorker.cs b/VetClinic/VetClinicBusinessLogic/MailWorker/MailWorker.cs new file mode 100644 index 0000000..77f50f5 --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/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 VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; + +namespace VetClinicBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IPharmacistLogic pharmacistLogic) : base(logger, pharmacistLogic) { } + + 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/VetClinic/VetClinicBusinessLogic/OfficePackage/AbstractSaveToPdfPharmacist.cs b/VetClinic/VetClinicBusinessLogic/OfficePackage/AbstractSaveToPdfPharmacist.cs index f739cd7..bb3dee7 100644 --- a/VetClinic/VetClinicBusinessLogic/OfficePackage/AbstractSaveToPdfPharmacist.cs +++ b/VetClinic/VetClinicBusinessLogic/OfficePackage/AbstractSaveToPdfPharmacist.cs @@ -27,68 +27,36 @@ namespace VetClinicBusinessLogic.OfficePackage = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - CreateTable(new List { "2cm", "3cm", "6cm", "3cm" }); + CreateTable(new List { "4cm", "4cm", "4cm", "4cm" }); CreateRow(new PdfRowParameters { - Texts = new List { "Номер", "Дата заказа", "Изделие", -"Сумма" }, + Texts = new List { "Дата", "Название медикамента", "Услуга рекомендации", +"Название визита" }, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); foreach (var medicine in info.Medicines) { - CreateParagraph(new PdfParagraph - { - Text = medicine.MedicineName, - Style - = "NormalTitle", - ParagraphAlignment = PdfParagraphAlignmentType.Center - }); - - - CreateParagraph(new PdfParagraph - { - Text = "Визиты", - Style - = "NormalTitle", - ParagraphAlignment = PdfParagraphAlignmentType.Left - }); - CreateTable(new List { "4cm", "4cm", "6cm" }); - CreateRow(new PdfRowParameters - { - Texts = new List { "Номер", "Дата визита", "Навзание визита"}, - Style = "NormalTitle", - ParagraphAlignment = PdfParagraphAlignmentType.Center - }); + CreateRow(new PdfRowParameters + { + Texts = new List { "", medicine.MedicineName, "", "" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); foreach(var visit in medicine.Visits) { - CreateRow(new PdfRowParameters - { - Texts = new List { visit.Id.ToString(), visit.DateVisit.ToString(), visit.NameVisit}, - Style = "Normal", - ParagraphAlignment = PdfParagraphAlignmentType.Center - }); - } - - CreateParagraph(new PdfParagraph - { - Text = "Рекомендации", - Style - = "NormalTitle", - ParagraphAlignment = PdfParagraphAlignmentType.Center - }); - CreateTable(new List { "4cm", "4cm", "6cm" }); - CreateRow(new PdfRowParameters - { - Texts = new List { "Номер", "Дата рекомендации", "Название услуги" }, - Style = "NormalTitle", - ParagraphAlignment = PdfParagraphAlignmentType.Center - }); + CreateRow(new PdfRowParameters + { + Texts = new List { visit.DateVisit.ToString(), "", "", visit.NameVisit }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + } foreach (var guidance in medicine.Guidances) { CreateRow(new PdfRowParameters { - Texts = new List { guidance.Id.ToString(), guidance.Date.ToString(), guidance.ServiceName }, + Texts = new List { guidance.Date.ToString(), "", guidance.ServiceName, "" }, Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); diff --git a/VetClinic/VetClinicContracts/BindingModels/MailConfigBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..b95217f --- /dev/null +++ b/VetClinic/VetClinicContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VetClinicContracts.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/VetClinic/VetClinicContracts/BindingModels/MailSendInfoBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..df45f6e --- /dev/null +++ b/VetClinic/VetClinicContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VetClinicContracts.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/VetClinic/VetClinicContracts/BindingModels/VisitsGuidesBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/VisitsGuidesBindingModel.cs index c95a0ab..2a605d1 100644 --- a/VetClinic/VetClinicContracts/BindingModels/VisitsGuidesBindingModel.cs +++ b/VetClinic/VetClinicContracts/BindingModels/VisitsGuidesBindingModel.cs @@ -11,5 +11,7 @@ namespace VetClinicContracts.BindingModels public string FileName { get; set; } = string.Empty; public DateTime DateFrom { get; set; } = DateTime.Now; public DateTime DateTo { get; set; } = DateTime.Now; + public int? PharmacistId { get; set; } + public string? Email { get; set; } } } diff --git a/VetClinic/VetClinicContracts/SearchModels/VisitGuidesSearchModel.cs b/VetClinic/VetClinicContracts/SearchModels/VisitGuidesSearchModel.cs index c25ab26..d9e777d 100644 --- a/VetClinic/VetClinicContracts/SearchModels/VisitGuidesSearchModel.cs +++ b/VetClinic/VetClinicContracts/SearchModels/VisitGuidesSearchModel.cs @@ -11,5 +11,6 @@ namespace VetClinicContracts.SearchModels public List? medicinesIds { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } + public int? PharmacistId { get; set; } } } diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/MedicineStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/MedicineStorage.cs index 2e17952..25abd25 100644 --- a/VetClinic/VetClinicDataBaseImplement/Implements/MedicineStorage.cs +++ b/VetClinic/VetClinicDataBaseImplement/Implements/MedicineStorage.cs @@ -36,7 +36,7 @@ namespace VetClinicDataBaseImplement.Implements public List>>>> GetGuidancesInfo(VisitGuidesSearchModel model) { using var context = new VetClinicDatabase(); - return context.Medicines + return context.Medicines.Where(medicine => medicine.PharmacistId == model.PharmacistId) .Select(medicine => new Tuple>>>(medicine.GetViewModel, context.ServiceMedicines.Include(service => service.Service) .Include(service => service.Medicine).Where(service => medicine.Id == service.MedicineId). @@ -48,8 +48,8 @@ namespace VetClinicDataBaseImplement.Implements public List>>>> GetVisitsInfo(VisitGuidesSearchModel model) { using var context = new VetClinicDatabase(); - return context.Medicines - .Select(medicine => new Tuple>>>(medicine.GetViewModel, + return context.Medicines.Where(medicine => medicine.PharmacistId == model.PharmacistId) + .Select(medicine => new Tuple>>>(medicine.GetViewModel, context.ServiceMedicines.Include(service => service.Service) .Include(service => service.Medicine).Where(service => medicine.Id == service.MedicineId). Select(service => new Tuple>(service.Service.GetViewModel, diff --git a/VetClinic/VetClinicRestApi/Controllers/ReportController.cs b/VetClinic/VetClinicRestApi/Controllers/ReportController.cs index f2c227d..2ecffa6 100644 --- a/VetClinic/VetClinicRestApi/Controllers/ReportController.cs +++ b/VetClinic/VetClinicRestApi/Controllers/ReportController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using VetClinicBusinessLogic.BusinessLogics; +using VetClinicBusinessLogic.MailWorker; using VetClinicContracts.BindingModels; using VetClinicContracts.BusinessLogicsContracts; using VetClinicDataBaseImplement.Implements; @@ -11,9 +12,11 @@ namespace VetClinicRestApi.Controllers public class ReportController : Controller { private readonly IReportLogicPharmacist _reportPharmacist; - public ReportController(ILogger logger, IReportLogicPharmacist reportPharmacist) + private readonly AbstractMailWorker _mailWorker; + public ReportController(ILogger logger, IReportLogicPharmacist reportPharmacist,AbstractMailWorker mailWorker) { _reportPharmacist = reportPharmacist; + _mailWorker = mailWorker; } public IActionResult Index(ReportLogicPharmacist reportPharmacist) { @@ -44,7 +47,7 @@ namespace VetClinicRestApi.Controllers } } [HttpGet] - public List GetVisitsGuidesReport(string dateFrom, string dateTo) + public List GetVisitsGuidesReport(string dateFrom, string dateTo, int pharmacistId) { try { @@ -53,6 +56,7 @@ namespace VetClinicRestApi.Controllers VisitsGuidesBindingModel model = new(); model.DateFrom = DateFrom; model.DateTo = DateTo; + model.PharmacistId = pharmacistId; return _reportPharmacist.GetMedicineVisitsAndGuidances(model); } catch (Exception ex) @@ -60,5 +64,24 @@ namespace VetClinicRestApi.Controllers throw; } } - } + + [HttpPost] + public void SendVisitsGuidesReportToEmail(VisitsGuidesBindingModel model) + { + try + { + _reportPharmacist.SaveMedicinesToPdfFile(model); + _mailWorker.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = model.Email!, + Subject = "Отчет по медикаментам", + Text = "Лови" + }); + } + catch (Exception ex) + { + throw; + } + } + } } diff --git a/VetClinic/VetClinicRestApi/Program.cs b/VetClinic/VetClinicRestApi/Program.cs index 0c7aa45..da7360b 100644 --- a/VetClinic/VetClinicRestApi/Program.cs +++ b/VetClinic/VetClinicRestApi/Program.cs @@ -6,6 +6,8 @@ using Microsoft.OpenApi.Models; using VetClinicBaseImplement.Implements; using VetClinicBusinessLogic.OfficePackage; using VetClinicBusinessLogic.OfficePackage.Implements; +using VetClinicBusinessLogic.MailWorker; +using VetClinicContracts.BindingModels; var builder = WebApplication.CreateBuilder(args); @@ -36,6 +38,8 @@ builder.Services.AddTransient(); 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 @@ -46,6 +50,24 @@ builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "VetClinicRestApi", 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/VetClinic/VetClinicRestApi/appsettings.json b/VetClinic/VetClinicRestApi/appsettings.json index 10f68b8..01ad592 100644 --- a/VetClinic/VetClinicRestApi/appsettings.json +++ b/VetClinic/VetClinicRestApi/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "sasda3183@gmail.com", + "MailPassword": "ozxp vjof uinv fcmj" }