using BMA.EHR.Extensions; using BMA.EHR.Profile.Service.Controllers; using BMA.EHR.Report.Service.Data; using BMA.EHR.Report.Service.Responses; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Swashbuckle.AspNetCore.Annotations; using Telerik.Reporting; using Telerik.Reporting.Processing; namespace BMA.EHR.Report.Service.Controllers { [Route("api/v{version:apiVersion}/report/recruit")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("รายงานข้อมูลการสอบแข่งขัน")] public class RecruitReportController : BaseController { #region " Fields " private readonly ApplicationDbContext _context; private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; private readonly string space = "ㅤ"; #endregion #region " Constructor and Destructor " public RecruitReportController(ApplicationDbContext context, IWebHostEnvironment hostingEnvironment, IConfiguration configuration) { this._context = context; this._hostingEnvironment = hostingEnvironment; this._configuration = configuration; } #endregion #region " Methods " /// /// แสดงหนังสือรับรอง /// /// รหัสรอบการสอบ /// เลขประจำตัวผู้สมัครสอบ /// /// เมื่อแสดงรายงานสำเร็จ /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("certificate/{id:length(36)}/{examId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetPeriodByIdAsync(Guid id, string examId) { try { var data = await _context.Recruits.AsQueryable() .Include(x => x.RecruitImport) .Where(x => x.RecruitImport.Id == id) .Where(x => x.ExamId == examId) .Join(_context.RecruitScores.AsQueryable() .Include(x => x.ScoreImport), rc => new { rc.RecruitImport.Year, rc.ExamId }, sc => new { sc.ScoreImport.Year, sc.ExamId }, (p, sr) => new { ExamID = p.ExamId, p.CitizenId, Order = p.RecruitImport.Order, Year = p.RecruitImport.Year, FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", ExamResult = sr == null ? "" : sr.ExamStatus, EndDate = DateTime.Now.ToThaiShortDate2() }) .FirstOrDefaultAsync(); var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", "rptCertificate.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 = data; System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable(); InstanceReportSource instanceReportSource = new InstanceReportSource() { ReportDocument = report }; ReportProcessor reportProcessor = new ReportProcessor(_configuration); RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo); var content = result.DocumentBytes; return File(content, "application/pdf", $"หนังสือรับรอง_{data.CitizenId}_{data.FullName}.pdf"); } catch (Exception ex) { return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน"); } } #endregion } }