hrms-api-backend/BMA.EHR.Report.Service/Controllers/CandidateReportController.cs
2023-10-13 06:19:30 +07:00

104 lines
No EOL
4.9 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;
using BMA.EHR.Application.Repositories;
using System.Drawing;
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;
private readonly MinIOExamService _minIOExamService;
public CandidateReportController(CandidateReportRepository service, IWebHostEnvironment hostingEnvironment,
MinIOExamService minIOExamService, IConfiguration configuration)
{
_service = service;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
_minIOExamService = minIOExamService;
}
#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}")]
[AllowAnonymous]
public async Task<ActionResult<ResponseObject>> GetExamCandidate([FromRoute] Guid Id, string exportType = "pdf")
{
var candidate = await _service.GetExamCandidateAsync(Id);
var careers = await _service.GetExamCareerCandidateAsync(Id);
var avatar = await _service.GetExamAvatarCandidateAsync(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;
var tblData = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["tblData"];
tblData.DataSource = careers;
Console.WriteLine("000000");
if (avatar != Guid.Parse("00000000-0000-0000-0000-000000000000"))
{
try
{
// Get avatar Image
var picContent = await _minIOExamService.DownloadFileAsync(avatar);
var pictureBox = (Telerik.Reporting.PictureBox)report.Items["detailSection1"].Items["picAvatar"];
pictureBox.Value = Image.FromStream(new MemoryStream(picContent.FileContent));
}
catch { }
}
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
}
}