2024-05-30 00:08:44 +04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-05-30 04:06:00 +04:00
|
|
|
|
using PolyclinicBusinessLogic.BusinessLogics;
|
2024-05-30 01:46:40 +04:00
|
|
|
|
using PolyclinicBusinessLogic.OfficePackage.HelperEnums;
|
|
|
|
|
using PolyclinicContracts.BindingModels;
|
2024-05-30 00:08:44 +04:00
|
|
|
|
using PolyclinicContracts.BusinessLogicsContracts;
|
2024-05-30 02:36:31 +04:00
|
|
|
|
using PolyclinicContracts.SearchModels;
|
2024-05-30 00:08:44 +04:00
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
using PolyclinicWebAppImplementer.Models;
|
|
|
|
|
|
|
|
|
|
namespace PolyclinicWebAppImplementer.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class ReportsController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IImplementerReportLogic _reportLogic;
|
|
|
|
|
private readonly IDiagnoseLogic _diagnoseLogic;
|
2024-05-30 04:06:00 +04:00
|
|
|
|
private readonly SendMailLogic _sendMailLogic;
|
2024-05-30 00:08:44 +04:00
|
|
|
|
|
2024-05-30 04:06:00 +04:00
|
|
|
|
public ReportsController(IImplementerReportLogic reportLogic, IDiagnoseLogic diagnoseLogic, SendMailLogic sendMailLogic)
|
2024-05-30 00:08:44 +04:00
|
|
|
|
{
|
|
|
|
|
_reportLogic = reportLogic;
|
|
|
|
|
_diagnoseLogic = diagnoseLogic;
|
2024-05-30 04:06:00 +04:00
|
|
|
|
_sendMailLogic = sendMailLogic;
|
2024-05-30 00:08:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult DiagnosesByPeriod(DiagnosesByPeriodFormModel model)
|
|
|
|
|
{
|
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "User");
|
|
|
|
|
}
|
|
|
|
|
if (HttpContext.Request.Method == "GET")
|
|
|
|
|
{
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-30 01:46:40 +04:00
|
|
|
|
var report = _reportLogic.GetReportDiagnosesByPeriod(model.DateStart, model.DateEnd);
|
|
|
|
|
if (model.ReportType == ReportType.Web)
|
|
|
|
|
{
|
|
|
|
|
model.Report = report;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ReportBindingModel reportInfo = new()
|
|
|
|
|
{
|
|
|
|
|
FileName = "diagnosesByPeriod.pdf",
|
|
|
|
|
DateFrom = model.DateStart,
|
|
|
|
|
DateTo = model.DateEnd
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_reportLogic.SaveReportDiagnosesByPeriodToPdfFile(reportInfo, report);
|
|
|
|
|
|
|
|
|
|
var fileName = reportInfo.FileName;
|
|
|
|
|
var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
|
|
|
|
|
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
|
|
|
|
|
|
2024-05-30 04:06:00 +04:00
|
|
|
|
if (System.IO.File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string txt = reportInfo.GetPeriodString();
|
|
|
|
|
var userEmail = currentUser.Email;
|
|
|
|
|
_sendMailLogic.SendEmail(userEmail, $"Здравствуйте, {currentUser.FIO}! В приложении отчет по болезням {txt} от {DateTime.Now.ToString()}", filePath);
|
|
|
|
|
|
|
|
|
|
model.Message = $"Отчет в формате .pdf был отправлен на почту {currentUser.Email}";
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
model.Error = $"Произошла ошибка при отправке отчета";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.Error = "Произошла ошибка при генерации отчета";
|
|
|
|
|
}
|
2024-05-30 01:46:40 +04:00
|
|
|
|
}
|
2024-05-30 00:08:44 +04:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult MedicamentsByDiagnoses(MedicamentsByDiagnosesFormModel model)
|
|
|
|
|
{
|
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login", "User");
|
|
|
|
|
}
|
2024-05-30 02:36:31 +04:00
|
|
|
|
var diagnoses = _diagnoseLogic.ReadList(new DiagnoseSearchModel { UserId = currentUser.Id });
|
|
|
|
|
if (model.SelectedDiagnoses != null)
|
|
|
|
|
{
|
|
|
|
|
model.Diagnoses = diagnoses.Select(x => (x, model.SelectedDiagnoses.Contains(x.Id))).ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.Diagnoses = diagnoses.Select(x => (x, false)).ToList();
|
|
|
|
|
}
|
2024-05-30 00:08:44 +04:00
|
|
|
|
if (HttpContext.Request.Method == "GET")
|
|
|
|
|
{
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-30 02:36:31 +04:00
|
|
|
|
var reportInfo = new ReportBindingModel();
|
|
|
|
|
|
|
|
|
|
var reportModel = _reportLogic.GetReportMedicamentsByDiagnoses(model.SelectedDiagnoses);
|
|
|
|
|
|
|
|
|
|
if (model.ReportType == ReportType.Word)
|
|
|
|
|
{
|
|
|
|
|
reportInfo.FileName = model.FileName + ".docx";
|
|
|
|
|
_reportLogic.SaveReportMedicamentsByDiagnosesToWordFile(reportInfo, reportModel);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reportInfo.FileName = model.FileName + ".xlsx";
|
|
|
|
|
_reportLogic.SaveReportMedicamentsByDiagnosesToExcelFile(reportInfo, reportModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var filePath = Path.Combine(Directory.GetCurrentDirectory(), reportInfo.FileName);
|
|
|
|
|
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
|
|
|
|
|
string mimeType = model.ReportType == ReportType.Word ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
2024-05-30 04:06:00 +04:00
|
|
|
|
|
2024-05-30 02:36:31 +04:00
|
|
|
|
return File(fileBytes, mimeType, reportInfo.FileName);
|
2024-05-30 00:08:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|