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

197 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-02-26 18:04:20 +07:00
import { ViewColumn, ViewEntity } from "typeorm";
@ViewEntity({
expression: `
2025-03-04 09:31:05 +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,
@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
)
2025-02-26 18:04:20 +07:00
SELECT
2025-03-04 09:31:05 +07:00
commandDateAffect,
2025-02-26 18:04:20 +07:00
positionName,
positionCee,
2025-03-04 09:31:05 +07:00
days_diff,
Years,
Months,
Days,
2025-02-26 18:04:20 +07:00
posNo,
positionExecutive,
positionType,
positionLevel,
OrgRoot,
orgChild1,
orgChild2,
orgChild3,
orgChild4,
commandCode,
commandName,
commandNo,
commandYear,
remark,
profileId,
2025-03-04 09:31:05 +07:00
orderNumber
FROM resultData
2025-02-26 18:04:20 +07:00
UNION ALL
SELECT
CURDATE() AS commandDateAffect,
2025-03-04 09:31:05 +07:00
NULL AS positionName,
NULL AS positionCee,
2025-02-26 18:04:20 +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-04 09:31:05 +07:00
TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days',
2025-02-26 18:04:20 +07:00
NULL AS posNo,
NULL AS positionExecutive,
NULL AS positionType,
NULL AS positionLevel,
NULL AS OrgRoot,
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-04 09:31:05 +07:00
NULL AS remark,
2025-02-26 18:04:20 +07:00
profileId,
NULL AS orderNumber
2025-03-04 09:31:05 +07:00
FROM resultData
2025-02-26 18:04:20 +07:00
`,
})
export class viewCurrentTenureOfficer {
2025-03-04 09:31:05 +07:00
@ViewColumn()
commandDateAffect: Date;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
days_diff: number;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
Years: number;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
Months: number;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
Days: number;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
positionName: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
positionCee: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
posNo: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
positionExecutive: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
positionType: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
positionLevel: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
OrgRoot: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
orgChild1: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
orgChild2: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
orgChild3: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
orgChild4: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
commandCode: number;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
commandName: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
commandNo: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
commandYear: number;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
remark: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
profileId: string;
2025-02-26 18:04:20 +07:00
2025-03-04 09:31:05 +07:00
@ViewColumn()
orderNumber: number;
2025-02-26 18:04:20 +07:00
}