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.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; using System.Security.Cryptography; 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 /// /// 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 placementTransfers = await _context.PlacementTransfers.AsQueryable() .Where(x => x.Profile == profile) .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 = 10000, p.CreatedAt, p.Organization, p.Reason, p.Status, p.IsActive, }) .ToListAsync(); return Success(placementTransfers); } /// /// list คำขอโอนของ Admin /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet()] public async Task> GetListByAdmin() { var placementTransfers = await _context.PlacementTransfers.AsQueryable() .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 = 10000, p.CreatedAt, p.Organization, p.Reason, p.Status, p.IsActive, }) .ToListAsync(); return Success(placementTransfers); } /// /// get รายละเอียดคำขอโอน /// /// Id คำขอโอน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("user/{id:length(36)}")] public async Task> GetDetailByUser(Guid id) { var data = await _context.PlacementTransfers.AsQueryable() .Where(x => x.Id == id) .Where(x => x.Profile != null) .Select(p => new { Id = 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, Reason = p.Reason, Status = p.Status, Organization = p.Organization, CreatedAt = p.CreatedAt, // doc = p.ReliefDoc == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.ReliefDoc.Id, }) .FirstOrDefaultAsync(); if (data == null) return Error(GlobalMessages.DataNotFound, 404) var _data = new { Id = data.Id, Reason = data.Reason, Status = data.Status, Organization = data.Organization, CreatedAt = data.CreatedAt, // period_doc = data.period_doc == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.period_doc), }; return Success(_data); } /// /// สร้างคำขอโอน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> Post([FromForm] PlacementTransferRequest req) { var profile = await _context.Profiles .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"] : "", 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 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(); } /// /// แก้ไขคำขอโอน /// /// Id คำขอโอน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("confirm/{id:length(36)}")] public async Task> Confirm(Guid id) { if (req == null) return BadRequest(); var uppdated = await _context.InsigniaPeriods.AsQueryable() .Include(x => x.ReliefDoc) .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return NotFound(); uppdated.Name = Request.Form.ContainsKey("Name") ? Request.Form["Name"] : ""; uppdated.Year = Request.Form.ContainsKey("Year") ? Int32.Parse(Request.Form["Year"]) : DateTime.Now.Year; uppdated.StartDate = Request.Form.ContainsKey("StartDate") ? DateTime.Parse(Request.Form["StartDate"]) : DateTime.Now; uppdated.EndDate = Request.Form.ContainsKey("EndDate") ? DateTime.Parse(Request.Form["EndDate"]) : DateTime.Now; uppdated.Amount = Request.Form.ContainsKey("Amount") ? Request.Form["Amount"] : ""; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; if (Request.Form.Files != null && Request.Form.Files.Count != 0) { if (uppdated.ReliefDoc != null) await _documentService.DeleteFileAsync(uppdated.ReliefDoc.Id); var file = Request.Form.Files[0]; var fileExtension = Path.GetExtension(file.FileName); var doc = await _documentService.UploadFileAsync(file, file.FileName); uppdated.ReliefDoc = doc; } await _context.SaveChangesAsync(); return Success(); } } }