using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Shared; using BMA.EHR.Application.Repositories.Reports; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; using Swashbuckle.AspNetCore.Annotations; using DocumentFormat.OpenXml.Drawing; using Telerik.Reporting; using Telerik.Reporting.Processing; using System.IO; using BMA.EHR.Application.Repositories; using BMA.EHR.Report.Service.Requests; namespace BMA.EHR.Report.Service.Controllers { [Route("api/v{version:apiVersion}/report/deceased")] [ApiVersion("2.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("API รายงานบันทึกเวียนแจ้งการถึงแก่กรรม")] public class DeceasedReportController : BaseController { #region " Fields " private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; private readonly GenericReportGenerator _reportGenerator; private readonly RetireReportRepository _repository; private readonly MinIOService _documentService; #endregion #region " Constuctor and Destructor " public DeceasedReportController(IWebHostEnvironment hostingEnvironment, IConfiguration configuration, MinIOService documentService, GenericReportGenerator reportGenerator, RetireReportRepository repository) { _hostingEnvironment = hostingEnvironment; _configuration = configuration; _reportGenerator = reportGenerator; _repository = repository; _documentService = documentService; } #endregion #region " Methods " #region 36-บันทึกเวียนแจ้งการถึงแก่กรรม /// /// 36-บันทึกเวียนแจ้งการถึงแก่กรรม /// /// Id รายการบันทึกเวียนแจ้งการถึงแก่กรรม /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("36/{exportType}/{id}")] public async Task> GetDeceasedReportAsync(Guid id, string exportType = "pdf") { try { var head = await _repository.GetHeadRetirementDeceasedAsync(id); var data = await _repository.GetRetirementDeceasedAsync(id); if (data != null || head != null) { var mergeData = new { Oc = head.GetType().GetProperty("Oc").GetValue(head), Number = head.GetType().GetProperty("Number").GetValue(head), Date = head.GetType().GetProperty("Date").GetValue(head), Subject = head.GetType().GetProperty("Subject").GetValue(head), Send = head.GetType().GetProperty("Send").GetValue(head), FullName = data.GetType().GetProperty("FullName").GetValue(data), Position = data.GetType().GetProperty("Position").GetValue(data), Reason = data.GetType().GetProperty("Reason").GetValue(data), DeceasedDate = data.GetType().GetProperty("Date").GetValue(data), CurrentDate = data.GetType().GetProperty("CurrentDate").GetValue(data), DeceasedNumber = data.GetType().GetProperty("Number").GetValue(data), Location = data.GetType().GetProperty("Location").GetValue(data), }; 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", $"36-บันทึกเวียนแจ้งการถึงแก่กรรม.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 = mergeData; 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(); } } catch { throw; } } /// /// 36-บันทึกเวียนแจ้งการถึงแก่กรรม /// /// Id รายการบันทึกเวียนแจ้งการถึงแก่กรรม /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("copy/36/{exportType}/{id}")] public async Task> GetDeceasedReportCopyAsync(Guid id, string exportType = "pdf") { try { var head = await _repository.GetHeadRetirementDeceasedAsync(id); var data = await _repository.GetRetirementDeceasedAsync(id); if (data != null || head != null) { var mergeData = new { Oc = head.GetType().GetProperty("Oc").GetValue(head), Number = head.GetType().GetProperty("Number").GetValue(head), Date = head.GetType().GetProperty("Date").GetValue(head), Subject = head.GetType().GetProperty("Subject").GetValue(head), Send = head.GetType().GetProperty("Send").GetValue(head), FullName = data.GetType().GetProperty("FullName").GetValue(data), Position = data.GetType().GetProperty("Position").GetValue(data), Reason = data.GetType().GetProperty("Reason").GetValue(data), DeceasedDate = data.GetType().GetProperty("Date").GetValue(data), CurrentDate = data.GetType().GetProperty("CurrentDate").GetValue(data), DeceasedNumber = data.GetType().GetProperty("Number").GetValue(data), Location = data.GetType().GetProperty("Location").GetValue(data), }; 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", $"36-บันทึกเวียนแจ้งการถึงแก่กรรม.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 = mergeData; 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(); } } catch { throw; } } /// /// อัปไฟล์บันทึกเวียนแจ้งการถึงแก่กรรม /// /// id /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("attachment/36/{id}")] public async Task> UploadDeceasedReportAsync([FromForm] FileRequest req, Guid id) { try { var data = await _repository.GetRetirementDeceasedAsync(id); if (data == null) return NotFound(); // check upload file if (Request.Form.Files == null || Request.Form.Files.Count == 0) { return Error(GlobalMessages.NoFileToUpload); } var file = Request.Form.Files[0]; var cover = await _documentService.UploadFileAsync(file); await _repository.UploadFileRetirementDeceasedAsync(id, cover); return Success(); } catch { throw; } } #endregion #endregion } }