Optimize handler_org batch writes
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
750947f34f
commit
6c1e4a1e42
1 changed files with 45 additions and 14 deletions
|
|
@ -1035,6 +1035,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
|
|
||||||
// Clone oldposMasterAct
|
// Clone oldposMasterAct
|
||||||
console.time("[AMQ] clone_oldposMasterAct");
|
console.time("[AMQ] clone_oldposMasterAct");
|
||||||
|
const posMasterActRowsToInsert: Partial<PosMasterAct>[] = [];
|
||||||
for (const act of oldposMasterAct) {
|
for (const act of oldposMasterAct) {
|
||||||
const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? "";
|
const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? "";
|
||||||
const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? "";
|
const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? "";
|
||||||
|
|
@ -1046,7 +1047,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
|
|
||||||
const { id, posMaster, posMasterChild, ...fields } = act;
|
const { id, posMaster, posMasterChild, ...fields } = act;
|
||||||
|
|
||||||
const newAct = {
|
posMasterActRowsToInsert.push({
|
||||||
...fields,
|
...fields,
|
||||||
posMasterId: newParentId,
|
posMasterId: newParentId,
|
||||||
posMasterChildId: newChildId,
|
posMasterChildId: newChildId,
|
||||||
|
|
@ -1056,9 +1057,13 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
lastUpdatedAt: new Date(),
|
lastUpdatedAt: new Date(),
|
||||||
lastUpdateFullName: user ? user.name : "system",
|
lastUpdateFullName: user ? user.name : "system",
|
||||||
lastUpdateUserId: user ? user.sub : "system",
|
lastUpdateUserId: user ? user.sub : "system",
|
||||||
};
|
});
|
||||||
|
}
|
||||||
await posMasterActRepository.save(newAct);
|
if (posMasterActRowsToInsert.length > 0) {
|
||||||
|
const chunks = chunkArray(posMasterActRowsToInsert, 500);
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
await posMasterActRepository.insert(chunk);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
console.timeEnd("[AMQ] clone_oldposMasterAct");
|
console.timeEnd("[AMQ] clone_oldposMasterAct");
|
||||||
|
|
||||||
|
|
@ -1554,11 +1559,13 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
profiles.forEach((profile) => profileEmployeeMap.set(profile.id, profile));
|
profiles.forEach((profile) => profileEmployeeMap.set(profile.id, profile));
|
||||||
}
|
}
|
||||||
const updatedProfileEmployeeIds = new Set<string>();
|
const updatedProfileEmployeeIds = new Set<string>();
|
||||||
|
const employeePosMasterIdsToTouch: string[] = [];
|
||||||
|
const employeeTempPosMasterIdsToTouch: string[] = [];
|
||||||
|
|
||||||
for (const item of employeePosMaster) {
|
for (const item of employeePosMaster) {
|
||||||
if (item.next_holderId != null) {
|
if (item.next_holderId != null) {
|
||||||
const profile = profileEmployeeMap.get(item.next_holderId);
|
const profile = profileEmployeeMap.get(item.next_holderId);
|
||||||
const position = await item.positions.find((x) => x.positionIsSelected == true);
|
const position = item.positions.find((x) => x.positionIsSelected == true);
|
||||||
const _null: any = null;
|
const _null: any = null;
|
||||||
if (profile != null) {
|
if (profile != null) {
|
||||||
profile.posLevelId = position?.posLevelId ?? _null;
|
profile.posLevelId = position?.posLevelId ?? _null;
|
||||||
|
|
@ -1567,15 +1574,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
updatedProfileEmployeeIds.add(profile.id);
|
updatedProfileEmployeeIds.add(profile.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
item.lastUpdateUserId = lastUpdateUserId;
|
employeePosMasterIdsToTouch.push(item.id);
|
||||||
item.lastUpdateFullName = lastUpdateFullName;
|
|
||||||
item.lastUpdatedAt = lastUpdatedAt;
|
|
||||||
await repoEmployeePosmaster.save(item);
|
|
||||||
}
|
}
|
||||||
for (const item of employeeTempPosMaster) {
|
for (const item of employeeTempPosMaster) {
|
||||||
if (item.next_holderId != null) {
|
if (item.next_holderId != null) {
|
||||||
const profile = profileEmployeeMap.get(item.next_holderId);
|
const profile = profileEmployeeMap.get(item.next_holderId);
|
||||||
const position = await item.positions.find((x) => x.positionIsSelected == true);
|
const position = item.positions.find((x) => x.positionIsSelected == true);
|
||||||
const _null: any = null;
|
const _null: any = null;
|
||||||
if (profile != null) {
|
if (profile != null) {
|
||||||
profile.posLevelId = position?.posLevelId ?? _null;
|
profile.posLevelId = position?.posLevelId ?? _null;
|
||||||
|
|
@ -1584,10 +1588,37 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
updatedProfileEmployeeIds.add(profile.id);
|
updatedProfileEmployeeIds.add(profile.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
item.lastUpdateUserId = lastUpdateUserId;
|
employeeTempPosMasterIdsToTouch.push(item.id);
|
||||||
item.lastUpdateFullName = lastUpdateFullName;
|
}
|
||||||
item.lastUpdatedAt = lastUpdatedAt;
|
if (employeePosMasterIdsToTouch.length > 0) {
|
||||||
await repoEmployeeTempPosmaster.save(item);
|
const chunks = chunkArray(employeePosMasterIdsToTouch, 500);
|
||||||
|
const employeePosMasterTableName = repoEmployeePosmaster.metadata.tableName;
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
const wherePlaceholders = chunk.map(() => "?").join(", ");
|
||||||
|
await manager.query(
|
||||||
|
`UPDATE \`${employeePosMasterTableName}\`
|
||||||
|
SET lastUpdateUserId = ?,
|
||||||
|
lastUpdateFullName = ?,
|
||||||
|
lastUpdatedAt = ?
|
||||||
|
WHERE id IN (${wherePlaceholders})`,
|
||||||
|
[lastUpdateUserId, lastUpdateFullName, lastUpdatedAt, ...chunk],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (employeeTempPosMasterIdsToTouch.length > 0) {
|
||||||
|
const chunks = chunkArray(employeeTempPosMasterIdsToTouch, 500);
|
||||||
|
const employeeTempPosMasterTableName = repoEmployeeTempPosmaster.metadata.tableName;
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
const wherePlaceholders = chunk.map(() => "?").join(", ");
|
||||||
|
await manager.query(
|
||||||
|
`UPDATE \`${employeeTempPosMasterTableName}\`
|
||||||
|
SET lastUpdateUserId = ?,
|
||||||
|
lastUpdateFullName = ?,
|
||||||
|
lastUpdatedAt = ?
|
||||||
|
WHERE id IN (${wherePlaceholders})`,
|
||||||
|
[lastUpdateUserId, lastUpdateFullName, lastUpdatedAt, ...chunk],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (updatedProfileEmployeeIds.size > 0) {
|
if (updatedProfileEmployeeIds.size > 0) {
|
||||||
const profilesToSave = Array.from(updatedProfileEmployeeIds)
|
const profilesToSave = Array.from(updatedProfileEmployeeIds)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue