using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.MessageQueue;
using BMA.EHR.Application.Repositories.Reports;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Placement.Service.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http.Headers;
using System.Security.Claims;
namespace BMA.EHR.Placement.Service.Controllers
{
[Route("api/v{version:apiVersion}/placement/probation")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("report ทดลองงาน")]
public class PlacementProbationController : BaseController
{
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
private readonly PermissionRepository _permission;
private readonly ProbationReportRepository _repository;
public PlacementProbationController(
ApplicationDBContext context,
MinIOService documentService,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration,
PermissionRepository permiss,
ProbationReportRepository repository)
{
_context = context;
_documentService = documentService;
_httpContextAccessor = httpContextAccessor;
_configuration = configuration;
_permission = permiss;
_repository = repository;
}
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
private string? token => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
#endregion
#region 13-แบบมอบหมายงานการทดลองปฏิบัติหน้าที่ราชการ
///
/// 13-แบบมอบหมายงานการทดลองปฏิบัติหน้าที่ราชการ
///
/// assign id แบบมอบหมายงานการทดลองปฏิบัติหน้าที่ราชการ
/// pdf, docx หรือ xlsx
///
/// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ
/// ไม่ได้ Login เข้าระบบ
/// เมื่อเกิดข้อผิดพลาดในการทำงาน
[HttpGet("13/{exportType}/{id}")]
public async Task> GetProbationReportAsync(Guid id, string exportType = "pdf")
{
try
{
string authorizationHeader = Request.Headers["Authorization"];
string token = string.Empty;
if (!string.IsNullOrEmpty(authorizationHeader) && authorizationHeader.StartsWith("Bearer "))
{
token = authorizationHeader.Substring("Bearer ".Length).Trim();
var probation = await _repository.GetProbationAssignAsync(id, token);
if (probation != 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 data = new
{
template = "probation-13",
reportName = "docx-report",
data = probation
};
return Success(data);
}
else
{
return NotFound();
}
}
else
{
return Unauthorized();
}
}
catch
{
throw;
}
}
#endregion
#region 14-แบบบันทึกผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับผู้ดูแล
///
/// 14-แบบบันทึกผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับผู้ดูแล
///
/// evaluate id
/// pdf, docx หรือ xlsx
///
/// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ
/// ไม่ได้ Login เข้าระบบ
/// เมื่อเกิดข้อผิดพลาดในการทำงาน
[HttpGet("14/{exportType}/{id}")]
public async Task> GetProbation14ConvertReportAsync(Guid id, string exportType = "pdf")
{
try
{
string authorizationHeader = Request.Headers["Authorization"];
string token = string.Empty;
if (!string.IsNullOrEmpty(authorizationHeader) && authorizationHeader.StartsWith("Bearer "))
{
token = authorizationHeader.Substring("Bearer ".Length).Trim();
var evaluateRecord = await _repository.GetEvaluateRecord14_15Async(id, token);
if (evaluateRecord != 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 data = new
{
template = "probation-14",
reportName = "docx-report",
data = evaluateRecord
};
return Success(data);
}
else
{
return NotFound();
}
}
else
{
return Unauthorized();
}
}
catch
{
throw;
}
}
#endregion
#region 15-แบบบันทึกผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับผู้บังคับบัญชา
///
/// 15-แบบบันทึกผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับผู้บังคับบัญชา
///
/// evaluate id
/// pdf, docx หรือ xlsx
///
/// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ
/// ไม่ได้ Login เข้าระบบ
/// เมื่อเกิดข้อผิดพลาดในการทำงาน
[HttpGet("15/{exportType}/{id}")]
public async Task> GetProbation15ConvertReportAsync(Guid id, string exportType = "pdf")
{
try
{
string authorizationHeader = Request.Headers["Authorization"];
string token = string.Empty;
if (!string.IsNullOrEmpty(authorizationHeader) && authorizationHeader.StartsWith("Bearer "))
{
token = authorizationHeader.Substring("Bearer ".Length).Trim();
var evaluateRecord = await _repository.GetEvaluateRecord14_15Async(id, token);
if (evaluateRecord != 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 data = new
{
template = "probation-15",
reportName = "docx-report",
data = evaluateRecord
};
return Success(data);
}
else
{
return NotFound();
}
}
else
{
return Unauthorized();
}
}
catch
{
throw;
}
}
#endregion
#region 16-แบบประเมินผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับผู้บังคับบัญชา
///
/// 16-แบบประเมินผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับผู้บังคับบัญชา
///
/// evaluate id
/// pdf, docx หรือ xlsx
///
/// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ
/// ไม่ได้ Login เข้าระบบ
/// เมื่อเกิดข้อผิดพลาดในการทำงาน
[HttpGet("16/{exportType}/{id}")]
public async Task> GetProbation16ConvertReportAsync(Guid id, string exportType = "pdf")
{
try
{
string authorizationHeader = Request.Headers["Authorization"];
string token = string.Empty;
if (!string.IsNullOrEmpty(authorizationHeader) && authorizationHeader.StartsWith("Bearer "))
{
token = authorizationHeader.Substring("Bearer ".Length).Trim();
var evaluateAssign = await _repository.GetEvaluateAssignAsync(id, token);
if (evaluateAssign != 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 data = new
{
template = "probation-16",
reportName = "docx-report",
data = evaluateAssign
};
return Success(data);
}
else
{
return NotFound();
}
}
else
{
return Unauthorized();
}
}
catch
{
throw;
}
}
#endregion
#region 17-แบบประเมินผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับคณะกรรมการ
///
/// 17-แบบประเมินผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับคณะกรรมการ
///
/// evaluate id
/// pdf, docx หรือ xlsx
///
/// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ
/// ไม่ได้ Login เข้าระบบ
/// เมื่อเกิดข้อผิดพลาดในการทำงาน
[HttpGet("17/{exportType}/{id}")]
public async Task> GetProbation17ConvertReportAsync(Guid id, string exportType = "pdf")
{
try
{
string authorizationHeader = Request.Headers["Authorization"];
string token = string.Empty;
if (!string.IsNullOrEmpty(authorizationHeader) && authorizationHeader.StartsWith("Bearer "))
{
token = authorizationHeader.Substring("Bearer ".Length).Trim();
var evaluateAssign = await _repository.GetEvaluateChairmanAssignAsync(id, token);
if (evaluateAssign != 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 data = new
{
template = "probation-17",
reportName = "docx-report",
data = evaluateAssign
};
return Success(data);
}
else
{
return NotFound();
}
}
else
{
return Unauthorized();
}
}
catch
{
throw;
}
}
#endregion
#region 18-แบบรายงานการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับประธาน และ กรณีขยายเวลา
///
/// 18-แบบรายงานการประเมินผลการทดลองปฏิบัติหน้าที่ราชการ สำหรับประธาน และ กรณีขยายเวลา
///
/// assign id แบบมอบหมายงานการทดลองปฏิบัติหน้าที่ราชการ
/// pdf, docx หรือ xlsx
///
/// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ
/// ไม่ได้ Login เข้าระบบ
/// เมื่อเกิดข้อผิดพลาดในการทำงาน
[HttpGet("18/{exportType}/{id}/{no}")]
public async Task> GetProbation19ConvertReportAsync(Guid id, int no, string exportType = "pdf")
{
try
{
string authorizationHeader = Request.Headers["Authorization"];
string token = string.Empty;
if (!string.IsNullOrEmpty(authorizationHeader) && authorizationHeader.StartsWith("Bearer "))
{
token = authorizationHeader.Substring("Bearer ".Length).Trim();
var evaluateAssign = await _repository.GetEvaluateResultAssignAsync(id, no, token);
if (evaluateAssign != 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 data = new
{
template = no == 1 ? "probation-18-1" : "probation-18-2",
reportName = "docx-report",
data = evaluateAssign
};
return Success(data);
}
else
{
return NotFound();
}
}
else
{
return Unauthorized();
}
}
catch
{
throw;
}
}
#endregion
}
}