import { ViewColumn, ViewEntity } from "typeorm";

@ViewEntity({
  expression: `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,
        profile.keycloak AS keycloakId,
        profile.isProbation AS isProbation,
        CONCAT(
            CASE 
                WHEN posMaster.orgChild1Id IS NULL THEN orgRoot.orgRootShortName
                WHEN posMaster.orgChild2Id IS NULL THEN orgChild1.orgChild1ShortName
                WHEN posMaster.orgChild3Id IS NULL THEN orgChild2.orgChild2ShortName
                WHEN posMaster.orgChild4Id IS NULL THEN orgChild3.orgChild3ShortName
                ELSE orgChild4.orgChild4ShortName
            END,
            posMaster.posMasterNo
        ) AS posNo,
        posMaster.isDirector AS isDirector,
        posLevel.posLevelName AS posLevel,
        posType.posTypeName AS posType,
        posExecutive.posExecutiveName AS posExecutiveName,
        orgRoot.isDeputy AS isDeputy,
        orgChild1.isOfficer AS isOfficer,
        posMaster.orgRevisionId AS orgRevisionId,
        posMaster.orgRootId AS orgRootId,
        posMaster.orgChild1Id AS orgChild1Id,
        posMaster.orgChild2Id AS orgChild2Id,
        posMaster.orgChild3Id AS orgChild3Id,
        posMaster.orgChild4Id AS orgChild4Id,
        posMaster.positionSign AS positionSign,
        CONCAT(posMaster.id, profile.id) AS \`key\`,
        (
          SELECT hrms_organization.acting.actFullNameKeycloakId
          FROM hrms_organization.view_director_acting acting
          WHERE ((hrms_organization.acting.Id = hrms_organization.posMaster.current_holderId)
          AND (hrms_organization.acting.orgRootId = hrms_organization.posMaster.orgRootId)
          AND ((hrms_organization.acting.orgChild1Id = hrms_organization.posMaster.orgChild1Id)
          OR (hrms_organization.acting.orgChild1Id IS NULL))
          AND ((hrms_organization.acting.orgChild2Id = hrms_organization.posMaster.orgChild2Id)
          OR (hrms_organization.acting.orgChild2Id IS NULL))
          AND ((hrms_organization.acting.orgChild3Id = hrms_organization.posMaster.orgChild3Id)
          OR (hrms_organization.acting.orgChild3Id IS NULL))
          AND ((hrms_organization.acting.orgChild4Id = hrms_organization.posMaster.orgChild4Id)
          OR (hrms_organization.acting.orgChild4Id IS NULL)))
          LIMIT 1
        ) AS actFullNameKeycloakId,
        (
          SELECT actFullNameId 
          FROM view_director_acting AS acting
          WHERE acting.Id = posMaster.current_holderId 
          AND acting.orgRootId = posMaster.orgRootId 
          AND (acting.orgChild1Id = posMaster.orgChild1Id OR acting.orgChild1Id IS NULL)
          AND (acting.orgChild2Id = posMaster.orgChild2Id OR acting.orgChild2Id IS NULL)
          AND (acting.orgChild3Id = posMaster.orgChild3Id OR acting.orgChild3Id IS NULL)
          AND (acting.orgChild4Id = posMaster.orgChild4Id OR acting.orgChild4Id IS NULL)
          LIMIT 1
        ) AS actFullNameId,
        (
          SELECT actFullName 
          FROM view_director_acting AS acting
          WHERE acting.Id = posMaster.current_holderId 
          AND acting.orgRootId = posMaster.orgRootId 
          AND (acting.orgChild1Id = posMaster.orgChild1Id OR acting.orgChild1Id IS NULL)
          AND (acting.orgChild2Id = posMaster.orgChild2Id OR acting.orgChild2Id IS NULL)
          AND (acting.orgChild3Id = posMaster.orgChild3Id OR acting.orgChild3Id IS NULL)
          AND (acting.orgChild4Id = posMaster.orgChild4Id OR acting.orgChild4Id IS NULL)
          LIMIT 1
        ) AS actFullName
    FROM
        posMaster
        JOIN profile ON posMaster.current_holderId = profile.id
        LEFT JOIN orgRoot ON posMaster.orgRootId = orgRoot.id
        LEFT JOIN orgChild1 ON posMaster.orgChild1Id = orgChild1.id
        LEFT JOIN orgChild2 ON posMaster.orgChild2Id = orgChild2.id
        LEFT JOIN orgChild3 ON posMaster.orgChild3Id = orgChild3.id
        LEFT JOIN orgChild4 ON posMaster.orgChild4Id = orgChild4.id
        JOIN posLevel ON profile.posLevelId = posLevel.id
        JOIN posType ON profile.posTypeId = posType.id
        LEFT JOIN position ON posMaster.id = position.posMasterId
        LEFT JOIN posExecutive ON position.posExecutiveId = posExecutive.id
        INNER JOIN orgRevision ON posMaster.orgRevisionId = orgRevision.id
    WHERE
        (position.positionIsSelected = TRUE)
        AND (orgRevision.orgRevisionIsCurrent = TRUE
        AND orgRevision.orgRevisionIsDraft = FALSE)`,
})
export class viewDirector {
  @ViewColumn()
  keycloakId: string;
  @ViewColumn()
  id: string;
  @ViewColumn()
  prefix: string;
  @ViewColumn()
  firstName: string;
  @ViewColumn()
  lastName: string;
  @ViewColumn()
  citizenId: string;
  @ViewColumn()
  position: string;
  @ViewColumn()
  posNo: string;
  @ViewColumn()
  posLevel: string;
  @ViewColumn()
  posType: string;
  @ViewColumn()
  posExecutiveName: string;
  @ViewColumn()
  isDirector: boolean;
  @ViewColumn()
  isDeputy: boolean;
  @ViewColumn()
  isOfficer: boolean;
  @ViewColumn()
  orgRevisionId: string;
  @ViewColumn()
  orgRootId: string;
  @ViewColumn()
  orgChild1Id: string;
  @ViewColumn()
  orgChild2Id: string;
  @ViewColumn()
  orgChild3Id: string;
  @ViewColumn()
  orgChild4Id: string;
  @ViewColumn()
  orgRoot: string;
  @ViewColumn()
  orgChild1: string;
  @ViewColumn()
  orgChild2: string;
  @ViewColumn()
  orgChild3: string;
  @ViewColumn()
  orgChild4: string;
  @ViewColumn()
  actFullNameId: string;
  @ViewColumn()
  actFullNameKeycloakId: string;
  @ViewColumn()
  actFullName: string;
  @ViewColumn()
  key: string;
  @ViewColumn()
  positionSign: string;
}
