diff --git a/BMA.EHR.Placement.Service/Controllers/PlacementController.cs b/BMA.EHR.Placement.Service/Controllers/PlacementController.cs index d45803ec..135d94ba 100644 --- a/BMA.EHR.Placement.Service/Controllers/PlacementController.cs +++ b/BMA.EHR.Placement.Service/Controllers/PlacementController.cs @@ -2395,51 +2395,67 @@ namespace BMA.EHR.Placement.Service.Controllers Console.WriteLine($"[CandidateReportExcecute] resultData built successfully with {resultData?.Count ?? 0} records"); - Console.WriteLine($"[CandidateReportExcecute] Calling external API: {_configuration["API"]}/org/command/excexute/create-officer-profile"); - var apiUrl = $"{_configuration["API"]}/org/command/excexute/create-officer-profile"; - using (var client = new HttpClient()) - { - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); - client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); - var _req = new HttpRequestMessage(HttpMethod.Post, apiUrl); - var _res = await client.PostAsJsonAsync(apiUrl, new - { - data = resultData - }); - var _result = await _res.Content.ReadAsStringAsync(); - Console.WriteLine($"[CandidateReportExcecute] External API response status: {_res.StatusCode}"); - if (_res.IsSuccessStatusCode) - { - Console.WriteLine("[CandidateReportExcecute] External API call successful - updating placement profiles"); - placementProfile.ForEach(profile => - { - profile.PlacementStatus = "DONE"; - if (req.refIds.Length > 0) - { - profile.commandId = req.refIds[0].commandId; - profile.refCommandCode = req.refIds[0].commandCode; - profile.refCommandDate = req.refIds[0].commandDateAffect; - profile.refCommandName = req.refIds[0].commandName; - profile.refCommandNo = $"{req.refIds[0].commandNo}/{req.refIds[0].commandYear.ToThaiYear()}"; - profile.templateDoc = req.refIds[0].remark; - } - }); - Console.WriteLine($"[CandidateReportExcecute] Saving changes to database for {placementProfile.Count} profiles"); - await _context.SaveChangesAsync(); - Console.WriteLine("[CandidateReportExcecute] Database save completed successfully"); - } - else - { - Console.Error.WriteLine($"[CandidateReportExcecute] External API call failed with status: {_res.StatusCode}"); - Console.Error.WriteLine($"[CandidateReportExcecute] Response content: {_result}"); - } - } + #region Old: Circular Flow + // Console.WriteLine($"[CandidateReportExcecute] Calling external API: {_configuration["API"]}/org/command/excexute/create-officer-profile"); + // var apiUrl = $"{_configuration["API"]}/org/command/excexute/create-officer-profile"; + // using (var client = new HttpClient()) + // { + // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); + // client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); + // var _req = new HttpRequestMessage(HttpMethod.Post, apiUrl); + // var _res = await client.PostAsJsonAsync(apiUrl, new + // { + // data = resultData + // }); + // var _result = await _res.Content.ReadAsStringAsync(); + // Console.WriteLine($"[CandidateReportExcecute] External API response status: {_res.StatusCode}"); + // if (_res.IsSuccessStatusCode) + // { + // Console.WriteLine("[CandidateReportExcecute] External API call successful - updating placement profiles"); + // placementProfile.ForEach(profile => + // { + // profile.PlacementStatus = "DONE"; + // if (req.refIds.Length > 0) + // { + // profile.commandId = req.refIds[0].commandId; + // profile.refCommandCode = req.refIds[0].commandCode; + // profile.refCommandDate = req.refIds[0].commandDateAffect; + // profile.refCommandName = req.refIds[0].commandName; + // profile.refCommandNo = $"{req.refIds[0].commandNo}/{req.refIds[0].commandYear.ToThaiYear()}"; + // profile.templateDoc = req.refIds[0].remark; + // } + // }); + // Console.WriteLine($"[CandidateReportExcecute] Saving changes to database for {placementProfile.Count} profiles"); + // await _context.SaveChangesAsync(); + // Console.WriteLine("[CandidateReportExcecute] Database save completed successfully"); + // } + // else + // { + // Console.Error.WriteLine($"[CandidateReportExcecute] External API call failed with status: {_res.StatusCode}"); + // Console.Error.WriteLine($"[CandidateReportExcecute] Response content: {_result}"); + // } + // } + #endregion - // // update placementstatus - // placementProfile.ForEach(profile => profile.PlacementStatus = "DONE"); - // await _context.SaveChangesAsync(); + // New: Linear Flow + placementProfile.ForEach(profile => + { + profile.PlacementStatus = "DONE"; + if (req.refIds.Length > 0) + { + profile.commandId = req.refIds[0].commandId; + profile.refCommandCode = req.refIds[0].commandCode; + profile.refCommandDate = req.refIds[0].commandDateAffect; + profile.refCommandName = req.refIds[0].commandName; + profile.refCommandNo = $"{req.refIds[0].commandNo}/{req.refIds[0].commandYear.ToThaiYear()}"; + profile.templateDoc = req.refIds[0].remark; + } + }); + Console.WriteLine($"[CandidateReportExcecute] Saving changes to database for {placementProfile.Count} profiles"); + await _context.SaveChangesAsync(); Console.WriteLine($"[CandidateReportExcecute] Process completed successfully at {DateTime.Now}"); - return Success(); + // Return resultData for Node to process directly (Linear Flow) + return Success(resultData); } catch (Exception ex) { diff --git a/BMA.EHR.Placement.Service/Controllers/PlacementReceiveController.cs b/BMA.EHR.Placement.Service/Controllers/PlacementReceiveController.cs index 7ad56249..32f91eae 100644 --- a/BMA.EHR.Placement.Service/Controllers/PlacementReceiveController.cs +++ b/BMA.EHR.Placement.Service/Controllers/PlacementReceiveController.cs @@ -1181,127 +1181,143 @@ namespace BMA.EHR.Placement.Service.Controllers [HttpPost("command/report/excecute")] public async Task> PostReportExecute([FromBody] ReportExecuteRequest req) { - var data = await _context.PlacementReceives - .Include(x => x.Avatar) - .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) - .ToListAsync(); - // Task #224 ปรับให้เป็น process ที่ควรบันทึกตามลำดับ - data.ForEach(profile => profile.Status = "DONE"); - await _context.SaveChangesAsync(); - - var resultData = (from p in data - join r in req.refIds - on p.Id.ToString() equals r.refId - select new - { - bodyProfile = new - { - rank = string.IsNullOrEmpty(p.rank) ? string.Empty : p.rank, - prefix = p.prefix == null ? string.Empty : p.prefix, - firstName = p.firstName == null ? string.Empty : p.firstName, - lastName = p.lastName == null ? string.Empty : p.lastName, - citizenId = p.citizenId == null ? string.Empty : p.citizenId, - position = p.position == null ? string.Empty : p.position, - posLevelId = p.posLevelId == null ? string.Empty : p.posLevelId, - posTypeId = p.posTypeId == null ? string.Empty : p.posTypeId, - email = (String?)null, - phone = p.TelephoneNumber == null ? string.Empty : p.TelephoneNumber, - keycloak = string.Empty, - isProbation = false, - isLeave = false, - dateRetire = (DateTime?)null, - dateAppoint = r.commandDateAffect, - dateStart = r.commandDateAffect, - govAgeAbsent = 0, - govAgePlus = 0, - birthDate = (p.DateOfBirth == null || p.DateOfBirth == DateTime.MinValue) ? (DateTime?)null : p.DateOfBirth, - reasonSameDate = (DateTime?)null, - ethnicity = p.Race == null ? string.Empty : p.Race, - telephoneNumber = (String?)null, - nationality = p.Nationality == null ? string.Empty : p.Nationality, - gender = p.Gender == null ? string.Empty : p.Gender, - relationship = p.Relationship == null ? string.Empty : p.Relationship, - religion = p.Religion == null ? string.Empty : p.Religion, - bloodGroup = p.BloodGroup == null ? string.Empty : p.BloodGroup, - registrationAddress = (String?)null, - registrationProvinceId = (String?)null, - registrationDistrictId = (String?)null, - registrationSubDistrictId = (String?)null, - registrationZipCode = (String?)null, - currentAddress = (String?)null, - currentProvinceId = (String?)null, - currentDistrictId = (String?)null, - currentSubDistrictId = (String?)null, - currentZipCode = (String?)null, - amount = r.amount, - amountSpecial = r.amountSpecial, - objectRefId = p.Avatar != null && p.Avatar?.ObjectRefId != null ? p.Avatar?.ObjectRefId.ToString("D") : null, - }, - bodySalarys = new - { - profileId = p.profileId, - amount = r.amount, - amountSpecial = r.amountSpecial, - positionSalaryAmount = r.positionSalaryAmount, - mouthSalaryAmount = r.mouthSalaryAmount, - positionExecutive = p.PositionExecutive, - positionExecutiveField = p.positionExecutiveField, - positionArea = p.positionArea, - positionType = p.posTypeName, - positionLevel = p.posLevelName, - commandId = r.commandId, - orgRoot = p.root, - orgChild1 = p.child1, - orgChild2 = p.child2, - orgChild3 = p.child3, - orgChild4 = p.child4, - commandNo = r.commandNo, - commandYear = r.commandYear, - posNo = p.posMasterNo?.ToString(), - posNoAbb = p.node == 4 ? $"{p.child4ShortName}" : - p.node == 3 ? $"{p.child3ShortName}" : - p.node == 2 ? $"{p.child2ShortName}" : - p.node == 1 ? $"{p.child1ShortName}" : - p.node == 0 ? $"{p.rootShortName}" : "", - commandDateAffect = r.commandDateAffect, - commandDateSign = r.commandDateSign, - positionName = p.position, - commandCode = r.commandCode, - commandName = r.commandName, - remark = r.remark, - }, - bodyPosition = new - { - posmasterId = p.posmasterId, - positionId = p.positionId, - positionName = p.position, - positionField = p.positionField, - posTypeId = p.posTypeId, - posLevelId = p.posLevelId, - posExecutiveId = p.posExecutiveId, - positionExecutiveField = p.positionExecutiveField, - positionArea = p.positionArea, - } - }).ToList(); - - var baseAPIOrg = _configuration["API"]; - var apiUrlOrg = $"{_configuration["API"]}/org/command/excexute/create-officer-profile"; - using (var client = new HttpClient()) + Console.WriteLine($"[ReceiveReportExcecute] Starting execution at {DateTime.Now}"); + try { - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); - client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); - var _res = await client.PostAsJsonAsync(apiUrlOrg, new - { - data = resultData, - }); - // // var _result = await _res.Content.ReadAsStringAsync(); - // // if (_res.IsSuccessStatusCode) - // // { - // // data.ForEach(profile => profile.Status = "DONE"); - // // await _context.SaveChangesAsync(); - // // } + var data = await _context.PlacementReceives + .Include(x => x.Avatar) + .Where(x => req.refIds.Select(x => x.refId).Contains(x.Id.ToString())) + .ToListAsync(); + + var resultData = (from p in data + join r in req.refIds + on p.Id.ToString() equals r.refId + select new + { + bodyProfile = new + { + rank = string.IsNullOrEmpty(p.rank) ? string.Empty : p.rank, + prefix = p.prefix == null ? string.Empty : p.prefix, + firstName = p.firstName == null ? string.Empty : p.firstName, + lastName = p.lastName == null ? string.Empty : p.lastName, + citizenId = p.citizenId == null ? string.Empty : p.citizenId, + position = p.position == null ? string.Empty : p.position, + posLevelId = p.posLevelId == null ? string.Empty : p.posLevelId, + posTypeId = p.posTypeId == null ? string.Empty : p.posTypeId, + email = (String?)null, + phone = p.TelephoneNumber == null ? string.Empty : p.TelephoneNumber, + keycloak = string.Empty, + isProbation = false, + isLeave = false, + dateRetire = (DateTime?)null, + dateAppoint = r.commandDateAffect, + dateStart = r.commandDateAffect, + govAgeAbsent = 0, + govAgePlus = 0, + birthDate = (p.DateOfBirth == null || p.DateOfBirth == DateTime.MinValue) ? (DateTime?)null : p.DateOfBirth, + reasonSameDate = (DateTime?)null, + ethnicity = p.Race == null ? string.Empty : p.Race, + telephoneNumber = (String?)null, + nationality = p.Nationality == null ? string.Empty : p.Nationality, + gender = p.Gender == null ? string.Empty : p.Gender, + relationship = p.Relationship == null ? string.Empty : p.Relationship, + religion = p.Religion == null ? string.Empty : p.Religion, + bloodGroup = p.BloodGroup == null ? string.Empty : p.BloodGroup, + registrationAddress = (String?)null, + registrationProvinceId = (String?)null, + registrationDistrictId = (String?)null, + registrationSubDistrictId = (String?)null, + registrationZipCode = (String?)null, + currentAddress = (String?)null, + currentProvinceId = (String?)null, + currentDistrictId = (String?)null, + currentSubDistrictId = (String?)null, + currentZipCode = (String?)null, + amount = r.amount, + amountSpecial = r.amountSpecial, + objectRefId = p.Avatar != null && p.Avatar?.ObjectRefId != null ? p.Avatar?.ObjectRefId.ToString("D") : null, + }, + bodySalarys = new + { + profileId = p.profileId, + amount = r.amount, + amountSpecial = r.amountSpecial, + positionSalaryAmount = r.positionSalaryAmount, + mouthSalaryAmount = r.mouthSalaryAmount, + positionExecutive = p.PositionExecutive, + positionExecutiveField = p.positionExecutiveField, + positionArea = p.positionArea, + positionType = p.posTypeName, + positionLevel = p.posLevelName, + commandId = r.commandId, + orgRoot = p.root, + orgChild1 = p.child1, + orgChild2 = p.child2, + orgChild3 = p.child3, + orgChild4 = p.child4, + commandNo = r.commandNo, + commandYear = r.commandYear, + posNo = p.posMasterNo?.ToString(), + posNoAbb = p.node == 4 ? $"{p.child4ShortName}" : + p.node == 3 ? $"{p.child3ShortName}" : + p.node == 2 ? $"{p.child2ShortName}" : + p.node == 1 ? $"{p.child1ShortName}" : + p.node == 0 ? $"{p.rootShortName}" : "", + commandDateAffect = r.commandDateAffect, + commandDateSign = r.commandDateSign, + positionName = p.position, + commandCode = r.commandCode, + commandName = r.commandName, + remark = r.remark, + }, + bodyPosition = new + { + posmasterId = p.posmasterId, + positionId = p.positionId, + positionName = p.position, + positionField = p.positionField, + posTypeId = p.posTypeId, + posLevelId = p.posLevelId, + posExecutiveId = p.posExecutiveId, + positionExecutiveField = p.positionExecutiveField, + positionArea = p.positionArea, + } + }).ToList(); + + Console.WriteLine($"[ReceiveReportExcecute] resultData built successfully with {resultData?.Count ?? 0} records"); + #region Old: Circular Flow + // var baseAPIOrg = _configuration["API"]; + // var apiUrlOrg = $"{_configuration["API"]}/org/command/excexute/create-officer-profile"; + // using (var client = new HttpClient()) + // { + // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); + // client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]); + // var _res = await client.PostAsJsonAsync(apiUrlOrg, new + // { + // data = resultData, + // }); + // // // var _result = await _res.Content.ReadAsStringAsync(); + // // // if (_res.IsSuccessStatusCode) + // // // { + // // // data.ForEach(profile => profile.Status = "DONE"); + // // // await _context.SaveChangesAsync(); + // // // } + // } + #endregion + + // New: Linear Flow + data.ForEach(profile => profile.Status = "DONE"); + Console.WriteLine($"[ReceiveReportExcecute] Saving changes to database for {data.Count} profiles"); + await _context.SaveChangesAsync(); + Console.WriteLine($"[ReceiveReportExcecute] Process completed successfully at {DateTime.Now}"); + return Success(resultData); + } + catch (Exception ex) + { + Console.Error.WriteLine($"[ReceiveReportExcecute] Error occurred: {ex.Message}"); + Console.Error.WriteLine($"[ReceiveReportExcecute] Stack trace: {ex.StackTrace}"); + throw; } - return Success(); } } }