hrms-api-org/src/migration/1741663507026-updateFieldIsHigh.ts
2025-03-11 10:36:59 +07:00

266 lines
18 KiB
TypeScript

import { MigrationInterface, QueryRunner } from "typeorm";
export class UpdateFieldIsHigh1741663507026 implements MigrationInterface {
name = 'UpdateFieldIsHigh1741663507026'
public async up(queryRunner: QueryRunner): Promise<void> {
// await queryRunner.query(`CREATE VIEW \`view_current_tenure_officer\` AS
// 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,
// LAG(commandDateSign) OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) AS prevCommandDateSign,
// 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)
// ORDER BY
// commandDateAffect ASC, commandDateSign ASC
// ) AS groupedPosition
// WHERE
// prevCommandDateSign IS NULL OR commandDateSign >= prevCommandDateSign
// GROUP BY
// profileId, groupedId, positionName
// )
// SELECT
// commandDateAffect,
// positionName,
// positionCee,
// days_diff,
// Years,
// Months,
// Days,
// posNo,
// positionExecutive,
// positionType,
// positionLevel,
// OrgRoot,
// orgChild1,
// orgChild2,
// orgChild3,
// orgChild4,
// commandCode,
// commandName,
// commandNo,
// commandYear,
// remark,
// profileId,
// 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 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,
// NULL AS remark,
// profileId,
// NULL AS orderNumber
// FROM resultData
// `);
// await queryRunner.query(`INSERT INTO \`bma_ehr_organization_demo\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, ["bma_ehr_organization_demo","VIEW","view_current_tenure_officer","WITH resultData AS (\n SELECT\n commandDateAffect,\n positionName,\n positionCee,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) AS days_diff,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) % 30.4375 AS 'Days',\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileId,\n ROW_NUMBER() OVER (PARTITION BY profileId ORDER BY commandDateAffect ASC) AS orderNumber\n FROM (\n SELECT\n commandDateAffect,\n commandDateSign,\n positionName,\n positionCee,\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileId,\n LAG(commandDateSign) OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) AS prevCommandDateSign,\n ROW_NUMBER() OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) -\n ROW_NUMBER() OVER (PARTITION BY positionName ORDER BY commandDateAffect ASC, commandDateSign ASC) as groupedId\n FROM\n profileSalary\n WHERE\n commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16)\n ORDER BY\n commandDateAffect ASC, commandDateSign ASC\n ) AS groupedPosition\n WHERE\n prevCommandDateSign IS NULL OR commandDateSign >= prevCommandDateSign\n GROUP BY\n profileId, groupedId, positionName\n )\n SELECT\n commandDateAffect,\n positionName,\n positionCee,\n days_diff,\n Years,\n Months,\n Days,\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileId,\n orderNumber\n FROM resultData\n\n UNION ALL\n\n SELECT\n CURDATE() AS commandDateAffect,\n NULL AS positionName,\n NULL AS positionCee,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days',\n NULL AS posNo,\n NULL AS positionExecutive,\n NULL AS positionType,\n NULL AS positionLevel,\n NULL AS OrgRoot,\n NULL AS orgChild1,\n NULL AS orgChild2,\n NULL AS orgChild3,\n NULL AS orgChild4,\n NULL AS commandCode,\n NULL AS commandName,\n NULL AS commandNo,\n NULL AS commandYear,\n NULL AS remark,\n profileId,\n NULL AS orderNumber\n FROM resultData"]);
// await queryRunner.query(`CREATE VIEW \`view_current_tenure_employee\` AS
// 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,
// LAG(commandDateSign) OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) AS prevCommandDateSign,
// 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)
// ORDER BY
// commandDateAffect ASC, commandDateSign ASC
// ) AS groupedPosition
// WHERE
// prevCommandDateSign IS NULL OR commandDateSign >= prevCommandDateSign
// GROUP BY
// profileEmployeeId, groupedId, positionName
// )
// SELECT
// commandDateAffect,
// positionName,
// positionCee,
// days_diff,
// Years,
// Months,
// Days,
// posNo,
// positionExecutive,
// positionType,
// positionLevel,
// OrgRoot,
// orgChild1,
// orgChild2,
// orgChild3,
// orgChild4,
// commandCode,
// commandName,
// commandNo,
// commandYear,
// remark,
// profileEmployeeId,
// 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 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,
// NULL AS remark,
// profileEmployeeId,
// NULL AS orderNumber
// FROM resultData
// `);
// await queryRunner.query(`INSERT INTO \`bma_ehr_organization_demo\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, ["bma_ehr_organization_demo","VIEW","view_current_tenure_employee","WITH resultData AS (\n SELECT\n commandDateAffect,\n positionName,\n positionCee,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) AS days_diff,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (ORDER BY commandDateAffect), MIN(commandDateAffect)) % 30.4375 AS 'Days',\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileEmployeeId,\n ROW_NUMBER() OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect ASC) AS orderNumber\n FROM (\n SELECT\n commandDateAffect,\n commandDateSign,\n positionName,\n positionCee,\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileEmployeeId,\n LAG(commandDateSign) OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) AS prevCommandDateSign,\n ROW_NUMBER() OVER (ORDER BY commandDateAffect ASC, commandDateSign ASC) -\n ROW_NUMBER() OVER (PARTITION BY positionName ORDER BY commandDateAffect ASC, commandDateSign ASC) as groupedId\n FROM\n profileSalary\n WHERE\n commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16)\n ORDER BY\n commandDateAffect ASC, commandDateSign ASC\n ) AS groupedPosition\n WHERE\n prevCommandDateSign IS NULL OR commandDateSign >= prevCommandDateSign\n GROUP BY\n profileEmployeeId, groupedId, positionName\n )\n SELECT\n commandDateAffect,\n positionName,\n positionCee,\n days_diff,\n Years,\n Months,\n Days,\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileEmployeeId,\n orderNumber\n FROM resultData\n\n UNION ALL\n\n SELECT\n CURDATE() AS commandDateAffect,\n NULL AS positionName,\n NULL AS positionCee,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days',\n NULL AS posNo,\n NULL AS positionExecutive,\n NULL AS positionType,\n NULL AS positionLevel,\n NULL AS OrgRoot,\n NULL AS orgChild1,\n NULL AS orgChild2,\n NULL AS orgChild3,\n NULL AS orgChild4,\n NULL AS commandCode,\n NULL AS commandName,\n NULL AS commandNo,\n NULL AS commandYear,\n NULL AS remark,\n profileEmployeeId,\n NULL AS orderNumber\n FROM resultData"]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// await queryRunner.query(`DELETE FROM \`bma_ehr_organization_demo\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","view_current_tenure_employee","bma_ehr_organization_demo"]);
// await queryRunner.query(`DROP VIEW \`view_current_tenure_employee\``);
// await queryRunner.query(`DELETE FROM \`bma_ehr_organization_demo\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","view_current_tenure_officer","bma_ehr_organization_demo"]);
// await queryRunner.query(`DROP VIEW \`view_current_tenure_officer\``);
}
}