hrms-api-backend/BMA.EHR.Report.Service/Controllers/CandidateReportController.cs
2023-10-08 13:54:20 +07:00

79 lines
No EOL
3.7 KiB
C#

using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Application.Repositories.Reports;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Swashbuckle.AspNetCore.Annotations;
using Telerik.Reporting;
using Telerik.Reporting.Processing;
namespace BMA.EHR.Report.Service.Controllers
{
[Route("api/v{version:apiVersion}/report/candidate")]
[ApiVersion("2.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API รายงานระบบสอบคัดเลือก")]
public class CandidateReportController : BaseController
{
private readonly CandidateReportRepository _service;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
public CandidateReportController(CandidateReportRepository service, IWebHostEnvironment hostingEnvironment, IConfiguration configuration)
{
_service = service;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
}
#region
/// <summary>
/// ใบสมัคร
/// </summary>
/// <param name="Id">Id ผู้สมัคร</param>
/// <param name="exportType">pdf, docx หรือ xlsx</param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{exportType}/{Id}")]
public async Task<ActionResult<ResponseObject>> GetExamCandidate([FromRoute] Guid Id, string exportType = "pdf")
{
var candidate = await _service.GetExamCandidateAsync(Id);
if (candidate != null)
{
var mimeType = "";
switch (exportType.Trim().ToLower())
{
case "pdf": mimeType = "application/pdf"; break;
case "docx": mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
case "xlsx": mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
}
var rptFile = System.IO.Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"ผลสอบคัดเลือกรายบุคคล.trdp");
ReportPackager reportPacker = new ReportPackager();
Telerik.Reporting.Report? report = null;
using (var sourceStream = System.IO.File.OpenRead(rptFile))
{
report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream);
}
report.DataSource = candidate;
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
InstanceReportSource instanceReportSource = new InstanceReportSource()
{
ReportDocument = report,
};
ReportProcessor reportProcessor = new ReportProcessor(_configuration);
RenderingResult result = reportProcessor.RenderReport($"{exportType}", instanceReportSource, deviceInfo);
return File(result.DocumentBytes, mimeType, $"ใบสมัครสอบคัดเลือก.{exportType.Trim().ToLower()}");
}
else
{
return NotFound();
}
}
#endregion
}
}