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.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; public RetirementResignController(RetirementRepository repository, NotificationRepository repositoryNoti, ApplicationDBContext context, MinIOService documentService, IHttpContextAccessor httpContextAccessor) { _repository = repository; _repositoryNoti = repositoryNoti; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; } #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; #endregion 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 profile = await _context.Profiles .FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(UserId)); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); var retirementResigns = await _context.RetirementResigns.AsQueryable() .Where(x => x.Profile == profile) .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, ProfileId = p.Profile.Id, Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name, p.Profile.FirstName, p.Profile.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, Prefix = p.Profile.Prefix == null ? null : p.Profile.Prefix.Name, PrefixId = p.Profile.Prefix == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.Profile.Prefix.Id, p.Profile.FirstName, p.Profile.LastName, ProfileId = p.Profile.Id, 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.RemarkHorizontal, 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 }), }) .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) }; retirementResignDocs.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.RemarkHorizontal, Avatar = data.Avatar == Guid.Parse("00000000-0000-0000-0000-000000000000") ? null : await _documentService.ImagesPath(data.Avatar), Docs = retirementResignDocs, }; 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, }; 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"), $"{profile.Prefix?.Name}{profile.FirstName} {profile.LastName} ได้ทำการยื่นขอลาออก", $"{profile.Prefix?.Name}{profile.FirstName} {profile.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(); } /// /// ลบลาออก /// /// 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 .Include(x => x.Profile) .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 .Include(x => x.Profile) .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("commander/confirm/{id:length(36)}")] public async Task> CommanderConfirm([FromBody] RetirementReasonRequest req, Guid id) { var updated = await _context.RetirementResigns .Include(x => x.Profile) .ThenInclude(x => x.Prefix) .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.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.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 .Include(x => x.Profile) .ThenInclude(x => x.Prefix) .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.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ถูกยับยั้งจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ถูกยับยั้งจากผู้บังคับบัญชา", "", true ); await _repositoryNoti.PushNotificationAsync( Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ถูกยับยั้งจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.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 .Include(x => x.Profile) .ThenInclude(x => x.Prefix) .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.PushNotificationAsync( updated.Profile.Id, $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ได้รับการอนุมัติจากผู้มีอำนาจ", $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.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 .Include(x => x.Profile) .ThenInclude(x => x.Prefix) .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.PushNotificationAsync( updated.Profile.Id, $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ถูกยับยั้งจากผู้มีอำนาจ", $"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.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.Profile.Prefix == null ? null : p.RetirementResign.Profile.Prefix.Name)}{p.RetirementResign.Profile.FirstName} {p.RetirementResign.Profile.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, PrefixId = p.RetirementResign.Profile.Prefix == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : p.RetirementResign.Profile.Prefix.Id, Position = p.RetirementResign.Profile.Position == null ? null : p.RetirementResign.Profile.Position.Name, PositionLevel = p.RetirementResign.Profile.PositionLevel == null ? null : p.RetirementResign.Profile.PositionLevel.Name, Org = p.RetirementResign.Profile.OcId == null ? null : p.RetirementResign.Profile.OcId, Fullname = $"{(p.RetirementResign.Profile.Prefix == null ? null : p.RetirementResign.Profile.Prefix.Name)}{p.RetirementResign.Profile.FirstName} {p.RetirementResign.Profile.LastName}", Prefix = p.RetirementResign.Profile.Prefix == null ? null : p.RetirementResign.Profile.Prefix.Name, 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, 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, Org = data.Org == null ? null : FindOCFullPath(data.Org.Value, true), 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) .ThenInclude(x => x.Profile) .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.PushNotificationAsync( uppdated.RetirementResign.Profile.Id, $"การนัดสัมภาษณ์เหตุผลการลาออก {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(); } } }