From 22a39140726af8835d4136c9eca089d0cf7605bf Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Wed, 28 May 2025 10:48:40 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?= =?UTF-8?q?=E0=B8=84=E0=B8=B3=E0=B8=99=E0=B8=A7=E0=B8=99=E0=B9=80=E0=B8=84?= =?UTF-8?q?=E0=B8=A3=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=87=E0=B8=A3=E0=B8=B2?= =?UTF-8?q?=E0=B8=8A=E0=B8=A2=E0=B9=8C=E0=B9=81=E0=B8=A2=E0=B8=81=E0=B8=95?= =?UTF-8?q?=E0=B8=B2=E0=B8=A1=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/InsigniaPeriodsRepository.cs | 232 +++++++++++++++++- BMA.EHR.Domain/Shared/GlobalMessages.cs | 4 + .../Controllers/InsigniaRequestController.cs | 51 ++++ 3 files changed, 278 insertions(+), 9 deletions(-) diff --git a/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs b/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs index 42d7ede9..db4f4c92 100644 --- a/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs +++ b/BMA.EHR.Application/Repositories/InsigniaPeriodsRepository.cs @@ -1,16 +1,19 @@ +using Amazon.S3.Model.Internal.MarshallTransformations; using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Requests; +using BMA.EHR.Application.Responses.Profiles; +using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Insignias; using BMA.EHR.Domain.Models.MetaData; using BMA.EHR.Domain.Models.OrganizationEmployee; -using BMA.EHR.Domain.Shared; -using BMA.EHR.Domain.Extensions; -using Microsoft.AspNetCore.Http; -using Microsoft.EntityFrameworkCore; -using Newtonsoft.Json; using BMA.EHR.Domain.Models.Organizations; -using Amazon.S3.Model.Internal.MarshallTransformations; -using BMA.EHR.Application.Responses.Profiles; +using BMA.EHR.Domain.Shared; +using GreatFriends.ThaiBahtText; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Nest; +using Newtonsoft.Json; using ProfileInsignia = BMA.EHR.Domain.Models.HR.ProfileInsignia; namespace BMA.EHR.Application.Repositories @@ -460,7 +463,7 @@ namespace BMA.EHR.Application.Repositories .FirstOrDefault() == null ? 0 : p.ProfileInsignia.Where(x => x.InsigniaId.Value == bcmRoyal.Id).OrderBy(x => x.Year) .FirstOrDefault().Year, - ProfileType = p.ProfileType, + ProfileType = p.ProfileType, MarkDiscipline = p.MarkDiscipline, MarkInsignia = p.MarkInsignia, @@ -8140,6 +8143,214 @@ namespace BMA.EHR.Application.Repositories #region " Public " + /// + /// ประมวลผลรายชื่อผู้ได้รับเครื่องราชย์ของแต่ละหน่วยงาน + /// + /// รหัสรอบการขอ + /// รหัสหน่วยงาน + /// officer or employee + /// ผลการคำนวนการขอพระราชทานเครื่องราชย์ + public async Task> GetInsigniaCandidateBKKByTypeAsync(Guid periodId, Guid ocId, string type = "officer") + { + try + { + var result_candidate = new List(); + + var period = await _dbContext.Set().Include(x => x.InsigniaEmployees).FirstOrDefaultAsync(p => p.Id == periodId); + if (period == null) + { + throw new Exception(GlobalMessages.InsigniaPeriodNotFound); + } + + if (type.ToLower().Trim() == "officer") + { + var allOfficerProfilesByRoot = (await _userProfileRepository.GetOfficerProfileByRootIdAsync(ocId, AccessToken)); + + // calculate ตามแต่ละชั้น + var type_coin = allOfficerProfilesByRoot.Count() > 0 ? await GetCoinCandidate(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type1_level1 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level1(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type1_level2 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level2(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type1_level3 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level3(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type1_level4 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level4(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type2_level5 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level5(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type2_level6 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level6(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type2_level7 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level7(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type2_level8 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level8(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type2_level9_1 = + allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level9_1(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type2_level9_2 = + allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level9_2(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type3_level10 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type3_Level10(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type3_level11 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type3_Level11(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type4_level10 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type4_Level10(periodId, ocId, allOfficerProfilesByRoot) : new List(); + var type4_level11 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type4_Level11(periodId, ocId, allOfficerProfilesByRoot) : new List(); + + + // union result + foreach (var r in type_coin) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type4_level11) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type4_level10) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type3_level11) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type3_level10) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type2_level9_2) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type2_level9_1) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type2_level8) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type2_level7) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type2_level6) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type2_level5) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type1_level4) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type1_level3) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type1_level2) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in type1_level1) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + // ย้ายที่ตามที่ มอสแจ้ง + if (period.Round != 1) + { + var insigniaIdList = await _dbContext.Set() + .Include(x => x.InsigniaType) + .Where(x => x.InsigniaType!.Name == "ชั้นสายสะพาย") + .Select(x => x.Id) + .ToListAsync(); + + + result_candidate = result_candidate.Where(x => insigniaIdList.Contains(x.RequestInsignia.Id)).ToList(); + } + } + else if (type.ToLower().Trim() == "employee") + { + var allEmployeeProfileByRoot = new List(); + if (period != null && period.InsigniaEmployees != null) + { + allEmployeeProfileByRoot = (await _userProfileRepository.GetEmployeeProfileByPositionAsync(ocId, period.InsigniaEmployees.Select(x => x.RefId!.ValueOrBlank()).ToArray(), AccessToken)); + } + + var type_coin = allEmployeeProfileByRoot.Count() > 0 ? await GetCoinCandidate(periodId, ocId, allEmployeeProfileByRoot) : new List(); + + var employee_type1 = allEmployeeProfileByRoot.Count() > 0 ? await GetEmployeeInsignia_Type1(periodId, ocId, allEmployeeProfileByRoot) : new List(); + var employee_type2 = allEmployeeProfileByRoot.Count() > 0 ? await GetEmployeeInsignia_Type2(periodId, ocId, allEmployeeProfileByRoot) : new List(); + + // union result + foreach (var r in type_coin) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in employee_type2) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + + foreach (var r in employee_type1) + { + var old = result_candidate.FirstOrDefault(x => x.ProfileId == r.ProfileId); + if (old == null) + result_candidate.Add(r); + } + } + else + throw new Exception(GlobalMessages.CalculateTypeNotValid); + + return result_candidate.OrderBy(x => x.Seq).ThenBy(x => x.Gender).ThenBy(x => x.ProfileId).ToList(); + } + catch + { + throw; + } + } + /// /// ประมวลผลรายชื่อผู้ได้รับเครื่องราชย์ของแต่ละหน่วยงาน /// @@ -8325,7 +8536,7 @@ namespace BMA.EHR.Application.Repositories // result_candidate.Add(r); //} - + return result_candidate.OrderBy(x => x.Seq).ThenBy(x => x.Gender).ThenBy(x => x.ProfileId).ToList(); } @@ -8498,6 +8709,9 @@ namespace BMA.EHR.Application.Repositories } } + + + #endregion #region " From Ming " diff --git a/BMA.EHR.Domain/Shared/GlobalMessages.cs b/BMA.EHR.Domain/Shared/GlobalMessages.cs index 19a8b339..8746de95 100644 --- a/BMA.EHR.Domain/Shared/GlobalMessages.cs +++ b/BMA.EHR.Domain/Shared/GlobalMessages.cs @@ -98,6 +98,10 @@ public static readonly string InsigniaBorrowNotFound = "ไม่พบรายการยืมเครื่องราชฯ"; public static readonly string InsigniaNotReturn = "รายการยืมเครื่องราชฯ นี้ได้ทำการยืมไว้แล้ว"; + + + public static readonly string CalculateTypeNotValid = "ประเภทผู้ขอพระราชทานเครื่องราชฯ ไม่ถูกต้อง กรุณาตรวจสอบข้อมูล"; + #endregion #region " Retirement " diff --git a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs index d2095a1c..8a71e214 100644 --- a/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs +++ b/BMA.EHR.Insignia/Controllers/InsigniaRequestController.cs @@ -500,6 +500,57 @@ namespace BMA.EHR.Insignia.Service.Controllers return Success(); } + /// + /// คำนวณราชชื่อผู้ได้รับเครื่องราช (แยกตาม officer, employee) + /// + /// officer or employee + /// Id รอบเครื่องราช + /// + /// + /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("{type}/{insigniaPeriodId:length(36)}")] + public async Task> CalculateInsigniaRequestByTypeAsync(string type, Guid insigniaPeriodId) + { + try + { + var selectPeriod = _context.InsigniaPeriods.AsNoTracking().Where(x => x.Id == insigniaPeriodId).FirstOrDefault(); + if (selectPeriod == null) + throw new Exception(GlobalMessages.InsigniaPeriodNotFound); + + var organizations = await _userProfileRepository.GetActiveRootAsync(AccessToken, selectPeriod.RevisionId); + + foreach (var organization in organizations) + { + if (organization == null) + continue; + + + var result = await _repository.GetInsigniaRequest(insigniaPeriodId, organization.Id); + if (result != null) + { + Guid period = result.PeriodId; + string requestStatus = result.RequestStatus; + var candidate = await _repository.GetInsigniaCandidateBKKByTypeAsync(insigniaPeriodId, organization.Id, type); + // ตรวจสอบว่ารายการอยู่ใน table insignia_request_new + if (requestStatus == null) + { + // บันทึกรายชื่อ + await _repository.InsertCandidate(period, organization.Id, organization.OrgRootName, candidate); + } + } + } + + return Success(); + } + catch (Exception ex) + { + return Error(ex); + } + + } + /// /// คำนวณราชชื่อผู้ได้รับเครื่องราช (Rabbit MQ) ///