parent
3b46a1bcde
commit
c302d9b848
2 changed files with 301 additions and 50 deletions
|
|
@ -13,6 +13,15 @@ using System.Text;
|
|||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
using System.Net.Http.Headers;
|
||||
using Newtonsoft.Json;
|
||||
using iText.Kernel.Font;
|
||||
using iText.Kernel.Pdf;
|
||||
using iText.Layout.Element;
|
||||
using iText.Layout.Properties;
|
||||
using iText.Layout;
|
||||
using iText.IO.Font;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace BMA.EHR.Report.Service.Controllers
|
||||
{
|
||||
|
|
@ -374,7 +383,7 @@ namespace BMA.EHR.Report.Service.Controllers
|
|||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("candidate-exam/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> GetCandidateExamListReportAsync(Guid id)
|
||||
|
|
@ -656,6 +665,183 @@ namespace BMA.EHR.Report.Service.Controllers
|
|||
}
|
||||
}
|
||||
|
||||
[HttpGet("candidate-new/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetCandidateListNewReportAsync(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 = !string.IsNullOrEmpty(p.ExamId) ? p.ExamId.ToThaiNumber() : "",
|
||||
FullName = $"{p.Prefix ?? ""}{p.FirstName ?? ""} {p.LastName ?? ""}",
|
||||
//PositionName = p.PositionName,
|
||||
ExamName =
|
||||
($"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round}/{p.PeriodExam.Year.Value.ToThaiYear()}").ToThaiNumber(),
|
||||
}).ToListAsync();
|
||||
|
||||
if (data.Count == 0)
|
||||
return Error("ไม่พบข้อมูลในระบบ");
|
||||
|
||||
// เตรียม MemoryStream
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
// สร้าง PDF document
|
||||
using (var writer = new PdfWriter(stream))
|
||||
using (var pdf = new PdfDocument(writer))
|
||||
using (var document = new Document(pdf, iText.Kernel.Geom.PageSize.A4))
|
||||
{
|
||||
// font TH-Sarabun
|
||||
var fontPath = System.IO.Path.Combine(Environment.CurrentDirectory, "Fonts", "THSarabun.ttf");
|
||||
var font = PdfFontFactory.CreateFont(fontPath, "Identity-H");
|
||||
document.SetFont(font).SetFontSize(16);
|
||||
|
||||
// margin top, right, bottom, left
|
||||
document.SetMargins(30, 15, 20, 15);
|
||||
|
||||
// title
|
||||
var title = new Paragraph($"รายชื่อผู้มีสิทธิ์สอบ\n{data.First().ExamName ?? ""}")
|
||||
.SetBold()
|
||||
.SetTextAlignment(TextAlignment.CENTER);
|
||||
|
||||
document.Add(title);
|
||||
|
||||
// create Table
|
||||
var table = new iText.Layout.Element.Table(iText.Layout.Properties.UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
|
||||
|
||||
// header
|
||||
string[] headers = { "เลขประจำตัวสอบ", "ชื่อ - นามสกุล" };
|
||||
foreach (var h in headers)
|
||||
{
|
||||
table.AddHeaderCell(new Cell().Add(new Paragraph(h)).SetTextAlignment(TextAlignment.CENTER).SetBold());
|
||||
}
|
||||
// details
|
||||
foreach (var x in data)
|
||||
{
|
||||
table.AddCell(new Cell().Add(new Paragraph(x.ExamId)).SetTextAlignment(TextAlignment.CENTER));
|
||||
table.AddCell(new Cell().Add(new Paragraph(x.FullName)).SetTextAlignment(TextAlignment.LEFT));
|
||||
}
|
||||
|
||||
document.Add(table);
|
||||
}
|
||||
|
||||
return File(stream.ToArray(), "application/pdf", $"รายชื่อผู้มีสิทธิ์สอบ_{data.First().ExamName}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("pass-new/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetPassExamListNewReportAsync(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,
|
||||
FullName = $"{p.Prefix}{p.FirstName} {p.LastName}",
|
||||
ExamResult = sr == null ? "" : sr.ExamStatus,
|
||||
ExamName = $"{p.PeriodExam.Name} ครั้งที่ {p.PeriodExam.Round.ToString().ToThaiNumber()}/{p.PeriodExam.Year.Value.ToThaiYear().ToString().ToThaiNumber()}",
|
||||
Number = sr == null || sr.Number == null ? 0 : Convert.ToInt32(sr.Number),
|
||||
SumScore = sr == null
|
||||
? "๐"
|
||||
: (((double)(sr.SumA > 0 ? sr.SumA : 0) + (sr.SumB > 0 ? sr.SumB : 0) + (sr.SumC > 0 ? sr.SumC : 0)).ToString("0.00")).ToThaiNumber(),
|
||||
})
|
||||
.OrderBy(x => x.Number)
|
||||
.Where(x => x.Id == id)
|
||||
.Where(x => x.ExamResult == "ผ่าน")
|
||||
.ToListAsync();
|
||||
|
||||
if (data.Count == 0)
|
||||
return Error("ไม่พบข้อมูลในระบบ");
|
||||
|
||||
var result = data.Select(x => new
|
||||
{
|
||||
Number = x.Number > 0 ? x.Number.ToString().ToThaiNumber() : "",
|
||||
x.FullName,
|
||||
x.SumScore
|
||||
}).ToList();
|
||||
|
||||
// เตรียม MemoryStream
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
// สร้าง PDF document
|
||||
using (var writer = new PdfWriter(stream))
|
||||
using (var pdf = new PdfDocument(writer))
|
||||
using (var document = new Document(pdf, iText.Kernel.Geom.PageSize.A4))
|
||||
{
|
||||
// font TH-Sarabun
|
||||
var fontPath = System.IO.Path.Combine(Environment.CurrentDirectory, "Fonts", "THSarabun.ttf");
|
||||
var font = PdfFontFactory.CreateFont(fontPath, "Identity-H");
|
||||
document.SetFont(font).SetFontSize(16);
|
||||
|
||||
// margin top, right, bottom, left
|
||||
document.SetMargins(30, 15, 20, 15);
|
||||
|
||||
// title
|
||||
var title = new Paragraph($"รายชื่อผู้สอบแข่งขันได้\n{data.First().ExamName ?? ""}")
|
||||
.SetBold()
|
||||
.SetTextAlignment(TextAlignment.CENTER);
|
||||
|
||||
document.Add(title);
|
||||
|
||||
// create Table
|
||||
var table = new iText.Layout.Element.Table(iText.Layout.Properties.UnitValue.CreatePercentArray(4)).UseAllAvailableWidth();
|
||||
|
||||
// header
|
||||
string[] headers = { "ลำดับที่", "สนามสอบ", "ชื่อ - นามสกุล", "คะแนนสอบ" };
|
||||
foreach (var h in headers)
|
||||
{
|
||||
table.AddHeaderCell(new Cell().Add(new Paragraph(h)).SetTextAlignment(TextAlignment.CENTER).SetBold());
|
||||
}
|
||||
// details
|
||||
foreach (var x in result)
|
||||
{
|
||||
table.AddCell(new Cell().Add(new Paragraph(x.Number)).SetTextAlignment(TextAlignment.CENTER));
|
||||
table.AddCell(new Cell());
|
||||
table.AddCell(new Cell().Add(new Paragraph(x.FullName)).SetTextAlignment(TextAlignment.LEFT));
|
||||
table.AddCell(new Cell().Add(new Paragraph(x.SumScore)).SetTextAlignment(TextAlignment.CENTER));
|
||||
}
|
||||
|
||||
document.Add(table);
|
||||
}
|
||||
|
||||
return File(stream.ToArray(), "application/pdf", $"รายชื่อผู้สอบแข่งขันได้_{data.First().ExamName}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex, "เกิดข้อผิดพลาดในการแสดงรายงาน");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue