From c5a57a1f1f3e7c7b22414e04f802212e67e94c48 Mon Sep 17 00:00:00 2001 From: moss <> Date: Fri, 28 Mar 2025 16:51:39 +0700 Subject: [PATCH] =?UTF-8?q?sort=20=E0=B9=80=E0=B8=81=E0=B8=A9=E0=B8=B5?= =?UTF-8?q?=E0=B8=A2=E0=B8=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/RetirementController.cs | 358 +++++++----------- 1 file changed, 129 insertions(+), 229 deletions(-) diff --git a/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs b/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs index feb87603..07036a35 100644 --- a/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs +++ b/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs @@ -69,7 +69,6 @@ namespace BMA.EHR.Retirement.Service.Controllers #endregion - #region " จัดลำดับเกษียณ " private class ObjectOrderRetire { public int Order { get; set; } @@ -81,173 +80,66 @@ namespace BMA.EHR.Retirement.Service.Controllers 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(); - } - - 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 + // 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 ประกาศเกษียณอายุราชการ @@ -538,7 +430,7 @@ namespace BMA.EHR.Retirement.Service.Controllers order++; } await _context.SaveChangesAsync(); - await GenOrderByYear(retire.Id); + // await GenOrderByYear(retire.Id); } else { @@ -1004,7 +896,7 @@ namespace BMA.EHR.Retirement.Service.Controllers retire_profile.LastUpdateUserId = UserId ?? ""; retire_profile.LastUpdatedAt = DateTime.Now; } - var num = 1; + var num = 0; var profileRawCount = await _context.RetirementRawProfiles .CountAsync(x => x.RetirementPeriod.Id == retireId); @@ -1119,10 +1011,10 @@ namespace BMA.EHR.Retirement.Service.Controllers _context.RetirementProfiles.Add(data); _context.RetirementRawProfiles.Add(dataRaw); await _context.SaveChangesAsync(); - if (retire.RetirementPeriodHistorys.Count() == 0) - { - await GenOrderByYear(retire.Id); - } + // if (retire.RetirementPeriodHistorys.Count() == 0) + // { + // await GenOrderByYear(retire.Id); + // } return Success(); } } @@ -1230,10 +1122,10 @@ namespace BMA.EHR.Retirement.Service.Controllers _context.RetirementProfiles.Add(data); _context.RetirementRawProfiles.Add(dataRaw); await _context.SaveChangesAsync(); - if (retire.RetirementPeriodHistorys.Count() == 0) - { - await GenOrderByYear(retire.Id); - } + // if (retire.RetirementPeriodHistorys.Count() == 0) + // { + // await GenOrderByYear(retire.Id); + // } return Success(); } } @@ -1313,7 +1205,7 @@ namespace BMA.EHR.Retirement.Service.Controllers { _context.RetirementProfiles.Remove(profile); _context.SaveChanges(); - await GenOrderByYear(profile.RetirementPeriod.Id); + // await GenOrderByYear(profile.RetirementPeriod.Id); } else { @@ -1375,7 +1267,7 @@ namespace BMA.EHR.Retirement.Service.Controllers if (retire == null) return Error(GlobalMessages.RetirementNotFound, 404); - await GenOrderByYear(retire.Id); + // await GenOrderByYear(retire.Id); return Success(); } @@ -1418,58 +1310,66 @@ namespace BMA.EHR.Retirement.Service.Controllers retire.LastUpdateUserId = UserId ?? ""; retire.LastUpdatedAt = DateTime.Now; } - 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()) + if(retire.TypeReport == null) { - 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) + 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()) { - 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; + 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 retire.RetirementProfiles + .OrderBy(x => 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 ?? ""))) + { + profile.Order = order; + order++; + } + + order = 1; + foreach (var profile in retire.RetirementRawProfiles + .OrderBy(x => 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 ?? ""))) + { + profile.Order = order; + order++; } - } - var order = 1; - foreach (var profile in retire.RetirementProfiles.OrderBy(x => 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 ?? ""))) - { - profile.Order = order; - order++; - } - order = 1; - foreach (var profile in retire.RetirementRawProfiles.OrderBy(x => 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 ?? ""))) - { - profile.Order = order; - order++; } await _context.SaveChangesAsync();