diff --git a/Controllers/ExamReportController.cs b/Controllers/ExamReportController.cs index e4734b9..eb63474 100644 --- a/Controllers/ExamReportController.cs +++ b/Controllers/ExamReportController.cs @@ -11,510 +11,505 @@ using BMA.EHR.Extensions; namespace BMA.EHR.Report.Service.Controllers { - [Route("api/v{version:apiVersion}/report/exam")] - [ApiVersion("1.0")] - [ApiController] - [Produces("application/json")] - [Authorize] - [SwaggerTag("รายงานข้อมูลการสอบคัดเลือก")] - public class ExamReportController : BaseController - { - #region " Fields " - - private readonly ExamDbContext _context; - private readonly IWebHostEnvironment _hostingEnvironment; - private readonly IConfiguration _configuration; - private readonly string space = "ㅤ"; - - #endregion - - #region " Constructor and Destructor " - - public ExamReportController(ExamDbContext context, - IWebHostEnvironment hostingEnvironment, - IConfiguration configuration) - { - this._context = context; - this._hostingEnvironment = hostingEnvironment; - this._configuration = configuration; - } - - #endregion - - #region " Methods " - - #region " Privates " - - private int GetExamCountTes(string citizenId) - { - try - { - var count = _context.Candidates.AsQueryable() - .Where(x => x.CitizenId == citizenId) - .Count(); - - return count; - } - catch - { - throw; - } - } - - #endregion - - /// - /// แสดงหนังสือรับรอง - /// - /// รหัสรอบการสอบ - /// เลขประจำตัวผู้สมัครสอบ - /// ชนิดของรายงาน - /// - /// เมื่อแสดงรายงานสำเร็จ - /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง - /// ไม่ได้ Login เข้าระบบ - /// เมื่อเกิดข้อผิดพลาดในการทำงาน - - [HttpGet("certificate/{type:int}/{id:length(36)}/{examId}")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetCertificateReportAsync(Guid id, string examId, int type) - { - try - { - var data = await _context.Disables.AsQueryable() - .Include(x => x.PeriodExam) - .Where(x => x.PeriodExam.Id == id) - .Where(x => x.ExamId == examId) - .Join(_context.DisableScores.AsQueryable() - .Include(x => x.ScoreImport), - rc => new { rc.PeriodExam.Year, rc.ExamId }, - sc => new { sc.ScoreImport.Year, sc.ExamId }, - (p, sr) => new - { - ExamID = p.ExamId, - p.CitizenId, - Order = p.PeriodExam.Round, - Year = p.PeriodExam.Year.Value.ToThaiYear(), - FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", - ExamResult = sr == null ? "" : sr.ExamStatus, - EndDate = p.PeriodExam.RegisterEndDate.ToThaiFullDate3(), - AuthName = "นายณัฐพงศ์ ดิษยบุตร", - AuthPosition = "หัวหน้าสำนักงาน ก.ก." - }) - .FirstOrDefaultAsync(); - - var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCertificate{type}.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, "เกิดข้อผิดพลาดในการแสดงรายงาน"); - } - } - - /// - /// แสดงเอกสารผลสอบ - /// - /// รหัสรอบการสอบ - /// เลขประจำตัวผู้สมัครสอบ - /// - /// เมื่อแสดงรายงานสำเร็จ - /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง - /// ไม่ได้ Login เข้าระบบ - /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpGet("score/{id:length(36)}/{examId}")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetScoreReportAsync(Guid id, string examId) - { - try - { - var data = await _context.Disables.AsQueryable() - .Include(x => x.PeriodExam) - .Include(x => x.Documents) - .ThenInclude(x => x.DocumentFile) - .Where(x => x.PeriodExam.Id == id) - .Where(x => x.ExamId == examId) - .Join(_context.DisableScores.AsQueryable() - .Include(x => x.ScoreImport), - rc => new { rc.PeriodExam.Year, rc.ExamId }, - sc => new { sc.ScoreImport.Year, sc.ExamId }, - (p, sr) => new - { - ExamId = p.ExamId, - CitizenId = p.CitizenId, - p.Prefix, - FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", - DateOfBirth = p.DateOfBirth.ToThaiShortDate(), - Gender = p.Gendor, - Degree = p.Educations.First().Degree, - Major = p.Educations.First().Major, - ExamResult = sr == null ? "" : sr.ExamStatus, - University = p.Educations.First().University, - PositionName = p.PositionName, - ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", - Number = sr == null ? "" : sr.Number, - // ExamCount = 10, - // ExamCount = GetExamCountTes(p.CitizenId), - ScoreExpire = p.PeriodExam.AnnouncementDate == null ? "" : p.PeriodExam.AnnouncementDate.AddYears(2).ToThaiShortDate(), - FullA = sr == null ? 0 : sr.FullA, - SumA = sr == null ? 0 : sr.SumA, - FullB = sr == null ? 0 : sr.FullB, - SumB = sr == null ? 0 : sr.SumB, - FullC = sr == null ? 0 : sr.FullC, - SumC = sr == null ? 0 : sr.SumC, - }) - .FirstOrDefaultAsync(); - var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptExamResult.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; - if (data != null) - { - var _data = new - { - ExamId = data.ExamId, - CitizenId = data.CitizenId, - Prefix = data.Prefix, - FullName = data.FullName, - DateOfBirth = data.DateOfBirth, - Gender = data.Gender, - Degree = data.Degree, - Major = data.Major, - ExamResult = data.ExamResult, - University = data.University, - PositionName = data.PositionName, - ExamName = data.ExamName, - Number = data.Number, - ExamCount = GetExamCountTes(data.CitizenId), - ScoreExpire = data.ScoreExpire, - FullA = data.FullA, - SumA = data.SumA, - FullB = data.FullB, - SumB = data.SumB, - FullC = data.FullC, - SumC = data.SumC, - }; - 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, "เกิดข้อผิดพลาดในการแสดงรายงาน"); - } - } - - [HttpGet("candidate/{id:length(36)}")] - [AllowAnonymous] - public async Task> GetCandidateListReportAsync(Guid id) - { - try - { - var data = await _context.Disables.AsQueryable() - .Include(x => x.PeriodExam) - .Where(x => x.PeriodExam.Id == id) - .OrderBy(x => x.ExamId) - .Select(p => new - { - ExamId = p.ExamId, - FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", - PositionName = p.PositionName, - ExamName = - $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", - }).ToListAsync(); - - var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.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.First().ExamName}.pdf"); - - } - catch (Exception ex) - { - return Error(ex); - } - } - - [HttpGet("candidate-exam/{id:length(36)}")] - [AllowAnonymous] - public async Task> GetCandidateExamListReportAsync(Guid id) - { - try - { - var data = await _context.Candidates.AsQueryable() - .Include(x => x.PeriodExam) - .Where(x => x.PeriodExam.Id == id) - .Where(x => x.SeatNumber != null || x.SeatNumber != "") - .OrderBy(x => x.SeatNumber) - .Select(p => new - { - ExamId = p.SeatNumber, - FullName = $"{p.FirstName} {p.LastName}", - PositionName = "", - ExamName = - $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", - }).ToListAsync(); - - if (data.Count == 0) return Success(); - - var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.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.First().ExamName}.pdf"); - - } - catch (Exception ex) - { - return Error(ex); - } - } - - [HttpGet("pass/{id:length(36)}")] - [AllowAnonymous] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetPassExamListReportAsync(Guid id) - { - try - { - var data = await _context.Disables.AsQueryable() - .Include(x => x.PeriodExam) - .ThenInclude(x => x.ScoreImport) - .Include(x => x.Documents) - .ThenInclude(x => x.DocumentFile) - - - .Join(_context.DisableScores.AsQueryable() - .Include(x => x.ScoreImport) - .Where(x => x.ScoreImport.PeriodExamId == id) - .Where(x => x.ExamStatus == "ผ่าน"), - rc => new { id = rc.PeriodExam.Id, examId = rc.ExamId }, - sc => new { id = sc.ScoreImport.PeriodExamId, examId = sc.ExamId }, - (p, sr) => new - { - Id = p.PeriodExam.Id, - ExamId = p.ExamId, - CitizenId = p.CitizenId, - p.Prefix, - FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", - DateOfBirth = p.DateOfBirth.ToThaiShortDate(), - Gender = p.Gendor, - Degree = p.Educations.First().Degree, - Major = p.Educations.First().Major, - - ExamResult = sr == null ? "" : sr.ExamStatus, - - University = p.Educations.First().University, - PositionName = p.PositionName, - ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", - - - Number = sr == null ? 99999 : Convert.ToInt32(sr.Number), - - - - FullA = sr == null ? 0 : sr.FullA, - SumA = sr == null ? 0 : sr.SumA, - FullB = sr == null ? 0 : sr.FullB, - SumB = sr == null ? 0 : sr.SumB, - FullC = sr == null ? 0 : sr.FullC, - SumC = sr == null ? 0 : sr.SumC, - SumScore = sr == null ? 0 : sr.SumA + sr.SumB + sr.SumC, - - - - }) - .OrderBy(x => x.Number) - .Where(x => x.Id == id) - .Where(x => x.ExamResult == "ผ่าน") - .ToListAsync(); - - if (data.Count == 0) return Success(); - - var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.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.First().ExamName}.pdf"); - } - catch (Exception ex) - { - return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน"); - } - } - - [HttpGet("pass-exam/{id:length(36)}")] - [AllowAnonymous] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetPassExam2ListReportAsync(Guid id) - { - try - { - var data = await _context.Candidates.AsQueryable() - .Include(x => x.PeriodExam) - .ThenInclude(x => x.ScoreImport) - - .Select(p => new - { - Id = p.PeriodExam.Id, - ExamId = p.SeatNumber, - CitizenId = p.CitizenId, - FullName = $"{p.FirstName} {p.LastName}", - DateOfBirth = p.DateOfBirth.Value.ToThaiShortDate(), - - ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", - - Number = p.Number == null ? 99999 : Convert.ToInt32(p.Number), - - FullA = 0, - SumA = 0, - FullB = p.PointTotalB, - SumB = p.PointB, - FullC = p.PointTotalC, - SumC = p.PointC, - SumScore = Convert.ToInt32(p.PointB ?? "0") + Convert.ToInt32(p.PointC ?? "0"), - ExamResult = p.Pass - - }) - .OrderBy(x => x.Number) - .Where(x => x.Id == id) - .Where(x => x.ExamResult == "ผ่าน") - .ToListAsync(); - - if (data.Count == 0) return Success(); - - var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.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.First().ExamName}.pdf"); - } - catch (Exception ex) - { - return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน"); - } - } - - #endregion - } + [Route("api/v{version:apiVersion}/report/exam")] + [ApiVersion("1.0")] + [ApiController] + [Produces("application/json")] + [Authorize] + [SwaggerTag("รายงานข้อมูลการสอบคัดเลือก")] + public class ExamReportController : BaseController + { + #region " Fields " + + private readonly ExamDbContext _context; + private readonly IWebHostEnvironment _hostingEnvironment; + private readonly IConfiguration _configuration; + private readonly string space = "ㅤ"; + + #endregion + + #region " Constructor and Destructor " + + public ExamReportController(ExamDbContext context, + IWebHostEnvironment hostingEnvironment, + IConfiguration configuration) + { + this._context = context; + this._hostingEnvironment = hostingEnvironment; + this._configuration = configuration; + } + + #endregion + + #region " Methods " + + #region " Privates " + + private int GetExamCountTes(string citizenId) + { + try + { + var count = _context.Candidates.AsQueryable() + .Where(x => x.CitizenId == citizenId) + .Count(); + + return count; + } + catch + { + throw; + } + } + + #endregion + + /// + /// แสดงหนังสือรับรอง + /// + /// รหัสรอบการสอบ + /// เลขประจำตัวผู้สมัครสอบ + /// ชนิดของรายงาน + /// + /// เมื่อแสดงรายงานสำเร็จ + /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + + [HttpGet("certificate/{type:int}/{id:length(36)}/{examId}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetCertificateReportAsync(Guid id, string examId, int type) + { + try + { + var data = await _context.Disables.AsQueryable() + .Include(x => x.PeriodExam) + .Where(x => x.PeriodExam.Id == id) + .Where(x => x.ExamId == examId) + .Join(_context.DisableScores.AsQueryable() + .Include(x => x.ScoreImport), + rc => new { rc.PeriodExam.Year, rc.ExamId }, + sc => new { sc.ScoreImport.Year, sc.ExamId }, + (p, sr) => new + { + ExamID = p.ExamId, + p.CitizenId, + Order = p.PeriodExam.Round, + Year = p.PeriodExam.Year.Value.ToThaiYear(), + FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", + ExamResult = sr == null ? "" : sr.ExamStatus, + EndDate = p.PeriodExam.RegisterEndDate.ToThaiFullDate3(), + AuthName = "นายณัฐพงศ์ ดิษยบุตร", + AuthPosition = "หัวหน้าสำนักงาน ก.ก." + }) + .FirstOrDefaultAsync(); + + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCertificate{type}.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, "เกิดข้อผิดพลาดในการแสดงรายงาน"); + } + } + + /// + /// แสดงเอกสารผลสอบ + /// + /// รหัสรอบการสอบ + /// เลขประจำตัวผู้สมัครสอบ + /// + /// เมื่อแสดงรายงานสำเร็จ + /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("score/{id:length(36)}/{examId}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetScoreReportAsync(Guid id, string examId) + { + try + { + var data = await _context.Disables.AsQueryable() + .Include(x => x.PeriodExam) + .Include(x => x.Documents) + .ThenInclude(x => x.DocumentFile) + .Where(x => x.PeriodExam.Id == id) + .Where(x => x.ExamId == examId) + .Join(_context.DisableScores.AsQueryable() + .Include(x => x.ScoreImport), + rc => new { rc.PeriodExam.Year, rc.ExamId }, + sc => new { sc.ScoreImport.Year, sc.ExamId }, + (p, sr) => new + { + ExamId = p.ExamId, + CitizenId = p.CitizenId, + p.Prefix, + FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", + DateOfBirth = p.DateOfBirth.ToThaiShortDate(), + Gender = p.Gendor, + Degree = p.Educations.First().Degree, + Major = p.Educations.First().Major, + ExamResult = sr == null ? "" : sr.ExamStatus, + University = p.Educations.First().University, + PositionName = p.PositionName, + ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", + Number = sr == null ? "" : sr.Number, + // ExamCount = 10, + // ExamCount = GetExamCountTes(p.CitizenId), + ScoreExpire = p.PeriodExam.AnnouncementDate == null ? "" : p.PeriodExam.AnnouncementDate.AddYears(2).ToThaiShortDate(), + FullA = sr == null ? 0 : sr.FullA, + SumA = sr == null ? 0 : sr.SumA, + FullB = sr == null ? 0 : sr.FullB, + SumB = sr == null ? 0 : sr.SumB, + FullC = sr == null ? 0 : sr.FullC, + SumC = sr == null ? 0 : sr.SumC, + }) + .FirstOrDefaultAsync(); + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptExamResult.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; + if (data != null) + { + var _data = new + { + ExamId = data.ExamId, + CitizenId = data.CitizenId, + Prefix = data.Prefix, + FullName = data.FullName, + DateOfBirth = data.DateOfBirth, + Gender = data.Gender, + Degree = data.Degree, + Major = data.Major, + ExamResult = data.ExamResult, + University = data.University, + PositionName = data.PositionName, + ExamName = data.ExamName, + Number = data.Number, + ExamCount = GetExamCountTes(data.CitizenId), + ScoreExpire = data.ScoreExpire, + FullA = data.FullA, + SumA = data.SumA, + FullB = data.FullB, + SumB = data.SumB, + FullC = data.FullC, + SumC = data.SumC, + }; + 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, "เกิดข้อผิดพลาดในการแสดงรายงาน"); + } + } + + [HttpGet("candidate/{id:length(36)}")] + [AllowAnonymous] + public async Task> GetCandidateListReportAsync(Guid id) + { + try + { + var data = await _context.Disables.AsQueryable() + .Include(x => x.PeriodExam) + .Where(x => x.PeriodExam.Id == id) + .OrderBy(x => x.ExamId) + .Select(p => new + { + ExamId = p.ExamId, + FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", + PositionName = p.PositionName, + ExamName = + $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", + }).ToListAsync(); + + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.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.First().ExamName}.pdf"); + + } + catch (Exception ex) + { + return Error(ex); + } + } + + [HttpGet("candidate-exam/{id:length(36)}")] + [AllowAnonymous] + public async Task> GetCandidateExamListReportAsync(Guid id) + { + try + { + var data = await _context.Candidates.AsQueryable() + .Include(x => x.PeriodExam) + .Where(x => x.PeriodExam.Id == id) + .Where(x => x.SeatNumber != null || x.SeatNumber != "") + .OrderBy(x => x.SeatNumber) + .Select(p => new + { + ExamId = p.SeatNumber, + FullName = $"{p.FirstName} {p.LastName}", + PositionName = "", + ExamName = + $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", + }).ToListAsync(); + + if (data.Count == 0) return Success(); + + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptCandidateList.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.First().ExamName}.pdf"); + + } + catch (Exception ex) + { + return Error(ex); + } + } + + [HttpGet("pass/{id:length(36)}")] + [AllowAnonymous] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetPassExamListReportAsync(Guid id) + { + try + { + var data = await _context.Disables.AsQueryable() + .Include(x => x.PeriodExam) + .ThenInclude(x => x.ScoreImport) + .Include(x => x.Documents) + .ThenInclude(x => x.DocumentFile) + + + .Join(_context.DisableScores.AsQueryable() + .Include(x => x.ScoreImport) + .Where(x => x.ScoreImport.PeriodExamId == id) + .Where(x => x.ExamStatus == "ผ่าน"), + rc => new { id = rc.PeriodExam.Id, examId = rc.ExamId }, + sc => new { id = sc.ScoreImport.PeriodExamId, examId = sc.ExamId }, + (p, sr) => new + { + Id = p.PeriodExam.Id, + ExamId = p.ExamId, + CitizenId = p.CitizenId, + p.Prefix, + FullName = $"{p.Prefix}{p.FirstName} {p.LastName}", + DateOfBirth = p.DateOfBirth.ToThaiShortDate(), + Gender = p.Gendor, + Degree = p.Educations.First().Degree, + Major = p.Educations.First().Major, + + ExamResult = sr == null ? "" : sr.ExamStatus, + + University = p.Educations.First().University, + PositionName = p.PositionName, + ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", + + + Number = sr == null ? 99999 : Convert.ToInt32(sr.Number), + + + + FullA = sr == null ? 0 : sr.FullA, + SumA = sr == null ? 0 : sr.SumA, + FullB = sr == null ? 0 : sr.FullB, + SumB = sr == null ? 0 : sr.SumB, + FullC = sr == null ? 0 : sr.FullC, + SumC = sr == null ? 0 : sr.SumC, + SumScore = sr == null ? 0 : sr.SumA + sr.SumB + sr.SumC, + + + + }) + .OrderBy(x => x.Number) + .Where(x => x.Id == id) + .Where(x => x.ExamResult == "ผ่าน") + .ToListAsync(); + + if (data.Count == 0) return Success(); + + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.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.First().ExamName}.pdf"); + } + catch (Exception ex) + { + return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน"); + } + } + + [HttpGet("pass-exam/{id:length(36)}")] + [AllowAnonymous] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetPassExam2ListReportAsync(Guid id) + { + try + { + var data = await _context.Candidates.AsQueryable() + .Include(x => x.PeriodExam) + .ThenInclude(x => x.ScoreImport) + .Select(p => new + { + Id = p.PeriodExam.Id, + ExamId = p.SeatNumber, + CitizenId = p.CitizenId, + FullName = $"{p.FirstName} {p.LastName}", + DateOfBirth = p.DateOfBirth.Value.ToThaiShortDate(), + ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}", + Number = p.Number == null ? 99999 : Convert.ToInt32(p.Number), + FullA = 0, + SumA = 0, + FullB = p.PointTotalB, + SumB = p.PointB, + FullC = p.PointTotalC, + SumC = p.PointC, + SumScore = Convert.ToInt32(p.PointB ?? "0") + Convert.ToInt32(p.PointC ?? "0"), + ExamResult = p.Pass + }) + .OrderBy(x => x.Number) + .Where(x => x.Id == id) + .Where(x => x.ExamResult == "ได้") + .ToListAsync(); + + // if (data.Count == 0) return Success(); + + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Report", "Recruit", $"rptPassExamList.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.First().ExamName}.pdf"); + } + catch (Exception ex) + { + return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน"); + } + } + + #endregion + } }