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 Swashbuckle.AspNetCore.Annotations; using System.Security.Claims; namespace BMA.EHR.Retirement.Service.Controllers { [Route("api/v{version:apiVersion}/retirement/expulsion")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("ระบบไล่ออก")] public class RetirementExpulsionController : BaseController { private readonly RetirementRepository _repository; private readonly NotificationRepository _repositoryNoti; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; public RetirementExpulsionController(RetirementRepository repository, NotificationRepository repositoryNoti, ApplicationDBContext context, MinIOService documentService, IHttpContextAccessor httpContextAccessor) { _repository = repository; _repositoryNoti = repositoryNoti; _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 รายการไล่ออกของ Admin /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet()] public async Task> GetListByAdmin() { var retirementExpulsions = await _context.RetirementExpulsions.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, position = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.Position == null ? null : p.Profile.Position.Name) : (p.Profile.PositionEmployeePosition == null ? null : p.Profile.PositionEmployeePosition.Name), 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 == null ? null : p.Profile.PositionEmployeeLevel.Name), 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(retirementExpulsions); } /// /// get รายละเอียดไล่ออกเจ้าหน้าที่ /// /// Id ไล่ออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{id:length(36)}")] public async Task> GetDetailAdmin(Guid id) { var data = await _context.RetirementExpulsions.AsQueryable() .Where(x => x.Id == id) .Where(x => x.Profile != null) .Select(p => new { p.Id, PrefixId = p.Profile.Prefix == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Prefix.Id, Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name, p.Profile.FirstName, p.Profile.LastName, ProfileId = p.Profile.Id, position = p.Profile.ProfileType.Trim().ToUpper().Contains("OFFICER") ? (p.Profile.Position == null ? null : p.Profile.Position.Name) : (p.Profile.PositionEmployeePosition == null ? null : p.Profile.PositionEmployeePosition.Name), 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 == null ? null : p.Profile.PositionEmployeeLevel.Name), organizationOrganization = p.Profile.OrganizationOrganization, p.Reason, p.Status, p.Organization, p.Date, salary = p.AmountOld, p.CreatedAt, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, 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); } /// /// สร้างไล่ออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> 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 retirementExpulsion = new RetirementExpulsion { 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, }; await _context.RetirementExpulsions.AddAsync(retirementExpulsion); await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขไล่ออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("{id:length(36)}")] public async Task> Put([FromBody] RetirementExpulsionEditRequest req, Guid id) { var uppdated = await _context.RetirementExpulsions .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.RetirementExpulsionNotFound, 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(); } /// /// อนุมัติไล่ออก /// /// Id ไล่ออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("confirm/{id:length(36)}")] public async Task> AdminConfirm(Guid id) { var uppdated = await _context.RetirementExpulsions .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.RetirementExpulsionNotFound, 404); uppdated.Status = "APPROVE"; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// ลบไล่ออก /// /// Id ไล่ออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("{id:length(36)}")] public async Task> Delete(Guid id) { var deleted = await _context.RetirementExpulsions.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (deleted == null) return NotFound(); _context.RetirementExpulsions.Remove(deleted); await _context.SaveChangesAsync(); return Success(); } /// /// สั่งรายชื่อไปออกคำสั่ง /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("report")] public async Task> PostToReport([FromBody] RetirementProfileRequest req) { foreach (var item in req.Id) { var uppdated = await _context.RetirementExpulsions .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(); } } }