Merge branch 'dev' into working
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 2m1s

This commit is contained in:
Suphonchai Phoonsawat 2026-06-05 05:48:08 +07:00
commit e17895de81
6 changed files with 99 additions and 28 deletions

View file

@ -8,6 +8,17 @@ using Newtonsoft.Json;
namespace BMA.EHR.Application.Repositories
{
/// <summary>
/// Response model จาก Org API (check-isLeave)
/// </summary>
public class OrgProfileResult
{
public string citizenId { get; set; } = "";
public string? profileId { get; set; }
public bool isLeave { get; set; }
public bool isActive { get; set; }
}
public class PlacementRepository : GenericRepository<Guid, Placement>
{
#region " Fields "
@ -83,39 +94,43 @@ namespace BMA.EHR.Application.Repositories
/// <summary>
/// Job อัพเดทสถานะผู้สอบผ่านที่ลาออกไปแล้วแต่ยังไม่ส่งไปออกคำสั่ง
/// และอัพเดทบุคคลภายนอกที่เข้ามาอยู่ในระบบแล้ว
/// ทำงานทุกวันเวลา 05:00 น.
/// </summary>
public async Task UpdateStatusPlacementProfiles()
{
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === STARTED ===");
var officerProfileIds = await _dbContext.Set<PlacementProfile>()
.Where(p => !string.IsNullOrEmpty(p.profileId)
&& p.IsOfficer == true
// 1. Query ทั้ง 2 กรณี: ทุกคนที่ยังไม่ DONE
var allCitizenIds = await _dbContext.Set<PlacementProfile>()
.Where(p => !string.IsNullOrEmpty(p.CitizenId)
&& p.PlacementStatus != "DONE"
// && p.Id == Guid.Parse("08deb7de-3030-4d1b-8519-8148584949fc")
// && p.CitizenId == "2536721883131"
)
.Select(p => p.profileId)
.Select(p => new { p.CitizenId, p.IsOfficer })
.ToListAsync();
if (!officerProfileIds.Any())
if (!allCitizenIds.Any())
{
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] No profiles to process");
return;
}
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] พบข้าราชการที่สอบผ่านทั้งหมด {officerProfileIds.Count} คน ที่ยังไม่ส่งไปออกคำสั่ง");
var officerCount = allCitizenIds.Count(x => x.IsOfficer == true);
var notOfficerCount = allCitizenIds.Count(x => x.IsOfficer == false);
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] พบข้าราชการ {officerCount} คน, บุคคลภายนอก {notOfficerCount} คน");
// 2. ส่ง citizenIds ทั้งหมดไป Org API ครั้งเดียว
var citizenIds = allCitizenIds.Select(x => x.CitizenId).Distinct().ToList();
var apiUrl = $"{_configuration["API"]}/org/dotnet/check-isLeave";
List<string> leaveProfileIds = new();
List<OrgProfileResult> orgResults = new();
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
var payload = new
{
profileIds = officerProfileIds.Distinct().ToList()
};
var payload = new { citizenIds };
var jsonPayload = JsonConvert.SerializeObject(payload);
var content = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json");
@ -123,15 +138,16 @@ namespace BMA.EHR.Application.Repositories
{
var response = await client.PostAsync(apiUrl, content);
var result = await response.Content.ReadAsStringAsync();
var responseObj = JsonConvert.DeserializeAnonymousType(result, new
{
status = 0,
message = "",
result = new List<string>()
result = new List<OrgProfileResult>()
});
leaveProfileIds = responseObj.result ?? new();
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] พบ {leaveProfileIds.Count} รายการที่ลาออก");
orgResults = responseObj?.result ?? new();
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] Org API ตอบกลับ {orgResults.Count} รายการ");
}
catch (Exception ex)
{
@ -140,18 +156,35 @@ namespace BMA.EHR.Application.Repositories
}
}
if (leaveProfileIds.Any())
if (!orgResults.Any())
{
var batchSize = 500;
var totalUpdated = 0;
var totalBatches = (int)Math.Ceiling((double)leaveProfileIds.Count / batchSize);
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] ไม่มีรายการต้องอัปเดต");
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === COMPLETED ===");
return;
}
// 3. แยกข้อมูลตามเงื่อนไข
var leaveCitizenIds = orgResults.Where(x => x.isLeave).Select(x => x.citizenId).ToList();
var inSystemProfiles = orgResults.Where(x => x.isActive && !x.isLeave && !string.IsNullOrEmpty(x.profileId)).ToList();
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] ลาออก {leaveCitizenIds.Count} รายการ, อยู่ที่ทะเบียนประวัติ {inSystemProfiles.Count} รายการ");
// 4. Split Batch Update (500 รายการ/batch)
var batchSize = 500;
var totalUpdated = 0;
// 4.1 Update คนลาออก → IsOfficer = false
if (leaveCitizenIds.Any())
{
var totalBatches = (int)Math.Ceiling((double)leaveCitizenIds.Count / batchSize);
for (int i = 0; i < totalBatches; i++)
{
var batch = leaveProfileIds.Skip(i * batchSize).Take(batchSize).ToList();
var batch = leaveCitizenIds.Skip(i * batchSize).Take(batchSize).ToList();
var profilesToUpdate = await _dbContext.Set<PlacementProfile>()
.Where(p => !string.IsNullOrEmpty(p.profileId) && batch.Contains(p.profileId))
.Where(p => !string.IsNullOrEmpty(p.CitizenId)
&& batch.Contains(p.CitizenId)
&& p.IsOfficer == true)
.ToListAsync();
foreach (var profile in profilesToUpdate)
@ -160,12 +193,43 @@ namespace BMA.EHR.Application.Repositories
}
await _dbContext.SaveChangesAsync();
totalUpdated += profilesToUpdate.Count;
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] Batch {i + 1}/{totalBatches} → อัปเดต {profilesToUpdate.Count} รายการ");
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] [ลาออก] Batch {i + 1}/{totalBatches} → อัปเดต {profilesToUpdate.Count} รายการ");
}
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] อัปเดตรวมทั้งหมด {totalUpdated} รายการ → IsOfficer = false");
}
// 4.2 Update คนที่อยู่ในทะเบียนประวัติ → profileId + IsOfficer = true
if (inSystemProfiles.Any())
{
var totalBatches = (int)Math.Ceiling((double)inSystemProfiles.Count / batchSize);
for (int i = 0; i < totalBatches; i++)
{
var batch = inSystemProfiles.Skip(i * batchSize).Take(batchSize).ToList();
var batchCitizenIds = batch.Select(x => x.citizenId).ToList();
var profilesToUpdate = await _dbContext.Set<PlacementProfile>()
.Where(p => !string.IsNullOrEmpty(p.CitizenId)
&& batchCitizenIds.Contains(p.CitizenId)
&& p.IsOfficer == false)
.ToListAsync();
foreach (var profile in profilesToUpdate)
{
var orgProfile = batch.FirstOrDefault(x => x.citizenId == profile.CitizenId);
if (orgProfile != null)
{
profile.profileId = orgProfile.profileId;
profile.IsOfficer = true;
}
}
await _dbContext.SaveChangesAsync();
totalUpdated += profilesToUpdate.Count;
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] [เข้าระบบ] Batch {i + 1}/{totalBatches} → อัปเดต {profilesToUpdate.Count} รายการ");
}
}
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] อัปเดตรวมทั้งหมด {totalUpdated} รายการ");
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === COMPLETED ===");
}

