hrms-api-backend/BMA.EHR.Retirement.Service/Controllers/RetirementOutController.cs

420 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.Retirement;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Retirement.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.Retirement.Service.Controllers
{
[Route("api/v{version:apiVersion}/retirement/out")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("ระบบให้ออก")]
public class RetirementOutController : BaseController
{
private readonly RetirementRepository _repository;
private readonly NotificationRepository _repositoryNoti;
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
public RetirementOutController(RetirementRepository 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? RetirementAdmin => _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 retirementOuts = await _context.RetirementOuts.AsQueryable()
.OrderByDescending(x => x.CreatedAt)
.Where(x => RetirementAdmin == 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,
})
.ToListAsync();
return Success(retirementOuts);
}
}
/// <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.RetirementOuts.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.OrganizationOld,
// Avatar = p.Profile.Avatar == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Avatar.Id,
})
.FirstOrDefaultAsync();
if (data == null)
return Error(GlobalMessages.DataNotFound, 404);
// var _data = new
// {
// data.Id,
// data.PrefixId,
// data.Prefix,
// data.FirstName,
// data.LastName,
// data.ProfileId,
// data.position,
// data.posNo,
// data.positionLevel,
// data.organizationOrganization,
// data.Reason,
// data.Status,
// data.Organization,
// data.Date,
// data.salary,
// data.CreatedAt,
// data.PositionTypeOld,
// data.PositionLevelOld,
// data.PositionNumberOld,
// data.OrganizationPositionOld,
// Avatar = data.Avatar == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.Avatar),
// };
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] RetirementAddProfileRequest req)
{
// var profile = await _context.Profiles
// .Include(x => x.PositionLevel)
// .Include(x => x.PositionType)
// .Include(x => x.PosNo)
// .Include(x => x.Salaries)
// .Include(x => x.Position)
// .FirstOrDefaultAsync(x => x.Id == req.Id);
// if (profile == null)
// return Error(GlobalMessages.DataNotFound, 404);
var retirementOut = new RetirementOut
{
// Profile = profile,
// Organization = Request.Form.ContainsKey("Organization") ? Request.Form["Organization"] : "",
// Reason = Request.Form.ContainsKey("Reason") ? Request.Form["Reason"] : "",
// // Date = req.Date,
// AmountOld = profile.Salaries.Count() == 0 ? null : profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount,
// PositionLevelOld = profile.PositionLevel == null ? null : profile.PositionLevel.Name,
// PositionTypeOld = profile.PositionType == null ? null : profile.PositionType.Name,
// PositionNumberOld = profile.PosNo == null ? null : profile.PosNo.Name,
// OrganizationPositionOld = profile.Position == null ? profile.Oc : $"{profile.Position.Name}-{profile.Oc}",
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);
retirementOut.profileId = org.result.profileId;
retirementOut.prefix = org.result.prefix;
retirementOut.firstName = org.result.firstName;
retirementOut.lastName = org.result.lastName;
retirementOut.citizenId = org.result.citizenId;
retirementOut.root = org.result.root;
retirementOut.rootId = org.result.rootId;
retirementOut.rootShortName = org.result.rootShortName;
retirementOut.child1 = org.result.child1;
retirementOut.child1Id = org.result.child1Id;
retirementOut.child1ShortName = org.result.child1ShortName;
retirementOut.child2 = org.result.child2;
retirementOut.child2Id = org.result.child2Id;
retirementOut.child2ShortName = org.result.child2ShortName;
retirementOut.child3 = org.result.child3;
retirementOut.child3Id = org.result.child3Id;
retirementOut.child3ShortName = org.result.child3ShortName;
retirementOut.child4 = org.result.child4;
retirementOut.child4Id = org.result.child4Id;
retirementOut.child4ShortName = org.result.child4ShortName;
retirementOut.posMasterNo = org.result.posMasterNo;
retirementOut.position = org.result.position;
retirementOut.posTypeId = org.result.posTypeId;
retirementOut.posTypeName = org.result.posTypeName;
retirementOut.posLevelId = org.result.posLevelId;
retirementOut.posLevelName = org.result.posLevelName;
retirementOut.PositionOld = org.result.position;
retirementOut.PositionLevelOld = org.result.posLevelName;
retirementOut.PositionTypeOld = org.result.posTypeName;
retirementOut.PositionNumberOld = org.result.nodeShortName + org.result.posMasterNo;
retirementOut.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 + "/");
retirementOut.OrganizationPositionOld = org.result.position + "-" + retirementOut.OrganizationOld;
}
await _context.RetirementOuts.AddAsync(retirementOut);
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] RetirementOutEditRequest req, Guid id)
{
var uppdated = await _context.RetirementOuts
.FirstOrDefaultAsync(x => x.Id == id);
if (uppdated == null)
return Error(GlobalMessages.RetirementOutNotFound, 404);
uppdated.PositionNumberOld = req.PositionNumberOld;
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.RetirementOuts
.FirstOrDefaultAsync(x => x.Id == id);
if (uppdated == null)
return Error(GlobalMessages.RetirementOutNotFound, 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.RetirementOuts.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == id);
if (deleted == null)
return NotFound();
_context.RetirementOuts.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] RetirementProfileRequest req)
{
foreach (var item in req.Id)
{
var uppdated = await _context.RetirementOuts
.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();
}
}
}