Видать сделал (по любасу еще чет допиливать придется)
This commit is contained in:
parent
bb68e9abcc
commit
67b8f12ede
@ -665,7 +665,7 @@ View(res);
|
||||
string dateFromS = dateFrom.ToString("s", CultureInfo.InvariantCulture);
|
||||
string dateToS = dateTo.ToString("s", CultureInfo.InvariantCulture);
|
||||
result = APIPharmacist.GetRequest<List<VisitsGuidesViewModel>>
|
||||
($"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 += "</div>";
|
||||
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");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -94,9 +94,9 @@ namespace VetClinicBusinessLogic.BusinessLogics
|
||||
{
|
||||
List<VisitsGuidesViewModel> ans = new();
|
||||
List<Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<GuidanceViewModel>>>>> 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<Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<VisitViewModel>>>>> 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<int, VisitsGuidesViewModel> dict = new();
|
||||
|
||||
foreach(var medicine in responseGuides)
|
||||
|
@ -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<AbstractMailWorker> 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);
|
||||
}
|
||||
}
|
50
VetClinic/VetClinicBusinessLogic/MailWorker/MailWorker.cs
Normal file
50
VetClinic/VetClinicBusinessLogic/MailWorker/MailWorker.cs
Normal file
@ -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<MailKitWorker> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -27,68 +27,36 @@ namespace VetClinicBusinessLogic.OfficePackage
|
||||
= "Normal",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||
});
|
||||
CreateTable(new List<string> { "2cm", "3cm", "6cm", "3cm" });
|
||||
CreateTable(new List<string> { "4cm", "4cm", "4cm", "4cm" });
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { "Номер", "Дата заказа", "Изделие",
|
||||
"Сумма" },
|
||||
Texts = new List<string> { "Дата", "Название медикамента", "Услуга рекомендации",
|
||||
"Название визита" },
|
||||
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<string> { "4cm", "4cm", "6cm" });
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { "Номер", "Дата визита", "Навзание визита"},
|
||||
Style = "NormalTitle",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||
});
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { "", medicine.MedicineName, "", "" },
|
||||
Style = "NormalTitle",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||
});
|
||||
foreach(var visit in medicine.Visits)
|
||||
{
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { 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<string> { "4cm", "4cm", "6cm" });
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { "Номер", "Дата рекомендации", "Название услуги" },
|
||||
Style = "NormalTitle",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||
});
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { visit.DateVisit.ToString(), "", "", visit.NameVisit },
|
||||
Style = "Normal",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||
});
|
||||
}
|
||||
foreach (var guidance in medicine.Guidances)
|
||||
{
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
Texts = new List<string> { guidance.Id.ToString(), guidance.Date.ToString(), guidance.ServiceName },
|
||||
Texts = new List<string> { guidance.Date.ToString(), "", guidance.ServiceName, "" },
|
||||
Style = "Normal",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||
});
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -11,5 +11,6 @@ namespace VetClinicContracts.SearchModels
|
||||
public List<int>? medicinesIds { get; set; }
|
||||
public DateTime? DateFrom { get; set; }
|
||||
public DateTime? DateTo { get; set; }
|
||||
public int? PharmacistId { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ namespace VetClinicDataBaseImplement.Implements
|
||||
public List<Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<GuidanceViewModel>>>>> GetGuidancesInfo(VisitGuidesSearchModel model)
|
||||
{
|
||||
using var context = new VetClinicDatabase();
|
||||
return context.Medicines
|
||||
return context.Medicines.Where(medicine => medicine.PharmacistId == model.PharmacistId)
|
||||
.Select(medicine => new Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<GuidanceViewModel>>>>(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<Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<VisitViewModel>>>>> GetVisitsInfo(VisitGuidesSearchModel model)
|
||||
{
|
||||
using var context = new VetClinicDatabase();
|
||||
return context.Medicines
|
||||
.Select(medicine => new Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<VisitViewModel>>>>(medicine.GetViewModel,
|
||||
return context.Medicines.Where(medicine => medicine.PharmacistId == model.PharmacistId)
|
||||
.Select(medicine => new Tuple<MedicineViewModel, List<Tuple<ServiceViewModel, List<VisitViewModel>>>>(medicine.GetViewModel,
|
||||
context.ServiceMedicines.Include(service => service.Service)
|
||||
.Include(service => service.Medicine).Where(service => medicine.Id == service.MedicineId).
|
||||
Select(service => new Tuple<ServiceViewModel, List<VisitViewModel>>(service.Service.GetViewModel,
|
||||
|
@ -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<ReportController> logger, IReportLogicPharmacist reportPharmacist)
|
||||
private readonly AbstractMailWorker _mailWorker;
|
||||
public ReportController(ILogger<ReportController> logger, IReportLogicPharmacist reportPharmacist,AbstractMailWorker mailWorker)
|
||||
{
|
||||
_reportPharmacist = reportPharmacist;
|
||||
_mailWorker = mailWorker;
|
||||
}
|
||||
public IActionResult Index(ReportLogicPharmacist reportPharmacist)
|
||||
{
|
||||
@ -44,7 +47,7 @@ namespace VetClinicRestApi.Controllers
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
public List<VisitsGuidesViewModel> GetVisitsGuidesReport(string dateFrom, string dateTo)
|
||||
public List<VisitsGuidesViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<IReportLogicPharmacist, ReportLogicPharmacist>();
|
||||
builder.Services.AddTransient<AbstractSaveToExcelPharmacist, SaveToExcelPharmacist>();
|
||||
builder.Services.AddTransient<AbstractSaveToWordPharmacist, SaveToWordPharmacist>();
|
||||
builder.Services.AddTransient<AbstractSaveToPdfPharmacist, SaveToPdfPharmacist>();
|
||||
builder.Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
|
||||
|
||||
|
||||
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<AbstractMailWorker>();
|
||||
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())
|
||||
|
@ -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"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user