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.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 Newtonsoft.Json; using Swashbuckle.AspNetCore.Annotations; using System.Net.Http.Headers; 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 NotificationRepository _repositoryNoti; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IConfiguration _configuration; public RetirementResignController(RetirementRepository repository, NotificationRepository repositoryNoti, ApplicationDBContext context, MinIOService documentService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) { _repository = repository; _repositoryNoti = repositoryNoti; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; _configuration = configuration; } #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"]; #endregion // private List GetOcNameFullPath(Guid id, bool showRoot = false) // { // try // { // var ocList = new List(); // var oc = (from o in _context.Organizations.Include(x => x.Parent).Include(x => x.OrganizationOrganization).Where(x => x.OrganizationOrganization != null).AsQueryable() // join oc_name in _context.OrganizationOrganizations.AsQueryable() on o.OrganizationOrganization.Id equals oc_name.Id // where o.Parent != null // select new // { // Id = o.Id, // Name = oc_name.Name, // o.IsActive, // o.Parent // }).FirstOrDefault(x => x.Id == id && x.IsActive); // if (oc == null) // return ocList; // ocList.Add(oc.Name); // if (oc.Parent?.Id != null) // { // ocList.AddRange(GetOcNameFullPath(oc.Parent.Id, showRoot)); // } // return ocList; // } // catch // { // throw; // } // } // private string FindOCFullPath(Guid id, bool showRoot = false) // { // try // { // var ocList = GetOcNameFullPath(id, showRoot); // var ret = String.Empty; // foreach (var oc in ocList) // { // ret = oc + "/" + ret; // } // if (ret.Length > 2) // ret = ret.Substring(0, ret.Length - 1); // return ret; // } // catch // { // throw; // } // } /// /// list รายการลาออกของ User /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("user")] public async Task> GetListByProfile() { var apiUrl = $"{_configuration["API"]}org/profile/keycloak/position"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); 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); var retirementResigns = await _context.RetirementResigns.AsQueryable() .Where(x => x.profileId == org.result.profileId) .OrderByDescending(x => x.CreatedAt) .Select(p => new { p.Id, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.ApproveReason, p.RejectReason, 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, p.profileId, p.prefix, p.firstName, p.lastName, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.Status, salary = p.AmountOld, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.RemarkHorizontal, p.ApproveReason, p.RejectReason, 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, p.prefix, p.firstName, p.lastName, p.profileId, p.Location, p.SendDate, p.ActiveDate, p.Reason, p.Status, salary = p.AmountOld, p.ApproveReason, p.RejectReason, p.IsActive, p.CreatedAt, p.PositionTypeOld, p.PositionLevelOld, p.PositionNumberOld, p.OrganizationPositionOld, p.OligarchReject, p.OligarchApproveReason, p.OligarchRejectReason, p.OligarchRejectDate, p.CommanderReject, p.CommanderApproveReason, p.CommanderRejectReason, p.CommanderRejectDate, p.OfficerReject, p.OfficerApproveReason, p.OfficerRejectReason, p.OfficerRejectDate, p.RemarkHorizontal, 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.PositionOld, p.posMasterNoOld, p.posTypeOldId, p.posTypeNameOld, p.posLevelOldId, p.posLevelNameOld, p.IsNoDebt, // Avatar = p.Profile.Avatar == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Avatar.Id, RetirementResignDocs = p.RetirementResignDocs.Where(d => d.Document != null).Select(d => new { d.Document.Id, d.Document.FileName }), // RetirementResignDebtDocs = p.RetirementResignDebtDocs.Where(d => d.Document != null).Select(d => new { d.Document.Id, d.Document.FileName }), }) .FirstOrDefaultAsync(); if (data == null) return Error(GlobalMessages.RetirementResignNotFound, 404); var retirementResignDocs = new List(); foreach (var doc in data.RetirementResignDocs) { var _doc = new { FileName = doc.FileName, PathName = await _documentService.ImagesPath(doc.Id), doc.Id, }; retirementResignDocs.Add(_doc); } // var retirementResignDebtDocs = new List(); // foreach (var doc in data.RetirementResignDebtDocs) // { // var _doc = new // { // FileName = doc.FileName, // PathName = await _documentService.ImagesPath(doc.Id), // doc.Id, // }; // retirementResignDebtDocs.Add(_doc); // } var _data = new { data.Id, data.profileId, data.prefix, // data.PrefixId, data.firstName, data.lastName, data.Location, data.SendDate, data.ActiveDate, data.Reason, data.Status, data.salary, data.PositionTypeOld, data.PositionLevelOld, data.PositionNumberOld, data.OrganizationPositionOld, data.ApproveReason, data.RejectReason, data.IsActive, data.CreatedAt, data.OligarchReject, data.OligarchApproveReason, data.OligarchRejectReason, data.OligarchRejectDate, data.CommanderReject, data.CommanderApproveReason, data.CommanderRejectReason, data.CommanderRejectDate, data.OfficerReject, data.OfficerApproveReason, data.OfficerRejectReason, data.OfficerRejectDate, data.RemarkHorizontal, 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, data.PositionOld, data.posMasterNoOld, data.posTypeOldId, data.posTypeNameOld, data.posLevelOldId, data.posLevelNameOld, data.IsNoDebt, // Avatar = data.Avatar == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.Avatar), Docs = retirementResignDocs, // DocDebts = retirementResignDebtDocs, }; return Success(_data); } /// /// สร้างการลาออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> Post([FromForm] RetirementResignRequest 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 retirementResign = new RetirementResign { // Profile = profile, Location = req.Location, SendDate = req.SendDate, ActiveDate = req.ActiveDate, Reason = req.Reason, // 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", IsActive = true, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; var apiUrl = $"{_configuration["API"]}org/profile/keycloak/position"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); 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); retirementResign.profileId = org.result.profileId; retirementResign.prefix = org.result.prefix; retirementResign.firstName = org.result.firstName; retirementResign.lastName = org.result.lastName; retirementResign.citizenId = org.result.citizenId; retirementResign.rootOld = org.result.root; retirementResign.rootOldId = org.result.rootId; retirementResign.rootShortNameOld = org.result.rootShortName; retirementResign.child1Old = org.result.child1; retirementResign.child1OldId = org.result.child1Id; retirementResign.child1ShortNameOld = org.result.child1ShortName; retirementResign.child2Old = org.result.child2; retirementResign.child2OldId = org.result.child2Id; retirementResign.child2ShortNameOld = org.result.child2ShortName; retirementResign.child3Old = org.result.child3; retirementResign.child3OldId = org.result.child3Id; retirementResign.child3ShortNameOld = org.result.child3ShortName; retirementResign.child4Old = org.result.child4; retirementResign.child4OldId = org.result.child4Id; retirementResign.child4ShortNameOld = org.result.child4ShortName; retirementResign.posMasterNoOld = org.result.posMasterNo; retirementResign.posTypeOldId = org.result.posTypeId; retirementResign.posTypeNameOld = org.result.posTypeName; retirementResign.posLevelOldId = org.result.posLevelId; retirementResign.posLevelNameOld = org.result.posLevelName; retirementResign.PositionOld = org.result.position; retirementResign.PositionLevelOld = org.result.posLevelName; retirementResign.PositionTypeOld = org.result.posTypeName; retirementResign.PositionNumberOld = org.result.nodeShortName + org.result.posMasterNo; retirementResign.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); retirementResign.OrganizationPositionOld = org.result.position + "-" + retirementResign.OrganizationOld; } await _context.RetirementResigns.AddAsync(retirementResign); 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 retirementResignDoc = new RetirementResignDoc { RetirementResign = retirementResign, Document = _doc, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementResignDocs.AddAsync(retirementResignDoc); } } } await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-6268-4e2c-80a3-aca65eedc6d0"), $"{retirementResign.prefix}{retirementResign.firstName} {retirementResign.lastName} ได้ทำการยื่นขอลาออก", $"{retirementResign.prefix}{retirementResign.firstName} {retirementResign.lastName} ได้ทำการยื่นขอลาออก", "", true ); await _context.SaveChangesAsync(); return Success(retirementResign); } /// /// แก้ไขการลาออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ 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.OrganizationPositionOld = req.OrganizationPositionOld; updated.RemarkHorizontal = req.RemarkHorizontal; updated.PositionTypeOld = req.PositionTypeOld; updated.PositionLevelOld = req.PositionLevelOld; updated.PositionNumberOld = req.PositionNumberOld; updated.AmountOld = req.AmountOld; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขรายการตรวจสอบเงื่อนไขต่าง ๆ /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("debt/{id:length(36)}")] public async Task> Debt([FromBody] RetirementResignDebtRequest req, Guid id) { var updated = await _context.RetirementResigns.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.IsNoDebt = req.IsNoDebt; updated.IsNoBurden = req.IsNoBurden; updated.IsDiscipline = req.IsDiscipline; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; // 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 retirementResignDebtDoc = new RetirementResignDebtDoc // { // RetirementResign = updated, // Document = _doc, // CreatedFullName = FullName ?? "System Administrator", // CreatedUserId = UserId ?? "", // CreatedAt = DateTime.Now, // LastUpdateFullName = FullName ?? "System Administrator", // LastUpdateUserId = UserId ?? "", // LastUpdatedAt = DateTime.Now, // }; // await _context.RetirementResignDebtDocs.AddAsync(retirementResignDebtDoc); // } // } // } await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขรายการตรวจสอบเงื่อนไขต่าง ๆ /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("debt/{id:length(36)}")] public async Task> DebtDelete(Guid id) { var deleted = await _context.RetirementResignDocs.AsQueryable() .FirstOrDefaultAsync(x => x.Document.Id == id); if (deleted == null) return Error(GlobalMessages.RetirementResignNotFound, 404); _context.RetirementResignDocs.Remove(deleted); var _deleted = await _context.Documents.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (_deleted == null) return Error(GlobalMessages.RetirementResignNotFound, 404); _context.Documents.Remove(_deleted); await _context.SaveChangesAsync(); return Success(); } /// /// ยกเลิกลาออก /// /// Id ลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("cancel/{id:length(36)}")] public async Task> Cancel([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Status = "CANCEL"; updated.CancelReason = 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); deleted.Status = "DELETE"; deleted.LastUpdateFullName = FullName ?? "System Administrator"; deleted.LastUpdateUserId = UserId ?? ""; deleted.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("confirm/{id:length(36)}")] public async Task> AdminConfirm([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Status = "APPROVE"; updated.ApproveReason = req.Reason; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// ไม่อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("reject/{id:length(36)}")] public async Task> AdminReject([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Status = "REJECT"; updated.RejectReason = req.Reason; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// การเจ้าหน้าที่ อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("officer/confirm/{id:length(36)}")] public async Task> OfficerConfirm([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); // updated.Status = "APPROVE"; updated.OfficerReject = false; updated.OfficerApproveReason = req.Reason; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// การเจ้าหน้าที่ ไม่อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("officer/reject/{id:length(36)}")] public async Task> OfficerReject([FromBody] RetirementReasonDateRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); if ((DateTime.Now - updated.CreatedAt).TotalDays >= 90) return Error("สามารถยับยั้งได้ไม่เกิน 90 วัน"); // updated.Status = "REJECT"; updated.OfficerReject = true; updated.OfficerRejectReason = req.Reason; updated.OfficerRejectDate = req.Date; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// ผู้บังคับบัญชา อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("commander/confirm/{id:length(36)}")] public async Task> CommanderConfirm([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); // updated.Status = "APPROVE"; updated.CommanderReject = false; updated.CommanderApproveReason = req.Reason; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// ผู้บังคับบัญชา ไม่อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("commander/reject/{id:length(36)}")] public async Task> CommanderReject([FromBody] RetirementReasonDateRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); if ((DateTime.Now - updated.CreatedAt).TotalDays >= 90) return Error("สามารถยับยั้งได้ไม่เกิน 90 วัน"); // updated.Status = "REJECT"; updated.CommanderReject = true; updated.CommanderRejectReason = req.Reason; updated.CommanderRejectDate = req.Date; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// ผู้มีอำนาจ อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("oligarch/confirm/{id:length(36)}")] public async Task> OligarchConfirm([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); updated.Status = "APPROVE"; updated.OligarchReject = false; updated.OligarchApproveReason = req.Reason; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsyncV2( updated.CreatedUserId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// ผู้มีอำนาจ ไม่อนุมัติคำลาออก /// /// Id คำลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("oligarch/reject/{id:length(36)}")] public async Task> OligarchReject([FromBody] RetirementReasonDateRequest req, Guid id) { var updated = await _context.RetirementResigns .FirstOrDefaultAsync(x => x.Id == id); if (updated == null) return Error(GlobalMessages.RetirementResignNotFound, 404); if ((DateTime.Now - updated.CreatedAt).TotalDays >= 90) return Error("สามารถยับยั้งได้ไม่เกิน 90 วัน"); updated.Status = "REJECT"; updated.OligarchReject = true; updated.OligarchRejectReason = req.Reason; updated.OligarchRejectDate = req.Date; updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsyncV2( updated.CreatedUserId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้มีอำนาจ", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้มีอำนาจ", "", true ); 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.RetirementResigns .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(); } /// /// list แบบสอบถามหลังลาออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("questionnaire")] public async Task> GetListQuestion() { var data = await _context.RetirementQuestions.AsQueryable() .OrderByDescending(x => x.CreatedAt) .Select(p => new { Id = p.Id, Fullname = $"{p.RetirementResign.prefix}{p.RetirementResign.firstName} {p.RetirementResign.lastName}", ReasonWork = p.ReasonWork == null ? p.ReasonWork : Newtonsoft.Json.JsonConvert.DeserializeObject(p.ReasonWork), ReasonWorkOther = p.ReasonWorkOther, TimeThink = p.TimeThink, ExitFactor = p.ExitFactor == null ? p.ExitFactor : Newtonsoft.Json.JsonConvert.DeserializeObject(p.ExitFactor), ExitFactorOther = p.ExitFactorOther, Adjust = p.Adjust == null ? p.Adjust : Newtonsoft.Json.JsonConvert.DeserializeObject(p.Adjust), AdjustOther = p.AdjustOther, RealReason = p.RealReason, NotExitFactor = p.NotExitFactor, Havejob = p.Havejob, HavejobReason = p.HavejobReason, SuggestFriends = p.SuggestFriends, SuggestFriendsReason = p.SuggestFriendsReason, FutureWork = p.FutureWork, FutureWorkReason = p.FutureWorkReason, Suggestion = p.Suggestion, LastUpdatedAt = p.LastUpdatedAt, CreatedAt = p.CreatedAt, AppointDate = p.AppointDate, }) .ToListAsync(); return Success(data); } /// /// get รายละเอียดแบบสอบถามหลังลาออก /// /// Id แบบสอบถามหลังลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("questionnaire/{id:length(36)}")] public async Task> GetByIdQuestion(Guid id) { var data = await _context.RetirementQuestions.AsQueryable() .Where(x => x.Id == id) .Select(p => new { Id = p.Id, Prefix = p.RetirementResign.prefix, FirstName = p.RetirementResign.firstName, LastName = p.RetirementResign.lastName, Position = p.RetirementResign.PositionOld, PositionLevel = p.RetirementResign.posLevelNameOld, Org = p.RetirementResign.OrganizationOld, Fullname = $"{p.RetirementResign.prefix}{p.RetirementResign.firstName} {p.RetirementResign.lastName}", // Avatar = p.RetirementResign.Profile.Avatar == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.RetirementResign.Profile.Avatar.Id, ReasonWork = p.ReasonWork == null ? p.ReasonWork : Newtonsoft.Json.JsonConvert.DeserializeObject(p.ReasonWork), ReasonWorkOther = p.ReasonWorkOther, TimeThink = p.TimeThink, ExitFactor = p.ExitFactor == null ? p.ExitFactor : Newtonsoft.Json.JsonConvert.DeserializeObject(p.ExitFactor), ExitFactorOther = p.ExitFactorOther, Adjust = p.Adjust == null ? p.Adjust : Newtonsoft.Json.JsonConvert.DeserializeObject(p.Adjust), AdjustOther = p.AdjustOther, RealReason = p.RealReason, NotExitFactor = p.NotExitFactor, Havejob = p.Havejob, HavejobReason = p.HavejobReason, SuggestFriends = p.SuggestFriends, SuggestFriendsReason = p.SuggestFriendsReason, FutureWork = p.FutureWork, FutureWorkReason = p.FutureWorkReason, Suggestion = p.Suggestion, AppointDate = p.AppointDate, LastUpdatedAt = p.LastUpdatedAt, OrganizationPositionOld = p.RetirementResign.OrganizationPositionOld, CreatedAt = p.CreatedAt, posTypeName = p.RetirementResign.posTypeNameOld, posLevelName = p.RetirementResign.posLevelNameOld, posMasterNo = p.RetirementResign.posMasterNoOld, root = p.RetirementResign.rootOld, rootShortName = p.RetirementResign.rootShortNameOld, child1 = p.RetirementResign.child1Old, child1ShortName = p.RetirementResign.child1ShortNameOld, child2 = p.RetirementResign.child2Old, child2ShortName = p.RetirementResign.child2ShortNameOld, child3 = p.RetirementResign.child3Old, child3ShortName = p.RetirementResign.child3ShortNameOld, child4 = p.RetirementResign.child4Old, child4ShortName = p.RetirementResign.child4ShortNameOld, Score1 = p.Score1, Score2 = p.Score2, Score3 = p.Score3, Score4 = p.Score4, Score5 = p.Score5, Score6 = p.Score6, Score7 = p.Score7, Score8 = p.Score8, Score9 = p.Score9, Score10 = p.Score10, ScoreTotal = p.ScoreTotal, Comment = p.Comment, }) .FirstOrDefaultAsync(); if (data == null) return Error(GlobalMessages.RetirementQuestionNotFound, 404); // var _data = new // { // data.Id, // // data.PrefixId, // data.Position, // data.PositionLevel, // data.Org, // data.Fullname, // data.Prefix, // // Avatar = data.Avatar == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.Avatar), // data.ReasonWork, // data.ReasonWorkOther, // data.TimeThink, // data.ExitFactor, // data.ExitFactorOther, // data.Adjust, // data.AdjustOther, // data.RealReason, // data.NotExitFactor, // data.Havejob, // data.HavejobReason, // data.SuggestFriends, // data.SuggestFriendsReason, // data.FutureWork, // data.FutureWorkReason, // data.Suggestion, // data.AppointDate, // data.OrganizationPositionOld, // data.LastUpdatedAt, // data.CreatedAt, // data.Score1, // data.Score2, // data.Score3, // data.Score4, // data.Score5, // data.Score6, // data.Score7, // data.Score8, // data.Score9, // data.Score10, // data.ScoreTotal, // data.Comment, // }; return Success(data); } /// /// สร้างแบบสอบถามหลังลาออก /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("questionnaire")] public async Task> PostQuestion([FromBody] RetirementQuestionRequest req) { var retirementResign = await _context.RetirementResigns.AsQueryable() .FirstOrDefaultAsync(x => x.Id == req.RetirementResignId); if (retirementResign == null) return Error(GlobalMessages.RetirementResignNotFound); var period = new RetirementQuestion { RetirementResign = retirementResign, ReasonWork = Newtonsoft.Json.JsonConvert.SerializeObject(req.ReasonWork), ReasonWorkOther = req.ReasonWorkOther, TimeThink = req.TimeThink, ExitFactor = Newtonsoft.Json.JsonConvert.SerializeObject(req.ExitFactor), ExitFactorOther = req.ExitFactorOther, Adjust = Newtonsoft.Json.JsonConvert.SerializeObject(req.Adjust), AdjustOther = req.AdjustOther, RealReason = req.RealReason, NotExitFactor = req.NotExitFactor, Havejob = req.Havejob, HavejobReason = req.HavejobReason, SuggestFriends = req.SuggestFriends, SuggestFriendsReason = req.SuggestFriendsReason, FutureWork = req.FutureWork, FutureWorkReason = req.FutureWorkReason, Suggestion = req.Suggestion, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementQuestions.AddAsync(period); await _context.SaveChangesAsync(); return Success(); } /// /// ลบแบบสอบถามหลังลาออก /// /// Id แบบสอบถามหลังลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("questionnaire/{id:length(36)}")] public async Task> DeleteQuestion(Guid id) { var deleted = await _context.RetirementQuestions.AsQueryable() .Where(x => x.Id == id) .FirstOrDefaultAsync(); if (deleted == null) return Error(GlobalMessages.RetirementQuestionNotFound); _context.RetirementQuestions.Remove(deleted); await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขแบบสอบถามหลังลาออก /// /// Id แบบสอบถามหลังลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("questionnaire/{id:length(36)}")] public async Task> PutQuestion([FromBody] RetirementQuestionRequest req, Guid id) { var uppdated = await _context.RetirementQuestions.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.RetirementQuestionNotFound); var retirementResign = await _context.RetirementResigns.AsQueryable() .FirstOrDefaultAsync(x => x.Id == req.RetirementResignId); if (retirementResign == null) return Error(GlobalMessages.InsigniaNotFound); // uppdated.RetirementResign = retirementResign; uppdated.ReasonWork = Newtonsoft.Json.JsonConvert.SerializeObject(req.ReasonWork); uppdated.ReasonWorkOther = req.ReasonWorkOther; uppdated.TimeThink = req.TimeThink; uppdated.ExitFactor = Newtonsoft.Json.JsonConvert.SerializeObject(req.ExitFactor); uppdated.ExitFactorOther = req.ExitFactorOther; uppdated.Adjust = Newtonsoft.Json.JsonConvert.SerializeObject(req.Adjust); uppdated.AdjustOther = req.AdjustOther; uppdated.RealReason = req.RealReason; uppdated.NotExitFactor = req.NotExitFactor; uppdated.Havejob = req.Havejob; uppdated.HavejobReason = req.HavejobReason; uppdated.SuggestFriends = req.SuggestFriends; uppdated.SuggestFriendsReason = req.SuggestFriendsReason; uppdated.FutureWork = req.FutureWork; uppdated.FutureWorkReason = req.FutureWorkReason; uppdated.Suggestion = req.Suggestion; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขคะแนนแบบสอบถามหลังลาออก /// /// Id แบบสอบถามหลังลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("questionnaire/comment/{id:length(36)}")] public async Task> PutQuestionComment([FromBody] RetirementQuestionCommentRequest req, Guid id) { var uppdated = await _context.RetirementQuestions.AsQueryable() .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.RetirementQuestionNotFound); uppdated.Score1 = req.Score1; uppdated.Score2 = req.Score2; uppdated.Score3 = req.Score3; uppdated.Score4 = req.Score4; uppdated.Score5 = req.Score5; uppdated.Score6 = req.Score6; uppdated.Score7 = req.Score7; uppdated.Score8 = req.Score8; uppdated.Score9 = req.Score9; uppdated.Score10 = req.Score10; uppdated.ScoreTotal = req.ScoreTotal; uppdated.Comment = req.Comment; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขกําหนดวันนัดหมายเพื่อทําการสัมภาษณ์การลาออก /// /// Id กําหนดวันนัดหมายเพื่อทําการสัมภาษณ์การลาออก /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("questionnaire/appoint/{id:length(36)}")] public async Task> UpdateAppointQuestion([FromBody] RetirementQuestionAppointRequest req, Guid id) { var uppdated = await _context.RetirementQuestions.AsQueryable() .Include(x => x.RetirementResign) .FirstOrDefaultAsync(x => x.Id == id); if (uppdated == null) return Error(GlobalMessages.RetirementQuestionNotFound); uppdated.AppointDate = req.AppointDate; uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsyncV2( uppdated.RetirementResign.CreatedUserId, $"การนัดสัมภาษณ์เหตุผลการลาออก {req.AppointDate.ToThaiFullDate()}", $"การนัดสัมภาษณ์เหตุผลการลาออก {req.AppointDate.ToThaiFullDate()}", "", true ); await _context.SaveChangesAsync(); return Success(); } /// /// list คำถาม /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("questionnaire/question")] public async Task> GetQuestionnaireQuestion() { var retirementQuestionnaireQuestion = await _context.RetirementQuestionnaireQuestions.AsQueryable() .Select(x => new { Question1Desc = x.Question1Desc, Question1Score = x.Question1Score, Question1Answer = x.Question1Answer == null ? null : JsonConvert.DeserializeObject>(x.Question1Answer), Question2Desc = x.Question2Desc, Question2Score = x.Question2Score, Question2Answer = x.Question2Answer == null ? null : JsonConvert.DeserializeObject>(x.Question2Answer), Question3Desc = x.Question3Desc, Question3Score = x.Question3Score, Question3Answer = x.Question3Answer == null ? null : JsonConvert.DeserializeObject>(x.Question3Answer), Question4Desc = x.Question4Desc, Question4Score = x.Question4Score, Question4Answer = x.Question4Answer == null ? null : JsonConvert.DeserializeObject>(x.Question4Answer), Question5Desc = x.Question5Desc, Question5Score = x.Question5Score, Question5Answer = x.Question5Answer == null ? null : JsonConvert.DeserializeObject>(x.Question5Answer), Question6Desc = x.Question6Desc, Question6Score = x.Question6Score, Question6Answer = x.Question6Answer == null ? null : JsonConvert.DeserializeObject>(x.Question6Answer), Question7Desc = x.Question7Desc, Question7Score = x.Question7Score, Question7Answer = x.Question7Answer == null ? null : JsonConvert.DeserializeObject>(x.Question7Answer), Question8Desc = x.Question8Desc, Question8Score = x.Question8Score, Question8Answer = x.Question8Answer == null ? null : JsonConvert.DeserializeObject>(x.Question8Answer), Question9Desc = x.Question9Desc, Question9Score = x.Question9Score, Question9Answer = x.Question9Answer == null ? null : JsonConvert.DeserializeObject>(x.Question9Answer), Question10Desc = x.Question10Desc, Question10Score = x.Question10Score, Question10Answer = x.Question10Answer == null ? null : JsonConvert.DeserializeObject>(x.Question10Answer), }) .FirstOrDefaultAsync(); if (retirementQuestionnaireQuestion == null) return Error(GlobalMessages.RetirementQuestionNotFound); return Success(retirementQuestionnaireQuestion); } /// /// update คำถาม /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("questionnaire/question")] public async Task> UpdateQuestionnaireQuestion([FromBody] RetirementQuestionnaireQuestionRequest req) { var uppdated = await _context.RetirementQuestionnaireQuestions.AsQueryable() .FirstOrDefaultAsync(); if (uppdated == null) return Error(GlobalMessages.RetirementQuestionnaireQuestionNotFound); uppdated.Question1Desc = req.Question1Desc; uppdated.Question1Score = req.Question1Score; uppdated.Question1Answer = JsonConvert.SerializeObject(req.Question1Answer); uppdated.Question2Desc = req.Question2Desc; uppdated.Question2Score = req.Question2Score; uppdated.Question2Answer = JsonConvert.SerializeObject(req.Question2Answer); uppdated.Question3Desc = req.Question3Desc; uppdated.Question3Score = req.Question3Score; uppdated.Question3Answer = JsonConvert.SerializeObject(req.Question3Answer); uppdated.Question4Desc = req.Question4Desc; uppdated.Question4Score = req.Question4Score; uppdated.Question4Answer = JsonConvert.SerializeObject(req.Question4Answer); uppdated.Question5Desc = req.Question5Desc; uppdated.Question5Score = req.Question5Score; uppdated.Question5Answer = JsonConvert.SerializeObject(req.Question5Answer); uppdated.Question6Desc = req.Question6Desc; uppdated.Question6Score = req.Question6Score; uppdated.Question6Answer = JsonConvert.SerializeObject(req.Question6Answer); uppdated.Question7Desc = req.Question7Desc; uppdated.Question7Score = req.Question7Score; uppdated.Question7Answer = JsonConvert.SerializeObject(req.Question7Answer); uppdated.Question8Desc = req.Question8Desc; uppdated.Question8Score = req.Question8Score; uppdated.Question8Answer = JsonConvert.SerializeObject(req.Question8Answer); uppdated.Question9Desc = req.Question9Desc; uppdated.Question9Score = req.Question9Score; uppdated.Question9Answer = JsonConvert.SerializeObject(req.Question9Answer); uppdated.Question10Desc = req.Question10Desc; uppdated.Question10Score = req.Question10Score; uppdated.Question10Answer = JsonConvert.SerializeObject(req.Question10Answer); uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); return Success(); } } }