แก้บัค สรรหา

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

View file

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