hrms-api-report-v1/Controllers/RecruitReportController.cs

382 lines
20 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BMA.EHR.Organization.Service.Extensions;
using BMA.EHR.Profile.Service.Controllers;
using BMA.EHR.Recruit.Service.Services;
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 = "";
private readonly RecruitService _recruitService;
#endregion
#region " Constructor and Destructor "
public RecruitReportController(ApplicationDbContext context,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration,
RecruitService recruitService)
{
this._context = context;
this._hostingEnvironment = hostingEnvironment;
this._configuration = configuration;
this._recruitService = recruitService;
}
#endregion
#region " Methods "
/// <summary>
/// แสดงหนังสือรับรอง
/// </summary>
/// <param name="id">รหัสรอบการสอบ</param>
/// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
/// <param name="type">ชนิดของรายงาน</param>
/// <returns></returns>
/// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("certificate/{type:int}/{id:length(36)}/{examId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetCertificateReportAsync(Guid id, string examId, int type)
{
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.ToThaiYear(),
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
ExamResult = sr == null ? "" : sr.ExamStatus,
EndDate = p.RecruitImport.RegisterEndDate == null ? "-" : p.RecruitImport.RegisterEndDate.Value.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, "เกิดข้อผิดพลาดในการแสดงรายงาน");
}
}
/// <summary>
/// แสดงเอกสารผลสอบ
/// </summary>
/// <param name="id">รหัสรอบการสอบ</param>
/// <param name="examId">เลขประจำตัวผู้สมัครสอบ</param>
/// <returns></returns>
/// <response code="200">เมื่อแสดงรายงานสำเร็จ</response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("score/{id:length(36)}/{examId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetScoreReportAsync(Guid id, string examId)
{
try
{
var data = await _context.Recruits.AsQueryable()
.Include(x => x.RecruitImport)
.Include(x => x.Documents)
.ThenInclude(x => x.DocumentFile)
.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,
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.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order}/{p.RecruitImport.Year.ToThaiYear()}",
Number = sr == null ? "" : sr.Number,
ExamCount = _recruitService.GetExamCount(p.CitizenId),
ScoreExpire = p.RecruitImport.AnnouncementDate == null ? "" : p.RecruitImport.AnnouncementDate.Value.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;
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<ActionResult<ResponseObject>> GetCandidateListReportAsync(Guid id)
{
try
{
var data = await _context.Recruits.AsQueryable()
.Include(x => x.RecruitImport)
.Include(x => x.Documents)
.ThenInclude(x => x.DocumentFile)
.Where(x => x.RecruitImport.Id == id)
.OrderBy(x => x.ExamId)
.Select(p => new
{
ExamId = p.ExamId != null ? p.ExamId.ToThaiNumber() : string.Empty,
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
PositionName = p.PositionName,
ExamName =
$"{p.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order.ToString().ToThaiNumber()}/{p.RecruitImport.Year.ToThaiYear().ToString().ToThaiNumber()}",
}).ToListAsync();
if (data.Count == 0) return Error("ไม่พบข้อมูลในระบบ");
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;
report.ReportParameters["ExamName"].Value = data[0].ExamName.ToThaiNumber();
var tbl = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["table1"];
tbl.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<ActionResult<ResponseObject>> GetPassExamListReportAsync(Guid id)
{
try
{
var data = await _context.Recruits.AsQueryable()
.Include(x => x.RecruitImport)
.ThenInclude(x => x.ScoreImport)
.Include(x => x.Documents)
.ThenInclude(x => x.DocumentFile)
.Join(_context.RecruitScores.AsQueryable()
.Include(x => x.ScoreImport)
.Where(x => x.ScoreImport.RecruitImportId == id)
.Where(x => x.ExamStatus == "ผ่าน"),
rc => new { id = rc.RecruitImport.Id, examId = rc.ExamId },
sc => new { id = sc.ScoreImport.RecruitImportId, examId = sc.ExamId },
(p, sr) => new
{
Id = p.RecruitImport.Id,
ExamId = p.ExamId != null ? p.ExamId.ToThaiNumber() : string.Empty,
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.RecruitImport.Name} ครั้งที่ {p.RecruitImport.Order.ToString().ToThaiNumber()}/{p.RecruitImport.Year.ToThaiYear().ToString().ToThaiNumber()}",
Number = sr == null ? "" : sr.Number,
ExamCount = _recruitService.GetExamCount(p.CitizenId),
ScoreExpire = p.RecruitImport.AnnouncementDate == null ? "" : p.RecruitImport.AnnouncementDate.Value.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,
SumScore = sr == null ? "" : (sr.SumA + sr.SumB + sr.SumC).ToString().ToThaiNumber(),
})
.OrderBy(x => x.Number)
.Where(x => x.Id == id)
.Where(x => x.ExamResult == "ผ่าน")
.ToListAsync();
if (data.Count == 0) return Error("ไม่พบข้อมูลในระบบ");
var data_ = data.Select(x => new
{
x.ExamName,
Number = x.Number.ToString().ToThaiNumber(),
x.ExamId,
x.FullName,
x.SumScore
}).ToList();
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;
report.ReportParameters["ExamName"].Value = data_[0].ExamName.ToThaiNumber();
var tbl = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["table1"];
tbl.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
}
}