Report V2

This commit is contained in:
Suphonchai Phoonsawat 2023-08-20 19:29:03 +07:00
parent 95168acc3b
commit f5d4038417
5 changed files with 319 additions and 6 deletions

View file

@ -9,6 +9,8 @@ using Swashbuckle.AspNetCore.Annotations;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Telerik.Reporting;
using Telerik.Reporting.Processing;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
namespace BMA.EHR.Report.Service.Controllers
{
@ -25,6 +27,7 @@ namespace BMA.EHR.Report.Service.Controllers
private readonly CommandRepository _repository;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
private readonly CommandReportRepository _commandReportRepository;
#endregion
@ -32,11 +35,13 @@ namespace BMA.EHR.Report.Service.Controllers
public CommandReportController(CommandRepository repository,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration)
IConfiguration configuration,
CommandReportRepository commandReportRepository)
{
_repository = repository;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
_commandReportRepository = commandReportRepository;
}
#endregion
@ -45,7 +50,7 @@ namespace BMA.EHR.Report.Service.Controllers
#region " Private "
private async Task<byte[]> GenerateCommandReportType01(Guid commandId, string exportType)
private async Task<byte[]> GenerateCommandReportType01_Cover(Guid commandId, string exportType)
{
try
{
@ -101,11 +106,74 @@ namespace BMA.EHR.Report.Service.Controllers
}
}
private async Task<byte[]> 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
[HttpGet("{exportType}/{id}")]
/// <summary>
/// รายงานหน้าคำสั่ง
/// </summary>
/// <param name="id">Record 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("cover/{exportType}/{id}")]
[AllowAnonymous]
public async Task<ActionResult<ResponseObject>> GetCommandReport(Guid id, string exportType)
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetCommandCoverReportAsync(Guid id, string exportType = "pdf")
{
try
{
@ -113,8 +181,73 @@ namespace BMA.EHR.Report.Service.Controllers
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");
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;
}
}
/// <summary>
/// รายงานเอกสารแนบท้าย
/// </summary>
/// <param name="id">Record 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("attachment/{exportType}/{id}")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> 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
{