แก้บัค สรรหา

This commit is contained in:
kittapath 2025-10-12 14:52:54 +07:00
parent 655f031952
commit 13ad60d2e3
2 changed files with 1 additions and 39 deletions

View file

@ -2611,7 +2611,7 @@ namespace BMA.EHR.Recruit.Service.Controllers
/// <response code="200">เมื่อโอนคนแข่งขันไปบรรจุสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("placement/{examId:length(36)}")]
[HttpPost("placement/{examId:length(36)}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]

View file

@ -183,40 +183,28 @@ namespace BMA.EHR.Recruit.Service.Services
// 🚀 Prepare HTTP client once
var httpClient1 = new HttpClient();
httpClient1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Replace("Bearer ", ""));
Console.WriteLine("118");
httpClient1.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
Console.WriteLine("117");
var apiUrl1 = $"{_configuration["API"]}/org/pos/level";
Console.WriteLine("116");
var response1 = await httpClient1.GetStringAsync(apiUrl1);
Console.WriteLine("115");
var posOptions = JsonConvert.DeserializeObject<RecruitPosRequest>(response1);
Console.WriteLine("114");
var recruitImport = await _context.RecruitImports.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == examId);
Console.WriteLine("113");
if (recruitImport == null)
throw new Exception(GlobalMessages.DataNotFound);
var _placement = await _contextMetadata.Placements.AsQueryable()
.FirstOrDefaultAsync(x => x.PlacementType.Name == "สอบแข่งขัน" && x.RefId == recruitImport.Id);
Console.WriteLine("112");
// if (_placement != null)
// throw new Exception("รอบการสอบนี้ได้ทำการบรรจุไปแล้ว");
// 🚀 Pre-load all lookup data once
var placementTypesCache = await _contextMetadata.PlacementTypes.ToListAsync();
Console.WriteLine("1011");
var provincesCache = await _contextOrg.province.ToListAsync();
Console.WriteLine("1012");
var districtsCache = await _contextOrg.district.ToListAsync();
Console.WriteLine("1013");
var subDistrictsCache = await _contextOrg.subDistrict.ToListAsync();
Console.WriteLine("1014");
var educationLevelsCache = await _contextOrg.educationLevel.ToListAsync();
Console.WriteLine("1015");
var placement = new Placement
{
@ -236,7 +224,6 @@ namespace BMA.EHR.Recruit.Service.Services
LastUpdateFullName = FullName ?? "",
};
await _contextMetadata.Placements.AddAsync(placement);
Console.WriteLine("191");
// 🚀 Load all related data with single queries
var candidates = await _context.Recruits.AsQueryable()
@ -263,7 +250,6 @@ namespace BMA.EHR.Recruit.Service.Services
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Replace("Bearer ", ""));
httpClient.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
Console.WriteLine("181");
// 🚀 Batch HTTP requests
var orgTasks = candidates.Select(async candidate =>
{
@ -281,7 +267,6 @@ namespace BMA.EHR.Recruit.Service.Services
return new { CitizenId = candidate.CitizenId ?? "", org = (dynamic?)null };
}
}).ToList();
Console.WriteLine("171");
var orgResults = await Task.WhenAll(orgTasks);
var orgDict = orgResults.ToDictionary(x => x.CitizenId ?? "", x => x.org);
@ -290,54 +275,40 @@ namespace BMA.EHR.Recruit.Service.Services
var placementProfiles = new List<PlacementProfile>();
var placementEducations = new List<PlacementEducation>();
var placementCertificates = new List<PlacementCertificate>();
Console.WriteLine("161");
foreach (var candidate in candidates)
{
Console.WriteLine("5111");
if (string.IsNullOrWhiteSpace(candidate.ExamId) ||
!recruitScoresDict.TryGetValue(candidate.ExamId, out var recruitScore))
continue;
Console.WriteLine("5112");
var org = orgDict.TryGetValue(candidate.CitizenId ?? "", out var orgValue) ? orgValue : null;
Console.WriteLine("5113");
var isOfficer = org?.result != null;
Console.WriteLine("5114");
// 🚀 Cache repeated calculations
var firstAddress = candidate.Addresses?.FirstOrDefault();
Console.WriteLine("5115");
var firstEducation = candidate.Educations?.FirstOrDefault();
Console.WriteLine("5116");
var firstCertificate = candidate.Certificates?.FirstOrDefault();
Console.WriteLine("5117");
var firstOccupation = candidate.Occupations?.FirstOrDefault();
Console.WriteLine("5118");
var registAddress = BuildAddress(firstAddress?.Address, firstAddress?.Moo, firstAddress?.Soi, firstAddress?.Road);
Console.WriteLine("5119");
var currentAddress = BuildAddress(firstAddress?.Address1, firstAddress?.Moo1, firstAddress?.Soi1, firstAddress?.Road1);
Console.WriteLine("5120");
// หาค่า posLevelName หลังสุด
var posLevelObject = posOptions?.result?.FirstOrDefault(x =>
!string.IsNullOrWhiteSpace(x.posLevelName) &&
!string.IsNullOrWhiteSpace(candidate.PositionName) &&
candidate.PositionName.Contains(x.posLevelName));
Console.WriteLine("5121");
// เก็บเฉพาะค่า posLevelName
var posLevelName = posLevelObject?.posLevelName;
// สร้างตัวแปร PositionName ที่ตัดค่า posLevelName ออก
var positionNameWithoutLevel = candidate.PositionName ?? "";
Console.WriteLine("5122");
if (!string.IsNullOrWhiteSpace(posLevelName))
{
positionNameWithoutLevel = positionNameWithoutLevel.Replace(posLevelName, "").Trim();
}
Console.WriteLine("5123");
var placementProfile = new PlacementProfile
{
@ -425,9 +396,7 @@ namespace BMA.EHR.Recruit.Service.Services
posLevelIdOld = org?.result?.posLevelId ?? "",
posLevelNameOld = org?.result?.posLevelName ?? "",
};
Console.WriteLine("5124");
placementProfiles.Add(placementProfile);
Console.WriteLine("5125");
var placementEducation = new PlacementEducation
{
@ -447,9 +416,7 @@ namespace BMA.EHR.Recruit.Service.Services
CreatedFullName = FullName ?? "",
LastUpdateFullName = FullName ?? "",
};
Console.WriteLine("5125");
placementEducations.Add(placementEducation);
Console.WriteLine("5126");
var placementCertificate = new PlacementCertificate
{
@ -466,17 +433,12 @@ namespace BMA.EHR.Recruit.Service.Services
LastUpdateFullName = FullName ?? "",
};
placementCertificates.Add(placementCertificate);
Console.WriteLine("511");
}
Console.WriteLine("141");
// 🚀 Batch insert all records
await _contextMetadata.PlacementProfiles.AddRangeAsync(placementProfiles);
Console.WriteLine("131");
await _contextMetadata.PlacementEducations.AddRangeAsync(placementEducations);
Console.WriteLine("121");
await _contextMetadata.PlacementCertificates.AddRangeAsync(placementCertificates);
Console.WriteLine("111");
// 🚀 Single SaveChanges at the end
await _contextMetadata.SaveChangesAsync();