using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; 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 NotificationRepository _repositoryNoti; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; public PlacementTransferController(PlacementRepository 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; 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) .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 == 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), // 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, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.IsActive, }) .ToListAsync(); return Success(placementTransfers); } /// /// list รายการคำขอโอนของ Admin /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet()] public async Task> GetListByAdmin() { var placementTransfers = await _context.PlacementTransfers.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), // 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, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.IsActive, }) .ToListAsync(); if (PlacementAdmin == true) placementTransfers = placementTransfers.Where(x => !x.Status.Trim().ToUpper().Contains("WAITTING")).ToList(); 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 { 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, 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(); foreach (var doc in data.PlacementTransferDocs) { var _doc = new { FileName = doc.FileName, PathName = await _documentService.ImagesPath(doc.Id) }; placementTransferDocs.Add(_doc); } var _data = new { data.Id, data.ProfileId, data.PrefixId, data.Prefix, data.FirstName, data.LastName, data.position, data.posNo, data.positionLevel, data.organizationOrganization, data.Reason, data.Status, data.Organization, data.CreatedAt, data.Date, data.salary, data.PositionTypeOld, data.PositionLevelOld, data.PositionNumberOld, data.OrganizationPositionOld, Avatar = data.Avatar == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.Avatar), Docs = placementTransferDocs, }; return Success(_data); } /// /// get รายละเอียดคำขอโอนเจ้าหน้าที่ /// /// Id คำขอโอน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{id:length(36)}")] public async Task> GetDetailAdmin(Guid id) { var data = await _context.PlacementTransfers.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, AvatarId = p.Profile.Avatar == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Avatar.Id, 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, 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(); foreach (var doc in data.PlacementTransferDocs) { var _doc = new { FileName = doc.FileName, PathName = await _documentService.ImagesPath(doc.Id) }; placementTransferDocs.Add(_doc); } var _data = new { data.Id, data.ProfileId, AvataPath = data.AvatarId == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.AvatarId), data.PrefixId, data.Prefix, data.FirstName, data.LastName, data.position, data.posNo, data.positionLevel, data.organizationOrganization, data.Reason, data.Status, data.Organization, data.CreatedAt, data.Date, data.salary, data.PositionTypeOld, data.PositionLevelOld, data.PositionNumberOld, data.OrganizationPositionOld, Docs = placementTransferDocs, }; return Success(_data); } /// /// สร้างคำขอโอน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> 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) .Include(x => x.Position) .Include(x => x.Prefix) .FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId)); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); var placementTransfer = new PlacementTransfer { Profile = profile, Organization = req.Organization, Reason = req.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.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, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.PlacementTransferDocs.AddAsync(placementTransferDoc); } } } await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-6268-4e2c-80a3-aca65eedc6d0"), $"{profile.Prefix?.Name}{profile.FirstName} {profile.LastName} ได้ทำการยื่นคำขอโอน", $"{profile.Prefix?.Name}{profile.FirstName} {profile.LastName} ได้ทำการยื่นคำขอโอนไปยัง {req.Organization}", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขคำขอโอน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("{id:length(36)}")] public async Task> Put([FromBody] PlacementTransferEditRequest req, Guid id) { var uppdated = await _context.PlacementTransfers .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.PlacementTransferNotFound, 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.PlacementTransfers .Include(x => x.Profile) .ThenInclude(x => x.Prefix) .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.PlacementTransferNotFound, 404); uppdated.Status = "APPROVE"; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-6268-4e2c-80a3-aca65eedc6d0"), $"{uppdated.Profile.Prefix?.Name}{uppdated.Profile.FirstName} {uppdated.Profile.LastName} ได้ทำการยื่นคำขอโอนได้รับการอนุมัติ", $"{uppdated.Profile.Prefix?.Name}{uppdated.Profile.FirstName} {uppdated.Profile.LastName} ได้ทำการยื่นคำขอโอนไปยัง {uppdated.Organization}ได้รับการอนุมัติ", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), $"{uppdated.Profile.Prefix?.Name}{uppdated.Profile.FirstName} {uppdated.Profile.LastName} ได้ทำการยื่นคำขอโอน", $"{uppdated.Profile.Prefix?.Name}{uppdated.Profile.FirstName} {uppdated.Profile.LastName} ได้ทำการยื่นคำขอโอนไปยัง {uppdated.Organization}ได้รับการอนุมัติ", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// ลบคำขอโอน /// /// Id คำขอโอน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("{id:length(36)}")] public async Task> 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(); 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(); } /// /// สั่งรายชื่อไปออกคำสั่ง /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("report")] public async Task> PostToReport([FromBody] PlacementProfileRequest req) { foreach (var item in req.Id) { var uppdated = await _context.PlacementTransfers .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(); } } }