PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicWebAppImplementer/Controllers/ReportsController.cs

133 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Mvc;
using PolyclinicBusinessLogic.BusinessLogics;
using PolyclinicBusinessLogic.OfficePackage.HelperEnums;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.ViewModels;
using PolyclinicWebAppImplementer.Models;
namespace PolyclinicWebAppImplementer.Controllers
{
public class ReportsController : Controller
{
private readonly IImplementerReportLogic _reportLogic;
private readonly IDiagnoseLogic _diagnoseLogic;
private readonly SendMailLogic _sendMailLogic;
public ReportsController(IImplementerReportLogic reportLogic, IDiagnoseLogic diagnoseLogic, SendMailLogic sendMailLogic)
{
_reportLogic = reportLogic;
_diagnoseLogic = diagnoseLogic;
_sendMailLogic = sendMailLogic;
}
[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
{
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);
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 = "Произошла ошибка при генерации отчета";
}
}
return View(model);
}
}
[HttpGet]
[HttpPost]
public IActionResult MedicamentsByDiagnoses(MedicamentsByDiagnosesFormModel model)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login", "User");
}
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();
}
if (HttpContext.Request.Method == "GET")
{
return View(model);
}
else
{
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";
return File(fileBytes, mimeType, reportInfo.FileName);
}
}
}
}