hrms-api-org/src/entities/view/viewCurrentTenureExcOfficer.ts

197 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-02-28 11:45:04 +07:00
import { ViewColumn, ViewEntity } from "typeorm";
@ViewEntity({
expression: `
2025-03-05 12:18:24 +07:00
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,
ROW_NUMBER() OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) -
ROW_NUMBER() OVER (PARTITION BY positionName ORDER BY commandDateAffect ASC, commandDateSign ASC) as groupedId
FROM
profileSalary
WHERE
commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) AND
positionExecutive <> ''
ORDER BY
commandDateAffect ASC, commandDateSign ASC
) AS groupedPositionExe
GROUP BY
groupedId, positionExecutive
)
2025-02-28 11:45:04 +07:00
SELECT
2025-03-05 12:18:24 +07:00
commandDateAffect,
2025-02-28 11:45:04 +07:00
positionName,
positionCee,
2025-03-05 12:18:24 +07:00
days_diff,
Years,
Months,
Days,
2025-02-28 11:45:04 +07:00
posNo,
positionExecutive,
positionType,
positionLevel,
2025-03-05 12:18:24 +07:00
orgRoot,
2025-02-28 11:45:04 +07:00
orgChild1,
orgChild2,
orgChild3,
orgChild4,
commandCode,
commandName,
commandNo,
commandYear,
remark,
profileId,
2025-03-05 12:18:24 +07:00
orderNumber
FROM resultData
2025-02-28 11:45:04 +07:00
UNION ALL
SELECT
CURDATE() AS commandDateAffect,
2025-03-05 12:18:24 +07:00
NULL AS positionName,
NULL AS positionCee,
2025-02-28 11:45:04 +07:00
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',
2025-03-05 12:18:24 +07:00
TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days',
2025-02-28 11:45:04 +07:00
NULL AS posNo,
NULL AS positionExecutive,
NULL AS positionType,
NULL AS positionLevel,
2025-03-05 12:18:24 +07:00
NULL AS orgRoot,
2025-02-28 11:45:04 +07:00
NULL AS orgChild1,
NULL AS orgChild2,
NULL AS orgChild3,
NULL AS orgChild4,
NULL AS commandCode,
NULL AS commandName,
NULL AS commandNo,
NULL AS commandYear,
2025-03-05 12:18:24 +07:00
NULL AS remark,
2025-02-28 11:45:04 +07:00
profileId,
NULL AS orderNumber
2025-03-05 12:18:24 +07:00
FROM resultData
2025-02-28 11:45:04 +07:00
`,
})
export class viewCurrentTenureExcOfficer {
@ViewColumn()
commandDateAffect: Date;
@ViewColumn()
days_diff: number;
@ViewColumn()
Years: number;
@ViewColumn()
Months: number;
@ViewColumn()
Days: number;
@ViewColumn()
positionName: string;
@ViewColumn()
positionCee: string;
@ViewColumn()
posNo: string;
@ViewColumn()
positionExecutive: string;
@ViewColumn()
positionType: string;
@ViewColumn()
positionLevel: string;
@ViewColumn()
OrgRoot: string;
@ViewColumn()
orgChild1: string;
@ViewColumn()
orgChild2: string;
@ViewColumn()
orgChild3: string;
@ViewColumn()
orgChild4: string;
@ViewColumn()
commandCode: number;
@ViewColumn()
commandName: string;
@ViewColumn()
commandNo: string;
@ViewColumn()
commandYear: number;
@ViewColumn()
remark: string;
@ViewColumn()
profileId: string;
@ViewColumn()
orderNumber: number;
}