From 53b6256c56cff7cb657bc2ef3987875580d73184 Mon Sep 17 00:00:00 2001 From: kittapath Date: Tue, 4 Mar 2025 09:31:05 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileSalaryController.ts | 2 +- .../view/viewCurrentTenureEmployee.ts | 211 +++++++++++------- src/entities/view/viewCurrentTenureOfficer.ts | 205 ++++++++++------- 3 files changed, 254 insertions(+), 164 deletions(-) diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 8c7c6e33..dc945be6 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -116,7 +116,7 @@ export class ProfileSalaryController extends Controller { year: curr.Years ? Math.floor(Number(curr.Years)) : 0, month: curr.Months ? Math.floor(Number(curr.Months)) : 0, day: curr.Days ? Math.floor(Number(curr.Days)) : 0, - name: _posExecutive[index]?.positionName, + name: _posExecutive[index]?.positionExecutive, })) : []; diff --git a/src/entities/view/viewCurrentTenureEmployee.ts b/src/entities/view/viewCurrentTenureEmployee.ts index 294e7045..de7cb1c6 100644 --- a/src/entities/view/viewCurrentTenureEmployee.ts +++ b/src/entities/view/viewCurrentTenureEmployee.ts @@ -2,23 +2,83 @@ import { ViewColumn, ViewEntity } from "typeorm"; @ViewEntity({ expression: ` + WITH resultData AS ( + SELECT + commandDateAffect, + positionName, + positionCee, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) AS days_diff, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 365.2524 AS 'Years', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) % 30.4375 AS 'Days', + posNo, + positionExecutive, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileEmployeeId, + ROW_NUMBER() OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect ASC) AS orderNumber + FROM ( + SELECT + commandDateAffect, + commandDateSign, + positionName, + positionCee, + posNo, + positionExecutive, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileEmployeeId, + @group := IF(@prevPosition = positionName, @group, @group + 1) AS groupedId, + @prevPosition := positionName + FROM + profileSalary, + (SELECT @group := 0, @prevPosition := NULL) AS vars + WHERE + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + ORDER BY + commandDateAffect ASC, commandDateSign ASC + ) AS groupedPosition + GROUP BY + profileEmployeeId, groupedId, positionName + ) SELECT - DATE(MIN(commandDateAffect)) AS commandDateAffect, - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) AS days_diff, - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years', - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days', + commandDateAffect, positionName, positionCee, + days_diff, + Years, + Months, + Days, posNo, + positionExecutive, positionType, positionLevel, OrgRoot, @@ -32,26 +92,21 @@ import { ViewColumn, ViewEntity } from "typeorm"; commandYear, remark, profileEmployeeId, - ROW_NUMBER() OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect ASC) AS orderNumber - FROM - profileSalary - WHERE - profileEmployeeId IS NOT NULL AND - commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) - GROUP BY - profileEmployeeId, positionName + orderNumber + FROM resultData UNION ALL SELECT CURDATE() AS commandDateAffect, + NULL AS positionName, + NULL AS positionCee, TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff, TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years', TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months', - TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', - NULL AS positionName, - NULL AS positionCee, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', NULL AS posNo, + NULL AS positionExecutive, NULL AS positionType, NULL AS positionLevel, NULL AS OrgRoot, @@ -63,89 +118,79 @@ import { ViewColumn, ViewEntity } from "typeorm"; NULL AS commandName, NULL AS commandNo, NULL AS commandYear, - 'Comparison with current date' AS remark, + NULL AS remark, profileEmployeeId, NULL AS orderNumber - FROM - profileSalary - WHERE - profileEmployeeId IS NOT NULL AND - commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) - GROUP BY - profileEmployeeId - - ORDER BY - profileEmployeeId, - commandDateAffect ASC + FROM resultData `, }) export class viewCurrentTenureEmployee { - @ViewColumn() - commandDateAffect: Date; + @ViewColumn() + commandDateAffect: Date; - @ViewColumn() - days_diff: number; + @ViewColumn() + days_diff: number; - @ViewColumn() - Years: number; + @ViewColumn() + Years: number; - @ViewColumn() - Months: number; + @ViewColumn() + Months: number; - @ViewColumn() - Days: number; + @ViewColumn() + Days: number; - @ViewColumn() - positionName: string; + @ViewColumn() + positionName: string; - @ViewColumn() - positionCee: string; + @ViewColumn() + positionCee: string; - @ViewColumn() - posNo: string; + @ViewColumn() + posNo: string; - @ViewColumn() - positionExecutive: string; + @ViewColumn() + positionExecutive: string; - @ViewColumn() - positionType: string; + @ViewColumn() + positionType: string; - @ViewColumn() - positionLevel: string; + @ViewColumn() + positionLevel: string; - @ViewColumn() - OrgRoot: string; + @ViewColumn() + OrgRoot: string; - @ViewColumn() - orgChild1: string; + @ViewColumn() + orgChild1: string; - @ViewColumn() - orgChild2: string; + @ViewColumn() + orgChild2: string; - @ViewColumn() - orgChild3: string; + @ViewColumn() + orgChild3: string; - @ViewColumn() - orgChild4: string; + @ViewColumn() + orgChild4: string; - @ViewColumn() - commandCode: number; + @ViewColumn() + commandCode: number; - @ViewColumn() - commandName: string; + @ViewColumn() + commandName: string; - @ViewColumn() - commandNo: string; + @ViewColumn() + commandNo: string; - @ViewColumn() - commandYear: number; + @ViewColumn() + commandYear: number; - @ViewColumn() - remark: string; + @ViewColumn() + remark: string; - @ViewColumn() - profileEmployeeId: string; + @ViewColumn() + profileEmployeeId: string; - @ViewColumn() - orderNumber: number; -} \ No newline at end of file + @ViewColumn() + orderNumber: number; +} diff --git a/src/entities/view/viewCurrentTenureOfficer.ts b/src/entities/view/viewCurrentTenureOfficer.ts index 5899687c..c7078c87 100644 --- a/src/entities/view/viewCurrentTenureOfficer.ts +++ b/src/entities/view/viewCurrentTenureOfficer.ts @@ -2,22 +2,81 @@ import { ViewColumn, ViewEntity } from "typeorm"; @ViewEntity({ expression: ` + WITH resultData AS ( + SELECT + commandDateAffect, + positionName, + positionCee, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) AS days_diff, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 365.2524 AS 'Years', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) % 30.4375 AS 'Days', + posNo, + positionExecutive, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileId, + ROW_NUMBER() OVER (PARTITION BY profileId ORDER BY commandDateAffect ASC) AS orderNumber + FROM ( + SELECT + commandDateAffect, + commandDateSign, + positionName, + positionCee, + posNo, + positionExecutive, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileId, + @group := IF(@prevPosition = positionName, @group, @group + 1) AS groupedId, + @prevPosition := positionName + FROM + profileSalary, + (SELECT @group := 0, @prevPosition := NULL) AS vars + WHERE + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + ORDER BY + commandDateAffect ASC, commandDateSign ASC + ) AS groupedPosition + GROUP BY + profileId, groupedId, positionName + ) SELECT - DATE(MIN(commandDateAffect)) AS commandDateAffect, - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) AS days_diff, - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years', - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', - TIMESTAMPDIFF( - DAY, - LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days', + commandDateAffect, positionName, positionCee, + days_diff, + Years, + Months, + Days, posNo, positionExecutive, positionType, @@ -33,24 +92,19 @@ import { ViewColumn, ViewEntity } from "typeorm"; commandYear, remark, profileId, - ROW_NUMBER() OVER (PARTITION BY profileId ORDER BY commandDateAffect ASC) AS orderNumber - FROM - profileSalary - WHERE - commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) - GROUP BY - profileId, positionName + orderNumber + FROM resultData UNION ALL SELECT CURDATE() AS commandDateAffect, + NULL AS positionName, + NULL AS positionCee, TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff, TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years', TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months', - TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', - NULL AS positionName, - NULL AS positionCee, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', NULL AS posNo, NULL AS positionExecutive, NULL AS positionType, @@ -64,88 +118,79 @@ import { ViewColumn, ViewEntity } from "typeorm"; NULL AS commandName, NULL AS commandNo, NULL AS commandYear, - 'Comparison with current date' AS remark, + NULL AS remark, profileId, NULL AS orderNumber - FROM - profileSalary - WHERE - commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) - GROUP BY - profileId - - ORDER BY - profileId, - commandDateAffect ASC + FROM resultData `, }) export class viewCurrentTenureOfficer { - @ViewColumn() - commandDateAffect: Date; + @ViewColumn() + commandDateAffect: Date; - @ViewColumn() - days_diff: number; + @ViewColumn() + days_diff: number; - @ViewColumn() - Years: number; + @ViewColumn() + Years: number; - @ViewColumn() - Months: number; + @ViewColumn() + Months: number; - @ViewColumn() - Days: number; + @ViewColumn() + Days: number; - @ViewColumn() - positionName: string; + @ViewColumn() + positionName: string; - @ViewColumn() - positionCee: string; + @ViewColumn() + positionCee: string; - @ViewColumn() - posNo: string; + @ViewColumn() + posNo: string; - @ViewColumn() - positionExecutive: string; + @ViewColumn() + positionExecutive: string; - @ViewColumn() - positionType: string; + @ViewColumn() + positionType: string; - @ViewColumn() - positionLevel: string; + @ViewColumn() + positionLevel: string; - @ViewColumn() - OrgRoot: string; + @ViewColumn() + OrgRoot: string; - @ViewColumn() - orgChild1: string; + @ViewColumn() + orgChild1: string; - @ViewColumn() - orgChild2: string; + @ViewColumn() + orgChild2: string; - @ViewColumn() - orgChild3: string; + @ViewColumn() + orgChild3: string; - @ViewColumn() - orgChild4: string; + @ViewColumn() + orgChild4: string; - @ViewColumn() - commandCode: number; + @ViewColumn() + commandCode: number; - @ViewColumn() - commandName: string; + @ViewColumn() + commandName: string; - @ViewColumn() - commandNo: string; + @ViewColumn() + commandNo: string; - @ViewColumn() - commandYear: number; + @ViewColumn() + commandYear: number; - @ViewColumn() - remark: string; + @ViewColumn() + remark: string; - @ViewColumn() - profileId: string; + @ViewColumn() + profileId: string; - @ViewColumn() - orderNumber: number; + @ViewColumn() + orderNumber: number; }