import { MigrationInterface, QueryRunner } from "typeorm";

export class AddTableViewCommanderDirector1729754336397 implements MigrationInterface {
    name = 'AddTableViewCommanderDirector1729754336397'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DELETE FROM \`bma_ehr_organization_demo\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","view_commander_director","bma_ehr_organization_demo"]);
        await queryRunner.query(`DROP VIEW \`view_commander_director\``);
        await queryRunner.query(`CREATE VIEW \`view_commander_director\` AS SELECT 
        \`bma_ehr_organization_demo\`.\`profile\`.\`id\` AS \`Id\`,
        \`bma_ehr_organization_demo\`.\`profile\`.\`prefix\` AS \`prefix\`,
        \`bma_ehr_organization_demo\`.\`profile\`.\`firstName\` AS \`firstName\`,
        \`bma_ehr_organization_demo\`.\`profile\`.\`lastName\` AS \`lastName\`,
        \`bma_ehr_organization_demo\`.\`profile\`.\`citizenId\` AS \`citizenId\`,
        \`bma_ehr_organization_demo\`.\`profile\`.\`position\` AS \`position\`,
        \`bma_ehr_organization_demo\`.\`posMaster\`.\`isDirector\` AS \`isDirector\`,
        \`bma_ehr_organization_demo\`.\`posLevel\`.\`posLevelName\` AS \`posLevel\`,
        \`bma_ehr_organization_demo\`.\`posType\`.\`posTypeName\` AS \`posType\`,
        \`bma_ehr_organization_demo\`.\`posMaster\`.\`orgRootId\` AS \`orgRootId\`,
        NULL AS \`actFullName\`
    FROM
        (((\`bma_ehr_organization_demo\`.\`posMaster\`
        JOIN \`bma_ehr_organization_demo\`.\`profile\` ON ((\`bma_ehr_organization_demo\`.\`posMaster\`.\`current_holderId\` = \`bma_ehr_organization_demo\`.\`profile\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`posLevel\` ON ((\`bma_ehr_organization_demo\`.\`profile\`.\`posLevelId\` = \`bma_ehr_organization_demo\`.\`posLevel\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`posType\` ON ((\`bma_ehr_organization_demo\`.\`profile\`.\`posTypeId\` = \`bma_ehr_organization_demo\`.\`posType\`.\`id\`))) 
    UNION SELECT 
        \`profileChild\`.\`id\` AS \`Id\`,
        \`profileChild\`.\`prefix\` AS \`prefix\`,
        \`profileChild\`.\`firstName\` AS \`firstName\`,
        \`profileChild\`.\`lastName\` AS \`lastName\`,
        \`profileChild\`.\`citizenId\` AS \`citizenId\`,
        \`profileChild\`.\`position\` AS \`position\`,
        \`posMasterChild\`.\`isDirector\` AS \`isDirector\`,
        \`bma_ehr_organization_demo\`.\`posLevel\`.\`posLevelName\` AS \`posLevel\`,
        \`bma_ehr_organization_demo\`.\`posType\`.\`posTypeName\` AS \`posType\`,
        \`bma_ehr_organization_demo\`.\`posMaster\`.\`orgRootId\` AS \`orgRootId\`,
        CONCAT(\`bma_ehr_organization_demo\`.\`profile\`.\`prefix\`,
                \`bma_ehr_organization_demo\`.\`profile\`.\`firstName\`,
                ' ',
                \`bma_ehr_organization_demo\`.\`profile\`.\`lastName\`) AS \`actFullName\`
    FROM
        ((((((\`bma_ehr_organization_demo\`.\`posMasterAct\`
        JOIN \`bma_ehr_organization_demo\`.\`posMaster\` \`posMasterChild\` ON ((\`bma_ehr_organization_demo\`.\`posMasterAct\`.\`posMasterChildId\` = \`posMasterChild\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`profile\` \`profileChild\` ON ((\`posMasterChild\`.\`current_holderId\` = \`profileChild\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`posLevel\` ON ((\`profileChild\`.\`posLevelId\` = \`bma_ehr_organization_demo\`.\`posLevel\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`posType\` ON ((\`profileChild\`.\`posTypeId\` = \`bma_ehr_organization_demo\`.\`posType\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`posMaster\` ON ((\`bma_ehr_organization_demo\`.\`posMasterAct\`.\`posMasterId\` = \`bma_ehr_organization_demo\`.\`posMaster\`.\`id\`)))
        JOIN \`bma_ehr_organization_demo\`.\`profile\` ON ((\`bma_ehr_organization_demo\`.\`posMaster\`.\`current_holderId\` = \`bma_ehr_organization_demo\`.\`profile\`.\`id\`)))`);
        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_commander_director","SELECT \n        `bma_ehr_organization_demo`.`profile`.`id` AS `Id`,\n        `bma_ehr_organization_demo`.`profile`.`prefix` AS `prefix`,\n        `bma_ehr_organization_demo`.`profile`.`firstName` AS `firstName`,\n        `bma_ehr_organization_demo`.`profile`.`lastName` AS `lastName`,\n        `bma_ehr_organization_demo`.`profile`.`citizenId` AS `citizenId`,\n        `bma_ehr_organization_demo`.`profile`.`position` AS `position`,\n        `bma_ehr_organization_demo`.`posMaster`.`isDirector` AS `isDirector`,\n        `bma_ehr_organization_demo`.`posLevel`.`posLevelName` AS `posLevel`,\n        `bma_ehr_organization_demo`.`posType`.`posTypeName` AS `posType`,\n        `bma_ehr_organization_demo`.`posMaster`.`orgRootId` AS `orgRootId`,\n        NULL AS `actFullName`\n    FROM\n        (((`bma_ehr_organization_demo`.`posMaster`\n        JOIN `bma_ehr_organization_demo`.`profile` ON ((`bma_ehr_organization_demo`.`posMaster`.`current_holderId` = `bma_ehr_organization_demo`.`profile`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`posLevel` ON ((`bma_ehr_organization_demo`.`profile`.`posLevelId` = `bma_ehr_organization_demo`.`posLevel`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`posType` ON ((`bma_ehr_organization_demo`.`profile`.`posTypeId` = `bma_ehr_organization_demo`.`posType`.`id`))) \n    UNION SELECT \n        `profileChild`.`id` AS `Id`,\n        `profileChild`.`prefix` AS `prefix`,\n        `profileChild`.`firstName` AS `firstName`,\n        `profileChild`.`lastName` AS `lastName`,\n        `profileChild`.`citizenId` AS `citizenId`,\n        `profileChild`.`position` AS `position`,\n        `posMasterChild`.`isDirector` AS `isDirector`,\n        `bma_ehr_organization_demo`.`posLevel`.`posLevelName` AS `posLevel`,\n        `bma_ehr_organization_demo`.`posType`.`posTypeName` AS `posType`,\n        `bma_ehr_organization_demo`.`posMaster`.`orgRootId` AS `orgRootId`,\n        CONCAT(`bma_ehr_organization_demo`.`profile`.`prefix`,\n                `bma_ehr_organization_demo`.`profile`.`firstName`,\n                ' ',\n                `bma_ehr_organization_demo`.`profile`.`lastName`) AS `actFullName`\n    FROM\n        ((((((`bma_ehr_organization_demo`.`posMasterAct`\n        JOIN `bma_ehr_organization_demo`.`posMaster` `posMasterChild` ON ((`bma_ehr_organization_demo`.`posMasterAct`.`posMasterChildId` = `posMasterChild`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`profile` `profileChild` ON ((`posMasterChild`.`current_holderId` = `profileChild`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`posLevel` ON ((`profileChild`.`posLevelId` = `bma_ehr_organization_demo`.`posLevel`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`posType` ON ((`profileChild`.`posTypeId` = `bma_ehr_organization_demo`.`posType`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`posMaster` ON ((`bma_ehr_organization_demo`.`posMasterAct`.`posMasterId` = `bma_ehr_organization_demo`.`posMaster`.`id`)))\n        JOIN `bma_ehr_organization_demo`.`profile` ON ((`bma_ehr_organization_demo`.`posMaster`.`current_holderId` = `bma_ehr_organization_demo`.`profile`.`id`)))"]);
    }

    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_commander_director","bma_ehr_organization_demo"]);
        await queryRunner.query(`DROP VIEW \`view_commander_director\``);
        await queryRunner.query(`CREATE VIEW \`view_commander_director\` AS SELECT \`profile\`.\`id\` AS \`id\`,\`profile\`.\`prefix\` AS \`prefix\`,\`profile\`.\`firstName\` AS \`firstName\`,\`profile\`.\`lastName\` AS \`lastName\`,\`profile\`.\`citizenId\` AS \`citizenId\`,\`profile\`.\`position\` AS \`position\`,\`posMaster\`.\`isDirector\` AS \`isDirector\`,\`posMaster\`.\`orgRootId\` AS \`orgRootId\`,NULL AS \`actFullName\`
    FROM (\`posMaster\` JOIN \`profile\` ON ((\`posMaster\`.\`current_holderId\` = \`profile\`.\`id\`))) 
    UNION SELECT \`profileChild\`.\`id\` AS \`id\`,\`profileChild\`.\`prefix\` AS \`prefix\`,\`profileChild\`.\`firstName\` AS \`firstName\`,\`profileChild\`.\`lastName\` AS \`lastName\`,\`profileChild\`.\`citizenId\` AS \`citizenId\`,\`profileChild\`.\`position\` AS \`position\`,\`posMasterChild\`.\`isDirector\` AS \`isDirector\`,\`posMaster\`.\`orgRootId\` AS \`orgRootId\`,CONCAT(\`profile\`.\`prefix\`,\`profile\`.\`firstName\`,' ',\`profile\`.\`lastName\`) AS \`actFullName\`
    FROM((((\`posMasterAct\` JOIN \`posMaster\` \`posMasterChild\` ON ((\`posMasterAct\`.\`posMasterChildId\` = \`posMasterChild\`.\`id\`))) JOIN \`profile\` \`profileChild\` ON ((\`posMasterChild\`.\`current_holderId\` = \`profileChild\`.\`id\`))) JOIN \`posMaster\` ON ((\`posMasterAct\`.\`posMasterId\` = \`posMaster\`.\`id\`))) JOIN \`profile\` ON ((\`posMaster\`.\`current_holderId\` = \`profile\`.\`id\`)))`);
        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_commander_director","SELECT `profile`.`id` AS `id`,`profile`.`prefix` AS `prefix`,`profile`.`firstName` AS `firstName`,`profile`.`lastName` AS `lastName`,`profile`.`citizenId` AS `citizenId`,`profile`.`position` AS `position`,`posMaster`.`isDirector` AS `isDirector`,`posMaster`.`orgRootId` AS `orgRootId`,NULL AS `actFullName`\n    FROM (`posMaster` JOIN `profile` ON ((`posMaster`.`current_holderId` = `profile`.`id`))) \n    UNION SELECT `profileChild`.`id` AS `id`,`profileChild`.`prefix` AS `prefix`,`profileChild`.`firstName` AS `firstName`,`profileChild`.`lastName` AS `lastName`,`profileChild`.`citizenId` AS `citizenId`,`profileChild`.`position` AS `position`,`posMasterChild`.`isDirector` AS `isDirector`,`posMaster`.`orgRootId` AS `orgRootId`,CONCAT(`profile`.`prefix`,`profile`.`firstName`,' ',`profile`.`lastName`) AS `actFullName`\n    FROM((((`posMasterAct` JOIN `posMaster` `posMasterChild` ON ((`posMasterAct`.`posMasterChildId` = `posMasterChild`.`id`))) JOIN `profile` `profileChild` ON ((`posMasterChild`.`current_holderId` = `profileChild`.`id`))) JOIN `posMaster` ON ((`posMasterAct`.`posMasterId` = `posMaster`.`id`))) JOIN `profile` ON ((`posMaster`.`current_holderId` = `profile`.`id`)))"]);
    }

}
