From 4ddcd270a58d288cfd2fa9672013017ae074dfa1 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 2 Jul 2025 15:50:06 +0700 Subject: [PATCH] update snap --- src/controllers/SalaryPeriodController.ts | 60 ++++++++++------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 0f43223..fbeae26 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -2746,6 +2746,7 @@ export class SalaryPeriodController extends Controller { }, }); console.log(`step3`); + const profilesToSave: SalaryProfile[] = []; if (salaryOrgNew != null) { console.log(`step4`); let salaryProfileNew = Object.assign(new SalaryProfile(), profile); @@ -2765,36 +2766,24 @@ export class SalaryPeriodController extends Controller { if (snapshot == "SNAP2") { console.log(`step5`); try{ - const salaryOrgOld = await this.salaryOrgRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, - }); - console.log(`ก่อนหา salaryOld`); - const salaryOld = await this.salaryProfileRepository.findOne({ - where: { - citizenId: salaryProfileNew.citizenId, - salaryOrgId: In(salaryOrgOld.map((x) => x.id)), - }, - }); - // const salaryOrgOldList = await this.salaryOrgRepository.find({ + // const salaryOrgOld = await this.salaryOrgRepository.find({ + // where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, + // }); + // console.log(`ก่อนหา salaryOld`); + // const salaryOld = await this.salaryProfileRepository.findOne({ // where: { - // salaryPeriodId: salaryPeriod.id, - // snapshot: "SNAP1", - // }, - // }); - - // const salaryProfileOldList = await this.salaryProfileRepository.find({ - // where: { - // salaryOrgId: In(salaryOrgOldList.map((org) => org.id)), + // citizenId: salaryProfileNew.citizenId, + // salaryOrgId: In(salaryOrgOld.map((x) => x.id)), // }, // }); + const salaryOld = await this.salaryProfileRepository + .createQueryBuilder("profile") + .innerJoin("profile.salaryOrg", "org") + .where("org.salaryPeriodId = :periodId", { periodId: salaryPeriod.id }) + .andWhere("org.snapshot = :snapshot", { snapshot: "SNAP1" }) + .andWhere("profile.citizenId = :citizenId", { citizenId: salaryProfileNew.citizenId }) + .getOne(); - // const salaryProfileMap = new Map(); - // salaryProfileOldList.forEach((item) => { - // salaryProfileMap.set(item.citizenId, item); - // }); - - // const salaryOld = salaryProfileMap.get(salaryProfileNew.citizenId); - salaryProfileNew.type = salaryOld == null ? "PENDING" : salaryOld.type; salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; @@ -2811,18 +2800,21 @@ export class SalaryPeriodController extends Controller { console.error(`❌ error processing employee:`, err); } } + profilesToSave.push(salaryProfileNew); console.log(`step6`); - await this.salaryProfileRepository.save(salaryProfileNew, { data: request }); + // console.log( + // `✅ [SNAP: ${snapshot}] Push SalaryProfile: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, + // ); + // await this.salaryProfileRepository.save(salaryProfileNew, { data: request }); + await this.salaryProfileRepository.save(profilesToSave, { chunk: 100, data: request }); setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); - console.log( - `✅ [SNAP: ${snapshot}] บันทึก SalaryProfile: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, - ); } else { console.warn(`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrg สำหรับ rootId: ${profile.rootId}`); } }), ); console.log("mlength", orgProfileEmployees.length); + // const profilesEmpToSave: SalaryProfileEmployee[] = []; await Promise.all( orgProfileEmployees.map(async (profile: any) => { const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({ @@ -2883,12 +2875,14 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; } + // profilesEmpToSave.push(salaryProfileNew); console.log(`step10`); - await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request }); - setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); console.log( - `✅ [SNAP: ${snapshot}] บันทึก SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, + `✅ [SNAP: ${snapshot}] Push SalaryProfileEmployee: ${salaryProfileNew.citizenId} (${salaryProfileNew.fullName ?? "-"})`, ); + await this.salaryProfileEmployeeRepository.save(salaryProfileNew, { data: request }); + // await this.salaryProfileEmployeeRepository.save(profilesEmpToSave, { chunk: 100, data: request }); + setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); } else { console.warn( `⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`,