167 lines
7.8 KiB
C#
167 lines
7.8 KiB
C#
using BMA.EHR.Application.Repositories;
|
|
using BMA.EHR.Application.Repositories.MessageQueue;
|
|
using BMA.EHR.Application.Responses;
|
|
using BMA.EHR.Application.Responses.Profiles;
|
|
using BMA.EHR.Discipline.Service.Requests;
|
|
using BMA.EHR.Domain.Common;
|
|
using BMA.EHR.Domain.Models.Discipline;
|
|
using BMA.EHR.Domain.Shared;
|
|
using BMA.EHR.Domain.Extensions;
|
|
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.Linq;
|
|
using Newtonsoft.Json;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using System.Security.Claims;
|
|
|
|
namespace BMA.EHR.DisciplineSuspend.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/discipline/report")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("ระบบวินัยรายงาน")]
|
|
public class DisciplineReportController : BaseController
|
|
{
|
|
private readonly DisciplineDbContext _context;
|
|
private readonly MinIODisciplineService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly PermissionRepository _permission;
|
|
private readonly UserProfileRepository _userProfileRepository;
|
|
|
|
public DisciplineReportController(DisciplineDbContext context,
|
|
MinIODisciplineService documentService,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
PermissionRepository permission,
|
|
UserProfileRepository userProfileRepository)
|
|
{
|
|
// _repository = repository;
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_permission = permission;
|
|
_userProfileRepository = userProfileRepository;
|
|
}
|
|
|
|
#region " Properties "
|
|
|
|
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
|
|
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
|
private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
|
|
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// รายชื่อออกคำสั่งลงโทษ
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost("{type}")]
|
|
public async Task<ActionResult<ResponseObject>> GetReportDiscipline([FromBody] DisciplineReportRequest req, string type)
|
|
{
|
|
var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_DISCIPLINE_REPORT");
|
|
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
|
|
if (jsonData["status"]?.ToString() != "200")
|
|
{
|
|
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
|
|
}
|
|
var profile = new List<GetProfileByKeycloakIdRootDto>();
|
|
if (type.Trim().ToUpper() == "OFFICER")
|
|
{
|
|
profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD");
|
|
}
|
|
else
|
|
{
|
|
profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId, jsonData["result"] == "OWNER" || jsonData["result"] == "CHILD");
|
|
}
|
|
|
|
if (req.posLevel != null || req.posType != null)
|
|
{
|
|
profile = profile.Where(x => x.PositionType == req.posType || x.PositionLevel == req.posLevel).ToList();
|
|
}
|
|
|
|
var data_search = await _context.DisciplineDisciplinarys
|
|
.Include(x => x.DisciplineDisciplinary_ProfileComplaintInvestigates)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.ToListAsync();
|
|
|
|
if (req.status.Trim().ToUpper() != "ALL")
|
|
data_search = data_search.Where(x => x.Status.Contains(req.status.Trim().ToUpper())).ToList();
|
|
|
|
if (req.offenseDetail.Trim().ToUpper() != "ALL")
|
|
data_search = data_search.Where(x => x.OffenseDetails.Contains(req.status.Trim().ToUpper())).ToList();
|
|
|
|
if (req.disciplinaryFaultLevel.Trim().ToUpper() != "ALL")
|
|
data_search = data_search.Where(x => x.DisciplinaryFaultLevel.Contains(req.status.Trim().ToUpper())).ToList();
|
|
|
|
var mapData = data_search
|
|
.SelectMany((d) => d.DisciplineDisciplinary_ProfileComplaintInvestigates.Select((profile) => new
|
|
{
|
|
fullName = $"{profile.Prefix}{profile.FirstName} {profile.LastName}",
|
|
citizenId = profile.CitizenId,
|
|
position = profile.Position,
|
|
posLevel = profile.posLevelName,
|
|
oc = profile.Organization,
|
|
CreatedAt = profile.CreatedAt,
|
|
offense = d.OffenseDetails != null && d.OffenseDetails != ""
|
|
? d.OffenseDetails == "NOT_SPECIFIED"
|
|
? "ความผิดวินัยยังไม่ระบุ"
|
|
: d.OffenseDetails == "NOT_DEADLY"
|
|
? "ความผิดวินัยไม่ร้ายแรง"
|
|
: d.OffenseDetails == "DEADLY"
|
|
? "ความผิดวินัยร้ายแรง"
|
|
: ""
|
|
: "",
|
|
faultLevel = d.DisciplinaryFaultLevel,
|
|
}))
|
|
.Where(x =>
|
|
x.citizenId != null && profile.Any(p => p.CitizenId == x.citizenId))
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.ToList();
|
|
|
|
var count = 1;
|
|
var employees = new List<dynamic>();
|
|
foreach (var p in mapData)
|
|
{
|
|
var emp = new
|
|
{
|
|
no = count,
|
|
fullName = p.fullName,
|
|
citizenId = p.citizenId,
|
|
position = p.position,
|
|
posLevel = p.posLevel,
|
|
oc = profile.FirstOrDefault(x => x.CitizenId == p.citizenId)?.Oc ?? "",
|
|
offense = p.offense,
|
|
faultLevel = p.faultLevel,
|
|
one = 0,
|
|
two = 0,
|
|
three = 0,
|
|
};
|
|
employees.Add(emp);
|
|
count++;
|
|
}
|
|
|
|
var result = new
|
|
{
|
|
template = "reportDiscipline",
|
|
reportName = "xlsx-report",
|
|
data = new
|
|
{
|
|
year = (req.year + 543).ToString().ToThaiNumber(),
|
|
dateCurrent = $"ณ วันที่ {DateTime.Now.Date.ToThaiShortDate().ToThaiNumber()}",
|
|
data = employees
|
|
}
|
|
};
|
|
return Success(result);
|
|
}
|
|
}
|
|
}
|