using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; 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 Newtonsoft.Json; using Newtonsoft.Json.Linq; using Swashbuckle.AspNetCore.Annotations; using System.Net.Http.Headers; using System.Security.Claims; namespace BMA.EHR.Placement.Service.Controllers { [Route("api/v{version:apiVersion}/placement/appointment")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("ระบบแต่งตั้ง-เลื่อน")] public class PlacementAppointmentController : BaseController { private readonly PlacementRepository _repository; private readonly NotificationRepository _repositoryNoti; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IConfiguration _configuration; private readonly PermissionRepository _permission; public PlacementAppointmentController(PlacementRepository repository, NotificationRepository repositoryNoti, ApplicationDBContext context, MinIOService documentService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration, PermissionRepository permiss) { _repository = repository; _repositoryNoti = repositoryNoti; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; _configuration = configuration; _permission = permiss; } #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; private string? token => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"]; //private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.Claims?.Any(claim => new[] { "placement", "placement1", "placement2" }.Contains(claim.Value)); #endregion /// /// list รายการแต่งตั้ง-เลื่อน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet()] // public async Task> GetListByAdmin(int page = 1, int pageSize = 10, string keyword = "") public async Task> GetListByAdmin() { var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_PROMOTION_OFFICER"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var placementAppointments = await _context.PlacementAppointments.AsQueryable() .Where(x => x.type == "OFFICER") .OrderByDescending(x => x.CreatedAt) .Select(p => new { p.Id, p.profileId, p.prefix, p.firstName, p.lastName, p.citizenId, p.DateOfBirth, p.root, p.rootId, p.rootShortName, p.child1, p.child1Id, p.child1ShortName, p.child2, p.child2Id, p.child2ShortName, p.child3, p.child3Id, p.child3ShortName, p.child4, p.child4Id, p.child4ShortName, p.orgRevisionId, p.positionId, p.posMasterNo, p.position, p.positionField, p.posTypeId, p.posTypeName, p.posLevelId, p.posLevelName, node = p.root == null ? (int?)null : (p.child1 == null ? 0 : (p.child2 == null ? 1 : (p.child3 == null ? 2 : (p.child4 == null ? 3 : 4)))), nodeName = p.root == null ? null : (p.child1 == null ? p.root : (p.child2 == null ? p.child1 : (p.child3 == null ? p.child2 : (p.child4 == null ? p.child3 : p.child4)))), nodeId = p.rootId == null ? null : (p.child1Id == null ? p.rootId : (p.child2Id == null ? p.child1Id : (p.child3Id == null ? p.child2Id : (p.child4Id == null ? p.child3Id : p.child4Id)))), nodeShortName = p.rootShortName == null ? null : (p.child1ShortName == null ? p.rootShortName : (p.child2ShortName == null ? p.child1ShortName : (p.child3ShortName == null ? p.child2ShortName : (p.child4ShortName == null ? p.child3ShortName : p.child4ShortName)))), p.rootOld, p.rootOldId, p.rootShortNameOld, p.child1Old, p.child1OldId, p.child1ShortNameOld, p.child2Old, p.child2OldId, p.child2ShortNameOld, p.child3Old, p.child3OldId, p.child3ShortNameOld, p.child4Old, p.child4OldId, p.child4ShortNameOld, p.posMasterNoOld, p.posTypeOldId, p.posTypeNameOld, p.posLevelOldId, p.posLevelNameOld, p.Status, p.Amount, // p.RecruitDate, p.ReportingDate, p.IsActive, p.PositionDate, p.Reason, p.EducationOld, salary = p.AmountOld, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.AmountOld, p.OrganizationOld, p.posmasterId, p.positionOld, p.CreatedAt, p.typeCommand, }) .ToListAsync(); // if (PlacementAdmin == true) // placementAppointments.Where(x => x.Status.Trim().ToUpper().Contains("PENDING")); // if (keyword != "") // { // var data = placementAppointments.Where(x => // (x.citizenId != null && x.citizenId.Contains(keyword)) || // (x.prefix != null && x.prefix.Contains(keyword)) || // (x.firstName != null && x.firstName.Contains(keyword)) || // (x.lastName != null && x.lastName.Contains(keyword)) || // (x.rootShortNameOld != null && x.rootShortNameOld.Contains(keyword)) || // (x.posMasterNoOld != null && x.posMasterNoOld.ToString().Contains(keyword)) || // (x.OrganizationOld != null && x.OrganizationOld.Contains(keyword)) || // (x.posTypeNameOld != null && x.posTypeNameOld.Contains(keyword)) || // (x.posLevelNameOld != null && x.posLevelNameOld.Contains(keyword)) || // (x.OrganizationPositionOld != null && x.OrganizationPositionOld.Contains(keyword)) || // (x.Reason != null && x.Reason.Contains(keyword)) || // (x.nodeName != null && x.nodeName.Contains(keyword))) // .OrderByDescending(x => x.CreatedAt) // .Skip((page - 1) * pageSize) // .Take(pageSize) // .ToList(); // placementAppointments = data; // } return Success(placementAppointments); } /// /// get รายละเอียดแต่งตั้ง-เลื่อน /// /// Id แต่งตั้ง-เลื่อน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{id:length(36)}")] public async Task> GetDetailByUser(Guid id) { var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_PROMOTION_OFFICER"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var data = await _context.PlacementAppointments.AsQueryable() .Where(x => x.type == "OFFICER") .Where(x => x.Id == id) .Select(p => new { p.Id, p.profileId, p.DateOfBirth, p.prefix, p.firstName, p.lastName, p.root, p.rootId, p.rootShortName, p.child1, p.child1Id, p.child1ShortName, p.child2, p.child2Id, p.child2ShortName, p.child3, p.child3Id, p.child3ShortName, p.child4, p.child4Id, p.child4ShortName, p.orgRevisionId, p.positionId, p.posMasterNo, p.position, p.positionField, p.posTypeId, p.posTypeName, p.posLevelId, p.posLevelName, p.rootOld, p.rootOldId, p.rootShortNameOld, p.child1Old, p.child1OldId, p.child1ShortNameOld, p.child2Old, p.child2OldId, p.child2ShortNameOld, p.child3Old, p.child3OldId, p.child3ShortNameOld, p.child4Old, p.child4OldId, p.child4ShortNameOld, p.posMasterNoOld, p.posTypeOldId, p.posTypeNameOld, p.posLevelOldId, p.posLevelNameOld, p.Status, p.Amount, p.ReportingDate, p.CreatedAt, p.Reason, p.EducationOld, salary = p.AmountOld, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.PositionDate, p.AmountOld, p.OrganizationOld, p.positionOld, // PlacementAppointmentDocs = p.PlacementAppointmentDocs.Where(d => d.Document != null).Select(d => new { d.Document.Id, d.Document.FileName }), p.typeCommand, p.posmasterId, }) .FirstOrDefaultAsync(); if (data == null) return Error(GlobalMessages.DataNotFound, 404); // var placementAppointmentDocs = new List(); // foreach (var doc in data.PlacementAppointmentDocs) // { // var _doc = new // { // doc.FileName, // PathName = await _documentService.ImagesPath(doc.Id) // }; // placementAppointmentDocs.Add(_doc); // } var _data = new { data.Id, data.profileId, data.prefix, data.firstName, data.lastName, data.DateOfBirth, data.root, data.rootShortName, data.child1, data.child1Id, data.child1ShortName, data.child2, data.child2Id, data.child2ShortName, data.child3, data.child3Id, data.child3ShortName, data.child4, data.child4Id, data.child4ShortName, node = data.root == null ? (int?)null : (data.child1 == null ? 0 : (data.child2 == null ? 1 : (data.child3 == null ? 2 : (data.child4 == null ? 3 : 4)))), nodeName = data.root == null ? null : (data.child1 == null ? data.root : (data.child2 == null ? data.child1 : (data.child3 == null ? data.child2 : (data.child4 == null ? data.child3 : data.child4)))), nodeId = data.rootId == null ? null : (data.child1Id == null ? data.rootId : (data.child2Id == null ? data.child1Id : (data.child3Id == null ? data.child2Id : (data.child4Id == null ? data.child3Id : data.child4Id)))), nodeShortName = data.rootShortName == null ? null : (data.child1ShortName == null ? data.rootShortName : (data.child2ShortName == null ? data.child1ShortName : (data.child3ShortName == null ? data.child2ShortName : (data.child4ShortName == null ? data.child3ShortName : data.child4ShortName)))), data.orgRevisionId, data.positionId, data.posMasterNo, data.position, data.positionField, data.posTypeId, data.posTypeName, data.posLevelId, data.posLevelName, data.rootOld, data.rootOldId, data.rootShortNameOld, data.child1Old, data.child1OldId, data.child1ShortNameOld, data.child2Old, data.child2OldId, data.child2ShortNameOld, data.child3Old, data.child3OldId, data.child3ShortNameOld, data.child4Old, data.child4OldId, data.child4ShortNameOld, nodeOld = data.rootOld == null ? (int?)null : (data.child1Old == null ? 0 : (data.child2Old == null ? 1 : (data.child3Old == null ? 2 : (data.child4Old == null ? 3 : 4)))), nodeNameOld = data.rootOld == null ? null : (data.child1Old == null ? data.root : (data.child2Old == null ? data.child1Old : (data.child3Old == null ? data.child2Old : (data.child4Old == null ? data.child3Old : data.child4Old)))), nodeOldId = data.rootOldId == null ? null : (data.child1OldId == null ? data.rootOldId : (data.child2OldId == null ? data.child1OldId : (data.child3OldId == null ? data.child2OldId : (data.child4OldId == null ? data.child3OldId : data.child4OldId)))), nodeShortNameOld = data.rootShortNameOld == null ? null : (data.child1ShortNameOld == null ? data.rootShortNameOld : (data.child2ShortNameOld == null ? data.child1ShortNameOld : (data.child3ShortNameOld == null ? data.child2ShortNameOld : (data.child4ShortNameOld == null ? data.child3ShortNameOld : data.child4ShortNameOld)))), data.posMasterNoOld, data.posTypeOldId, data.posTypeNameOld, data.posLevelOldId, data.posLevelNameOld, data.Status, data.Amount, data.ReportingDate, data.CreatedAt, data.Reason, data.EducationOld, data.salary, data.PositionTypeOld, data.PositionLevelOld, data.PositionNumberOld, data.OrganizationPositionOld, data.PositionDate, data.AmountOld, data.OrganizationOld, data.positionOld, // Docs = placementAppointmentDocs, data.typeCommand, data.posmasterId, }; return Success(_data); } /// /// สร้างแต่งตั้ง-เลื่อน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> Post([FromForm] PlacementAddProfileRequest req) { var placementAppointment = new PlacementAppointment { // Profile = profile, // CitizenId = profile.CitizenId, // Prefix = profile.Prefix, // Firstname = profile.FirstName, // Lastname = profile.LastName, // DateOfBirth = profile.BirthDate, // Gender = profile.Gender, // Nationality = profile.Nationality, // Race = profile.Race, // Religion = await _context.Religions.FindAsync(profile.ReligionId), // BloodGroup = await _context.BloodGroups.FindAsync(profile.BloodGroupId), // Relationship = await _context.Relationships.FindAsync(profile.RelationshipId), // TelephoneNumber = profile.TelephoneNumber, // EducationOld = profile.Educations.Count() == 0 ? null : $"{profile.Educations.OrderByDescending(x => x.FinishDate).FirstOrDefault().Degree}-{profile.Educations.OrderByDescending(x => x.FinishDate).FirstOrDefault().Field}", // 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}", // PositionDate = profile.Salaries.Count() == 0 ? null : profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Date, type = "OFFICER", Status = "WAITTING", CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; var apiUrl = $"{_configuration["API"]}/org/profile/profileid/position/{req.Id}"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl); var _res = await client.SendAsync(_req); var _result = await _res.Content.ReadAsStringAsync(); var org = JsonConvert.DeserializeObject(_result); if (org == null || org.result == null) return Error("ไม่พบหน่วยงานของผู้ใช้งานคนนี้", 404); placementAppointment.profileId = org.result.profileId; placementAppointment.prefix = org.result.prefix; placementAppointment.firstName = org.result.firstName; placementAppointment.lastName = org.result.lastName; placementAppointment.citizenId = org.result.citizenId; placementAppointment.DateOfBirth = org.result.birthDate; placementAppointment.rootOld = org.result.root; placementAppointment.rootOldId = org.result.rootId; placementAppointment.rootShortNameOld = org.result.rootShortName; placementAppointment.child1Old = org.result.child1; placementAppointment.child1OldId = org.result.child1Id; placementAppointment.child1ShortNameOld = org.result.child1ShortName; placementAppointment.child2Old = org.result.child2; placementAppointment.child2OldId = org.result.child2Id; placementAppointment.child2ShortNameOld = org.result.child2ShortName; placementAppointment.child3Old = org.result.child3; placementAppointment.child3OldId = org.result.child3Id; placementAppointment.child3ShortNameOld = org.result.child3ShortName; placementAppointment.child4Old = org.result.child4; placementAppointment.child4OldId = org.result.child4Id; placementAppointment.child4ShortNameOld = org.result.child4ShortName; placementAppointment.posMasterNoOld = org.result.posMasterNo; placementAppointment.posTypeOldId = org.result.posTypeId; placementAppointment.posTypeNameOld = org.result.posTypeName; placementAppointment.posLevelOldId = org.result.posLevelId; placementAppointment.posLevelNameOld = org.result.posLevelName; // placementAppointment.EducationOld = profile.Educations.Count() == 0 ? null : $"{profile.Educations.OrderByDescending(x => x.FinishDate).FirstOrDefault().Degree}-{profile.Educations.OrderByDescending(x => x.FinishDate).FirstOrDefault().Field}"; // placementAppointment.AmountOld = profile.Salaries.Count() == 0 ? null : profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount; placementAppointment.positionOld = org.result.position; placementAppointment.PositionLevelOld = org.result.posLevelName; placementAppointment.PositionTypeOld = org.result.posTypeName; placementAppointment.PositionNumberOld = org.result.nodeShortName + org.result.posMasterNo; placementAppointment.OrganizationOld = (org.result.child4 == null ? "" : org.result.child4 + "/") + (org.result.child3 == null ? "" : org.result.child3 + "/") + (org.result.child2 == null ? "" : org.result.child2 + "/") + (org.result.child1 == null ? "" : org.result.child1 + "/") + (org.result.root == null ? "" : org.result.root); placementAppointment.OrganizationPositionOld = org.result.position + "-" + placementAppointment.OrganizationOld; placementAppointment.AmountOld = org.result.salary; } await _context.PlacementAppointments.AddAsync(placementAppointment); 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 placementAppointmentDoc = new PlacementAppointmentDoc // { // PlacementAppointment = placementAppointment, // Document = _doc, // CreatedFullName = FullName ?? "System Administrator", // CreatedUserId = UserId ?? "", // CreatedAt = DateTime.Now, // LastUpdateFullName = FullName ?? "System Administrator", // LastUpdateUserId = UserId ?? "", // LastUpdatedAt = DateTime.Now, // }; // await _context.PlacementAppointmentDocs.AddAsync(placementAppointmentDoc); // } // } // } await _context.SaveChangesAsync(); return Success(); } /// /// เลือกหน่วยงาน /// /// Id แต่งตั้ง-เลื่อน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("position/{id:length(36)}")] public async Task> UpdatePositionPlacementAppointment([FromBody] PersonSelectPositionAppointmentRequest req, Guid id) { var uppdated = await _context.PlacementAppointments .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.PlacementAppointmentNotFound, 404); var apiUrl = $"{_configuration["API"]}/org/find/all"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _req = new HttpRequestMessage(HttpMethod.Post, apiUrl); var _res = await client.PostAsJsonAsync(apiUrl, new { node = req.node, nodeId = req.nodeId, }); var _result = await _res.Content.ReadAsStringAsync(); var org = JsonConvert.DeserializeObject(_result); if (org == null || org.result == null) return Error("ไม่พบหน่วยงานนี้ในระบบ", 404); uppdated.root = org.result.root; uppdated.rootId = org.result.rootId; uppdated.rootShortName = org.result.rootShortName; uppdated.child1 = req.node <= 0 ? null : org.result.child1; uppdated.child1Id = req.node <= 0 ? null : org.result.child1Id; uppdated.child1ShortName = req.node <= 0 ? null : org.result.child1ShortName; uppdated.child2 = req.node <= 1 ? null : org.result.child2; uppdated.child2Id = req.node <= 1 ? null : org.result.child2Id; uppdated.child2ShortName = req.node <= 1 ? null : org.result.child2ShortName; uppdated.child3 = req.node <= 2 ? null : org.result.child3; uppdated.child3Id = req.node <= 2 ? null : org.result.child3Id; uppdated.child3ShortName = req.node <= 2 ? null : org.result.child3ShortName; uppdated.child4 = req.node <= 3 ? null : org.result.child4; uppdated.child4Id = req.node <= 3 ? null : org.result.child4Id; uppdated.child4ShortName = req.node <= 3 ? null : org.result.child4ShortName; } uppdated.typeCommand = req.typeCommand == null ? null : req.typeCommand.Trim().ToUpper(); uppdated.posmasterId = req.posmasterId; uppdated.node = req.node; uppdated.nodeId = req.nodeId; uppdated.orgRevisionId = req.orgRevisionId; uppdated.positionId = req.positionId; uppdated.posMasterNo = req.posMasterNo; uppdated.position = req.positionName; uppdated.positionField = req.positionField; uppdated.posTypeId = req.posTypeId; uppdated.posTypeName = req.posTypeName; uppdated.posLevelId = req.posLevelId; uppdated.posLevelName = req.posLevelName; uppdated.Amount = req.Amount; uppdated.ReportingDate = req.reportingDate; uppdated.Status = "PENDING"; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขแต่งตั้ง-เลื่อน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("{id:length(36)}")] public async Task> Put([FromBody] PlacementAppointmentEditRequest req, Guid id) { var getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_PROMOTION_OFFICER"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var uppdated = await _context.PlacementAppointments .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.PlacementAppointmentNotFound, 404); // if (req.PrefixId != null) // { // var save = await _context.Prefixes.FindAsync(req.PrefixId); // if (save == null) // return Error(GlobalMessages.PrefixNotFound, 404); // uppdated.Prefix = save; // } // uppdated.CitizenId = req.CitizenId; // uppdated.Firstname = req.Firstname; // uppdated.Lastname = req.Lastname; uppdated.EducationOld = req.EducationOld; uppdated.Reason = req.Reason; uppdated.OrganizationPositionOld = req.OrganizationPositionOld; uppdated.PositionTypeOld = req.PositionTypeOld; uppdated.PositionLevelOld = req.PositionLevelOld; uppdated.PositionNumberOld = req.PositionNumberOld; uppdated.AmountOld = req.AmountOld; uppdated.PositionDate = req.PositionDate; 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 getPermission = await _permission.GetPermissionAPIAsync("DELETE", "SYS_PROMOTION_OFFICER"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var deleted = await _context.PlacementAppointments.AsQueryable() .Include(x => x.PlacementAppointmentDocs) .ThenInclude(x => x.Document) .FirstOrDefaultAsync(x => x.Id == id); if (deleted == null) return NotFound(); var placementAppointmentDocs = new List(); foreach (var doc in deleted.PlacementAppointmentDocs) { if (doc.Document != null) placementAppointmentDocs.Add(doc.Document.Id); } _context.PlacementAppointmentDocs.RemoveRange(deleted.PlacementAppointmentDocs); await _context.SaveChangesAsync(); _context.PlacementAppointments.Remove(deleted); foreach (var doc in placementAppointmentDocs) { if (doc != null) await _documentService.DeleteFileAsync(doc); } await _context.SaveChangesAsync(); return Success(); } /// /// สั่งรายชื่อไปออกคำสั่ง /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("report")] public async Task> PostToReport([FromBody] PlacementProfileRequest req) { var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_PROMOTION_OFFICER"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } foreach (var item in req.Id) { var uppdated = await _context.PlacementAppointments // .Where(x => x.Profile.ProfileType == "officer") .FirstOrDefaultAsync(x => x.Id == item); if (uppdated == null) continue; // uppdated.CommandType = await _context.CommandTypes.FindAsync(commandTypeId); uppdated.Status = "REPORT"; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; } await _context.SaveChangesAsync(); return Success(); } /// /// หน่วยงานที่ถูกเลือกไปแล้ว /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("use")] public async Task> GetAppointmentsUse() { var appointments = await _context.PlacementAppointments .Where(x => x.posmasterId != null) .Where(x => x.Status != "DONE") .Where(x => x.type == "OFFICER") .Select(x => x.posmasterId) .ToListAsync(); return Success(appointments); } /// /// ส่งรายชื่อออกคำสั่ง C-PM-05 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("appoint/report")] public async Task> PostReportAppoint([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "REPORT"); await _context.SaveChangesAsync(); return Success(); } /// /// ลบรายชื่อออกคำสั่ง C-PM-05 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("appoint/report/delete")] public async Task> PostReportDeleteAppoint([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.Status.ToUpper() == "REPORT") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "PENDING"); await _context.SaveChangesAsync(); return Success(); } /// /// เอกสารแนบท้าย C-PM-05 /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("appoint/report/attachment")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PostReportAppointAttachment([FromBody] ReportAttachmentRequest req) { try { var report_data = (from p in _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToList() join r in req.refIds on p.Id.ToString() equals r.refId orderby r.Sequence select new { No = r.Sequence.ToString().ToThaiNumber(), FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Education = p.EducationOld == null ? "-" : p.EducationOld, OldOc = (p.OrganizationPositionOld == null ? "" : p.OrganizationPositionOld) + "/" + (p.rootOld == null ? "" : p.rootOld), OldPositionType = p.PositionTypeOld == null ? "" : p.PositionTypeOld, OldPositionLevel = p.PositionLevelOld == null ? "" : p.PositionLevelOld, OldPositionNumber = p.PositionNumberOld == null ? "" : p.PositionNumberOld.ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), LeaveDate = "", NewOc = (p.position == null ? "" : p.position) + "/" + (p.root == null ? "" : p.root), NewPositionType = p.posTypeName == null ? "" : p.posTypeName, NewPositionLevel = p.posLevelName == null ? "" : p.posLevelName, NewPositionNumber = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}".ToThaiNumber() : "", NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.ReportingDate == null ? "" : p.ReportingDate.Value.ToThaiShortDate2().ToThaiNumber(), RemarkHorizontal = r.RemarkHorizontal, RemarkVertical = r.RemarkVertical, }).ToList(); return Success(report_data); } catch { throw; } } /// /// ออกคำสั่ง C-PM-05 คำสั่งแต่งตั้ง /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("appoint/report/excecute")] public async Task> PostReportExecuteAppoint([FromBody] ReportExecuteRequest req) { var data = await _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToListAsync(); var resultData = (from p in data join r in req.refIds on p.Id.ToString() equals r.refId select new { profileId = p.profileId, date = r.commandAffectDate, amount = r.amount, commandId = r.commandId, positionSalaryAmount = r.positionSalaryAmount, mouthSalaryAmount = r.mouthSalaryAmount, posNo = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}" : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}" : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}" : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}" : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}" : "", position = p.position, positionLine = "", positionPathSide = "", positionExecutive = "", positionType = p.posTypeName, positionLevel = p.posLevelName, refCommandNo = $"{r.commandNo}/{r.commandYear.ToThaiYear()}", templateDoc = r.templateDoc, posmasterId = p.posmasterId, positionId = p.positionId, }).ToList(); var baseAPIOrg = _configuration["API"]; var apiUrlOrg = $"{baseAPIOrg}/org/command/excexute/salary-current"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _res = await client.PostAsJsonAsync(apiUrlOrg, new { data = resultData, }); var _result = await _res.Content.ReadAsStringAsync(); if (_res.IsSuccessStatusCode) { data.ForEach(profile => profile.Status = "DONE"); await _context.SaveChangesAsync(); } } return Success(); } /// /// ส่งรายชื่อออกคำสั่ง C-PM-07 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("move/report")] public async Task> PostReportMove([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "REPORT"); await _context.SaveChangesAsync(); return Success(); } /// /// ลบรายชื่อออกคำสั่ง C-PM-07 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("move/report/delete")] public async Task> PostReportDeleteMove([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.Status.ToUpper() == "REPORT") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "PENDING"); await _context.SaveChangesAsync(); return Success(); } /// /// เอกสารแนบท้าย C-PM-07 /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("move/report/attachment")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PostReporMoveAttachment([FromBody] ReportAttachmentRequest req) { try { var report_data = (from p in _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToList() join r in req.refIds on p.Id.ToString() equals r.refId orderby r.Sequence select new { No = r.Sequence.ToString().ToThaiNumber(), FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Education = p.EducationOld == null ? "-" : p.EducationOld, OldOc = (p.OrganizationPositionOld == null ? "" : p.OrganizationPositionOld) + "/" + (p.rootOld == null ? "" : p.rootOld), OldPositionType = p.PositionTypeOld == null ? "" : p.PositionTypeOld, OldPositionLevel = p.PositionLevelOld == null ? "" : p.PositionLevelOld, OldPositionNumber = p.PositionNumberOld == null ? "" : p.PositionNumberOld.ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), LeaveDate = "", NewOc = (p.position == null ? "" : p.position) + "/" + (p.root == null ? "" : p.root), NewPositionType = p.posTypeName == null ? "" : p.posTypeName, NewPositionLevel = p.posLevelName == null ? "" : p.posLevelName, NewPositionNumber = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}".ToThaiNumber() : "", NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.ReportingDate == null ? "" : p.ReportingDate.Value.ToThaiShortDate2().ToThaiNumber(), RemarkHorizontal = r.RemarkHorizontal, RemarkVertical = r.RemarkVertical, }).ToList(); return Success(report_data); } catch { throw; } } /// /// ออกคำสั่ง C-PM-07 ย้ายข้าราชการ /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("move/report/excecute")] public async Task> PostReportExecuteMove([FromBody] ReportExecuteRequest req) { var data = await _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToListAsync(); var resultData = (from p in data join r in req.refIds on p.Id.ToString() equals r.refId select new { profileId = p.profileId, date = r.commandAffectDate, amount = r.amount, commandId = r.commandId, positionSalaryAmount = r.positionSalaryAmount, mouthSalaryAmount = r.mouthSalaryAmount, posNo = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}" : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}" : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}" : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}" : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}" : "", position = p.position, positionLine = "", positionPathSide = "", positionExecutive = "", positionType = p.posTypeName, positionLevel = p.posLevelName, refCommandNo = $"{r.commandNo}/{r.commandYear.ToThaiYear()}", templateDoc = r.templateDoc, posmasterId = p.posmasterId, positionId = p.positionId, }).ToList(); var baseAPIOrg = _configuration["API"]; var apiUrlOrg = $"{baseAPIOrg}/org/command/excexute/salary-current"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _res = await client.PostAsJsonAsync(apiUrlOrg, new { data = resultData, }); var _result = await _res.Content.ReadAsStringAsync(); if (_res.IsSuccessStatusCode) { data.ForEach(profile => profile.Status = "DONE"); await _context.SaveChangesAsync(); } } return Success(); } /// /// ส่งรายชื่อออกคำสั่ง C-PM-22 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-appoint/report")] public async Task> PostReportEmployeeAppoint([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.type == "EMPLOYEE") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "REPORT"); await _context.SaveChangesAsync(); return Success(); } /// /// ลบรายชื่อออกคำสั่ง C-PM-22 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-appoint/report/delete")] public async Task> PostReportDeleteEmployeeAppoint([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.type == "EMPLOYEE") // .Where(x => x.Status.ToUpper() == "REPORT") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "PENDING"); await _context.SaveChangesAsync(); return Success(); } /// /// เอกสารแนบท้าย C-PM-22 /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-appoint/report/attachment")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PostReportEmployeeAppointAttachment([FromBody] ReportAttachmentRequest req) { try { var report_data = (from p in _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) // .Where(x => x.type == "EMPLOYEE") .ToList() join r in req.refIds on p.Id.ToString() equals r.refId orderby r.Sequence select new { No = r.Sequence.ToString().ToThaiNumber(), FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOrganization = (p.positionOld == null ? "" : p.positionOld) + "/" + (p.OrganizationOld == null ? "" : p.OrganizationOld), OldPositionLevel = p.PositionLevelOld == null ? "" : p.PositionLevelOld.ToThaiNumber(), OldPositionNumber = p.PositionNumberOld == null ? null : p.PositionNumberOld.ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = (p.position == null ? "" : p.position) + "/" + (p.root == null ? "" : p.root), NewPositionLevel = p.posLevelName == null ? "" : p.posLevelName.ToThaiNumber(), NewPositionNumber = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}".ToThaiNumber() : "", NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), RemarkHorizontal = r.RemarkHorizontal, RemarkVertical = r.RemarkVertical, }).ToList(); return Success(report_data); } catch { throw; } } /// /// ออกคำสั่ง C-PM-22 คำสั่งปรับระดับชั้นงาน /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-appoint/report/excecute")] public async Task> PostReportExecuteEmployeeAppoint([FromBody] ReportExecuteRequest req) { var data = await _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToListAsync(); var resultData = (from p in data join r in req.refIds on p.Id.ToString() equals r.refId select new { profileId = p.profileId, date = r.commandAffectDate, amount = r.amount, commandId = r.commandId, positionSalaryAmount = r.positionSalaryAmount, mouthSalaryAmount = r.mouthSalaryAmount, posNo = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}" : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}" : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}" : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}" : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}" : "", position = p.position, positionType = p.posTypeName, positionLevel = p.posLevelName, refCommandNo = $"{r.commandNo}/{r.commandYear.ToThaiYear()}", templateDoc = r.templateDoc, posmasterId = p.posmasterId, positionId = p.positionId, }).ToList(); var baseAPIOrg = _configuration["API"]; var apiUrlOrg = $"{baseAPIOrg}/org/command/excexute/salary-employee-current"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _res = await client.PostAsJsonAsync(apiUrlOrg, new { data = resultData, }); var _result = await _res.Content.ReadAsStringAsync(); if (_res.IsSuccessStatusCode) { data.ForEach(profile => profile.Status = "DONE"); await _context.SaveChangesAsync(); } } return Success(); } /// /// ส่งรายชื่อออกคำสั่ง C-PM-24 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-move/report")] public async Task> PostReportEmployeeMove([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.type == "EMPLOYEE") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "REPORT"); await _context.SaveChangesAsync(); return Success(); } /// /// ลบรายชื่อออกคำสั่ง C-PM-24 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-move/report/delete")] public async Task> PostReportDeleteEmployeeMove([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.type == "EMPLOYEE") // .Where(x => x.Status.ToUpper() == "REPORT") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "PENDING"); await _context.SaveChangesAsync(); return Success(); } /// /// เอกสารแนบท้าย C-PM-24 /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-move/report/attachment")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PostReportEmployeeMoveAttachment([FromBody] ReportAttachmentRequest req) { try { var report_data = (from p in _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) // .Where(x => x.type == "EMPLOYEE") .ToList() join r in req.refIds on p.Id.ToString() equals r.refId orderby r.Sequence select new { No = r.Sequence.ToString().ToThaiNumber(), FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", OldOc = (p.positionOld == null ? "" : p.positionOld) + "/" + (p.OrganizationOld == null ? "" : p.OrganizationOld), OldPositionNumber = p.PositionNumberOld == null ? null : p.PositionNumberOld.ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = (p.position == null ? "" : p.position) + "/" + (p.root == null ? "" : p.root), NewPositionNumber = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}".ToThaiNumber() : "", NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), RemarkHorizontal = r.RemarkHorizontal, RemarkVertical = r.RemarkVertical, }).ToList(); return Success(report_data); } catch { throw; } } /// /// ออกคำสั่ง C-PM-24 คำสั่งย้ายลูกจ้างประจำ /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("employee-move/report/excecute")] public async Task> PostReportExecuteEmployeeMove([FromBody] ReportExecuteRequest req) { var data = await _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToListAsync(); var resultData = (from p in data join r in req.refIds on p.Id.ToString() equals r.refId select new { profileId = p.profileId, date = r.commandAffectDate, amount = r.amount, commandId = r.commandId, positionSalaryAmount = r.positionSalaryAmount, mouthSalaryAmount = r.mouthSalaryAmount, posNo = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}" : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}" : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}" : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}" : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}" : "", position = p.position, positionType = p.posTypeName, positionLevel = p.posLevelName, refCommandNo = $"{r.commandNo}/{r.commandYear.ToThaiYear()}", templateDoc = r.templateDoc, posmasterId = p.posmasterId, positionId = p.positionId, }).ToList(); var baseAPIOrg = _configuration["API"]; var apiUrlOrg = $"{baseAPIOrg}/org/command/excexute/salary-employee-current"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _res = await client.PostAsJsonAsync(apiUrlOrg, new { data = resultData, }); var _result = await _res.Content.ReadAsStringAsync(); if (_res.IsSuccessStatusCode) { data.ForEach(profile => profile.Status = "DONE"); await _context.SaveChangesAsync(); } } return Success(); } /// /// ส่งรายชื่อออกคำสั่ง C-PM-39 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("slip/report")] public async Task> PostReportExecuteSlip([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments // .Include(x => x.Placement) // .ThenInclude(x => x.PlacementType) .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.Placement!.PlacementType!.Name == "เลื่อนข้าราชการ") // .Where(x => x.typeCommand.Trim().ToUpper() == "SLIP") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "REPORT"); await _context.SaveChangesAsync(); return Success(); } /// /// ลบรายชื่อออกคำสั่ง C-PM-39 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("slip/report/delete")] public async Task> PostReportExecuteDeleteSlip([FromBody] ReportPersonRequest req) { var placementProfiles = await _context.PlacementAppointments .Where(x => req.refIds.Contains(x.Id.ToString())) // .Where(x => x.PlacementStatus.ToUpper() == "REPORT") .ToListAsync(); placementProfiles.ForEach(profile => profile.Status = "PREPARE-CONTAIN"); await _context.SaveChangesAsync(); return Success(); } /// /// เอกสารแนบท้าย C-PM-39 /// /// Record Id ของคำสั่ง /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("slip/report/attachment")] [AllowAnonymous] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> PostReportExecuteSlipAttachment([FromBody] ReportAttachmentRequest req) { try { var report_data = (from p in _context.PlacementAppointments //.Include(x => x.PlacementEducation) // .ThenInclude(x => x.PlacementType) .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) // .Where(x => x.Placement!.PlacementType!.Name == "เลื่อนข้าราชการ") // .Where(x => x.typeCommand.Trim().ToUpper() == "SLIP") .ToList() join r in req.refIds on p.Id.ToString() equals r.refId orderby r.Sequence select new { No = r.Sequence.ToString().ToThaiNumber(), FullName = $"{r.Prefix}{r.FirstName} {r.LastName}", Education = p.EducationOld == null ? "" : p.EducationOld, OldOc = (p.positionOld == null ? "" : p.positionOld) + "/" + (p.rootOld == null ? "" : p.rootOld), OldPositionType = p.posTypeNameOld == null ? "" : p.posTypeNameOld, OldPositionLevel = p.posLevelNameOld == null ? "" : p.posLevelNameOld, OldPositionNumber = p.PositionNumberOld == null ? "" : p.PositionNumberOld.ToThaiNumber(), OldSalary = p.AmountOld == null ? "" : p.AmountOld.Value.ToNumericNoDecimalText().ToThaiNumber(), NewOc = (p.position == null ? "" : p.position) + "/" + (p.root == null ? "" : p.root), NewPositionType = p.posTypeName == null ? "" : p.posTypeName, NewPositionLevel = p.posLevelName == null ? "" : p.posLevelName, NewPositionNumber = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}".ToThaiNumber() : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}".ToThaiNumber() : "", NewSalary = r.Amount == null ? "" : r.Amount.Value.ToNumericNoDecimalText().ToThaiNumber(), AppointDate = p.ReportingDate == null ? "" : p.ReportingDate.Value.ToThaiShortDate2().ToThaiNumber(), RemarkHorizontal = r.RemarkHorizontal, RemarkVertical = r.RemarkVertical, }).ToList(); return Success(report_data); } catch { throw; } } /// /// ออกคำสั่ง C-PM-39 เลื่อนข้าราชการ /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("slip/report/excecute")] public async Task> PostReportSlipExecute([FromBody] ReportExecuteRequest req) { var data = await _context.PlacementAppointments .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) .ToListAsync(); var resultData = (from p in data join r in req.refIds on p.Id.ToString() equals r.refId select new { profileId = p.profileId, date = r.commandAffectDate, amount = r.amount, commandId = r.commandId, positionSalaryAmount = r.positionSalaryAmount, mouthSalaryAmount = r.mouthSalaryAmount, posNo = p.posMasterNo == null ? "" : p.node == 4 ? $"{p.child4ShortName}{p.posMasterNo}" : p.node == 3 ? $"{p.child3ShortName}{p.posMasterNo}" : p.node == 2 ? $"{p.child2ShortName}{p.posMasterNo}" : p.node == 1 ? $"{p.child1ShortName}{p.posMasterNo}" : p.node == 0 ? $"{p.rootShortName}{p.posMasterNo}" : "", position = p.position, positionLine = "", positionPathSide = "", positionExecutive = "", positionType = p.posTypeName, positionLevel = p.posLevelName, refCommandNo = $"{r.commandNo}/{r.commandYear.ToThaiYear()}", templateDoc = r.templateDoc, posmasterId = p.posmasterId, positionId = p.positionId, }).ToList(); var baseAPIOrg = _configuration["API"]; var apiUrlOrg = $"{baseAPIOrg}/org/command/excexute/salary-current"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]); var _res = await client.PostAsJsonAsync(apiUrlOrg, new { data = resultData, }); var _result = await _res.Content.ReadAsStringAsync(); if (_res.IsSuccessStatusCode) { data.ForEach(profile => profile.Status = "DONE"); await _context.SaveChangesAsync(); } } return Success(); } } }