406 lines
21 KiB
C#
406 lines
21 KiB
C#
using BMA.EHR.Application.Repositories;
|
|
using BMA.EHR.Application.Repositories.MessageQueue;
|
|
using BMA.EHR.Domain.Common;
|
|
using BMA.EHR.Domain.Models.Placement;
|
|
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 Swashbuckle.AspNetCore.Annotations;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Claims;
|
|
|
|
namespace BMA.EHR.Placement.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/placement/repatriation")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("ระบบส่งตัวกลับ")]
|
|
public class PlacementRepatriationController : BaseController
|
|
{
|
|
private readonly PlacementRepository _repository;
|
|
private readonly NotificationRepository _repositoryNoti;
|
|
private readonly ApplicationDBContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public PlacementRepatriationController(PlacementRepository repository,
|
|
NotificationRepository repositoryNoti,
|
|
ApplicationDBContext context,
|
|
MinIOService documentService,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
IConfiguration configuration)
|
|
{
|
|
_repository = repository;
|
|
_repositoryNoti = repositoryNoti;
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#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"];
|
|
|
|
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// list รายการส่งตัวกลับของ Admin
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet()]
|
|
public async Task<ActionResult<ResponseObject>> GetListByAdmin()
|
|
{
|
|
var rootId = "";
|
|
var child1Id = "";
|
|
var child2Id = "";
|
|
var child3Id = "";
|
|
var child4Id = "";
|
|
var apiUrl = $"{_configuration["API"]}org/profile/keycloak/position";
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", ""));
|
|
var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
|
var _res = await client.SendAsync(_req);
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
var org = JsonConvert.DeserializeObject<OrgRequest>(_result);
|
|
|
|
if (org == null || org.result == null)
|
|
return Error("ไม่พบหน่วยงานของผู้ใช้งานคนนี้", 404);
|
|
rootId = org.result.rootId == null ? "" : org.result.rootId;
|
|
child1Id = org.result.child1Id == null ? "" : org.result.child1Id;
|
|
child2Id = org.result.child2Id == null ? "" : org.result.child2Id;
|
|
child3Id = org.result.child3Id == null ? "" : org.result.child3Id;
|
|
child4Id = org.result.child4Id == null ? "" : org.result.child4Id;
|
|
|
|
var placementRepatriations = await _context.PlacementRepatriations.AsQueryable()
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Where(x => PlacementAdmin == true ? true : (rootId == "" ? true : (child1Id == "" ? x.rootId == rootId : (child2Id == "" ? x.child1Id == child1Id : (child3Id == "" ? x.child2Id == child2Id : (child4Id == "" ? x.child3Id == child3Id : x.child4Id == child4Id))))))
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
p.profileId,
|
|
p.prefix,
|
|
p.firstName,
|
|
p.lastName,
|
|
p.root,
|
|
p.rootShortName,
|
|
p.child1,
|
|
p.child1ShortName,
|
|
p.child2,
|
|
p.child2ShortName,
|
|
p.child3,
|
|
p.child3ShortName,
|
|
p.child4,
|
|
p.child4ShortName,
|
|
p.posMasterNo,
|
|
p.position,
|
|
p.posLevelName,
|
|
p.posTypeName,
|
|
p.CreatedAt,
|
|
p.Organization,
|
|
p.Reason,
|
|
p.Status,
|
|
p.Date,
|
|
salary = p.AmountOld,
|
|
p.PositionTypeOld,
|
|
p.PositionLevelOld,
|
|
p.PositionNumberOld,
|
|
p.OrganizationPositionOld,
|
|
p.IsActive,
|
|
p.DateRepatriation,
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Success(placementRepatriations);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// get รายละเอียดส่งตัวกลับเจ้าหน้าที่
|
|
/// </summary>
|
|
/// <param name="id">Id ส่งตัวกลับ</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetDetailAdmin(Guid id)
|
|
{
|
|
var data = await _context.PlacementRepatriations.AsQueryable()
|
|
.Where(x => x.Id == id)
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
p.profileId,
|
|
p.prefix,
|
|
p.firstName,
|
|
p.lastName,
|
|
p.root,
|
|
p.rootShortName,
|
|
p.child1,
|
|
p.child1ShortName,
|
|
p.child2,
|
|
p.child2ShortName,
|
|
p.child3,
|
|
p.child3ShortName,
|
|
p.child4,
|
|
p.child4ShortName,
|
|
p.posMasterNo,
|
|
p.position,
|
|
p.posLevelName,
|
|
p.posTypeName,
|
|
p.Reason,
|
|
p.Status,
|
|
p.Organization,
|
|
p.Date,
|
|
salary = p.AmountOld,
|
|
p.CreatedAt,
|
|
p.PositionTypeOld,
|
|
p.PositionLevelOld,
|
|
p.PositionNumberOld,
|
|
p.OrganizationPositionOld,
|
|
p.PositionOld,
|
|
p.DateRepatriation,
|
|
})
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(GlobalMessages.DataNotFound, 404);
|
|
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// สร้างส่งตัวกลับ
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost()]
|
|
public async Task<ActionResult<ResponseObject>> Post([FromForm] PlacementAddProfileRequest req)
|
|
{
|
|
var placementRepatriation = new PlacementRepatriation
|
|
{
|
|
Organization = Request.Form.ContainsKey("Organization") ? Request.Form["Organization"] : "",
|
|
Reason = Request.Form.ContainsKey("Reason") ? Request.Form["Reason"] : "",
|
|
Status = "WAITTING",
|
|
CreatedFullName = FullName ?? "System Administrator",
|
|
CreatedUserId = UserId ?? "",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
var apiUrl = $"{_configuration["API"]}org/profile/profileid/position/{req.Id}";
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", ""));
|
|
var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
|
var _res = await client.SendAsync(_req);
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
var org = JsonConvert.DeserializeObject<OrgRequest>(_result);
|
|
|
|
if (org == null || org.result == null)
|
|
return Error("ไม่พบหน่วยงานของผู้ใช้งานคนนี้", 404);
|
|
|
|
placementRepatriation.profileId = org.result.profileId;
|
|
placementRepatriation.prefix = org.result.prefix;
|
|
placementRepatriation.firstName = org.result.firstName;
|
|
placementRepatriation.lastName = org.result.lastName;
|
|
placementRepatriation.citizenId = org.result.citizenId;
|
|
placementRepatriation.root = org.result.root;
|
|
placementRepatriation.rootId = org.result.rootId;
|
|
placementRepatriation.rootShortName = org.result.rootShortName;
|
|
placementRepatriation.child1 = org.result.child1;
|
|
placementRepatriation.child1Id = org.result.child1Id;
|
|
placementRepatriation.child1ShortName = org.result.child1ShortName;
|
|
placementRepatriation.child2 = org.result.child2;
|
|
placementRepatriation.child2Id = org.result.child2Id;
|
|
placementRepatriation.child2ShortName = org.result.child2ShortName;
|
|
placementRepatriation.child3 = org.result.child3;
|
|
placementRepatriation.child3Id = org.result.child3Id;
|
|
placementRepatriation.child3ShortName = org.result.child3ShortName;
|
|
placementRepatriation.child4 = org.result.child4;
|
|
placementRepatriation.child4Id = org.result.child4Id;
|
|
placementRepatriation.child4ShortName = org.result.child4ShortName;
|
|
placementRepatriation.posMasterNo = org.result.posMasterNo;
|
|
placementRepatriation.position = org.result.position;
|
|
placementRepatriation.posTypeId = org.result.posTypeId;
|
|
placementRepatriation.posTypeName = org.result.posTypeName;
|
|
placementRepatriation.posLevelId = org.result.posLevelId;
|
|
placementRepatriation.posLevelName = org.result.posLevelName;
|
|
|
|
placementRepatriation.PositionOld = org.result.position;
|
|
placementRepatriation.PositionLevelOld = org.result.posLevelName;
|
|
placementRepatriation.PositionTypeOld = org.result.posTypeName;
|
|
placementRepatriation.PositionNumberOld = org.result.nodeShortName + org.result.posMasterNo;
|
|
placementRepatriation.OrganizationOld = (org.result.child4 == null ? "" : org.result.child4 + "/") +
|
|
(org.result.child3 == null ? "" : org.result.child3 + "/") +
|
|
(org.result.child2 == null ? "" : org.result.child2 + "/") +
|
|
(org.result.child1 == null ? "" : org.result.child1 + "/") +
|
|
(org.result.root == null ? "" : org.result.root + "/");
|
|
placementRepatriation.OrganizationPositionOld = org.result.position + "-" + placementRepatriation.OrganizationOld;
|
|
}
|
|
await _context.PlacementRepatriations.AddAsync(placementRepatriation);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขส่งตัวกลับ
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> Put([FromBody] PlacementRepatriationEditRequest req, Guid id)
|
|
{
|
|
var uppdated = await _context.PlacementRepatriations
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.PlacementRepatriationNotFound, 404);
|
|
|
|
uppdated.PositionNumberOld = req.PositionNumberOld;
|
|
uppdated.DateRepatriation = req.DateRepatriation;
|
|
uppdated.OrganizationPositionOld = req.OrganizationPositionOld;
|
|
uppdated.PositionLevelOld = req.PositionLevelOld;
|
|
uppdated.PositionTypeOld = req.PositionTypeOld;
|
|
uppdated.AmountOld = req.AmountOld;
|
|
uppdated.Organization = req.Organization;
|
|
uppdated.Reason = req.Reason;
|
|
uppdated.Date = req.Date;
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// อนุมัติส่งตัวกลับ
|
|
/// </summary>
|
|
/// <param name="id">Id ส่งตัวกลับ</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("confirm/{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> AdminConfirm(Guid id)
|
|
{
|
|
var uppdated = await _context.PlacementRepatriations
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.PlacementRepatriationNotFound, 404);
|
|
|
|
uppdated.Status = "APPROVE";
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ลบส่งตัวกลับ
|
|
/// </summary>
|
|
/// <param name="id">Id ส่งตัวกลับ</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpDelete("{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> Delete(Guid id)
|
|
{
|
|
var deleted = await _context.PlacementRepatriations.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (deleted == null)
|
|
return NotFound();
|
|
_context.PlacementRepatriations.Remove(deleted);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// สั่งรายชื่อไปออกคำสั่ง
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost("report")]
|
|
public async Task<ActionResult<ResponseObject>> PostToReport([FromBody] PlacementProfileRequest req)
|
|
{
|
|
foreach (var item in req.Id)
|
|
{
|
|
var uppdated = await _context.PlacementRepatriations
|
|
.FirstOrDefaultAsync(x => x.Id == item);
|
|
if (uppdated == null)
|
|
continue;
|
|
uppdated.Status = "REPORT";
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
// /// <summary>
|
|
// /// วันที่ส่งตัวกลับ
|
|
// /// </summary>
|
|
// /// <param name="id">Id ส่งตัวกลับ</param>
|
|
// /// <returns></returns>
|
|
// /// <response code="200"></response>
|
|
// /// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
// /// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
// /// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
// [HttpPut("date/{id:length(36)}")]
|
|
// public async Task<ActionResult<ResponseObject>> UpdateDateRepatriation([FromBody] PlacementRepatriationDateRequest req, Guid id)
|
|
// {
|
|
// var uppdated = await _context.PlacementRepatriations
|
|
// .FirstOrDefaultAsync(x => x.Id == id);
|
|
// if (uppdated == null)
|
|
// return Error(GlobalMessages.PlacementRepatriationNotFound, 404);
|
|
// uppdated.DateRepatriation = req.Date;
|
|
// uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
// uppdated.LastUpdateUserId = UserId ?? "";
|
|
// uppdated.LastUpdatedAt = DateTime.Now;
|
|
|
|
// await _context.SaveChangesAsync();
|
|
|
|
// return Success();
|
|
// }
|
|
}
|
|
}
|