using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; using BMA.EHR.Application.Repositories.Reports; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.Annotations; using Telerik.Reporting; using Telerik.Reporting.Processing; namespace BMA.EHR.Report.Service.Controllers { [Route("api/v{version:apiVersion}/report/retire")] [ApiVersion("2.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("API รายงานระบบเกษียณ")] public class RetireReportController : BaseController { private readonly RetireReportRepository _service; private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; public RetireReportController(RetireReportRepository service, IWebHostEnvironment hostingEnvironment, IConfiguration configuration) { _service = service; _hostingEnvironment = hostingEnvironment; _configuration = configuration; } #region ประกาศเกษียณราชการ /// /// รายงานหน้าประกาศเกษียณ /// /// Id ของรอบเกษียณ /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{exportType}/{Id}")] public async Task> GetProfileRetirement([FromRoute] Guid Id, string exportType = "pdf") { var retire = await _service.GetProfileRetirementdAsync(Id); if (retire == null) { return NotFound(); } else { var reportfile = ""; var returnfile = ""; exportType = exportType.Trim(); switch (retire.GetType().GetProperty("Type").GetValue(retire)) { case "OFFICER": if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "ADD") { reportfile = $"31-ประกาศเกษียณข้าราชการ-1.trdp"; returnfile = $"ประกาศเกษียณข้าราชการ.{exportType}"; } else if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "EDIT") { reportfile = $"31-ประกาศเกษียณข้าราชการ-2.trdp"; returnfile = $"แก้ไขประกาศเกษียณข้าราชการ.{exportType}"; } else if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "REMOVE") { reportfile = $"31-ประกาศเกษียณข้าราชการ-3.trdp"; returnfile = $"ยกเลิกประกาศเกษียณข้าราชการ.{exportType}"; } else { return Error(retire.GetType().GetProperty("TypeReport").ToString()); } break; case "EMPLOYEE": reportfile = $"32-ประกาศเกษียณลูกจ้างประจำ.trdp"; returnfile = $"ประกาศเกษียณลูกจ้าง"; break; default: return BadRequest(retire); } var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", reportfile); ReportPackager reportPacker = new ReportPackager(); Telerik.Reporting.Report? report = null; using (var sourceStream = System.IO.File.OpenRead(rptFile)) { report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream); } //Add Parameter report.ReportParameters["Year"].Value = retire.GetType().GetProperty("Year").GetValue(retire); report.ReportParameters["Total"].Value = retire.GetType().GetProperty("Total").GetValue(retire); var _profileList = new List(); foreach (var profile in retire.GetType().GetProperty("profile").GetValue(retire)) { string thaiOrder = profile.GetType().GetProperty("order").GetValue(profile).ToString() + "."; thaiOrder = thaiOrder.ToThaiNumber(); _profileList.Add(new { order = thaiOrder, fullName = profile.GetType().GetProperty("fullName").GetValue(profile).ToString(), position = profile.GetType().GetProperty("position").GetValue(profile).ToString(), posNo = profile.GetType().GetProperty("posNo").GetValue(profile).ToString(), organizationOrganization = profile.GetType().GetProperty("organizationOrganization").GetValue(profile).ToString(), }); } //Binding to Table var tblProfile = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["table1"]; tblProfile.DataSource = _profileList; 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 File(content, $"application/{exportType}", returnfile); } } #endregion } }