fix: bug save posMasterHistory, tuning performance script
This commit is contained in:
parent
9927c73547
commit
ef17236eb0
3 changed files with 186 additions and 18 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { In } from "typeorm";
|
||||
import { SavePosMasterHistory } from "./../interfaces/OrgMapping";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
||||
|
|
@ -320,3 +321,85 @@ export async function SavePosMasterHistoryOfficer(
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export interface BatchPosMasterHistoryOperation {
|
||||
posMasterDnaId: string;
|
||||
profileId: string | null;
|
||||
pm: SavePosMasterHistory | null;
|
||||
}
|
||||
|
||||
export async function BatchSavePosMasterHistoryOfficer(
|
||||
queryRunner: any,
|
||||
operations: BatchPosMasterHistoryOperation[],
|
||||
): Promise<boolean> {
|
||||
if (operations.length === 0) return true;
|
||||
|
||||
try {
|
||||
const repoPosMasterHistory = queryRunner.manager.getRepository(PosMasterHistory);
|
||||
const dnaIds = operations.map((op) => op.posMasterDnaId);
|
||||
|
||||
// Fetch all existing history records in ONE query
|
||||
const existingHistory = await repoPosMasterHistory.find({
|
||||
where: { ancestorDNA: In(dnaIds) },
|
||||
order: { createdAt: "DESC" },
|
||||
});
|
||||
|
||||
// Build lookup map
|
||||
const historyByDna = new Map<string, PosMasterHistory[]>();
|
||||
for (const h of existingHistory) {
|
||||
if (!historyByDna.has(h.ancestorDNA)) {
|
||||
historyByDna.set(h.ancestorDNA, []);
|
||||
}
|
||||
historyByDna.get(h.ancestorDNA)!.push(h);
|
||||
}
|
||||
|
||||
// Process operations and collect new records
|
||||
const newRecords: PosMasterHistory[] = [];
|
||||
const _null: any = null;
|
||||
|
||||
for (const op of operations) {
|
||||
const existing = historyByDna.get(op.posMasterDnaId)?.[0];
|
||||
const shouldInsert = !existing && op.profileId && op.pm;
|
||||
const profileChanged = existing && existing.profileId !== op.profileId;
|
||||
|
||||
if (shouldInsert || profileChanged) {
|
||||
const newPmh = new PosMasterHistory();
|
||||
newPmh.ancestorDNA = op.posMasterDnaId;
|
||||
newPmh.prefix = op.pm?.prefix ?? _null;
|
||||
newPmh.firstName = op.pm?.firstName ?? _null;
|
||||
newPmh.lastName = op.pm?.lastName ?? _null;
|
||||
newPmh.position = op.pm?.position ?? _null;
|
||||
newPmh.posType = op.pm?.posType ?? _null;
|
||||
newPmh.posLevel = op.pm?.posLevel ?? _null;
|
||||
newPmh.posExecutive = op.pm?.posExecutive ?? _null;
|
||||
newPmh.profileId = op.profileId ?? _null;
|
||||
newPmh.rootDnaId = op.pm?.rootDnaId ?? _null;
|
||||
newPmh.child1DnaId = op.pm?.child1DnaId ?? _null;
|
||||
newPmh.child2DnaId = op.pm?.child2DnaId ?? _null;
|
||||
newPmh.child3DnaId = op.pm?.child3DnaId ?? _null;
|
||||
newPmh.child4DnaId = op.pm?.child4DnaId ?? _null;
|
||||
newPmh.shortName = op.pm?.shortName ?? _null;
|
||||
newPmh.posMasterNoPrefix = op.pm?.posMasterNoPrefix ?? _null;
|
||||
newPmh.posMasterNo = op.pm?.posMasterNo ?? _null;
|
||||
newPmh.posMasterNoSuffix = op.pm?.posMasterNoSuffix ?? _null;
|
||||
newPmh.createdUserId = "system";
|
||||
newPmh.createdFullName = "system";
|
||||
newPmh.lastUpdateUserId = "system";
|
||||
newPmh.lastUpdateFullName = "system";
|
||||
newPmh.createdAt = new Date();
|
||||
newPmh.lastUpdatedAt = new Date();
|
||||
newRecords.push(newPmh);
|
||||
}
|
||||
}
|
||||
|
||||
// Batch insert all new records
|
||||
if (newRecords.length > 0) {
|
||||
await queryRunner.manager.save(PosMasterHistory, newRecords);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error("BatchSavePosMasterHistoryOfficer error:", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue