using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Application.Repositories.Reports; 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 Newtonsoft.Json; using Newtonsoft.Json.Linq; using Swashbuckle.AspNetCore.Annotations; using System.Net.Http.Headers; using System.Security.Claims; using System.Text; 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; private readonly PermissionRepository _permission; private readonly DisciplineDbContext _contextDiscipline; private readonly RetireReportRepository _service; public RetirementController(RetirementRepository repository, NotificationRepository repositoryNoti, ApplicationDBContext context, MinIOService documentService, IConfiguration configuration, OrganizationCommonRepository organizationCommonRepository, IHttpContextAccessor httpContextAccessor, PermissionRepository permission, DisciplineDbContext contextDiscipline, RetireReportRepository service) { _repository = repository; _repositoryNoti = repositoryNoti; _context = context; _documentService = documentService; _organizationCommonRepository = organizationCommonRepository; _httpContextAccessor = httpContextAccessor; _configuration = configuration; _permission = permission; _contextDiscipline = contextDiscipline; _service = service; } #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 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.Order, // // // Order1 = x.Order == null ? 999999999 : x.Order, // // Order2 = x.posTypeRank, // // Order3 = x.posLevelRank, // // 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.Order, // // // Order1 = x.Order == null ? 999999999 : x.Order, // // 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(); // } /// /// list ประกาศเกษียณอายุราชการ /// /// ประเภทUser(officer,employee)(ตัวใหญ่หรือเล็กก็ได้) /// ปีงบประมาณ(ค.ศ.) /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{type}/{year}")] public async Task> GetRetirement(string type, int year) { var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } 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) { var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } 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) .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, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementPeriods.AddAsync(retire); var profiles = new List(); if (req.Type.Trim().ToUpper().Contains("OFFICER")) { var apiUrl = $"{_configuration["API"]}/org/profile/profileid/retire/{req.Year}"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl); var _res = await client.SendAsync(_req); var _result = await _res.Content.ReadAsStringAsync(); var org = JsonConvert.DeserializeObject(_result); if (org != null && org.result != null) profiles = org.result; } } else { var apiUrl = $"{_configuration["API"]}/org/profile-employee/profileid/retire/{req.Year}"; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl); var _res = await client.SendAsync(_req); var _result = await _res.Content.ReadAsStringAsync(); var org = JsonConvert.DeserializeObject(_result); if (org != null && org.result != null) profiles = org.result; } } // var retires = new List(); var _apiUrl = $"{_configuration["API"]}/org/root/search/sort"; dynamic rootOrder = new List(); dynamic child1Order = new List(); dynamic child2Order = new List(); dynamic child3Order = new List(); dynamic child4Order = new List(); dynamic posTypeNameOrder = new List(); dynamic posLevelNameOrder = new List(); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); var _req = new HttpRequestMessage(HttpMethod.Get, _apiUrl); var _res = await client.SendAsync(_req); var _result = await _res.Content.ReadAsStringAsync(); var org = JsonConvert.DeserializeObject(_result); if (org != null && org.result != null) { rootOrder = org.result.root; child1Order = org.result.child1; child2Order = org.result.child2; child3Order = org.result.child3; child4Order = org.result.child4; posTypeNameOrder = org.result.posTypeNameOrder; posLevelNameOrder = org.result.posLevelNameOrder; } } var order = 1; foreach (var profile in profiles .OrderBy(x => string.IsNullOrEmpty(x.root) ? int.MaxValue : rootOrder.ToObject>().IndexOf(x.root)) .ThenBy(x => child1Order.ToObject>().IndexOf(x.child1 ?? "")) .ThenBy(x => child2Order.ToObject>().IndexOf(x.child2 ?? "")) .ThenBy(x => child3Order.ToObject>().IndexOf(x.child3 ?? "")) .ThenBy(x => child4Order.ToObject>().IndexOf(x.child4 ?? "")) .ThenBy(x => posTypeNameOrder.ToObject>().IndexOf(x.posTypeName ?? "")) .ThenBy(x => posLevelNameOrder.ToObject>().IndexOf(x.posLevelName ?? "")) .ThenBy(x => x.posNo)) { var data = new RetirementProfile { Order = order, Remove = "PENDING", RetirementPeriod = retire, profileId = profile.profileId, prefix = profile.prefix, firstName = profile.firstName, lastName = profile.lastName, citizenId = profile.citizenId, root = profile.root, rootId = profile.rootId, rootShortName = profile.rootShortName, child1 = profile.child1, child1Id = profile.child1Id, child1ShortName = profile.child1ShortName, child2 = profile.child2, child2Id = profile.child2Id, child2ShortName = profile.child2ShortName, child3 = profile.child3, child3Id = profile.child3Id, child3ShortName = profile.child3ShortName, child4 = profile.child4, child4Id = profile.child4Id, child4ShortName = profile.child4ShortName, posMasterNo = profile.posMasterNo, position = profile.position, posTypeId = profile.posTypeId, posTypeName = profile.posTypeName, posTypeRank = profile.posTypeRank, posLevelId = profile.posLevelId, posLevelName = profile.posLevelName, posLevelRank = profile.posLevelRank, posExecutiveId = profile.posExecutiveId, posExecutiveName = profile.posExecutiveName, positionExecutiveField = profile.positionExecutiveField, positionArea = profile.positionArea, posNo = profile.posNo, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; var dataRaw = new RetirementRawProfile { Order = order, Remove = "PENDING", RetirementPeriod = retire, profileId = profile.profileId, prefix = profile.prefix, firstName = profile.firstName, lastName = profile.lastName, citizenId = profile.citizenId, root = profile.root, rootId = profile.rootId, rootShortName = profile.rootShortName, child1 = profile.child1, child1Id = profile.child1Id, child1ShortName = profile.child1ShortName, child2 = profile.child2, child2Id = profile.child2Id, child2ShortName = profile.child2ShortName, child3 = profile.child3, child3Id = profile.child3Id, child3ShortName = profile.child3ShortName, child4 = profile.child4, child4Id = profile.child4Id, child4ShortName = profile.child4ShortName, posMasterNo = profile.posMasterNo, position = profile.position, posTypeId = profile.posTypeId, posTypeName = profile.posTypeName, posTypeRank = profile.posTypeRank, posLevelId = profile.posLevelId, posLevelName = profile.posLevelName, posLevelRank = profile.posLevelRank, posExecutiveId = profile.posExecutiveId, posExecutiveName = profile.posExecutiveName, positionExecutiveField = profile.positionExecutiveField, positionArea = profile.positionArea, posNo = profile.posNo, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; await _context.RetirementProfiles.AddAsync(data); await _context.RetirementRawProfiles.AddAsync(dataRaw); 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.profileId, prefix = x.prefix, firstName = x.firstName, lastName = x.lastName, citizenId = x.citizenId, root = x.root, rootId = x.rootId, rootShortName = x.rootShortName, child1 = x.child1, child1Id = x.child1Id, child1ShortName = x.child1ShortName, child2 = x.child2, child2Id = x.child2Id, child2ShortName = x.child2ShortName, child3 = x.child3, child3Id = x.child3Id, child3ShortName = x.child3ShortName, child4 = x.child4, child4Id = x.child4Id, child4ShortName = x.child4ShortName, posMasterNo = x.posMasterNo, position = x.position, posTypeId = x.posTypeId, posTypeName = x.posTypeName, posTypeRank = x.posTypeRank, posLevelId = x.posLevelId, posLevelName = x.posLevelName, posLevelRank = x.posLevelRank, posExecutiveId = x.posExecutiveId, posExecutiveName = x.posExecutiveName, positionExecutiveField = x.positionExecutiveField, positionArea = x.positionArea, posNo = x.posNo, }) .OrderBy(x => x.order) .ToListAsync(); var file_name = $"retire_tmp_{DateTime.Now.ToString("yyyyMMddTHHmmss")}"; var profile = Newtonsoft.Json.JsonConvert.SerializeObject(profile_old); 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.CreatedFullName = FullName ?? "System Administrator"; retire.CreatedUserId = UserId ?? ""; 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 data = new RetirementProfile { Order = profileOld.order, Remove = "PENDING", RetirementPeriod = retire, profileId = profileOld.profileId, prefix = profileOld.prefix, firstName = profileOld.firstName, lastName = profileOld.lastName, citizenId = profileOld.citizenId, root = profileOld.root, rootId = profileOld.rootId, rootShortName = profileOld.rootShortName, child1 = profileOld.child1, child1Id = profileOld.child1Id, child1ShortName = profileOld.child1ShortName, child2 = profileOld.child2, child2Id = profileOld.child2Id, child2ShortName = profileOld.child2ShortName, child3 = profileOld.child3, child3Id = profileOld.child3Id, child3ShortName = profileOld.child3ShortName, child4 = profileOld.child4, child4Id = profileOld.child4Id, child4ShortName = profileOld.child4ShortName, posMasterNo = profileOld.posMasterNo, position = profileOld.position, posTypeId = profileOld.posTypeId, posTypeName = profileOld.posTypeName, posTypeRank = profileOld.posTypeRank, posLevelId = profileOld.posLevelId, posLevelName = profileOld.posLevelName, posLevelRank = profileOld.posLevelRank, posExecutiveId = profileOld.posExecutiveId, posExecutiveName = profileOld.posExecutiveName, positionExecutiveField = profileOld.positionExecutiveField, positionArea = profileOld.positionArea, posNo = profileOld.posNo, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", 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) .OrderBy(x => x.Order) .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, profileId = profileOld.profileId, prefix = profileOld.prefix, firstName = profileOld.firstName, lastName = profileOld.lastName, citizenId = profileOld.citizenId, root = profileOld.root, rootId = profileOld.rootId, rootShortName = profileOld.rootShortName, child1 = profileOld.child1, child1Id = profileOld.child1Id, child1ShortName = profileOld.child1ShortName, child2 = profileOld.child2, child2Id = profileOld.child2Id, child2ShortName = profileOld.child2ShortName, child3 = profileOld.child3, child3Id = profileOld.child3Id, child3ShortName = profileOld.child3ShortName, child4 = profileOld.child4, child4Id = profileOld.child4Id, child4ShortName = profileOld.child4ShortName, posMasterNo = profileOld.posMasterNo, position = profileOld.position, posTypeId = profileOld.posTypeId, posTypeName = profileOld.posTypeName, posTypeRank = profileOld.posTypeRank, posLevelId = profileOld.posLevelId, posLevelName = profileOld.posLevelName, posLevelRank = profileOld.posLevelRank, posExecutiveId = profileOld.posExecutiveId, posExecutiveName = profileOld.posExecutiveName, positionExecutiveField = profileOld.positionExecutiveField, positionArea = profileOld.positionArea, posNo = profileOld.posNo, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", 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.profileId, citizenId = x.citizenId, prefix = x.prefix, firstName = x.firstName, lastName = x.lastName, fullName = $"{x.prefix}{x.firstName} {x.lastName}", root = x.root, rootId = x.rootId, child1 = x.child1, child1Id = x.child1Id, child2 = x.child2, child2Id = x.child2Id, child3 = x.child3, child3Id = x.child3Id, child4 = x.child4, child4Id = x.child4Id, position = x.position, positionType = x.posTypeName, positionLevel = x.posLevelName, positionExecutive = x.posExecutiveName, positionExecutiveField = x.positionExecutiveField, positionArea = x.positionArea, posNo = x.posNo, }) .OrderBy(x => x.order) .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_new }); } /// /// View รายชื่อผู้เกษียณอายุราชการในประกาศ /// /// Id ประกาศ /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{retireId:length(36)}")] public async Task> GetProfileRetirement(Guid retireId) { var getWorkflow = await _permission.GetPermissionAPIWorkflowAsync(retireId.ToString(), "SYS_RETIREMENT"); if (getWorkflow == false) { var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } } 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 = results.IsSuccessStatusCode ? Newtonsoft.Json.JsonConvert.DeserializeObject>(results.Content.ReadAsStringAsync().Result) : null, 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.profileId, citizenId = x.citizenId, prefix = x.prefix, firstName = x.firstName, lastName = x.lastName, fullName = $"{x.prefix}{x.firstName} {x.lastName}", root = x.root, rootId = x.rootId, child1 = x.child1, child1Id = x.child1Id, child2 = x.child2, child2Id = x.child2Id, child3 = x.child3, child3Id = x.child3Id, child4 = x.child4, child4Id = x.child4Id, position = x.position, posTypeName = x.posTypeName, posLevelName = x.posLevelName, posExecutiveName = x.posExecutiveName, positionExecutiveField = x.positionExecutiveField, positionArea = x.positionArea, posNo = x.posNo, // 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, }) .OrderBy(x => x.order) .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_new, 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 getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var retire = await _context.RetirementPeriods .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); var retireProfile = await _context.RetirementProfiles .FirstOrDefaultAsync(x => x.profileId == req.ProfileId && x.RetirementPeriod.Id == retireId); if (retireProfile != null) return Error("บุคคลนี้ได้ทำการเลือกไว้อยู่แล้ว"); var numRaw = 0; if (retire.TypeReport != null) { numRaw = 1; var profileRawCount = await _context.RetirementRawProfiles .Where(x => x.RetirementPeriod.Id == retireId) .OrderByDescending(x => x.Order) .FirstOrDefaultAsync(); if (profileRawCount != null) numRaw = profileRawCount.Order + 1; } if (retire.Type == "OFFICER") { var apiUrl = $"{_configuration["API"]}/org/profile/profileid/position/{req.ProfileId}"; using (var client = new HttpClient()) { var data = new RetirementProfile { Order = numRaw, Remove = "ADD", RetirementPeriod = retire, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; var dataRaw = new RetirementRawProfile { Order = numRaw, Remove = "ADD", RetirementPeriod = retire, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; 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); data.profileId = org.result.profileId; data.prefix = org.result.prefix; data.firstName = org.result.firstName; data.lastName = org.result.lastName; data.citizenId = org.result.citizenId; data.root = org.result.root; data.rootId = org.result.rootId; data.rootShortName = org.result.rootShortName; data.child1 = org.result.child1; data.child1Id = org.result.child1Id; data.child1ShortName = org.result.child1ShortName; data.child2 = org.result.child2; data.child2Id = org.result.child2Id; data.child2ShortName = org.result.child2ShortName; data.child3 = org.result.child3; data.child3Id = org.result.child3Id; data.child3ShortName = org.result.child3ShortName; data.child4 = org.result.child4; data.child4Id = org.result.child4Id; data.child4ShortName = org.result.child4ShortName; data.posMasterNo = org.result.posMasterNo; data.position = org.result.position; data.posTypeId = org.result.posTypeId; data.posTypeName = org.result.posTypeName; data.posTypeRank = org.result.posTypeRank; data.posLevelId = org.result.posLevelId; data.posLevelName = org.result.posLevelName; data.posLevelRank = org.result.posLevelRank; data.posExecutiveId = org.result.posExecutiveId; data.posExecutiveName = org.result.posExecutiveName; data.positionExecutiveField = org.result.positionExecutiveField; data.positionArea = org.result.positionArea; data.posNo = org.result.posNo; dataRaw.profileId = org.result.profileId; dataRaw.prefix = org.result.prefix; dataRaw.firstName = org.result.firstName; dataRaw.lastName = org.result.lastName; dataRaw.citizenId = org.result.citizenId; dataRaw.root = org.result.root; dataRaw.rootId = org.result.rootId; dataRaw.rootShortName = org.result.rootShortName; dataRaw.child1 = org.result.child1; dataRaw.child1Id = org.result.child1Id; dataRaw.child1ShortName = org.result.child1ShortName; dataRaw.child2 = org.result.child2; dataRaw.child2Id = org.result.child2Id; dataRaw.child2ShortName = org.result.child2ShortName; dataRaw.child3 = org.result.child3; dataRaw.child3Id = org.result.child3Id; dataRaw.child3ShortName = org.result.child3ShortName; dataRaw.child4 = org.result.child4; dataRaw.child4Id = org.result.child4Id; dataRaw.child4ShortName = org.result.child4ShortName; dataRaw.posMasterNo = org.result.posMasterNo; dataRaw.position = org.result.position; dataRaw.posTypeId = org.result.posTypeId; dataRaw.posTypeName = org.result.posTypeName; dataRaw.posTypeRank = org.result.posTypeRank; dataRaw.posLevelId = org.result.posLevelId; dataRaw.posLevelName = org.result.posLevelName; dataRaw.posLevelRank = org.result.posLevelRank; dataRaw.posExecutiveId = org.result.posExecutiveId; dataRaw.posExecutiveName = org.result.posExecutiveName; dataRaw.positionExecutiveField = org.result.positionExecutiveField; dataRaw.positionArea = org.result.positionArea; dataRaw.posNo = org.result.posNo; _context.RetirementProfiles.Add(data); _context.RetirementRawProfiles.Add(dataRaw); await _context.SaveChangesAsync(); // if (retire.RetirementPeriodHistorys.Count() == 0) // { // await GenOrderByYear(retire.Id); // } return Success(); } } else { var apiUrl = $"{_configuration["API"]}/org/profile-employee/profileid/position/{req.ProfileId}"; using (var client = new HttpClient()) { var data = new RetirementProfile { Order = numRaw, Remove = "ADD", RetirementPeriod = retire, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; var dataRaw = new RetirementRawProfile { Order = numRaw, Remove = "ADD", RetirementPeriod = retire, CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; 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); data.profileId = org.result.profileId; data.prefix = org.result.prefix; data.firstName = org.result.firstName; data.lastName = org.result.lastName; data.citizenId = org.result.citizenId; data.root = org.result.root; data.rootId = org.result.rootId; data.rootShortName = org.result.rootShortName; data.child1 = org.result.child1; data.child1Id = org.result.child1Id; data.child1ShortName = org.result.child1ShortName; data.child2 = org.result.child2; data.child2Id = org.result.child2Id; data.child2ShortName = org.result.child2ShortName; data.child3 = org.result.child3; data.child3Id = org.result.child3Id; data.child3ShortName = org.result.child3ShortName; data.child4 = org.result.child4; data.child4Id = org.result.child4Id; data.child4ShortName = org.result.child4ShortName; data.posMasterNo = org.result.posMasterNo; data.position = org.result.position; data.posTypeId = org.result.posTypeId; data.posTypeName = org.result.posTypeName; data.posTypeRank = org.result.posTypeRank; data.posLevelId = org.result.posLevelId; data.posLevelName = org.result.posLevelName; data.posLevelRank = org.result.posLevelRank; //data.posExecutiveId = org.result.posExecutiveId; //data.posExecutiveName = org.result.posExecutiveName; data.posNo = org.result.posNo; dataRaw.profileId = org.result.profileId; dataRaw.prefix = org.result.prefix; dataRaw.firstName = org.result.firstName; dataRaw.lastName = org.result.lastName; dataRaw.citizenId = org.result.citizenId; dataRaw.root = org.result.root; dataRaw.rootId = org.result.rootId; dataRaw.rootShortName = org.result.rootShortName; dataRaw.child1 = org.result.child1; dataRaw.child1Id = org.result.child1Id; dataRaw.child1ShortName = org.result.child1ShortName; dataRaw.child2 = org.result.child2; dataRaw.child2Id = org.result.child2Id; dataRaw.child2ShortName = org.result.child2ShortName; dataRaw.child3 = org.result.child3; dataRaw.child3Id = org.result.child3Id; dataRaw.child3ShortName = org.result.child3ShortName; dataRaw.child4 = org.result.child4; dataRaw.child4Id = org.result.child4Id; dataRaw.child4ShortName = org.result.child4ShortName; dataRaw.posMasterNo = org.result.posMasterNo; dataRaw.position = org.result.position; dataRaw.posTypeId = org.result.posTypeId; dataRaw.posTypeName = org.result.posTypeName; dataRaw.posTypeRank = org.result.posTypeRank; dataRaw.posLevelId = org.result.posLevelId; dataRaw.posLevelName = org.result.posLevelName; dataRaw.posLevelRank = org.result.posLevelRank; dataRaw.posNo = org.result.posNo; _context.RetirementProfiles.Add(data); _context.RetirementRawProfiles.Add(dataRaw); 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 getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } var profile = await _context.RetirementProfiles .FirstOrDefaultAsync(x => x.Id == req.RetireProfileId); if (profile == null) return Error(GlobalMessages.DataNotFound, 404); var profileRaw = await _context.RetirementRawProfiles .FirstOrDefaultAsync(x => x.profileId == profile.profileId); if (profileRaw == 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; profileRaw.Remove = "EDIT"; profileRaw.Reason = req.Reason; profileRaw.LastUpdateFullName = FullName ?? "System Administrator"; profileRaw.LastUpdateUserId = UserId ?? ""; profileRaw.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); var profileRaw = await _context.RetirementRawProfiles .FirstOrDefaultAsync(x => x.profileId == profile.profileId); if (profileRaw == 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; } profileRaw.Remove = "REMOVE"; profileRaw.Reason = req.Reason; profileRaw.LastUpdateFullName = FullName ?? "System Administrator"; profileRaw.LastUpdateUserId = UserId ?? ""; profileRaw.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 getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } // แยกดึงข้อมูลเพื่อลดภาระ Database var retire = await _context.RetirementPeriods //.Include(x => x.RetirementProfiles) //.Include(x => x.RetirementRawProfiles) .FirstOrDefaultAsync(x => x.Id == retireId); if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); // โหลดข้อมูลลูกแยกกัน var profiles = await _context.RetirementProfiles.Where(x => x.RetirementPeriod.Id == retireId).ToListAsync(); var rawProfiles = await _context.RetirementRawProfiles.Where(x => x.RetirementPeriod.Id == retireId).ToListAsync(); 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.Document = _doc; retire.LastUpdateFullName = FullName ?? "System Administrator"; retire.LastUpdateUserId = UserId ?? ""; retire.LastUpdatedAt = DateTime.Now; } else { retire.SignDate = req.signDate; retire.LastUpdateFullName = FullName ?? "System Administrator"; retire.LastUpdateUserId = UserId ?? ""; retire.LastUpdatedAt = DateTime.Now; } if (retire.TypeReport == null) { var apiUrl = $"{_configuration["API"]}/org/root/search/sort"; dynamic rootOrder = new List(); dynamic child1Order = new List(); dynamic child2Order = new List(); dynamic child3Order = new List(); dynamic child4Order = new List(); dynamic posTypeNameOrder = new List(); dynamic posLevelNameOrder = new List(); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl); var _res = await client.SendAsync(_req); var _result = await _res.Content.ReadAsStringAsync(); var org = JsonConvert.DeserializeObject(_result); if (org != null && org.result != null) { rootOrder = org.result.root; child1Order = org.result.child1; child2Order = org.result.child2; child3Order = org.result.child3; child4Order = org.result.child4; posTypeNameOrder = org.result.posTypeNameOrder; posLevelNameOrder = org.result.posLevelNameOrder; } } var order = 1; foreach (var profile in profiles .OrderBy(x => string.IsNullOrEmpty(x.root) ? int.MaxValue : rootOrder.ToObject>().IndexOf(x.root)) .ThenBy(x => child1Order.ToObject>().IndexOf(x.child1 ?? "")) .ThenBy(x => child2Order.ToObject>().IndexOf(x.child2 ?? "")) .ThenBy(x => child3Order.ToObject>().IndexOf(x.child3 ?? "")) .ThenBy(x => child4Order.ToObject>().IndexOf(x.child4 ?? "")) .ThenBy(x => posTypeNameOrder.ToObject>().IndexOf(x.posTypeName ?? "")) .ThenBy(x => posLevelNameOrder.ToObject>().IndexOf(x.posLevelName ?? "")) .ThenBy(x => x.posNo)) { profile.Order = order; order++; } order = 1; foreach (var profile in rawProfiles .OrderBy(x => string.IsNullOrEmpty(x.root) ? int.MaxValue : rootOrder.ToObject>().IndexOf(x.root)) .ThenBy(x => child1Order.ToObject>().IndexOf(x.child1 ?? "")) .ThenBy(x => child2Order.ToObject>().IndexOf(x.child2 ?? "")) .ThenBy(x => child3Order.ToObject>().IndexOf(x.child3 ?? "")) .ThenBy(x => child4Order.ToObject>().IndexOf(x.child4 ?? "")) .ThenBy(x => posTypeNameOrder.ToObject>().IndexOf(x.posTypeName ?? "")) .ThenBy(x => posLevelNameOrder.ToObject>().IndexOf(x.posLevelName ?? "")) .ThenBy(x => x.posNo)) { profile.Order = order; order++; } } 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 getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_RETIREMENT"); var jsonData = JsonConvert.DeserializeObject(getPermission); if (jsonData["status"]?.ToString() != "200") { return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); } 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); } /// /// report1 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("report1")] public async Task> report1(string nodeId = "", int? node = null, DateTime? startDate = null, DateTime? endDate = null) { var apiUrl = $"{_configuration["API"]}/org/find/node-all"; var data = new object(); var data1 = new List(); var data2 = new List(); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); var _req = new HttpRequestMessage(HttpMethod.Post, apiUrl); var _res = await client.PostAsJsonAsync(apiUrl, new { node, nodeId }); var _result = await _res.Content.ReadAsStringAsync(); var orgs = JsonConvert.DeserializeObject(_result); if (orgs == null || orgs.result == null) return Error("ไม่พบหน่วยงานนี้ในระบบ", 404); int no = 1; foreach (var org in orgs.result.isRootTrue) { //เกษียณ var retire = _context.RetirementPeriods .Include(x => x.RetirementProfiles.Where(x => (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true))) .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Type.Trim().ToUpper() == "OFFICER") .FirstOrDefault(); var rt_count = retire != null ? retire.RetirementProfiles.Count() : 0; //ลาออก var retirementResigns = _context.RetirementResigns .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.rootOldId == org.rootId && x.IsCancel == false && x.Status == "DONE") .ToList(); var career_count = retirementResigns.Where(x => x.Reason == "CAREER").Count(); var move_count = retirementResigns.Where(x => x.Reason == "MOVE").Count(); var family_count = retirementResigns.Where(x => x.Reason == "FAMILY").Count(); var education_count = retirementResigns.Where(x => x.Reason == "EDUCATION").Count(); var other_count = retirementResigns.Where(x => x.Reason == "OTHER").Count(); //ขอโอน var pt_count = _context.PlacementTransfers .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && (node == 0 ? x.rootDnaOldId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaOldId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaOldId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaOldId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaOldId.Contains(org.child4DnaId) : true) && x.Status.Trim().ToUpper() == "DONE") .Count(); //ถึงแก่กรรม var rd_count = _context.RetirementDeceaseds .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "OFFICER" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ให้ออก var ro_count = _context.RetirementOuts .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Status.Trim().ToUpper() == "DONE" && x.profileType.Trim().ToUpper() == "OFFICER" && (node == 0 ? x.rootDnaOldId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaOldId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaOldId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaOldId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaOldId.Contains(org.child4DnaId) : true)) .Count(); //ปลดออก var dl_cm19_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "OFFICER" && x.commandType.Trim().ToUpper() == "C-PM-19" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ไล่ออก var dl_cm20_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "OFFICER" && x.commandType.Trim().ToUpper() == "C-PM-20" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); data1.Add(new { no = no, rootName = org.name, rt_count, career_count, move_count, family_count, education_count, other_count, pt_count, rd_count, ro_count, dl_cm19_count, dl_cm20_count, total = (rt_count + career_count + move_count + family_count + education_count + other_count + pt_count + rd_count + ro_count + dl_cm19_count + dl_cm20_count) }); no++; } no = 1; foreach (var org in orgs.result.isRootFalse) { //เกษียณ var retire = _context.RetirementPeriods .Include(x => x.RetirementProfiles.Where(x => (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true))) .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Type.Trim().ToUpper() == "OFFICER") .FirstOrDefault(); var rt_count = retire != null ? retire.RetirementProfiles.Count() : 0; //ลาออก var retirementResigns = _context.RetirementResigns .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && (node == 0 ? x.rootDnaOldId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaOldId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaOldId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaOldId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaOldId.Contains(org.child4DnaId) : true) && x.IsCancel == false && x.Status == "DONE") .ToList(); var career_count = retirementResigns.Where(x => x.Reason == "CAREER").Count(); var move_count = retirementResigns.Where(x => x.Reason == "MOVE").Count(); var family_count = retirementResigns.Where(x => x.Reason == "FAMILY").Count(); var education_count = retirementResigns.Where(x => x.Reason == "EDUCATION").Count(); var other_count = retirementResigns.Where(x => x.Reason == "OTHER").Count(); //ขอโอน var pt_count = _context.PlacementTransfers .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && (node == 0 ? x.rootDnaOldId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaOldId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaOldId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaOldId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaOldId.Contains(org.child4DnaId) : true) && x.Status.Trim().ToUpper() == "DONE") .Count(); //ถึงแก่กรรม var rd_count = _context.RetirementDeceaseds .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "OFFICER" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ให้ออก var ro_count = _context.RetirementOuts .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Status.Trim().ToUpper() == "DONE" && x.profileType.Trim().ToUpper() == "OFFICER" && (node == 0 ? x.rootDnaOldId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaOldId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaOldId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaOldId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaOldId.Contains(org.child4DnaId) : true)) .Count(); //ปลดออก var dl_cm19_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "OFFICER" && x.commandType.Trim().ToUpper() == "C-PM-19" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ไล่ออก var dl_cm20_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "OFFICER" && x.commandType.Trim().ToUpper() == "C-PM-20" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); data2.Add(new { no = no, rootName = org.name, rt_count, career_count, move_count, family_count, education_count, other_count, pt_count, rd_count, ro_count, dl_cm19_count, dl_cm20_count, total = (rt_count + career_count + move_count + family_count + education_count + other_count + pt_count + rd_count + ro_count + dl_cm19_count + dl_cm20_count) }); no++; } } data = new { template = "reportRetirement01All", reportName = "xlsx-report", data = new { date = $"ตั้งแต่วันที่ {startDate.Value.Date.ToThaiShortDate().ToThaiNumber()} ถึง {endDate.Value.Date.ToThaiShortDate().ToThaiNumber()}", dateCurrent = $"ณ วันที่ {DateTime.Now.Date.ToThaiShortDate().ToThaiNumber()}", data1, data1Count = new { rt_count = data1.Where(x => x.rt_count > 0).Sum(x => x.rt_count), career_count = data1.Where(x => x.career_count > 0).Sum(x => x.career_count), move_count = data1.Where(x => x.move_count > 0).Sum(x => x.move_count), family_count = data1.Where(x => x.family_count > 0).Sum(x => x.family_count), education_count = data1.Where(x => x.education_count > 0).Sum(x => x.education_count), other_count = data1.Where(x => x.other_count > 0).Sum(x => x.other_count), pt_count = data1.Where(x => x.pt_count > 0).Sum(x => x.pt_count), rd_count = data1.Where(x => x.rd_count > 0).Sum(x => x.rd_count), ro_count = data1.Where(x => x.ro_count > 0).Sum(x => x.ro_count), dl_cm19_count = data1.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count), dl_cm20_count = data1.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count), total = data1.Where(x => x.total > 0).Sum(x => x.total) }, data2, data2Count = new { rt_count = data2.Where(x => x.rt_count > 0).Sum(x => x.rt_count), career_count = data2.Where(x => x.career_count > 0).Sum(x => x.career_count), move_count = data2.Where(x => x.move_count > 0).Sum(x => x.move_count), family_count = data2.Where(x => x.family_count > 0).Sum(x => x.family_count), education_count = data2.Where(x => x.education_count > 0).Sum(x => x.education_count), other_count = data2.Where(x => x.other_count > 0).Sum(x => x.other_count), pt_count = data2.Where(x => x.pt_count > 0).Sum(x => x.pt_count), rd_count = data2.Where(x => x.rd_count > 0).Sum(x => x.rd_count), ro_count = data2.Where(x => x.ro_count > 0).Sum(x => x.ro_count), dl_cm19_count = data2.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count), dl_cm20_count = data2.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count), total = data2.Where(x => x.total > 0).Sum(x => x.total) }, sum = new { rt_count = data1.Where(x => x.rt_count > 0).Sum(x => x.rt_count) + data2.Where(x => x.rt_count > 0).Sum(x => x.rt_count), career_count = data1.Where(x => x.career_count > 0).Sum(x => x.career_count) + data2.Where(x => x.career_count > 0).Sum(x => x.career_count), move_count = data1.Where(x => x.move_count > 0).Sum(x => x.move_count) + data2.Where(x => x.move_count > 0).Sum(x => x.move_count), family_count = data1.Where(x => x.family_count > 0).Sum(x => x.family_count) + data2.Where(x => x.family_count > 0).Sum(x => x.family_count), education_count = data1.Where(x => x.education_count > 0).Sum(x => x.education_count) + data2.Where(x => x.education_count > 0).Sum(x => x.education_count), other_count = data1.Where(x => x.other_count > 0).Sum(x => x.other_count) + data2.Where(x => x.other_count > 0).Sum(x => x.other_count), pt_count = data1.Where(x => x.pt_count > 0).Sum(x => x.pt_count) + data2.Where(x => x.pt_count > 0).Sum(x => x.pt_count), rd_count = data1.Where(x => x.rd_count > 0).Sum(x => x.rd_count) + data2.Where(x => x.rd_count > 0).Sum(x => x.rd_count), ro_count = data1.Where(x => x.ro_count > 0).Sum(x => x.ro_count) + data2.Where(x => x.ro_count > 0).Sum(x => x.ro_count), dl_cm19_count = data1.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count) + data2.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count), dl_cm20_count = data1.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count) + data2.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count), total = data1.Where(x => x.total > 0).Sum(x => x.total) + data2.Where(x => x.total > 0).Sum(x => x.total) } } }; return Success(data); } /// /// report2 /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("report2")] public async Task> report2(string nodeId = "", int? node = null, DateTime? startDate = null, DateTime? endDate = null) { var apiUrl = $"{_configuration["API"]}/org/find/node-all"; var data = new object(); var data1 = new List(); var data2 = new List(); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); var _req = new HttpRequestMessage(HttpMethod.Post, apiUrl); var _res = await client.PostAsJsonAsync(apiUrl, new { node, nodeId }); var _result = await _res.Content.ReadAsStringAsync(); var orgs = JsonConvert.DeserializeObject(_result); if (orgs == null || orgs.result == null) return Error("ไม่พบหน่วยงานนี้ในระบบ", 404); int no = 1; foreach (var org in orgs.result.isRootTrue) { //เกษียณ var retire = _context.RetirementPeriods .Include(x => x.RetirementProfiles.Where(x => (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true))) .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Type.Trim().ToUpper() == "EMPLOYEE") .FirstOrDefault(); var rt_count = retire != null ? retire.RetirementProfiles.Count() : 0; //ลาออก var retirementResigns = _context.RetirementResignEmployees .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.rootOldId == org.rootId && x.IsCancel == false && x.Status == "DONE") .ToList(); var career_count = retirementResigns.Where(x => x.Reason == "CAREER").Count(); var move_count = retirementResigns.Where(x => x.Reason == "MOVE").Count(); var family_count = retirementResigns.Where(x => x.Reason == "FAMILY").Count(); var education_count = retirementResigns.Where(x => x.Reason == "EDUCATION").Count(); var other_count = retirementResigns.Where(x => x.Reason == "OTHER").Count(); //ถึงแก่กรรม var rd_count = _context.RetirementDeceaseds .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "EMPLOYEE" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ให้ออก var ro_count = _context.RetirementOuts .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Status.Trim().ToUpper() == "DONE" && x.profileType.Trim().ToUpper() == "EMPLOYEE" && x.rootOldId.Contains(org.rootId)) .Count(); //ปลดออก var dl_cm19_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "EMPLOYEE" && x.commandType.Trim().ToUpper() == "C-PM-19" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ไล่ออก var dl_cm20_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "EMPLOYEE" && x.commandType.Trim().ToUpper() == "C-PM-20" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); data1.Add(new { no = no, rootName = org.name, rt_count, career_count, move_count, family_count, education_count, other_count, rd_count, ro_count, dl_cm19_count, dl_cm20_count, total = (rt_count + career_count + move_count + family_count + education_count + other_count + rd_count + ro_count + dl_cm19_count + dl_cm20_count) }); no++; } no = 1; foreach (var org in orgs.result.isRootFalse) { //เกษียณ var retire = _context.RetirementPeriods .Include(x => x.RetirementProfiles.Where(x => (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true))) .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Type.Trim().ToUpper() == "EMPLOYEE") .FirstOrDefault(); var rt_count = retire != null ? retire.RetirementProfiles.Count() : 0; //ลาออก var retirementResigns = _context.RetirementResignEmployees .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.rootOldId == org.rootId && x.IsCancel == false && x.Status == "DONE") .ToList(); var career_count = retirementResigns.Where(x => x.Reason == "CAREER").Count(); var move_count = retirementResigns.Where(x => x.Reason == "MOVE").Count(); var family_count = retirementResigns.Where(x => x.Reason == "FAMILY").Count(); var education_count = retirementResigns.Where(x => x.Reason == "EDUCATION").Count(); var other_count = retirementResigns.Where(x => x.Reason == "OTHER").Count(); //ถึงแก่กรรม var rd_count = _context.RetirementDeceaseds .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "EMPLOYEE" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ให้ออก var ro_count = _context.RetirementOuts .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.Status.Trim().ToUpper() == "DONE" && x.profileType.Trim().ToUpper() == "EMPLOYEE" && x.rootOldId.Contains(org.rootId)) .Count(); //ปลดออก var dl_cm19_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "EMPLOYEE" && x.commandType.Trim().ToUpper() == "C-PM-19" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); //ไล่ออก var dl_cm20_count = _contextDiscipline.ProfileComplaintInvestigate .Where(x => x.CreatedAt.Date >= startDate && x.CreatedAt.Date <= endDate && x.profileType.Trim().ToUpper() == "EMPLOYEE" && x.commandType.Trim().ToUpper() == "C-PM-20" && (node == 0 ? x.rootDnaId.Contains(org.rootDnaId) : true) && (node == 1 ? x.child1DnaId.Contains(org.child1DnaId) : true) && (node == 2 ? x.child2DnaId.Contains(org.child2DnaId) : true) && (node == 3 ? x.child3DnaId.Contains(org.child3DnaId) : true) && (node == 4 ? x.child4DnaId.Contains(org.child4DnaId) : true)) .Count(); data2.Add(new { no = no, rootName = org.name, rt_count, career_count, move_count, family_count, education_count, other_count, rd_count, ro_count, dl_cm19_count, dl_cm20_count, total = (rt_count + career_count + move_count + family_count + education_count + other_count + rd_count + ro_count + dl_cm19_count + dl_cm20_count) }); no++; } } data = new { template = "reportRetirement02All", reportName = "xlsx-report", data = new { date = $"ตั้งแต่วันที่ {startDate.Value.Date.ToThaiShortDate().ToThaiNumber()} ถึง {endDate.Value.Date.ToThaiShortDate().ToThaiNumber()}", dateCurrent = $"ณ วันที่ {DateTime.Now.Date.ToThaiShortDate().ToThaiNumber()}", data1, data1Count = new { rt_count = data1.Where(x => x.rt_count > 0).Sum(x => x.rt_count), career_count = data1.Where(x => x.career_count > 0).Sum(x => x.career_count), move_count = data1.Where(x => x.move_count > 0).Sum(x => x.move_count), family_count = data1.Where(x => x.family_count > 0).Sum(x => x.family_count), education_count = data1.Where(x => x.education_count > 0).Sum(x => x.education_count), other_count = data1.Where(x => x.other_count > 0).Sum(x => x.other_count), rd_count = data1.Where(x => x.rd_count > 0).Sum(x => x.rd_count), ro_count = data1.Where(x => x.ro_count > 0).Sum(x => x.ro_count), dl_cm19_count = data1.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count), dl_cm20_count = data1.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count), total = data1.Where(x => x.total > 0).Sum(x => x.total) }, data2, data2Count = new { rt_count = data2.Where(x => x.rt_count > 0).Sum(x => x.rt_count), career_count = data2.Where(x => x.career_count > 0).Sum(x => x.career_count), move_count = data2.Where(x => x.move_count > 0).Sum(x => x.move_count), family_count = data2.Where(x => x.family_count > 0).Sum(x => x.family_count), education_count = data2.Where(x => x.education_count > 0).Sum(x => x.education_count), other_count = data2.Where(x => x.other_count > 0).Sum(x => x.other_count), rd_count = data2.Where(x => x.rd_count > 0).Sum(x => x.rd_count), ro_count = data2.Where(x => x.ro_count > 0).Sum(x => x.ro_count), dl_cm19_count = data2.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count), dl_cm20_count = data2.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count), total = data2.Where(x => x.total > 0).Sum(x => x.total) }, sum = new { rt_count = data1.Where(x => x.rt_count > 0).Sum(x => x.rt_count) + data2.Where(x => x.rt_count > 0).Sum(x => x.rt_count), career_count = data1.Where(x => x.career_count > 0).Sum(x => x.career_count) + data2.Where(x => x.career_count > 0).Sum(x => x.career_count), move_count = data1.Where(x => x.move_count > 0).Sum(x => x.move_count) + data2.Where(x => x.move_count > 0).Sum(x => x.move_count), family_count = data1.Where(x => x.family_count > 0).Sum(x => x.family_count) + data2.Where(x => x.family_count > 0).Sum(x => x.family_count), education_count = data1.Where(x => x.education_count > 0).Sum(x => x.education_count) + data2.Where(x => x.education_count > 0).Sum(x => x.education_count), other_count = data1.Where(x => x.other_count > 0).Sum(x => x.other_count) + data2.Where(x => x.other_count > 0).Sum(x => x.other_count), rd_count = data1.Where(x => x.rd_count > 0).Sum(x => x.rd_count) + data2.Where(x => x.rd_count > 0).Sum(x => x.rd_count), ro_count = data1.Where(x => x.ro_count > 0).Sum(x => x.ro_count) + data2.Where(x => x.ro_count > 0).Sum(x => x.ro_count), dl_cm19_count = data1.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count) + data2.Where(x => x.dl_cm19_count > 0).Sum(x => x.dl_cm19_count), dl_cm20_count = data1.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count) + data2.Where(x => x.dl_cm20_count > 0).Sum(x => x.dl_cm20_count), total = data1.Where(x => x.total > 0).Sum(x => x.total) + data2.Where(x => x.total > 0).Sum(x => x.total) } } }; return Success(data); } /// /// เกษียณอายุราชการ /// /// ประเภทUser(officer,employee)(ตัวใหญ่หรือเล็กก็ได้) /// ปีงบประมาณ(ค.ศ.) /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("update-status/{type}/{year}")] public async Task> UpdateStatusRetirement(string type, int year) { var retirePeriodOfficer = await _context.RetirementPeriods .Include(x => x.RetirementRawProfiles.Where(y => y.Remove != "REMOVE")) .Where(x => x.Year == year && (x.Type ?? "").Trim().ToUpper().Contains(type) && x.Round == 1) .FirstOrDefaultAsync(); if (retirePeriodOfficer == null) return Error("ไม่พบรอบประกาศเกษียณอายุราชการ"); var data = new { signDate = retirePeriodOfficer.SignDate ?? null, profiles = retirePeriodOfficer.RetirementRawProfiles .Select(x => new { profileId = x.profileId }) .ToList() }; return Success(data); } #region 31-ประกาศเกษียณข้าราชการ & 32-ประกาศเกษียณลูกจ้างประจำ /// /// 31-ประกาศเกษียณข้าราชการ & 32-ประกาศเกษียณลูกจ้างประจำ /// /// Id ของรอบเกษียณ /// pdf, docx หรือ xlsx /// /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("31/{exportType}/{Id}")] public async Task> GetProfileRetirement([FromRoute] Guid Id, string exportType = "pdf") { var retire = await _service.GetProfileRetirementdAsync(Id, token); if (retire != null) { var reportfile = string.Empty; exportType = exportType.Trim(); switch (retire.GetType().GetProperty("Type").GetValue(retire)) { case "OFFICER": if (string.IsNullOrEmpty(retire.GetType().GetProperty("TypeReport").GetValue(retire))) { reportfile = $"retire-1"; } else if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "ADD" || retire.GetType().GetProperty("TypeReport").GetValue(retire) == "EDIT") { reportfile = $"retire-2"; } else if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "REMOVE") { reportfile = $"retire-3"; } else { return Error(retire.GetType().GetProperty("TypeReport").GetValue(retire)); } break; case "EMPLOYEE": if (string.IsNullOrEmpty(retire.GetType().GetProperty("TypeReport").GetValue(retire))) { reportfile = $"retire-emp-1"; } else if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "ADD" || retire.GetType().GetProperty("TypeReport").GetValue(retire) == "EDIT") { reportfile = $"retire-emp-2"; } else if (retire.GetType().GetProperty("TypeReport").GetValue(retire) == "REMOVE") { reportfile = $"retire-emp-3"; } else { return Error(retire.GetType().GetProperty("TypeReport").GetValue(retire)); } break; default: return Error(retire.GetType().GetProperty("Type").GetValue(retire)); } var data = new { template = reportfile, reportName = "docx-report", data = retire }; return Success(data); } else { return NotFound(); } } #endregion } }