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.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 Swashbuckle.AspNetCore.Annotations; using System.Security.Claims; namespace BMA.EHR.Retirement.Service.Controllers { [Route("api/v{version:apiVersion}/retirement")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("ระบบพ้นราชการ")] public class RetirementController : BaseController { private readonly RetirementRepository _repository; private readonly NotificationRepository _repositoryNoti; private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly OrganizationCommonRepository _organizationCommonRepository; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IConfiguration _configuration; public RetirementController(RetirementRepository repository, NotificationRepository repositoryNoti, ApplicationDBContext context, MinIOService documentService, IConfiguration configuration, OrganizationCommonRepository organizationCommonRepository, IHttpContextAccessor httpContextAccessor) { _repository = repository; _repositoryNoti = repositoryNoti; _context = context; _documentService = documentService; _organizationCommonRepository = organizationCommonRepository; _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; #endregion #region " จัดลำดับเกษียณ " private class ObjectOrderRetire { public int Order { get; set; } public int? Order1 { get; set; } public int? Order2 { get; set; } public int? Order3 { get; set; } public string LastUpdateFullName { get; set; } public string LastUpdateUserId { get; set; } public DateTime? LastUpdatedAt { get; set; } public Guid? Id { get; set; } } private async Task GenOrderByYear(Guid id) { var Org = await _context.Organizations.FirstOrDefaultAsync(x => x.OrganizationOrder == null); var ocIdList = await _context.Organizations.Select(x => x.Id).ToListAsync(); if (Org != null) ocIdList = _documentService.GetAllIdByRoot(Org.Id); var retire = await _context.RetirementPeriods .FirstOrDefaultAsync(x => x.Id == id); if (retire == null) return; var _retireProfile = await _context.RetirementProfiles .Where(x => x.RetirementPeriod == retire) .ToListAsync(); var profiles = new List(); if (retire.Type.Trim().ToUpper().Contains("OFFICER")) { profiles = await (from x in _context.RetirementProfiles where x.RetirementPeriod == retire select new ObjectOrderRetire { Id = x.Id, Order = x.Order, Order1 = x.Profile == null ? 999999999 : ocIdList.IndexOf((Guid)(x.Profile.OcId)), Order2 = x.Profile == null || x.Profile.PositionType == null ? 999999999 : x.Profile.PositionType.Order, Order3 = x.Profile == null || x.Profile.PositionLevel == null ? 999999999 : x.Profile.PositionLevel.Level, LastUpdateFullName = x.LastUpdateFullName, LastUpdateUserId = x.LastUpdateUserId, LastUpdatedAt = x.LastUpdatedAt, }).ToListAsync(); } if (retire.Type.Trim().ToUpper().Contains("EMPLOYEE")) { profiles = await (from x in _context.RetirementProfiles where x.RetirementPeriod == retire // x.Profile.PositionEmployeeLevelId == null ? null : x.Profile.PositionEmployeeLevelId.Order select new ObjectOrderRetire { Id = x.Id, Order = x.Order, Order1 = x.Profile == null ? null : ocIdList.IndexOf((Guid)(x.Profile.OcId)), LastUpdateFullName = x.LastUpdateFullName, LastUpdateUserId = x.LastUpdateUserId, LastUpdatedAt = x.LastUpdatedAt, }).ToListAsync(); } var _profiles = profiles.AsQueryable().OrderBy(x => x.Order1).ThenBy(x => x.Order2).ThenBy(x => x.Order3).ToList(); var order = 1; foreach (var profile in _profiles) { var retireProfile = _retireProfile.Find(x => x.Id == profile.Id); retireProfile.Order = order; retireProfile.LastUpdateFullName = FullName ?? "System Administrator"; retireProfile.LastUpdateUserId = UserId ?? ""; retireProfile.LastUpdatedAt = DateTime.Now; order++; } await _context.SaveChangesAsync(); } private int SortOrg(Guid? ocId) { if (ocId == null) return 999999999; var Org = _context.Organizations.Include(x => x.OrganizationOrganization).FirstOrDefault(x => x.OrganizationOrder == null); if (Org != null && Org.OrganizationOrganization != null && Org.OrganizationOrganization.Name.Contains("ปลัด")) return -1; var ocIdList = _context.Organizations.Select(x => x.Id).ToList(); if (Org != null) ocIdList = _documentService.GetAllIdByRoot(Org.Id); int index = ocIdList.IndexOf((Guid)ocId); if (index == -1) return 999999999; return index; } private async Task GetAgency(Guid profileId) { var organizationAgency = "-"; var _profile = await _context.Profiles .Where(x => x.Id == profileId) .FirstOrDefaultAsync(); if (_profile != null) { if (_profile.ProfileType == "officer") { var organization = await _context.Organizations .Where(x => x.Id == _profile.OcId) .FirstOrDefaultAsync(); if (organization != null) { var _organizationAgency = await _context.Organizations .Include(x => x.OrganizationOrganization) .Where(x => x.Id == organization.OrganizationAgencyId) .FirstOrDefaultAsync(); if (_organizationAgency != null && _organizationAgency.OrganizationOrganization != null) { organizationAgency = _organizationAgency.OrganizationOrganization.Name; } } } else { var profilePosition = await _context.OrganizationEmployees .Include(x => x.OrganizationAgency) .ThenInclude(x => x.OrganizationOrganization) .Where(x => x.Organization != null) .Where(x => x.OrganizationAgency != null) .Where(x => x.OrganizationAgency.OrganizationOrganization != null) .Where(x => x.OrganizationAgency.OrganizationOrganization.Name != null) .Where(x => x.Organization.Id == _profile.OcId) .FirstOrDefaultAsync(); if (profilePosition != null) { organizationAgency = profilePosition.OrganizationAgency.OrganizationOrganization.Name; } } } return organizationAgency; } private async Task GetGovermentAgency(Guid profileId) { var organizationGovernmentAgency = "-"; var _profile = await _context.Profiles .Where(x => x.Id == profileId) .FirstOrDefaultAsync(); if (_profile != null) { if (_profile.ProfileType == "officer") { var organization = await _context.Organizations .Where(x => x.Id == _profile.OcId) .FirstOrDefaultAsync(); if (organization != null) { var _organizationGovernmentAgency = await _context.Organizations .Include(x => x.OrganizationOrganization) .Where(x => x.Id == organization.OrganizationGovernmentAgencyId) .FirstOrDefaultAsync(); if (_organizationGovernmentAgency != null && _organizationGovernmentAgency.OrganizationOrganization != null) { organizationGovernmentAgency = _organizationGovernmentAgency.OrganizationOrganization.Name; } } } else { var profilePosition = await _context.OrganizationEmployees .Include(x => x.OrganizationGovernmentAgency) .ThenInclude(x => x.OrganizationOrganization) .Where(x => x.Organization != null) .Where(x => x.OrganizationGovernmentAgency != null) .Where(x => x.OrganizationGovernmentAgency.OrganizationOrganization != null) .Where(x => x.OrganizationGovernmentAgency.OrganizationOrganization.Name != null) .Where(x => x.Organization.Id == _profile.OcId) .FirstOrDefaultAsync(); if (profilePosition != null) { organizationGovernmentAgency = profilePosition.OrganizationGovernmentAgency.OrganizationOrganization.Name; } } } return organizationGovernmentAgency; } #endregion /// /// list ประกาศเกษียณอายุราชการ /// /// ประเภทUser(officer,employee)(ตัวใหญ่หรือเล็กก็ได้) /// ปีงบประมาณ(ค.ศ.) /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{type}/{year}")] public async Task> GetRetirement(string type, int year) { if (type.Trim().ToUpper().Contains("OFFICER") || type.Trim().ToUpper().Contains("EMPLOYEE")) { var retire_old = await _context.RetirementPeriodHistorys .Where(x => x.Year == year) .Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper())) .OrderByDescending(x => x.Round) .Select(x => new { Id = x.Id, CreatedAt = x.CreatedAt, Year = x.Year, Round = x.Round, Total = x.Total, TypeReport = x.TypeReport, SignDate = x.SignDate, Detail = x.Detail, Json = true, Document = x.Document == null ? false : true, }) .ToListAsync(); var retire = await _context.RetirementPeriods .Include(x => x.RetirementProfiles) .Include(x => x.Document) .Where(x => x.Year == year) .Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper())) .FirstOrDefaultAsync(); if (retire != null) { retire_old.Add(new { Id = retire.Id, CreatedAt = retire.CreatedAt, Year = retire.Year, Round = retire.Round, Total = retire.RetirementProfiles.Count(), TypeReport = retire.TypeReport, SignDate = retire.SignDate, Detail = retire.Detail, Json = false, Document = retire.Document == null ? false : true, }); } return Success(retire_old); } return Success(); } /// /// สร้างประกาศเกษียณใหม่ /// /// Id ประวัติรอบพ้นราชการ /// ประเภทประกาศ /// ประเภทUser(officer,employee)(ตัวใหญ่หรือเล็กก็ได้) /// ปีงบประมาณ(ค.ศ.) /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("profile")] public async Task> CreateProfileRetirement([FromBody] PeriodRequest req) { if (!req.Type.Trim().ToUpper().Contains("EMPLOYEE") && !req.Type.Trim().ToUpper().Contains("OFFICER")) return Error("ประเภทพ้นราชการไม่ถูกต้อง"); if (req.Year == 0) { req.Year = DateTime.Now.Year; if (req.Year > 2500) req.Year = req.Year - 543; } var round = 1; var retire = await _context.RetirementPeriods .Include(x => x.Document) .Include(x => x.RetirementPeriodHistorys) .Include(x => x.RetirementProfiles) .ThenInclude(x => x.Profile) .Where(x => x.Year == req.Year) .Where(x => x.Type.Trim().ToUpper().Contains(req.Type.Trim().ToUpper())) .FirstOrDefaultAsync(); if (retire == null) { retire = new RetirementPeriod { Round = round, TypeReport = null, Detail = "มาตรา ๑๖ และมาตรา ๒๑ แห่งพระราชบัญญัตบำเหน็จบำนาญข้าราชการส่วนท้องถิ่น พ.ศ.๒๕๗๗ มาตรา ๘ แห่งพระราชบัญญัติบัเหน็จบำนาญข้าราชการกรุงเทพมหานคร พ.ศ.๒๕๕๔ ประกอบกับมติ ก.ก. ครั้งที่๑๑/๒๕๕๕ เมื่อวันที่ ๑๕ พฤศจิกายน ๒๕๕๕และมติ อ.ก.ก. สามัญข้ารายการสามัญครั้งที่ ๑/๒๕๖๕ เมื่อวันที่ ๒๑ กุมภาพันธ์ ๒๕๖๕", Type = req.Type.Trim().ToUpper(), Year = req.Year, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementPeriods.AddAsync(retire); var profiles = await _context.Profiles.AsQueryable() .Where(x => x.ProfileType.Trim().ToUpper().Contains(req.Type.Trim().ToUpper())) .ToListAsync(); profiles = profiles.Where(x => x.BirthDate.CalculateRetireDate().Year == req.Year).ToList(); var order = 1; foreach (var profile in profiles) { var data = new RetirementProfile { Order = order, Remove = "PENDING", RetirementPeriod = retire, Profile = profile, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementProfiles.AddAsync(data); order++; } await _context.SaveChangesAsync(); await GenOrderByYear(retire.Id); } else { if (retire.Document == null) return Error(GlobalMessages.RetirementNotCreated); if (req.Option == null) req.Option = "EDIT"; var profile_old = await _context.RetirementProfiles .Where(x => x.RetirementPeriod == retire) .Select(x => new { order = x.Order, id = x.Id, reason = x.Reason, remove = x.Remove, profileId = x.Profile.Id, citizenId = x.Profile.CitizenId, prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name, fullName = $"{(x.Profile.Prefix == null ? null : x.Profile.Prefix.Name)}{x.Profile.FirstName} {x.Profile.LastName}", organizationOrganization = x.Profile.OrganizationOrganization, ocId = x.Profile.OcId, position = x.Profile.Position == null ? null : x.Profile.Position.Name, positionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name, positionExecutive = x.Profile.PositionExecutive, posNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name, positionEmployeePosition = x.Profile.PositionEmployeePosition == null ? null : x.Profile.PositionEmployeePosition.Name, positionEmployeeLevel = x.Profile.PositionEmployeeLevel == null ? null : x.Profile.PositionEmployeeLevel.Name, positionEmployeeGroup = x.Profile.PositionEmployeeGroup == null ? null : x.Profile.PositionEmployeeGroup.Name, posNoEmployee = x.Profile.PosNoEmployee, }) .ToListAsync(); var file_name = $"retire_tmp_{DateTime.Now.ToString("yyyyMMddTHHmmss")}"; var profile_olds = new List(); foreach (var r in profile_old) { var organizationAgency = await GetAgency(r.profileId); var organizationGovernmentAgency = await GetGovermentAgency(r.profileId); var data = new ProfileJsonRequest { order = r.order, id = r.id, reason = r.reason, remove = r.remove, profileId = r.profileId, citizenId = r.citizenId, prefix = r.prefix, fullName = r.fullName, organizationOrganization = r.organizationOrganization, oc = r.ocId == null ? null : _organizationCommonRepository.GetOrganizationNameFullPath(r.ocId.Value), position = r.position, positionType = r.positionType, positionExecutive = r.positionExecutive, posNo = r.posNo, positionEmployeePosition = r.positionEmployeePosition, positionEmployeeLevel = r.positionEmployeeLevel, positionEmployeeGroup = r.positionEmployeeGroup, posNoEmployee = r.posNoEmployee, organizationAgency = organizationAgency,//หน่วยงานต้นสังกัด organizationGovernmentAgency = organizationGovernmentAgency,//ส่วนราชการต้นสังกัด }; profile_olds.Add(data); } var profile = Newtonsoft.Json.JsonConvert.SerializeObject(profile_olds); await _documentService.GenerateJsonFile(profile, "/retire", file_name); var history = new RetirementPeriodHistory { RetirementPeriod = retire, Round = retire.Round, TypeReport = retire.TypeReport, SignDate = retire.SignDate, Detail = retire.Detail, Year = retire.Year, Type = retire.Type, Document = retire.Document, Total = retire.RetirementProfiles.Count(), ProfileFile = file_name, CreatedUserId = retire.CreatedUserId, CreatedFullName = retire.CreatedFullName, CreatedAt = retire.CreatedAt, LastUpdateFullName = retire.LastUpdateFullName, LastUpdateUserId = retire.LastUpdateUserId, LastUpdatedAt = retire.LastUpdatedAt, }; await _context.RetirementPeriodHistorys.AddAsync(history); // await _context.SaveChangesAsync(); retire.Document = null; retire.Round = retire.Round + 1; retire.TypeReport = req.Option.Trim().ToUpper(); retire.SignDate = null; retire.CreatedUserId = UserId ?? "System Administrator"; retire.CreatedFullName = FullName ?? ""; retire.CreatedAt = DateTime.Now; retire.LastUpdateFullName = FullName ?? "System Administrator"; retire.LastUpdateUserId = UserId ?? ""; retire.LastUpdatedAt = DateTime.Now; _context.RetirementProfiles.RemoveRange(retire.RetirementProfiles); if (!req.Option.Trim().ToUpper().Contains("ADD")) { var profileHistorys = await _context.RetirementPeriodHistorys.AsQueryable() .FirstOrDefaultAsync(x => x.Id == req.RetireHistoryId); // var profileOlds = new List(); if (profileHistorys != null) { using (var client = new HttpClient()) { // var url = $"https://s3cluster.frappet.com/bma-ehr-fpt/{profileHistorys.ProfileFile}.json"; var url = await _documentService.ImagesPathByName($"{profileHistorys.ProfileFile}.json"); var responseTask = client.GetAsync(url); var results = responseTask.Result; var profileOlds = Newtonsoft.Json.JsonConvert.DeserializeObject>(results.Content.ReadAsStringAsync().Result); if (profileOlds != null) { profileOlds = profileOlds.Where(x => !x.remove.Trim().ToUpper().Contains("REMOVE")).ToList(); foreach (var profileOld in profileOlds) { var _profile = await _context.Profiles.AsQueryable() .FirstOrDefaultAsync(x => x.CitizenId == profileOld.citizenId); var data = new RetirementProfile { Order = profileOld.order, Remove = "PENDING", RetirementPeriod = retire, Profile = _profile, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; retire.RetirementProfiles.Add(data); } } } } else { var retireOld = await _context.RetirementPeriods .FirstOrDefaultAsync(x => x.Id == req.RetireHistoryId); if (retireOld == null) return Error(GlobalMessages.RetirementNotFound, 404); var profileOlds = await _context.RetirementProfiles.AsQueryable() .Where(x => x.RetirementPeriod == retireOld) .ToListAsync(); if (profileOlds != null) { profileOlds = profileOlds.Where(x => !x.Remove.Trim().ToUpper().Contains("REMOVE")).ToList(); foreach (var profileOld in profileOlds) { var data = new RetirementProfile { Order = profileOld.Order, Remove = "PENDING", RetirementPeriod = retire, Profile = profileOld.Profile, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; retire.RetirementProfiles.Add(data); } } } } await _context.SaveChangesAsync(); } // await _context.SaveChangesAsync(); var profile_new = await _context.RetirementProfiles .Where(x => x.RetirementPeriod == retire) .Select(x => new { order = x.Order, id = x.Id, reason = x.Reason, remove = x.Remove, profileId = x.Profile.Id, citizenId = x.Profile.CitizenId, prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name, fullName = $"{(x.Profile.Prefix == null ? null : x.Profile.Prefix.Name)}{x.Profile.FirstName} {x.Profile.LastName}", organizationOrganization = x.Profile.OrganizationOrganization, ocId = x.Profile.OcId, position = x.Profile.Position == null ? null : x.Profile.Position.Name, positionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name, positionExecutive = x.Profile.PositionExecutive, posNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name, positionEmployeePosition = x.Profile.PositionEmployeePosition == null ? null : x.Profile.PositionEmployeePosition.Name, positionEmployeeLevel = x.Profile.PositionEmployeeLevel == null ? null : x.Profile.PositionEmployeeLevel.Name, positionEmployeeGroup = x.Profile.PositionEmployeeGroup == null ? null : x.Profile.PositionEmployeeGroup.Name, posNoEmployee = x.Profile.PosNoEmployee, }) .ToListAsync(); var profile_news = new List(); foreach (var r in profile_new) { var organizationAgency = await GetAgency(r.profileId); var organizationGovernmentAgency = await GetGovermentAgency(r.profileId); var data = new ProfileJsonRequest { order = r.order, id = r.id, reason = r.reason, remove = r.remove, profileId = r.profileId, citizenId = r.citizenId, prefix = r.prefix, fullName = r.fullName, organizationOrganization = r.organizationOrganization, oc = r.ocId == null ? null : _organizationCommonRepository.GetOrganizationNameFullPath(r.ocId.Value), position = r.position, positionType = r.positionType, positionExecutive = r.positionExecutive, posNo = r.posNo, positionEmployeePosition = r.positionEmployeePosition, positionEmployeeLevel = r.positionEmployeeLevel, positionEmployeeGroup = r.positionEmployeeGroup, posNoEmployee = r.posNoEmployee, organizationAgency = organizationAgency,//หน่วยงานต้นสังกัด organizationGovernmentAgency = organizationGovernmentAgency,//ส่วนราชการต้นสังกัด }; profile_news.Add(data); } return Success(new { retire.Id, retire.CreatedAt, retire.Year, retire.Round, retire.Type, retire.TypeReport, retire.SignDate, retire.Detail, profile = profile_news }); } /// /// View รายชื่อผู้เกษียณอายุราชการในประกาศ /// /// Id ประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{retireId:length(36)}")] public async Task> GetProfileRetirement(Guid retireId) { var retire = await _context.RetirementPeriods .Include(x => x.RetirementProfiles) .Include(x => x.Document) .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) { var profileHistorys = await _context.RetirementPeriodHistorys.AsQueryable() .Include(x => x.Document) .FirstOrDefaultAsync(x => x.Id == retireId); if (profileHistorys == null) return Error(GlobalMessages.RetirementHistoryNotFound, 404); using (var client = new HttpClient()) { // var Endpoint = _configuration["MinIO:Endpoint"]; // var BucketName = _configuration["MinIO:BucketName"]; var url = await _documentService.ImagesPathByName($"{profileHistorys.ProfileFile}.json"); // var url = $"{Endpoint}{BucketName}/{profileHistorys.ProfileFile}.json"; var responseTask = client.GetAsync(url); var results = responseTask.Result; var filehis = profileHistorys.Document == null ? null : await _documentService.ImagesPath(profileHistorys.Document.Id); return Success(new { Json = true, profileHistorys.Id, profileHistorys.CreatedAt, profileHistorys.Year, profileHistorys.Round, profileHistorys.Type, profileHistorys.TypeReport, profileHistorys.SignDate, profileHistorys.Detail, profile = Newtonsoft.Json.JsonConvert.DeserializeObject>(results.Content.ReadAsStringAsync().Result), File = filehis }); } } var profile_new = await _context.RetirementProfiles .Where(x => x.RetirementPeriod == retire) .Select(x => new { order = x.Order, id = x.Id, reason = x.Reason, remove = x.Remove, profileId = x.Profile.Id, citizenId = x.Profile.CitizenId, prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name, fullName = $"{(x.Profile.Prefix == null ? null : x.Profile.Prefix.Name)}{x.Profile.FirstName} {x.Profile.LastName}", organizationOrganization = x.Profile.OrganizationOrganization, ocId = x.Profile.OcId, position = x.Profile.Position == null ? null : x.Profile.Position.Name, positionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name, positionExecutive = x.Profile.PositionExecutive, posNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name, positionEmployeePosition = x.Profile.PositionEmployeePosition == null ? null : x.Profile.PositionEmployeePosition.Name, positionEmployeeLevel = x.Profile.PositionEmployeeLevel == null ? null : x.Profile.PositionEmployeeLevel.Name, positionEmployeeGroup = x.Profile.PositionEmployeeGroup == null ? null : x.Profile.PositionEmployeeGroup.Name, posNoEmployee = x.Profile.PosNoEmployee, }) .ToListAsync(); var profile_news = new List(); foreach (var r in profile_new) { var organizationAgency = await GetAgency(r.profileId); var organizationGovernmentAgency = await GetGovermentAgency(r.profileId); var data = new ProfileJsonRequest { order = r.order, id = r.id, reason = r.reason, remove = r.remove, profileId = r.profileId, citizenId = r.citizenId, prefix = r.prefix, fullName = r.fullName, organizationOrganization = r.organizationOrganization, oc = r.ocId == null ? null : _organizationCommonRepository.GetOrganizationNameFullPath(r.ocId.Value), position = r.position, positionType = r.positionType, positionExecutive = r.positionExecutive, posNo = r.posNo, positionEmployeePosition = r.positionEmployeePosition, positionEmployeeLevel = r.positionEmployeeLevel, positionEmployeeGroup = r.positionEmployeeGroup, posNoEmployee = r.posNoEmployee, organizationAgency = organizationAgency,//หน่วยงานต้นสังกัด organizationGovernmentAgency = organizationGovernmentAgency,//ส่วนราชการต้นสังกัด }; profile_news.Add(data); } var file = retire.Document == null ? null : await _documentService.ImagesPath(retire.Document.Id); return Success(new { Json = false, retire.Id, retire.CreatedAt, retire.Year, retire.Round, retire.Type, retire.TypeReport, retire.SignDate, retire.Detail, profile = profile_news, File = file }); } /// /// Delete รายชื่อผู้เกษียณอายุราชการในประกาศ /// /// Id ผู้ใช้งานในประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน // [HttpDelete("profile/{retireProfileId:length(36)}")] // public async Task> DeleteProfileRetirement(Guid retireProfileId) // { // var profile = await _context.RetirementProfiles // .FirstOrDefaultAsync(x => x.Id == retireProfileId); // if (profile == null) // return Error(GlobalMessages.DataNotFound, 404); // _context.RetirementProfiles.Remove(profile); // _context.SaveChanges(); // return Success(); // } /// /// Add รายชื่อผู้เกษียณอายุราชการในประกาศ /// /// Id ประกาศ /// Id ผู้ใช้งาน /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("profile/{retireId:length(36)}")] public async Task> AddProfileRetirement([FromBody] ProfileRequest req, Guid retireId) { var profile = await _context.Profiles .FirstOrDefaultAsync(x => x.Id == req.ProfileId); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); var retire = await _context.RetirementPeriods .Include(x => x.RetirementPeriodHistorys) .Include(x => x.RetirementProfiles) .ThenInclude(x => x.Profile) .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); if (retire.RetirementProfiles.Where(x => x.Profile == profile).Count() > 0) return Error("บุคคลนี้ได้ทำการเลือกไว้อยู่แล้ว"); foreach (var retire_profile in retire.RetirementProfiles) { retire_profile.Order++; retire_profile.LastUpdateFullName = FullName ?? "System Administrator"; retire_profile.LastUpdateUserId = UserId ?? ""; retire_profile.LastUpdatedAt = DateTime.Now; } var num = 1; // var old_retire = retire.RetirementProfiles.OrderByDescending(x => x.Order).FirstOrDefault(); // if (old_retire != null) // num = old_retire.Order + 1; var data = new RetirementProfile { Order = num, Remove = "ADD", RetirementPeriod = retire, Profile = profile, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; _context.RetirementProfiles.Add(data); await _context.SaveChangesAsync(); if (retire.RetirementPeriodHistorys.Count() == 0) { await GenOrderByYear(retire.Id); } return Success(); } /// /// แก้ไขข้อมูลบุคคล /// /// Id ผู้ใช้งานในประกาศ /// เหตุผล /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("edit")] public async Task> EditReasonProfileRetirement([FromBody] ProfileRetireRequest req) { var profile = await _context.RetirementProfiles .FirstOrDefaultAsync(x => x.Id == req.RetireProfileId); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); profile.Remove = "EDIT"; profile.Reason = req.Reason; profile.LastUpdateFullName = FullName ?? "System Administrator"; profile.LastUpdateUserId = UserId ?? ""; profile.LastUpdatedAt = DateTime.Now; _context.SaveChanges(); return Success(); } /// /// ลบข้อมูลบุคคล /// /// Id ผู้ใช้งานในประกาศ /// เหตุผล /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("remove")] public async Task> RemoveReasonProfileRetirement([FromBody] ProfileRetireRequest req) { var profile = await _context.RetirementProfiles .Include(x => x.RetirementPeriod) .ThenInclude(x => x.RetirementPeriodHistorys) .FirstOrDefaultAsync(x => x.Id == req.RetireProfileId); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); if (profile.RetirementPeriod.RetirementPeriodHistorys.Count() == 0) { _context.RetirementProfiles.Remove(profile); _context.SaveChanges(); await GenOrderByYear(profile.RetirementPeriod.Id); } else { profile.Remove = "REMOVE"; profile.Reason = req.Reason; profile.LastUpdateFullName = FullName ?? "System Administrator"; profile.LastUpdateUserId = UserId ?? ""; profile.LastUpdatedAt = DateTime.Now; } _context.SaveChanges(); return Success(); } /// /// View เหตุผลแก้ไขข้อมูลบุคคล /// /// Id ผู้ใช้งานในประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("reason/{retireProfileId:length(36)}")] public async Task> ViewReasonProfileRetirement(Guid retireProfileId) { var profile = await _context.RetirementProfiles .Select(x => new { Id = x.Id, Reason = x.Reason, }) .FirstOrDefaultAsync(x => x.Id == retireProfileId); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); return Success(profile); } /// /// จัดอันดับเกษียณ /// /// Id ประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost("{retireId:length(36)}")] public async Task> UpdateProfileRetirement(Guid retireId) { var retire = await _context.RetirementPeriods .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); await GenOrderByYear(retire.Id); return Success(); } /// /// อัพเอกสารเกษียณอายุราชการ /// /// Id ประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("upload/{retireId:length(36)}")] public async Task> UploadRetirement([FromForm] RetirementFileRequest req, Guid retireId) { var retire = await _context.RetirementPeriods .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); if (Request.Form.Files != null && Request.Form.Files.Count != 0) { var file = Request.Form.Files[0]; 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); retire.SignDate = req.signDate; retire.Document = _doc; retire.LastUpdateFullName = FullName ?? "System Administrator"; retire.LastUpdateUserId = UserId ?? ""; retire.LastUpdatedAt = DateTime.Now; } await _context.SaveChangesAsync(); return Success(); } /// /// ตรวจสอบการอัพเอกสารเกษียญ false=ยังไม่อัพโหลด true=อัพโหลดเอกสารแล้ว /// /// Id ประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("upload/check/{retireId:length(36)}")] public async Task> CheckUploadRetirement(Guid retireId) { var retire = await _context.RetirementPeriods .Include(x => x.Document) .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); if (retire.Document == null) return Success(false); return Success(true); } /// /// แก้ไขข้อความมติ อ.ก.ก. /// /// Id ประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("detail/{retireId:length(36)}")] public async Task> UpdateDetailRetirement([FromForm] RetirementDetailRequest req, Guid retireId) { var retire = await _context.RetirementPeriods .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) { var retireHistory = await _context.RetirementPeriodHistorys .FirstOrDefaultAsync(x => x.Id == retireId); if (retireHistory == null) return Error(GlobalMessages.RetirementNotFound, 404); retireHistory.Detail = req.Detail; } else { retire.Detail = req.Detail; } await _context.SaveChangesAsync(); return Success(true); } } }