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

This commit is contained in:
Suphonchai Phoonsawat 2026-05-29 15:46:56 +07:00
commit 4827906d1d
2 changed files with 97 additions and 1 deletions

View file

@ -2,6 +2,9 @@
using BMA.EHR.Domain.Models.Placement; using BMA.EHR.Domain.Models.Placement;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Net.Http.Headers;
using Newtonsoft.Json;
namespace BMA.EHR.Application.Repositories namespace BMA.EHR.Application.Repositories
{ {
@ -11,15 +14,17 @@ namespace BMA.EHR.Application.Repositories
private readonly IApplicationDBContext _dbContext; private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor; private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
#endregion #endregion
#region " Constructor and Destructor " #region " Constructor and Destructor "
public PlacementRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) public PlacementRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) : base(dbContext, httpContextAccessor)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_configuration = configuration;
} }
#endregion #endregion
@ -76,6 +81,94 @@ namespace BMA.EHR.Application.Repositories
return data; return data;
} }
/// <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
&& p.PlacementStatus != "DONE"
// && p.Id == Guid.Parse("08deb7de-3030-4d1b-8519-8148584949fc")
)
.Select(p => p.profileId)
.ToListAsync();
if (!officerProfileIds.Any())
{
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] No profiles to process");
return;
}
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] พบข้าราชการที่สอบผ่านทั้งหมด {officerProfileIds.Count} คน ที่ยังไม่ส่งไปออกคำสั่ง");
var apiUrl = $"{_configuration["API"]}/org/dotnet/check-isLeave";
List<string> leaveProfileIds = new();
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
var payload = new
{
profileIds = officerProfileIds.Distinct().ToList()
};
var jsonPayload = JsonConvert.SerializeObject(payload);
var content = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json");
try
{
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>()
});
leaveProfileIds = responseObj.result ?? new();
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] พบ {leaveProfileIds.Count} รายการที่ลาออก");
}
catch (Exception ex)
{
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] Call API failed: {ex.Message}");
return;
}
}
if (leaveProfileIds.Any())
{
var batchSize = 500;
var totalUpdated = 0;
var totalBatches = (int)Math.Ceiling((double)leaveProfileIds.Count / batchSize);
for (int i = 0; i < totalBatches; i++)
{
var batch = leaveProfileIds.Skip(i * batchSize).Take(batchSize).ToList();
var profilesToUpdate = await _dbContext.Set<PlacementProfile>()
.Where(p => !string.IsNullOrEmpty(p.profileId) && batch.Contains(p.profileId))
.ToListAsync();
foreach (var profile in profilesToUpdate)
{
profile.IsOfficer = false;
}
await _dbContext.SaveChangesAsync();
totalUpdated += profilesToUpdate.Count;
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] Batch {i + 1}/{totalBatches} → อัปเดต {profilesToUpdate.Count} รายการ");
}
Console.WriteLine($"[Job:UpdateStatusPlacementProfiles] อัปเดตรวมทั้งหมด {totalUpdated} รายการ → IsOfficer = false");
}
Console.WriteLine("[Job:UpdateStatusPlacementProfiles] === COMPLETED ===");
}
#endregion #endregion
} }
} }

View file

@ -21,6 +21,7 @@ using System.Text;
using System.Transactions; using System.Transactions;
using BMA.EHR.Placement.Service.Filters; using BMA.EHR.Placement.Service.Filters;
using BMA.EHR.Application.Repositories.Reports; using BMA.EHR.Application.Repositories.Reports;
using BMA.EHR.Application.Repositories;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
{ {
@ -164,6 +165,8 @@ var app = builder.Build();
if (manager != null) if (manager != null)
{ {
manager.AddOrUpdate("แจ้งเตือนระบบทดลองงาน", Job.FromExpression<ProbationReportRepository>(x => x.NotifyProbation()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local); manager.AddOrUpdate("แจ้งเตือนระบบทดลองงาน", Job.FromExpression<ProbationReportRepository>(x => x.NotifyProbation()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local);
// Job: อัพเดทสถานะผู้สอบผ่านที่ลาออกไปแล้วแต่ยังไม่ส่งไปออกคำสั่ง ทำงานทุกวันเวลา 05:00 น.
manager.AddOrUpdate("ประมวลผลข้าราชการฯ กทม.", Job.FromExpression<PlacementRepository>(x => x.UpdateStatusPlacementProfiles()), Cron.Daily(5), TimeZoneInfo.Local);
} }
// apply migrations // apply migrations