402 lines
22 KiB
C#
402 lines
22 KiB
C#
using BMA.EHR.Application.Repositories;
|
|
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 Swashbuckle.AspNetCore.Annotations;
|
|
using System.Security.Claims;
|
|
|
|
namespace BMA.EHR.Placement.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/placement/transfer")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("ระบบคำขอโอน")]
|
|
public class PlacementTransferController : BaseController
|
|
{
|
|
private readonly PlacementRepository _repository;
|
|
private readonly ApplicationDBContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public PlacementTransferController(PlacementRepository repository,
|
|
ApplicationDBContext context,
|
|
MinIOService documentService,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_repository = repository;
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
#region " Properties "
|
|
|
|
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
|
|
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
|
|
|
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// list รายการคำขอโอนของ User
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("user")]
|
|
public async Task<ActionResult<ResponseObject>> GetListByProfile()
|
|
{
|
|
var profile = await _context.Profiles
|
|
.FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId));
|
|
if (profile == null)
|
|
return Error(GlobalMessages.DataNotFound, 404);
|
|
|
|
var placementTransfers = await _context.PlacementTransfers.AsQueryable()
|
|
.Where(x => x.Profile == profile)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
position = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.Position == null ? null : p.Profile.Position.Name) : p.Profile.PositionEmployeePosition,
|
|
posNo = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.PosNo == null ? null : p.Profile.PosNo.Name) : p.Profile.PosNoEmployee,
|
|
positionLevel = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.PositionLevel == null ? null : p.Profile.PositionLevel.Name) : p.Profile.PositionEmployeeLevel,
|
|
// salary = p.Profile.Salaries.Count() == 0 ? null : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().PositionSalaryAmount,
|
|
p.CreatedAt,
|
|
p.Organization,
|
|
p.Reason,
|
|
p.Status,
|
|
p.Date,
|
|
salary = p.AmountOld,
|
|
PositionTypeOld = p.PositionTypeOld == null ? null : p.PositionTypeOld.Name,
|
|
PositionLevelOld = p.PositionLevelOld == null ? null : p.PositionLevelOld.Name,
|
|
PositionNumberOld = p.PositionNumberOld == null ? null : p.PositionNumberOld.Name,
|
|
PositionTypeOldId = p.PositionTypeOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionTypeOld.Id,
|
|
PositionLevelOldId = p.PositionLevelOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLevelOld.Id,
|
|
PositionNumberOldId = p.PositionNumberOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionNumberOld.Id,
|
|
OrganizationPositionOldId = p.OrganizationPositionOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.OrganizationPositionOld.Id,
|
|
p.IsActive,
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Success(placementTransfers);
|
|
}
|
|
|
|
/// <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 placementTransfers = await _context.PlacementTransfers.AsQueryable()
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
position = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.Position == null ? null : p.Profile.Position.Name) : p.Profile.PositionEmployeePosition,
|
|
posNo = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.PosNo == null ? null : p.Profile.PosNo.Name) : p.Profile.PosNoEmployee,
|
|
positionLevel = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.PositionLevel == null ? null : p.Profile.PositionLevel.Name) : p.Profile.PositionEmployeeLevel,
|
|
// salary = p.Profile.Salaries.Count() == 0 ? null : p.Profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().PositionSalaryAmount,
|
|
p.CreatedAt,
|
|
p.Organization,
|
|
p.Reason,
|
|
p.Status,
|
|
p.Date,
|
|
salary = p.AmountOld,
|
|
PositionTypeOld = p.PositionTypeOld == null ? null : p.PositionTypeOld.Name,
|
|
PositionLevelOld = p.PositionLevelOld == null ? null : p.PositionLevelOld.Name,
|
|
PositionNumberOld = p.PositionNumberOld == null ? null : p.PositionNumberOld.Name,
|
|
PositionTypeOldId = p.PositionTypeOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionTypeOld.Id,
|
|
PositionLevelOldId = p.PositionLevelOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLevelOld.Id,
|
|
PositionNumberOldId = p.PositionNumberOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionNumberOld.Id,
|
|
OrganizationPositionOldId = p.OrganizationPositionOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.OrganizationPositionOld.Id,
|
|
p.IsActive,
|
|
})
|
|
.ToListAsync();
|
|
if (PlacementAdmin == true)
|
|
placementTransfers.Where(x => x.Status.Trim().ToUpper().Contains("DONE"));
|
|
|
|
return Success(placementTransfers);
|
|
}
|
|
|
|
/// <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("user/{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetDetailByUser(Guid id)
|
|
{
|
|
var data = await _context.PlacementTransfers.AsQueryable()
|
|
.Where(x => x.Id == id)
|
|
.Where(x => x.Profile != null)
|
|
.Select(p => new
|
|
{
|
|
p.Id,
|
|
position = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.Position == null ? null : p.Profile.Position.Name) : p.Profile.PositionEmployeePosition,
|
|
posNo = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.PosNo == null ? null : p.Profile.PosNo.Name) : p.Profile.PosNoEmployee,
|
|
positionLevel = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.PositionLevel == null ? null : p.Profile.PositionLevel.Name) : p.Profile.PositionEmployeeLevel,
|
|
organizationOrganization = p.Profile.OrganizationOrganization,
|
|
p.Reason,
|
|
p.Status,
|
|
p.Organization,
|
|
p.Date,
|
|
salary = p.AmountOld,
|
|
p.CreatedAt,
|
|
PositionTypeOld = p.PositionTypeOld == null ? null : p.PositionTypeOld.Name,
|
|
PositionLevelOld = p.PositionLevelOld == null ? null : p.PositionLevelOld.Name,
|
|
PositionNumberOld = p.PositionNumberOld == null ? null : p.PositionNumberOld.Name,
|
|
PositionTypeOldId = p.PositionTypeOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionTypeOld.Id,
|
|
PositionLevelOldId = p.PositionLevelOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLevelOld.Id,
|
|
PositionNumberOldId = p.PositionNumberOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionNumberOld.Id,
|
|
OrganizationPositionOldId = p.OrganizationPositionOld == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.OrganizationPositionOld.Id,
|
|
PlacementTransferDocs = p.PlacementTransferDocs.Where(d => d.Document != null).Select(d => new { d.Document.Id, d.Document.FileName }),
|
|
})
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(GlobalMessages.DataNotFound, 404);
|
|
|
|
var placementTransferDocs = new List<dynamic>();
|
|
foreach (var doc in data.PlacementTransferDocs)
|
|
{
|
|
var _doc = new
|
|
{
|
|
FileName = doc.FileName,
|
|
PathName = await _documentService.ImagesPath(doc.Id)
|
|
};
|
|
placementTransferDocs.Add(_doc);
|
|
}
|
|
var _data = new
|
|
{
|
|
Id = data.Id,
|
|
Reason = data.Reason,
|
|
Status = data.Status,
|
|
Organization = data.Organization,
|
|
CreatedAt = data.CreatedAt,
|
|
Date = data.Date,
|
|
salary = data.salary,
|
|
PositionTypeOld = data.PositionTypeOld,
|
|
PositionLevelOld = data.PositionLevelOld,
|
|
PositionNumberOld = data.PositionNumberOld,
|
|
PositionTypeOldId = data.PositionTypeOldId,
|
|
PositionLevelOldId = data.PositionLevelOldId,
|
|
PositionNumberOldId = data.PositionNumberOldId,
|
|
OrganizationPositionOldId = data.OrganizationPositionOldId,
|
|
Docs = placementTransferDocs,
|
|
};
|
|
|
|
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] PlacementTransferRequest req)
|
|
{
|
|
var profile = await _context.Profiles
|
|
.Include(x => x.PositionLevel)
|
|
.Include(x => x.PositionType)
|
|
.Include(x => x.PosNo)
|
|
.Include(x => x.Salaries)
|
|
.FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId));
|
|
if (profile == null)
|
|
return Error(GlobalMessages.DataNotFound, 404);
|
|
|
|
var placementTransfer = new PlacementTransfer
|
|
{
|
|
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().PositionSalaryAmount,
|
|
PositionLevelOld = profile.PositionLevel,
|
|
PositionTypeOld = profile.PositionType,
|
|
PositionNumberOld = profile.PosNo,
|
|
OrganizationPositionOld = await _context.OrganizationPositions.FirstOrDefaultAsync(x => x.PositionNumber == profile.PosNo),
|
|
Status = "PENDING",
|
|
CreatedUserId = FullName ?? "",
|
|
CreatedFullName = UserId ?? "System Administrator",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.PlacementTransfers.AddAsync(placementTransfer);
|
|
await _context.SaveChangesAsync();
|
|
if (Request.Form.Files != null && Request.Form.Files.Count != 0)
|
|
{
|
|
foreach (var file in Request.Form.Files)
|
|
{
|
|
var fileExtension = Path.GetExtension(file.FileName);
|
|
|
|
var doc = await _documentService.UploadFileAsync(file, file.FileName);
|
|
var _doc = await _context.Documents.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == doc.Id);
|
|
if (_doc != null)
|
|
{
|
|
var placementTransferDoc = new PlacementTransferDoc
|
|
{
|
|
PlacementTransfer = placementTransfer,
|
|
Document = _doc,
|
|
CreatedUserId = FullName ?? "",
|
|
CreatedFullName = UserId ?? "System Administrator",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.PlacementTransferDocs.AddAsync(placementTransferDoc);
|
|
|
|
}
|
|
}
|
|
}
|
|
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] PlacementTransferEditRequest req, Guid id)
|
|
{
|
|
var uppdated = await _context.PlacementTransfers
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.PlacementTransferNotFound, 404);
|
|
|
|
if (req.PosNoOld != null)
|
|
{
|
|
var save_posNo = await _context.PositionNumbers.FindAsync(req.PosNoOld);
|
|
if (save_posNo == null)
|
|
return Error(GlobalMessages.PositionPosNoNotFound, 404);
|
|
uppdated.PositionNumberOld = save_posNo;
|
|
|
|
var save_orgPosition = await _context.OrganizationPositions.FirstOrDefaultAsync(x => x.PositionNumber == save_posNo);
|
|
if (save_orgPosition == null)
|
|
return Error(GlobalMessages.PositionPosNoNotFound, 404);
|
|
uppdated.OrganizationPositionOld = save_orgPosition;
|
|
}
|
|
if (req.PositionLevelOld != null)
|
|
{
|
|
var save = await _context.PositionLevels.FindAsync(req.PositionLevelOld);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionLevelNotFound, 404);
|
|
uppdated.PositionLevelOld = save;
|
|
}
|
|
if (req.PositionTypeOld != null)
|
|
{
|
|
var save = await _context.PositionTypes.FindAsync(req.PositionTypeOld);
|
|
if (save == null)
|
|
return Error(GlobalMessages.PositionTypeNotFound, 404);
|
|
uppdated.PositionTypeOld = save;
|
|
}
|
|
|
|
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.PlacementTransfers
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.PlacementTransferNotFound, 404);
|
|
|
|
uppdated.Status = "DONE";
|
|
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.PlacementTransfers.AsQueryable()
|
|
.Include(x => x.PlacementTransferDocs)
|
|
.ThenInclude(x => x.Document)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (deleted == null)
|
|
return NotFound();
|
|
var placementTransferDocs = new List<dynamic>();
|
|
foreach (var doc in deleted.PlacementTransferDocs)
|
|
{
|
|
if (doc.Document != null)
|
|
placementTransferDocs.Add(doc.Document.Id);
|
|
}
|
|
_context.PlacementTransferDocs.RemoveRange(deleted.PlacementTransferDocs);
|
|
await _context.SaveChangesAsync();
|
|
_context.PlacementTransfers.Remove(deleted);
|
|
foreach (var doc in placementTransferDocs)
|
|
{
|
|
if (doc != null)
|
|
await _documentService.DeleteFileAsync(doc);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
}
|
|
}
|