แก้ไข rabbitMQ เผยแพร่โครงสร้างค้าง
This commit is contained in:
parent
7c6991abe5
commit
f96edde325
1 changed files with 90 additions and 36 deletions
|
|
@ -631,43 +631,67 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
}
|
}
|
||||||
|
|
||||||
const _null: any = null;
|
const _null: any = null;
|
||||||
for (const item of posMaster) {
|
|
||||||
|
|
||||||
|
// ===== BATCH PROCESSING: เตรียมข้อมูลก่อน loop =====
|
||||||
|
// 1. รวบรวม profileIds ทั้งหมดที่ต้องอัพเดท
|
||||||
|
const profileIds = posMaster
|
||||||
|
.filter(item => item.next_holderId != null)
|
||||||
|
.map(item => item.next_holderId!)
|
||||||
|
.filter(id => id != null && id !== "");
|
||||||
|
|
||||||
|
// 2. Batch load profiles ทั้งหมดในครั้งเดียว (แก้ปัญหา N+1 Query)
|
||||||
|
const profilesMap = new Map<string, Profile>();
|
||||||
|
if (profileIds.length > 0) {
|
||||||
|
const profiles = await repoProfile.findBy({
|
||||||
|
id: In(profileIds)
|
||||||
|
});
|
||||||
|
profiles.forEach(p => profilesMap.set(p.id, p));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. เตรียม arrays สำหรับ batch operations
|
||||||
|
const profilesToSave: Profile[] = [];
|
||||||
|
const posMasterAssignsToSave: PosMasterAssign[] = [];
|
||||||
|
const historyCreateIds: string[] = [];
|
||||||
|
const posMasterUpdates: { id: string; current_holderId: string | null | undefined }[] = [];
|
||||||
|
|
||||||
|
// ===== LOOP: เก็บข้อมูลทั้งหมด =====
|
||||||
|
for (const item of posMaster) {
|
||||||
const dna = item.ancestorDNA?.trim();
|
const dna = item.ancestorDNA?.trim();
|
||||||
const oldPm = dna ? oldPosMasterMap.get(dna) : null;
|
const oldPm = dna ? oldPosMasterMap.get(dna) : null;
|
||||||
|
|
||||||
// Task #2160 Clone posMasterAssign
|
// Task #2160 Clone posMasterAssign
|
||||||
const assigns = assignMap.get(item.ancestorDNA);
|
const assigns = assignMap.get(item.ancestorDNA);
|
||||||
if (assigns && assigns.length > 0) {
|
if (assigns && assigns.length > 0) {
|
||||||
const newAssigns = assigns.map(({ id, ...fields }) => ({
|
const newAssigns = assigns.map(({ id, ...fields }) =>
|
||||||
...fields, // copy ทุก field ยกเว้น id
|
posMasterAssignRepository.create({
|
||||||
posMasterId: item.id, // ผูกกับ posMasterId ใหม่
|
...fields,
|
||||||
createdAt: lastUpdatedAt,
|
posMasterId: item.id,
|
||||||
createdFullName: lastUpdateFullName,
|
createdAt: lastUpdatedAt,
|
||||||
createdUserId: lastUpdateUserId,
|
createdFullName: lastUpdateFullName,
|
||||||
lastUpdatedAt: lastUpdatedAt,
|
createdUserId: lastUpdateUserId,
|
||||||
lastUpdateFullName: lastUpdateFullName,
|
lastUpdatedAt: lastUpdatedAt,
|
||||||
lastUpdateUserId: lastUpdateUserId,
|
lastUpdateFullName: lastUpdateFullName,
|
||||||
}));
|
lastUpdateUserId: lastUpdateUserId,
|
||||||
await posMasterAssignRepository.save(newAssigns);
|
})
|
||||||
|
);
|
||||||
|
posMasterAssignsToSave.push(...newAssigns);
|
||||||
}
|
}
|
||||||
|
|
||||||
// อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit
|
// เตรียมข้อมูลสำหรับ update profile
|
||||||
if (item.next_holderId != null) {
|
if (item.next_holderId != null && item.next_holderId !== "") {
|
||||||
const profile = await repoProfile.findOne({
|
const profile = profilesMap.get(item.next_holderId);
|
||||||
where: { id: item.next_holderId == null ? "" : item.next_holderId },
|
if (profile) {
|
||||||
});
|
|
||||||
if (profile != null) {
|
|
||||||
profile.posMasterNo = getPosMasterNo(item) ?? _null;
|
profile.posMasterNo = getPosMasterNo(item) ?? _null;
|
||||||
profile.org = getOrgFullName(item) ?? _null;
|
profile.org = getOrgFullName(item) ?? _null;
|
||||||
|
|
||||||
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
|
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
|
||||||
if (!item.isSit && item.positions.length > 0) {
|
if (!item.isSit && item.positions.length > 0) {
|
||||||
let position = await item.positions.find((x) => x.positionIsSelected == true);
|
let position = item.positions.find((x) => x.positionIsSelected == true);
|
||||||
if (position == null) {
|
if (position == null) {
|
||||||
position = await item.positions.find((x) => x.posLevelId == profile?.posLevelId);
|
position = item.positions.find((x) => x.posLevelId == profile?.posLevelId);
|
||||||
if (position == null) {
|
if (position == null) {
|
||||||
position = await item.positions.sort((a, b) => a.orderNo - b.orderNo)[0];
|
const sorted = [...item.positions].sort((a, b) => a.orderNo - b.orderNo);
|
||||||
|
position = sorted[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -679,29 +703,59 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
profile.positionArea = position?.positionArea ?? _null;
|
profile.positionArea = position?.positionArea ?? _null;
|
||||||
profile.positionExecutiveField = position?.positionExecutiveField ?? _null;
|
profile.positionExecutiveField = position?.positionExecutiveField ?? _null;
|
||||||
}
|
}
|
||||||
await repoProfile.save(profile);
|
|
||||||
|
profilesToSave.push(profile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// item.current_holderId = item.next_holderId;
|
|
||||||
// item.next_holderId = null;
|
// เก็บข้อมูลสำหรับ update posMaster
|
||||||
// item.lastUpdateUserId = lastUpdateUserId;
|
posMasterUpdates.push({
|
||||||
// item.lastUpdateFullName = lastUpdateFullName;
|
id: item.id,
|
||||||
// item.lastUpdatedAt = lastUpdatedAt;
|
|
||||||
await repoPosmaster.update(item.id, {
|
|
||||||
current_holderId: item.next_holderId,
|
current_holderId: item.next_holderId,
|
||||||
|
});
|
||||||
|
|
||||||
|
// เก็บ IDs ที่ต้องสร้าง history
|
||||||
|
const oldHolderId = oldPm ? oldPm.current_holderId : null;
|
||||||
|
const newHolderId = item?.next_holderId;
|
||||||
|
const isHolderChanged = oldHolderId !== newHolderId;
|
||||||
|
|
||||||
|
if (isHolderChanged) {
|
||||||
|
historyCreateIds.push(item.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== BATCH EXECUTION: save ทีละ batch =====
|
||||||
|
|
||||||
|
// 4. Batch save posMasterAssign (chunk 500)
|
||||||
|
if (posMasterAssignsToSave.length > 0) {
|
||||||
|
const chunks = chunkArray(posMasterAssignsToSave, 500);
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
await posMasterAssignRepository.save(chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Batch save profiles (chunk 200)
|
||||||
|
if (profilesToSave.length > 0) {
|
||||||
|
const chunks = chunkArray(profilesToSave, 200);
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
await repoProfile.save(chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Batch update posMasters
|
||||||
|
for (const update of posMasterUpdates) {
|
||||||
|
await repoPosmaster.update(update.id, {
|
||||||
|
current_holderId: update.current_holderId,
|
||||||
next_holderId: null,
|
next_holderId: null,
|
||||||
lastUpdateUserId,
|
lastUpdateUserId,
|
||||||
lastUpdateFullName,
|
lastUpdateFullName,
|
||||||
lastUpdatedAt,
|
lastUpdatedAt,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const oldHolderId = oldPm ? oldPm.current_holderId : null;
|
// 7. Batch create history
|
||||||
const newHolderId = item ? item.next_holderId : null;
|
for (const id of historyCreateIds) {
|
||||||
const isHolderChanged = oldHolderId !== newHolderId;
|
await CreatePosMasterHistoryOfficer(id, null);
|
||||||
|
|
||||||
if (isHolderChanged) {
|
|
||||||
await CreatePosMasterHistoryOfficer(item.id, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const act of oldposMasterAct) {
|
for (const act of oldposMasterAct) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue