From d8ec3b220132f47a60fda8aa92c00aff67b722c4 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 19 Sep 2025 02:28:36 +0700 Subject: [PATCH] add edu report2 --- src/controllers/ReportController.ts | 234 ++++++++++++++++++---------- 1 file changed, 150 insertions(+), 84 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 4b993247..13d3e8a2 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3658,17 +3658,26 @@ export class ReportController extends Controller { if (profileIds.length > 0) { educationsData = await this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...profileIds)", { profileIds }) - .getRawMany(); - - // Filter เฉพาะ latest education - educationsData = educationsData.filter((ed: any) => ed.rn === 1); + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...profileIds) + GROUP BY pe2.profileId + )`, + { profileIds }, + ) + .getMany(); } // Step 5: ดึงข้อมูล Salary ล่าสุดของแต่ละ Profile @@ -3676,17 +3685,18 @@ export class ReportController extends Controller { if (profileIds.length > 0) { salariesData = await this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...profileIds)", { profileIds }) - .getRawMany(); - - // Filter เฉพาะ latest salary - salariesData = salariesData.filter((sal: any) => sal.rn === 1); + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...profileIds) + GROUP BY ps2.profileId + )`, + { profileIds }, + ) + .getMany(); } // Step 6: ดึงข้อมูล Positions ที่เกี่ยวข้อง @@ -3832,27 +3842,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child1ProfileIds) + GROUP BY pe2.profileId + )`, + { child1ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child1ProfileIds) + GROUP BY ps2.profileId + )`, + { child1ProfileIds }, + ) + .getMany(), ]); } @@ -3998,27 +4022,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child2ProfileIds) + GROUP BY pe2.profileId + )`, + { child2ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child2ProfileIds) + GROUP BY ps2.profileId + )`, + { child2ProfileIds }, + ) + .getMany(), ]); } @@ -4164,27 +4202,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child3ProfileIds) + GROUP BY pe2.profileId + )`, + { child3ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child3ProfileIds) + GROUP BY ps2.profileId + )`, + { child3ProfileIds }, + ) + .getMany(), ]); } @@ -4328,27 +4380,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child4ProfileIds) + GROUP BY pe2.profileId + )`, + { child4ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child4ProfileIds) + GROUP BY ps2.profileId + )`, + { child4ProfileIds }, + ) + .getMany(), ]); }