using BMA.EHR.Application.Repositories; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.MetaData; 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 Swashbuckle.AspNetCore.Annotations; using System.Security.Claims; using System.Security.Cryptography; namespace BMA.EHR.Retirement.Service.Controllers { [Route("api/v{version:apiVersion}/retirement/resign")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("ระบบลาออก")] public class RetirementResignController : BaseController { private readonly RetirementRepository _repository; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; public RetirementResignController(RetirementRepository 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; #endregion /// /// list รายการลาออกของ User /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("user")] public async Task> GetListByProfile() { var profile = await _context.Profiles .FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId)); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); var retirementResigns = await _context.RetirementResigns.AsQueryable() .Where(x => x.Profile == profile) .OrderByDescending(x => x.CreatedAt) .Select(p => new { p.Id, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.Status, p.IsActive, }) .ToListAsync(); return Success(retirementResigns); } /// /// list รายการลาออกของ Admin /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet()] public async Task> GetListByAdmin() { var retirementResigns = await _context.RetirementResigns.AsQueryable() .OrderByDescending(x => x.CreatedAt) .Select(p => new { p.Id, Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name, p.Profile.FirstName, p.Profile.LastName, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.Status, PositionType = p.PositionType == null ? null : p.PositionType.Name, PositionLine = p.PositionLine == null ? null : p.PositionLine.Name, PositionLevel = p.PositionLevel == null ? null : p.PositionLevel.Name, PositionExecutive = p.PositionExecutive == null ? null : p.PositionExecutive.Name, OrganizationOrganization = p.OrganizationOrganization == null ? null : p.OrganizationOrganization.Name, p.IsActive, p.CreatedAt, }) .ToListAsync(); return Success(retirementResigns); } /// /// get รายละเอียดลาออก /// /// Id ลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{id:length(36)}")] public async Task> GetDetailByUser(Guid id) { var data = await _context.RetirementResigns.AsQueryable() .Where(x => x.Id == id) .Select(p => new { p.Id, Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name, PrefixId = p.Profile.Prefix == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Prefix.Id, p.Profile.FirstName, p.Profile.LastName, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.Status, PositionType = p.PositionType == null ? null : p.PositionType.Name, PositionTypeId = p.PositionType == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionType.Id, PositionLine = p.PositionLine == null ? null : p.PositionLine.Name, PositionLineId = p.PositionLine == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLine.Id, PositionLevel = p.PositionLevel == null ? null : p.PositionLevel.Name, PositionLevelId = p.PositionLevel == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionLevel.Id, PositionExecutive = p.PositionExecutive == null ? null : p.PositionExecutive.Name, PositionExecutiveId = p.PositionExecutive == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.PositionExecutive.Id, OrganizationOrganization = p.OrganizationOrganization == null ? null : p.OrganizationOrganization.Name, OrganizationOrganizationId = p.OrganizationOrganization == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.OrganizationOrganization.Id, p.IsActive, p.CreatedAt, }) .FirstOrDefaultAsync(); if (data == null) return Error(GlobalMessages.RetirementResignNotFound, 404); return Success(data); } /// /// สร้างการลาออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> Post([FromForm] RetirementResignRequest req) { var profile = await _context.Profiles .Include(x => x.PositionType) .Include(x => x.PositionLevel) .FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId)); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); var retirementResign = new RetirementResign { Profile = profile, Location = req.Location, SendDate = req.SendDate, ActiveDate = req.ActiveDate, Reason = req.Reason, PositionType = profile.PositionType, PositionLine = await _context.PositionLines .FirstOrDefaultAsync(x => x.Id == profile.PositionLineId), PositionLevel = profile.PositionLevel, PositionExecutive = await _context.PositionExecutives .FirstOrDefaultAsync(x => x.Id == profile.PositionExecutiveId), OrganizationOrganization = await _context.OrganizationOrganizations .FirstOrDefaultAsync(x => x.Id == profile.OrganizationOrganizationId), Status = "PENDING", IsActive = true, CreatedUserId = FullName ?? "", CreatedFullName = UserId ?? "System Administrator", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementResigns.AddAsync(retirementResign); await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขการลาออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("{id:length(36)}")] public async Task> Put([FromForm] RetirementResignRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Location = req.Location; updated.SendDate = req.SendDate; updated.ActiveDate = req.ActiveDate; updated.Reason = req.Reason; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// ลบลาออก /// /// Id ลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("{id:length(36)}")] public async Task> Delete(Guid id) { var deleted = await _context.RetirementResigns.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (deleted == null) return Error(GlobalMessages.RetirementResignNotFound, 404); _context.RetirementResigns.Remove(deleted); await _context.SaveChangesAsync(); return Success(); } /// /// อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("confirm/{id:length(36)}")] public async Task> AdminConfirm(Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Status = "APPROVE"; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// ไม่อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("reject/{id:length(36)}")] public async Task> AdminReject(Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Status = "REJECT"; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } } }