Linear Flow (C-PM-01, C-PM-02, C-PM-14) #224
All checks were successful
Build & Deploy Placement Service / build (push) Successful in 5m38s

This commit is contained in:
harid 2026-06-18 15:46:22 +07:00
parent 4a8d349415
commit 35179d8cfc
2 changed files with 194 additions and 162 deletions

View file

@ -1181,127 +1181,143 @@ namespace BMA.EHR.Placement.Service.Controllers
[HttpPost("command/report/excecute")]
public async Task<ActionResult<ResponseObject>> 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();
}
}
}