hrms-api-backend/BMA.EHR.Report.Service/Controllers/CommandReportController.cs

127 lines
4.6 KiB
C#

using BMA.EHR.Application.Repositories.Commands;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Telerik.Reporting;
using Telerik.Reporting.Processing;
namespace BMA.EHR.Report.Service.Controllers
{
[Route("api/v{version:apiVersion}/report/order")]
[ApiVersion("2.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API รายงานระบบออกคำสั่ง")]
public class CommandReportController : BaseController
{
#region " Fields "
private readonly CommandRepository _repository;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
#endregion
#region " Constuctor and Destructor "
public CommandReportController(CommandRepository repository,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration)
{
_repository = repository;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
}
#endregion
#region " Methods "
#region " Private "
private async Task<byte[]> GenerateCommandReportType01(Guid commandId, string exportType)
{
try
{
var raw_data = await _repository.GetByIdAsync(commandId);
if (raw_data == null)
{
throw new Exception(GlobalMessages.CommandNotFound);
}
var command = new
{
CommandNo = raw_data.CommandNo,
CommandYear = raw_data.CommandYear.ToInteger().ToThaiYear().ToString(),
IssuerOrganizationName = raw_data.IssuerOrganizationName,
ConclusionRegisterNo = raw_data.ConclusionRegisterNo,
ConclusionRegisterDate = raw_data.ConclusionRegisterDate.ToThaiFullDate3(),
ConclusionResultNo = raw_data.ConclusionResultNo,
ConclusionResultDate = raw_data.ConclusionResultDate.ToThaiFullDate3(),
PositionList = "",
Count = raw_data.Receivers.Count,
CommandAffectDate = raw_data.CommandAffectDate == null ? "" : raw_data.CommandAffectDate.Value.ToThaiFullDate3()
};
var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"01-คำสั่งบรรจุและแต่งตั้งผู้สอบแข่งขันได้-1.trdp");
ReportPackager reportPackager = new ReportPackager();
Telerik.Reporting.Report? report = null;
using (var sourceStream = System.IO.File.OpenRead(rptFile))
{
report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
}
report.DataSource = command;
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);
var content = result.DocumentBytes;
return content;
}
catch
{
throw;
}
}
#endregion
[HttpGet("{exportType}/{id}")]
[AllowAnonymous]
public async Task<ActionResult<ResponseObject>> GetCommandReport(Guid id, string exportType)
{
try
{
var cmd = await _repository.GetByIdAsync(id);
if (cmd == null)
throw new Exception(GlobalMessages.CommandNotFound);
var contentData = await GenerateCommandReportType01(id, exportType);
return File(contentData, "application/pdf", $"command-{cmd.CommandNo}-{cmd.CommandYear.ToInteger().ToThaiYear()}.pdf");
}
catch
{
throw;
}
}
#endregion
}
}