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

305 lines
7.8 KiB
TypeScript
Raw Normal View History

2025-02-19 11:03:30 +07:00
import { ViewColumn, ViewEntity } from "typeorm";
@ViewEntity({
expression: `
WITH Position AS (
SELECT
pn.posMasterId,
ROW_NUMBER() OVER (PARTITION BY pn.posMasterId) AS pn_number
FROM employeePosition pn
WHERE pn.positionIsSelected IS TRUE
),
PosMaster AS (
SELECT
pm.current_holderId,
pm.posMasterNo,
pm.orgRootId,
pm.orgChild1Id,
pm.orgChild2Id,
pm.orgChild3Id,
pm.orgChild4Id,
orgRoot.orgRootName,
orgChild1.orgChild1Name,
orgChild2.orgChild2Name,
orgChild3.orgChild3Name,
orgChild4.orgChild4Name,
CASE
WHEN pm.orgChild1Id IS NULL THEN CONCAT(orgRoot.orgRootShortName, " ", pm.posMasterNo)
WHEN pm.orgChild2Id IS NULL THEN CONCAT(orgChild1.orgChild1ShortName, " ", pm.posMasterNo)
WHEN pm.orgChild3Id IS NULL THEN CONCAT(orgChild2.orgChild2ShortName, " ", pm.posMasterNo)
WHEN pm.orgChild4Id IS NULL THEN CONCAT(orgChild3.orgChild3ShortName, " ", pm.posMasterNo)
ELSE CONCAT(orgChild4.orgChild4ShortName, " ", pm.posMasterNo)
2025-02-19 11:03:30 +07:00
END AS searchShortName,
ROW_NUMBER() OVER (PARTITION BY pm.current_holderId ORDER BY pm.posMasterNo DESC) AS pm_number
FROM employeePosMaster pm
LEFT JOIN orgRevision ON orgRevision.id = pm.orgRevisionId
LEFT JOIN orgRoot ON orgRoot.id = pm.orgRootId
LEFT JOIN orgChild1 ON orgChild1.id = pm.orgChild1Id
LEFT JOIN orgChild2 ON orgChild2.id = pm.orgChild2Id
LEFT JOIN orgChild3 ON orgChild3.id = pm.orgChild3Id
LEFT JOIN orgChild4 ON orgChild4.id = pm.orgChild4Id
LEFT JOIN Position pn ON pm.id = pn.posMasterId AND pn.pn_number = 1
WHERE orgRevision.orgRevisionIsCurrent IS TRUE
AND orgRevision.orgRevisionIsDraft IS FALSE
),
2025-03-07 18:09:29 +07:00
Educations AS (
SELECT
eds.profileEmployeeId,
JSON_ARRAYAGG(
JSON_OBJECT(
'degree', eds.degree,
'field', eds.field,
'educationLevel', eds.educationLevel,
'isEducation', eds.isEducation,
'isHigh', eds.isHigh
)
) AS Educations
FROM (
SELECT DISTINCT
eds.degree,
eds.field,
eds.educationLevel,
eds.isEducation,
eds.isHigh,
2025-03-11 10:36:59 +07:00
eds.profileEmployeeId,
eds.level
2025-03-07 18:09:29 +07:00
FROM profileEducation eds
) AS eds
GROUP BY eds.profileEmployeeId
2025-03-11 10:36:59 +07:00
ORDER BY eds.level DESC
2025-03-07 18:09:29 +07:00
),
EducationLevels AS (
SELECT
edls.profileEmployeeId,
GROUP_CONCAT(DISTINCT edls.educationLevel ORDER BY edls.educationLevel SEPARATOR ', ') AS educationLevels
FROM profileEducation edls
WHERE edls.educationLevel IS NOT NULL AND edls.educationLevel != ''
GROUP BY edls.profileEmployeeId
ORDER BY edls.level DESC
),
Degrees AS (
SELECT
degs.profileEmployeeId,
GROUP_CONCAT(DISTINCT degs.degree ORDER BY degs.degree SEPARATOR ', ') AS degrees
FROM profileEducation degs
WHERE degs.degree IS NOT NULL AND degs.degree != ''
GROUP BY degs.profileEmployeeId
),
Fields AS (
SELECT
fies.profileEmployeeId,
GROUP_CONCAT(DISTINCT fies.field ORDER BY fies.field SEPARATOR ', ') AS fields
FROM profileEducation fies
WHERE fies.field IS NOT NULL AND fies.field != ''
GROUP BY fies.profileEmployeeId
),
2025-03-05 18:14:40 +07:00
PositionDate AS (
SELECT
vcto.Years,
vcto.Months,
vcto.Days,
vcto.profileEmployeeId
FROM tenurePositionEmployee vcto
),
PositionLevelDate AS (
SELECT
vctlo.Years,
vctlo.Months,
vctlo.Days,
vctlo.profileEmployeeId
FROM tenureLevelEmployee vctlo
)
2025-02-19 11:03:30 +07:00
SELECT
p.id as profileEmployeeId,
2025-02-19 11:03:30 +07:00
p.citizenId,
2025-02-20 12:12:04 +07:00
p.rank,
2025-02-19 11:03:30 +07:00
p.prefix,
p.firstName,
p.lastName,
p.isProbation,
p.isLeave,
p.isRetirement,
p.leaveType,
2025-02-20 12:12:04 +07:00
p.employeeClass,
2025-02-19 11:03:30 +07:00
pm.posMasterNo,
pm.orgRootId,
pm.orgChild1Id,
pm.orgChild2Id,
pm.orgChild3Id,
pm.orgChild4Id,
pm.orgRootName,
pm.orgChild1Name,
pm.orgChild2Name,
pm.orgChild3Name,
pm.orgChild4Name,
CASE
WHEN pm.orgChild1Id IS NULL THEN pm.orgRootName
WHEN pm.orgChild2Id IS NULL THEN CONCAT(pm.orgChild1Name, " ", pm.orgRootName)
WHEN pm.orgChild3Id IS NULL THEN CONCAT(pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
WHEN pm.orgChild4Id IS NULL THEN CONCAT(pm.orgChild3Name, " ", pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
ELSE CONCAT(pm.orgChild4Name, " ", pm.orgChild3Name, " ", pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
END AS org,
pm.searchShortName,
p.position,
posType.posTypeName,
2025-02-24 14:49:30 +07:00
CONCAT(posType.posTypeShortName, ' ', posLevel.posLevelName) AS "posLevelName",
2025-02-19 11:03:30 +07:00
p.gender,
p.relationship,
p.dateAppoint,
2025-02-24 12:16:47 +07:00
p.dateRetire,
p.dateRetireLaw,
2025-02-19 11:03:30 +07:00
p.birthdate,
2025-03-07 18:09:29 +07:00
eds.Educations,
edls.educationLevels,
degs.degrees,
fies.fields,
2025-03-05 18:14:40 +07:00
TIMESTAMPDIFF(YEAR, p.birthdate, CURDATE()) AS age,
vcto.Years,
vcto.Months,
vcto.Days,
vctlo.Years AS levelYears,
vctlo.Months AS levelMonths,
vctlo.Days AS levelDays
2025-02-19 11:03:30 +07:00
FROM profileEmployee p
LEFT JOIN employeePosLevel posLevel ON p.posLevelId = posLevel.id
LEFT JOIN employeePosType posType ON p.posTypeId = posType.id
LEFT JOIN PosMaster pm ON p.id = pm.current_holderId AND pm.pm_number = 1
2025-03-07 18:09:29 +07:00
LEFT JOIN Educations eds ON p.id = eds.profileEmployeeId
LEFT JOIN EducationLevels edls ON p.id = edls.profileEmployeeId
LEFT JOIN Degrees degs ON p.id = degs.profileEmployeeId
LEFT JOIN Fields fies ON p.id = fies.profileEmployeeId
2025-03-05 18:14:40 +07:00
LEFT JOIN PositionDate vcto ON p.id = vcto.profileEmployeeId
LEFT JOIN PositionLevelDate vctlo ON p.id = vctlo.profileEmployeeId
2025-02-19 11:03:30 +07:00
`,
})
export class viewRegistryEmployee {
2025-03-06 15:18:43 +07:00
@ViewColumn()
profileEmployeeId: string;
@ViewColumn()
citizenId: string;
@ViewColumn()
prefix: string;
@ViewColumn()
firstName: string;
@ViewColumn()
lastName: string;
@ViewColumn()
isProbation: boolean;
@ViewColumn()
isLeave: boolean;
@ViewColumn()
isRetirement: boolean;
@ViewColumn()
leaveType: string;
@ViewColumn()
posMasterNo: string;
@ViewColumn()
orgRootId: string;
@ViewColumn()
orgChild1Id: string;
@ViewColumn()
orgChild2Id: string;
@ViewColumn()
orgChild3Id: string;
@ViewColumn()
orgChild4Id: string;
@ViewColumn()
orgRootName: string;
@ViewColumn()
orgChild1Name: string;
@ViewColumn()
orgChild2Name: string;
@ViewColumn()
orgChild3Name: string;
@ViewColumn()
orgChild4Name: string;
@ViewColumn()
org: string;
@ViewColumn()
searchShortName: string;
@ViewColumn()
position: string;
@ViewColumn()
posTypeName: string;
@ViewColumn()
posLevelName: string;
@ViewColumn()
gender: string;
@ViewColumn()
relationship: string;
@ViewColumn()
dateAppoint: Date;
@ViewColumn()
dateRetire: Date;
@ViewColumn()
dateRetireLaw: Date;
@ViewColumn()
birthdate: Date;
@ViewColumn()
2025-03-10 09:48:05 +07:00
degrees: string;
2025-03-06 15:18:43 +07:00
@ViewColumn()
age: number;
@ViewColumn()
Years: number;
@ViewColumn()
Months: number;
@ViewColumn()
Days: number;
@ViewColumn()
levelYears: number;
@ViewColumn()
levelMonths: number;
@ViewColumn()
levelDays: number;
2025-03-07 18:09:29 +07:00
@ViewColumn()
Educations: object;
@ViewColumn()
educationLevels: string;
@ViewColumn()
fields: string;
2025-08-28 17:07:45 +07:00
@ViewColumn()
employeeClass: string;
2025-02-19 11:03:30 +07:00
}