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.Mvc; using Swashbuckle.AspNetCore.Annotations; 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; private readonly CommandReportRepository _commandReportRepository; #endregion #region " Constuctor and Destructor " public CommandReportController(CommandRepository repository, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, CommandReportRepository commandReportRepository) { _repository = repository; _hostingEnvironment = hostingEnvironment; _configuration = configuration; _commandReportRepository = commandReportRepository; } #endregion #region " Methods " #region " Private " private async Task GenerateCommandReportType01_Cover(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; } } private async Task GenerateCommandReportType01_Attachment(Guid commandId, string exportType) { try { var command = await _repository.GetByIdAsync(commandId); if (command == null) { throw new Exception(GlobalMessages.CommandNotFound); } var data = await _commandReportRepository.GetCommandType01AttachmentAsync(commandId); var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"01-คำสั่งบรรจุและแต่งตั้งผู้สอบแข่งขันได้-2.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); } var tblData = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["tblData"]; tblData.DataSource = data; report.ReportParameters["IssuerOrganizationName"].Value = command.IssuerOrganizationName; report.ReportParameters["CommandNo"].Value = command.CommandNo; report.ReportParameters["CommandYear"].Value = command.CommandYear.ToInteger().ToThaiYear().ToString(); report.ReportParameters["CommandExecuteDate"].Value = command.CommandExcecuteDate == null ? "" : command.CommandExcecuteDate.Value.ToThaiFullDate3(); 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 /// /// รายงานหน้าคำสั่ง /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("cover/{exportType}/{id}")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandCoverReportAsync(Guid id, string exportType = "pdf") { try { var cmd = await _repository.GetByIdAsync(id); if (cmd == null) throw new Exception(GlobalMessages.CommandNotFound); 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; } switch (cmd.CommandType.CommandCode.Trim().ToUpper()) { case "C-PM-01": case "C-PM-02": case "C-PM-03": { var contentData = await GenerateCommandReportType01_Cover(id, exportType); return File(contentData, mimeType, $"command-{cmd.CommandNo}-{cmd.CommandYear.ToInteger().ToThaiYear()}.{exportType.Trim().ToLower()}"); } default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement); } } catch { throw; } } /// /// รายงานเอกสารแนบท้าย /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("attachment/{exportType}/{id}")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetCommandAttachmentReportAsync(Guid id, string exportType = "pdf") { try { var cmd = await _repository.GetByIdAsync(id); if (cmd == null) throw new Exception(GlobalMessages.CommandNotFound); 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; } switch (cmd.CommandType.CommandCode.Trim().ToUpper()) { case "C-PM-01": case "C-PM-02": case "C-PM-03": { var contentData = await GenerateCommandReportType01_Attachment(id, exportType); return File(contentData, mimeType, $"command-attachment-{cmd.CommandNo}-{cmd.CommandYear.ToInteger().ToThaiYear()}.{exportType.Trim().ToLower()}"); } default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement); } } catch { throw; } } #endregion } }