From 0ab3445ad033cd619435513741f757c06e9aad7e Mon Sep 17 00:00:00 2001 From: harid Date: Tue, 9 Jun 2026 10:35:30 +0700 Subject: [PATCH] lookup province, district, subDistrict IDs with parent filtering --- Models/District.cs | 3 +++ Models/SubDistrict.cs | 3 +++ Services/PeriodExamService.cs | 34 ++++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Models/District.cs b/Models/District.cs index 6240c7d..655e51c 100644 --- a/Models/District.cs +++ b/Models/District.cs @@ -9,6 +9,9 @@ namespace BMA.EHR.Recurit.Exam.Service.Models [Required, MaxLength(150), Column(Order = 1), Comment("เขต/อำเภอ")] public string name { get; set; } = string.Empty; + [Column(Order = 2), Comment("รหัสอ้างอิงจังหวัด")] + public Guid? provinceId { get; set; } = null; + // [Column(Order = 2), Comment("สถานะการใช้งาน")] // public bool IsActive { get; set; } = true; diff --git a/Models/SubDistrict.cs b/Models/SubDistrict.cs index 0610c37..b2185f4 100644 --- a/Models/SubDistrict.cs +++ b/Models/SubDistrict.cs @@ -12,6 +12,9 @@ namespace BMA.EHR.Recurit.Exam.Service.Models [MaxLength(10), Column(Order = 2), Comment("รหัสไปรษณีย์")] public string? zipCode { get; set; } = null; + [Column(Order = 3), Comment("รหัสอ้างอิงอำเภอ")] + public Guid? districtId { get; set; } = null; + // [Column(Order = 3), Comment("สถานะการใช้งาน")] // public bool IsActive { get; set; } = true; diff --git a/Services/PeriodExamService.cs b/Services/PeriodExamService.cs index 46ee1a4..5aaf756 100644 --- a/Services/PeriodExamService.cs +++ b/Services/PeriodExamService.cs @@ -3292,6 +3292,28 @@ namespace BMA.EHR.Recurit.Exam.Service.Services positionNameWithoutLevel = positionNameWithoutLevel.Replace(posLevelName, "").Trim(); } + // lookup province, district, subDistrict IDs with parent filtering + var registProvinceId = provincesCache.FirstOrDefault(x => x.name == firstAddress?.Province)?.Id; + var registDistrictId = districtsCache.FirstOrDefault(x => x.name == firstAddress?.Amphur && x.provinceId == registProvinceId)?.Id; + var registSubDistrictId = subDistrictsCache.FirstOrDefault(x => x.name == firstAddress?.District && x.districtId == registDistrictId)?.Id; + var currentProvinceId = provincesCache.FirstOrDefault(x => x.name == firstAddress?.Province1)?.Id; + var currentDistrictId = districtsCache.FirstOrDefault(x => x.name == firstAddress?.Amphur1 && x.provinceId == currentProvinceId)?.Id; + var currentSubDistrictId = subDistrictsCache.FirstOrDefault(x => x.name == firstAddress?.District1 && x.districtId == currentDistrictId)?.Id; + + // log warning when address lookup fails + if (registProvinceId == null && !string.IsNullOrWhiteSpace(firstAddress?.Province)) + Console.WriteLine($"[WARN] Regist province not found: {firstAddress?.Province}"); + if (registDistrictId == null && !string.IsNullOrWhiteSpace(firstAddress?.Amphur)) + Console.WriteLine($"[WARN] Regist district not found: {firstAddress?.Amphur}, Province: {firstAddress?.Province}"); + if (registSubDistrictId == null && !string.IsNullOrWhiteSpace(firstAddress?.District)) + Console.WriteLine($"[WARN] Regist subdistrict not found: {firstAddress?.District}, District: {firstAddress?.Amphur}"); + if (currentProvinceId == null && !string.IsNullOrWhiteSpace(firstAddress?.Province1)) + Console.WriteLine($"[WARN] Current province not found: {firstAddress?.Province1}"); + if (currentDistrictId == null && !string.IsNullOrWhiteSpace(firstAddress?.Amphur1)) + Console.WriteLine($"[WARN] Current district not found: {firstAddress?.Amphur1}, Province: {firstAddress?.Province1}"); + if (currentSubDistrictId == null && !string.IsNullOrWhiteSpace(firstAddress?.District1)) + Console.WriteLine($"[WARN] Current subdistrict not found: {firstAddress?.District1}, District: {firstAddress?.Amphur1}"); + var placementProfile = new PlacementProfile { Placement = placement, @@ -3313,15 +3335,15 @@ namespace BMA.EHR.Recurit.Exam.Service.Services Telephone = firstAddress?.Telephone ?? "", MobilePhone = firstAddress?.Mobile ?? "", RegistAddress = registAddress, - RegistProvinceId = provincesCache.FirstOrDefault(x => x.name == firstAddress?.Province)?.Id, - RegistDistrictId = districtsCache.FirstOrDefault(x => x.name == firstAddress?.Amphur)?.Id, - RegistSubDistrictId = subDistrictsCache.FirstOrDefault(x => x.name == firstAddress?.District)?.Id, + RegistProvinceId = registProvinceId, + RegistDistrictId = registDistrictId, + RegistSubDistrictId = registSubDistrictId, RegistZipCode = firstAddress?.ZipCode ?? "", RegistSame = false, CurrentAddress = currentAddress, - CurrentProvinceId = provincesCache.FirstOrDefault(x => x.name == firstAddress?.Province1)?.Id, - CurrentDistrictId = districtsCache.FirstOrDefault(x => x.name == firstAddress?.Amphur1)?.Id, - CurrentSubDistrictId = subDistrictsCache.FirstOrDefault(x => x.name == firstAddress?.District1)?.Id, + CurrentProvinceId = currentProvinceId, + CurrentDistrictId = currentDistrictId, + CurrentSubDistrictId = currentSubDistrictId, CurrentZipCode = firstAddress?.ZipCode1, Marry = candidate.Marry?.Contains("สมรส") ?? false, OccupationPositionType = "other",