เพิ่มคำนวนเครื่องราชย์แยกตาม type

This commit is contained in:
Suphonchai Phoonsawat 2025-05-28 10:48:40 +07:00
parent 355f5b9591
commit 22a3914072
3 changed files with 278 additions and 9 deletions

View file

@ -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 "
/// <summary>
/// ประมวลผลรายชื่อผู้ได้รับเครื่องราชย์ของแต่ละหน่วยงาน
/// </summary>
/// <param name="periodId">รหัสรอบการขอ</param>
/// <param name="ocId">รหัสหน่วยงาน</param>
/// <param name="type">officer or employee</param>
/// <returns>ผลการคำนวนการขอพระราชทานเครื่องราชย์</returns>
public async Task<List<InsigniaResultSet>> GetInsigniaCandidateBKKByTypeAsync(Guid periodId, Guid ocId, string type = "officer")
{
try
{
var result_candidate = new List<InsigniaResultSet>();
var period = await _dbContext.Set<InsigniaPeriod>().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<InsigniaResultSet>();
var type1_level1 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level1(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type1_level2 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level2(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type1_level3 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level3(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type1_level4 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type1_Level4(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type2_level5 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level5(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type2_level6 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level6(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type2_level7 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level7(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type2_level8 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level8(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type2_level9_1 =
allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level9_1(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type2_level9_2 =
allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type2_Level9_2(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type3_level10 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type3_Level10(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type3_level11 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type3_Level11(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type4_level10 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type4_Level10(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
var type4_level11 = allOfficerProfilesByRoot.Count() > 0 ? await GetInsigniaCandidate_Type4_Level11(periodId, ocId, allOfficerProfilesByRoot) : new List<InsigniaResultSet>();
// 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<Insignia>()
.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<GetProfileByRootIdDto>();
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<InsigniaResultSet>();
var employee_type1 = allEmployeeProfileByRoot.Count() > 0 ? await GetEmployeeInsignia_Type1(periodId, ocId, allEmployeeProfileByRoot) : new List<InsigniaResultSet>();
var employee_type2 = allEmployeeProfileByRoot.Count() > 0 ? await GetEmployeeInsignia_Type2(periodId, ocId, allEmployeeProfileByRoot) : new List<InsigniaResultSet>();
// 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;
}
}
/// <summary>
/// ประมวลผลรายชื่อผู้ได้รับเครื่องราชย์ของแต่ละหน่วยงาน
/// </summary>
@ -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 "

View file

@ -98,6 +98,10 @@
public static readonly string InsigniaBorrowNotFound = "ไม่พบรายการยืมเครื่องราชฯ";
public static readonly string InsigniaNotReturn = "รายการยืมเครื่องราชฯ นี้ได้ทำการยืมไว้แล้ว";
public static readonly string CalculateTypeNotValid = "ประเภทผู้ขอพระราชทานเครื่องราชฯ ไม่ถูกต้อง กรุณาตรวจสอบข้อมูล";
#endregion
#region " Retirement "

View file

@ -500,6 +500,57 @@ namespace BMA.EHR.Insignia.Service.Controllers
return Success();
}
/// <summary>
/// คำนวณราชชื่อผู้ได้รับเครื่องราช (แยกตาม officer, employee)
/// </summary>
/// <param name="type">officer or employee</param>
/// <param name="insigniaPeriodId">Id รอบเครื่องราช</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{type}/{insigniaPeriodId:length(36)}")]
public async Task<ActionResult<ResponseObject>> 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);
}
}
/// <summary>
/// คำนวณราชชื่อผู้ได้รับเครื่องราช (Rabbit MQ)
/// </summary>