using Microsoft.AspNetCore.Mvc; 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; public ReportsController(IImplementerReportLogic reportLogic, IDiagnoseLogic diagnoseLogic) { _reportLogic = reportLogic; _diagnoseLogic = diagnoseLogic; } [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); return File(fileBytes, "application/pdf", fileName); } 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); } } } }