View file

@ -148,6 +148,7 @@ namespace BMA.EHR.Placement.Service.Controllers
p.child4OldId,
p.child4ShortNameOld,
p.PositionOld,
p.PositionNumberOld,
p.PositionExecutiveOld,
p.positionExecutiveFieldOld,
p.positionAreaOld,

View file

@ -1513,7 +1513,10 @@ namespace BMA.EHR.Retirement.Service.Controllers
return Error(GlobalMessages.RetirementResignNotFound, 404);
updated.Location = req.Location;
updated.ActiveDate = req.ActiveDate;
if (req.SendDate != null)
{
updated.SendDate = req.SendDate;
}
// updated.Reason = req.Reason;
updated.Remark = req.Remark;
updated.ReasonResign = req.Reason;

View file

@ -1450,7 +1450,10 @@ namespace BMA.EHR.Retirement.Service.Controllers
return Error(GlobalMessages.RetirementResignEmployeeNotFound, 404);
updated.Location = req.Location;
updated.ActiveDate = req.ActiveDate;
if (req.SendDate != null)
{
updated.SendDate = req.SendDate;
}
// updated.Reason = req.Reason;
updated.Remark = req.Remark;
updated.ReasonResign = req.Reason;

View file

@ -6,7 +6,7 @@ namespace BMA.EHR.Retirement.Service.Requests
public class RetirementResignEmployeeRequest
{
public string? Location { get; set; }
// public DateTime? SendDate { get; set; }
public DateTime? SendDate { get; set; }
public DateTime? ActiveDate { get; set; }
public string? Reason { get; set; }
public string? Remark { get; set; }

View file

@ -6,7 +6,7 @@ namespace BMA.EHR.Retirement.Service.Requests
public class RetirementResignRequest
{
public string? Location { get; set; }
// public DateTime? SendDate { get; set; }
public DateTime? SendDate { get; set; }
public DateTime? ActiveDate { get; set; }
public string? Reason { get; set; }
public string? Remark { get; set; }