fix อัพเดทสถานะบุคคลภายนอก #2518
All checks were successful
Build & Deploy Placement Service / build (push) Successful in 1m57s
All checks were successful
Build & Deploy Placement Service / build (push) Successful in 1m57s
This commit is contained in:
parent
6f1ca58f04
commit
4c44bdf237
1 changed files with 88 additions and 24 deletions
|
|
@ -8,6 +8,17 @@ using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace BMA.EHR.Application.Repositories
|
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>
|
public class PlacementRepository : GenericRepository<Guid, Placement>
|
||||||
{
|
{
|
||||||
#region " Fields "
|
#region " Fields "
|
||||||
|
|
@ -83,39 +94,43 @@ namespace BMA.EHR.Application.Repositories
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Job อัพเดทสถานะผู้สอบผ่านที่ลาออกไปแล้วแต่ยังไม่ส่งไปออกคำสั่ง
|
/// Job อัพเดทสถานะผู้สอบผ่านที่ลาออกไปแล้วแต่ยังไม่ส่งไปออกคำสั่ง
|
||||||
|
/// และอัพเดทบุคคลภายนอกที่เข้ามาอยู่ในระบบแล้ว
|
||||||
/// ทำงานทุกวันเวลา 05:00 น.
|
/// ทำงานทุกวันเวลา 05:00 น.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task UpdateStatusPlacementProfiles()
|
public async Task UpdateStatusPlacementProfiles()
|
||||||
{
|
{
|
||||||
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === STARTED ===");
|
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === STARTED ===");
|
||||||
|
|
||||||
var officerProfileIds = await _dbContext.Set<PlacementProfile>()
|
// 1. Query ทั้ง 2 กรณี: ทุกคนที่ยังไม่ DONE
|
||||||
.Where(p => !string.IsNullOrEmpty(p.profileId)
|
var allCitizenIds = await _dbContext.Set<PlacementProfile>()
|
||||||
&& p.IsOfficer == true
|
.Where(p => !string.IsNullOrEmpty(p.CitizenId)
|
||||||
&& p.PlacementStatus != "DONE"
|
&& 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();
|
.ToListAsync();
|
||||||
|
|
||||||
if (!officerProfileIds.Any())
|
if (!allCitizenIds.Any())
|
||||||
{
|
{
|
||||||
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] No profiles to process");
|
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] No profiles to process");
|
||||||
return;
|
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";
|
var apiUrl = $"{_configuration["API"]}/org/dotnet/check-isLeave";
|
||||||
List<string> leaveProfileIds = new();
|
|
||||||
|
List<OrgProfileResult> orgResults = new();
|
||||||
|
|
||||||
using (var client = new HttpClient())
|
using (var client = new HttpClient())
|
||||||
{
|
{
|
||||||
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
||||||
var payload = new
|
|
||||||
{
|
var payload = new { citizenIds };
|
||||||
profileIds = officerProfileIds.Distinct().ToList()
|
|
||||||
};
|
|
||||||
var jsonPayload = JsonConvert.SerializeObject(payload);
|
var jsonPayload = JsonConvert.SerializeObject(payload);
|
||||||
var content = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json");
|
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 response = await client.PostAsync(apiUrl, content);
|
||||||
var result = await response.Content.ReadAsStringAsync();
|
var result = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
var responseObj = JsonConvert.DeserializeAnonymousType(result, new
|
var responseObj = JsonConvert.DeserializeAnonymousType(result, new
|
||||||
{
|
{
|
||||||
status = 0,
|
status = 0,
|
||||||
message = "",
|
message = "",
|
||||||
result = new List<string>()
|
result = new List<OrgProfileResult>()
|
||||||
});
|
});
|
||||||
|
|
||||||
leaveProfileIds = responseObj.result ?? new();
|
orgResults = responseObj?.result ?? new();
|
||||||
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] พบ {leaveProfileIds.Count} รายการที่ลาออก");
|
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] Org API ตอบกลับ {orgResults.Count} รายการ");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -140,18 +156,35 @@ namespace BMA.EHR.Application.Repositories
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leaveProfileIds.Any())
|
if (!orgResults.Any())
|
||||||
{
|
{
|
||||||
var batchSize = 500;
|
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] ไม่มีรายการต้องอัปเดต");
|
||||||
var totalUpdated = 0;
|
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === COMPLETED ===");
|
||||||
var totalBatches = (int)Math.Ceiling((double)leaveProfileIds.Count / batchSize);
|
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++)
|
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>()
|
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();
|
.ToListAsync();
|
||||||
|
|
||||||
foreach (var profile in profilesToUpdate)
|
foreach (var profile in profilesToUpdate)
|
||||||
|
|
@ -160,12 +193,43 @@ namespace BMA.EHR.Application.Repositories
|
||||||
}
|
}
|
||||||
|
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
totalUpdated += profilesToUpdate.Count;
|
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 ===");
|
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === COMPLETED ===");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue