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; using BMA.EHR.Application.Repositories; using System.Drawing; namespace BMA.EHR.Report.Service.Controllers { [Route("api/v{version:apiVersion}/report/candidate")] [ApiVersion("2.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("API รายงานระบบสอบคัดเลือก")] public class CandidateReportController : BaseController { private readonly CandidateReportRepository _service; private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; private readonly MinIOExamService _minIOExamService; public CandidateReportController(CandidateReportRepository service, IWebHostEnvironment hostingEnvironment, MinIOExamService minIOExamService, IConfiguration configuration) { _service = service; _hostingEnvironment = hostingEnvironment; _configuration = configuration; _minIOExamService = minIOExamService; } #region ใบสมัคร /// /// ใบสมัคร /// /// Id ผู้สมัคร /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{exportType}/{Id}")] [AllowAnonymous] public async Task> GetExamCandidate([FromRoute] Guid Id, string exportType = "pdf") { var candidate = await _service.GetExamCandidateAsync(Id); var careers = await _service.GetExamCareerCandidateAsync(Id); var avatar = await _service.GetExamAvatarCandidateAsync(Id); if (candidate != null) { 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", $"ผลสอบคัดเลือกรายบุคคล.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 = candidate; var tblData = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["tblData"]; tblData.DataSource = careers; Console.WriteLine("000000"); if (avatar != Guid.Parse("00000000-0000-0000-0000-000000000000")) { try { // Get avatar Image var picContent = await _minIOExamService.DownloadFileAsync(avatar); var pictureBox = (Telerik.Reporting.PictureBox)report.Items["detailSection1"].Items["picAvatar"]; pictureBox.Value = Image.FromStream(new MemoryStream(picContent.FileContent)); } catch { } } 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(); } } #endregion } }