diff --git a/BMA.EHR.Domain/Shared/GlobalConstants.cs b/BMA.EHR.Domain/Shared/GlobalConstants.cs index 22ed9b28..9d87dabd 100644 --- a/BMA.EHR.Domain/Shared/GlobalConstants.cs +++ b/BMA.EHR.Domain/Shared/GlobalConstants.cs @@ -4,5 +4,9 @@ { public static readonly string TYPE_ATTACHMENT = "attachment"; public static readonly string TYPE_COVER = "cover"; + + public static readonly string EXPORT_PDF = "PDF"; + public static readonly string EXPORT_WORD = "DOCX"; + public static readonly string EXPORT_EXCEL = "XLSX"; } } diff --git a/BMA.EHR.Report.Service/Controllers/CommandReportController.cs b/BMA.EHR.Report.Service/Controllers/CommandReportController.cs new file mode 100644 index 00000000..06f71c46 --- /dev/null +++ b/BMA.EHR.Report.Service/Controllers/CommandReportController.cs @@ -0,0 +1,127 @@ +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 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> 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 + } +} diff --git a/BMA.EHR.Report.Service/Reports/01-คำสั่งบรรจุและแต่งตั้งผู้สอบแข่งขันได้-1.trdp b/BMA.EHR.Report.Service/Reports/01-คำสั่งบรรจุและแต่งตั้งผู้สอบแข่งขันได้-1.trdp index aed6b1b1..5ca6ca92 100644 Binary files a/BMA.EHR.Report.Service/Reports/01-คำสั่งบรรจุและแต่งตั้งผู้สอบแข่งขันได้-1.trdp and b/BMA.EHR.Report.Service/Reports/01-คำสั่งบรรจุและแต่งตั้งผู้สอบแข่งขันได้-1.trdp differ