diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 79cfbdb..5278d02 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -2820,89 +2820,87 @@ export class SalaryPeriodController extends Controller { console.log("mlength", orgProfileEmployees.length); const profilesEmpToSave: SalaryProfileEmployee[] = []; console.time("⏱ SalaryProfileEmployee - Total Time"); - console.time("⏱ Step5: Process ProfileEmps"); - for (const profile of orgProfileEmployees) { - const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({ - where: { - salaryPeriodId: salaryPeriod.id, - rootId: profile.rootId, - snapshot: snapshot, - group: "GROUP1", - }, - }); - //console.log(`step7`); - if (salaryOrgNew != null) { - //console.log(`step8`); - let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile); - delete salaryProfileNew.id; - // const beforeSalaryProfileNew = structuredClone(salaryProfileNew); + // await Promise.all( + // orgProfileEmployees.map(async (profile: any) => { - salaryProfileNew.salaryOrgId = salaryOrgNew.id; - salaryProfileNew.revisionId = salaryPeriod.revisionId; - salaryProfileNew.createdUserId = request.user.sub; - salaryProfileNew.createdFullName = request.user.name; - salaryProfileNew.lastUpdateUserId = request.user.sub; - salaryProfileNew.lastUpdateFullName = request.user.name; - salaryProfileNew.createdAt = new Date(); - salaryProfileNew.lastUpdatedAt = new Date(); + const batchSize = 1000; + for (let i = 0; i < orgProfileEmployees.length; i += batchSize) { + const profile = orgProfileEmployees.slice(i, i + batchSize); + const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: profile.rootId, + snapshot: snapshot, + group: "GROUP1", + }, + }); + //console.log(`step7`); + if (salaryOrgNew != null) { + //console.log(`step8`); + let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile); + delete salaryProfileNew.id; + // const beforeSalaryProfileNew = structuredClone(salaryProfileNew); - if (snapshot == "SNAP2") { - //console.log(`step9`); - const salaryOrgOld = await this.salaryOrgEmployeeRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, - }); - const salaryOld = await this.salaryProfileEmployeeRepository.findOne({ - where: { - citizenId: salaryProfileNew.citizenId, - salaryOrgId: In(salaryOrgOld.map((x) => x.id)), - }, - }); - salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type; - // salaryProfileNew.salaryLevel = salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : salaryOld?.salaryLevel; - // salaryProfileNew.group = salaryOld && salaryOld.groupNew ? salaryOld.groupNew : salaryOld?.group; - salaryProfileNew.salaryLevelNew = - salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : null; - salaryProfileNew.groupNew = - salaryOld && salaryOld.groupNew ? salaryOld.groupNew : null; - salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; - salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; - salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; - salaryProfileNew.positionSalaryAmount = - salaryOld == null ? 0 : salaryOld.positionSalaryAmount; - salaryProfileNew.positionSalaryDayAmount = - salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; - salaryProfileNew.positionSalaryAmountPer = - salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer; - salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark; - salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; - salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; - salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; - salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; - salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; - } - profilesEmpToSave.push(salaryProfileNew); - await this.salaryProfileEmployeeRepository - .createQueryBuilder() - .insert() - .into(SalaryProfileEmployee) - .values(profilesEmpToSave) - .orIgnore() - .execute(); - //console.log(`step10`); - // console.log( - // `✅ [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}`, - ); + salaryProfileNew.salaryOrgId = salaryOrgNew.id; + salaryProfileNew.revisionId = salaryPeriod.revisionId; + salaryProfileNew.createdUserId = request.user.sub; + salaryProfileNew.createdFullName = request.user.name; + salaryProfileNew.lastUpdateUserId = request.user.sub; + salaryProfileNew.lastUpdateFullName = request.user.name; + salaryProfileNew.createdAt = new Date(); + salaryProfileNew.lastUpdatedAt = new Date(); + + if (snapshot == "SNAP2") { + //console.log(`step9`); + const salaryOrgOld = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, + }); + const salaryOld = await this.salaryProfileEmployeeRepository.findOne({ + where: { + citizenId: salaryProfileNew.citizenId, + salaryOrgId: In(salaryOrgOld.map((x) => x.id)), + }, + }); + salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type; + // salaryProfileNew.salaryLevel = salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : salaryOld?.salaryLevel; + // salaryProfileNew.group = salaryOld && salaryOld.groupNew ? salaryOld.groupNew : salaryOld?.group; + salaryProfileNew.salaryLevelNew = + salaryOld && salaryOld.salaryLevelNew ? salaryOld.salaryLevelNew : null; + salaryProfileNew.groupNew = + salaryOld && salaryOld.groupNew ? salaryOld.groupNew : null; + salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; + salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; + salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; + salaryProfileNew.positionSalaryAmount = + salaryOld == null ? 0 : salaryOld.positionSalaryAmount; + salaryProfileNew.positionSalaryDayAmount = + salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; + salaryProfileNew.positionSalaryAmountPer = + salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer; + salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark; + salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; + salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; + salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; + salaryProfileNew.isRetired = salaryOld == null ? false : salaryOld.isRetired; + salaryProfileNew.isGood = salaryOld == null ? false : salaryOld.isGood; } - }; - console.timeEnd("⏱ SalaryProfileEmployee - Total Time"); - } + // profilesEmpToSave.push(salaryProfileNew); + //console.log(`step10`); + // console.log( + // `✅ [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}`, + ); + } + } + // ); + console.timeEnd("⏱ SalaryProfileEmployee - Total Time"); + } const salaryOrgNew = await this.salaryOrgRepository.find({ where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot },