import { ViewColumn, ViewEntity } from "typeorm";

@ViewEntity({
  expression: `SELECT 
    employeePosMaster.id,
    employeePosMaster.posMasterNoPrefix,
    employeePosMaster.posMasterNo,
    employeePosMaster.posMasterNoSuffix,
    employeePosMaster.orgRevisionId,
    employeePosMaster.orgRootId,
    employeePosMaster.orgChild1Id,
    employeePosMaster.orgChild2Id,
    employeePosMaster.orgChild3Id,
    employeePosMaster.orgChild4Id,
    employeePosMaster.current_holderId,
    profileEmployee.id as profileId,
    profileEmployee.prefix,
    profileEmployee.firstName,
    profileEmployee.lastName,
    profileEmployee.citizenId,
    profileEmployee.position,
    profileEmployee.amount,
    profileEmployee.dateRetire,
    profileEmployee.birthDate,
    profileEmployee.salaryLevel,
    profileEmployee.group,
    orgRoot.id as rootId,
    orgRoot.orgRootShortName,
    orgRoot.orgRootOrder,
    orgRoot.orgRootName,
    orgChild1.id as child1Id,
    orgChild1.orgChild1ShortName,
    orgChild1.orgChild1Order,
    orgChild1.orgChild1Name,
    orgChild2.id as child2Id,
    orgChild2.orgChild2ShortName,
    orgChild2.orgChild2Order,
    orgChild2.orgChild2Name,
    orgChild3.id as child3Id,
    orgChild3.orgChild3ShortName,
    orgChild3.orgChild3Order,
    orgChild3.orgChild3Name,
    orgChild4.id as child4Id,
    orgChild4.orgChild4ShortName,
    orgChild4.orgChild4Order,
    orgChild4.orgChild4Name,
    position.id as positionId,
    position.positionIsSelected,
    position.posExecutiveId as positionPosExecutiveId,
    position.isSpecial,
    posExecutive.id as posExecutiveId,
    posExecutive.posExecutiveName,
    profileDiscipline.id as profileDisciplineId,
    profileDiscipline.date as disCriplineDate,
    profileLeave.id as profileLeaveId,
    profileAssessment.id as profileAssessmentId,
    profileAssessment.pointSum,
    employeePosLevel.id as posLevelId,
    employeePosLevel.posLevelName,
	employeePosType.id as posTypeId,
    employeePosType.posTypeName,
    employeePosType.posTypeShortName
    FROM 
        employeePosMaster
    LEFT JOIN 
        profileEmployee ON employeePosMaster.current_holderId = profileEmployee.id 
    LEFT JOIN 
        orgRoot ON employeePosMaster.orgRootId = orgRoot.id
    LEFT JOIN 
        orgChild1 ON employeePosMaster.orgChild1Id = orgChild1.id
    LEFT JOIN 
        orgChild2 ON employeePosMaster.orgChild2Id = orgChild2.id
    LEFT JOIN 
        orgChild3 ON employeePosMaster.orgChild3Id = orgChild3.id
    LEFT JOIN 
        orgChild4 ON employeePosMaster.orgChild4Id = orgChild4.id
    LEFT JOIN 
        position ON employeePosMaster.id = position.posMasterId
    LEFT JOIN 
        posExecutive ON position.posExecutiveId = posExecutive.id
    LEFT JOIN (
        SELECT *
        FROM profileDisciplineEmployee pd1
        WHERE pd1.date = (
            SELECT MAX(pd2.date)
            FROM profileDisciplineEmployee pd2
            WHERE pd2.profileId = pd1.profileId
        )
    ) AS profileDiscipline ON profileDiscipline.profileId = profileEmployee.id
    LEFT JOIN (
    SELECT pl1.*
    FROM profileLeave pl1
    INNER JOIN (
        SELECT profileId, MAX(createdAt) AS maxDate
        FROM profileLeave
        GROUP BY profileId
    ) pl2 ON pl1.profileId = pl2.profileId
         AND pl1.createdAt = pl2.maxDate
    ) AS profileLeave ON profileLeave.profileId = employeePosMaster.current_holderId
    LEFT JOIN (
    SELECT pa1.*
    FROM profileAssessment pa1
    INNER JOIN (
        SELECT profileId, MAX(createdAt) AS maxDate
        FROM profileAssessment
        GROUP BY profileId
    ) pa2 ON pa1.profileId = pa2.profileId
            AND pa1.createdAt = pa2.maxDate
    ) AS profileAssessment 
     ON profileAssessment.profileId = profileEmployee.id
    LEFT JOIN 
        employeePosLevel ON profileEmployee.posLevelId = employeePosLevel.id
    LEFT JOIN 
        employeePosType ON profileEmployee.posTypeId = employeePosType.id
    WHERE 	
      employeePosMaster.current_holderId IS NOT NULL
    ORDER BY 
        profileEmployee.citizenId ASC
    `,
})

export class viewEmployeePosMaster {
  @ViewColumn()
  id: string;
  @ViewColumn()
  posMasterNoPrefix: string;
  @ViewColumn()
  posMasterNo: number;
  @ViewColumn()
  posMasterNoSuffix: string;
  @ViewColumn()
  orgRevisionId: string;
  @ViewColumn()
  orgRootId: string;
  @ViewColumn()
  orgChild1Id: string;
  @ViewColumn()
  orgChild2Id: string;
  @ViewColumn()
  orgChild3Id: string;
  @ViewColumn()
  orgChild4Id: string;
  @ViewColumn()
  current_holderId: string;
  @ViewColumn()
  profileId: string;
  @ViewColumn()
  prefix: string;
  @ViewColumn()
  firstName: string;
  @ViewColumn()
  lastName: string;
  @ViewColumn()
  citizenId: number;
  @ViewColumn()
  position: string;
  @ViewColumn()
  amount: number;
  @ViewColumn()
  dateRetire: Date;
  @ViewColumn()
  birthDate: Date;
  @ViewColumn()
  rootId: string;
  @ViewColumn()
  orgRootShortName: string;
  @ViewColumn()
  orgRootOrder: string;
  @ViewColumn()
  orgRootName: string;
  @ViewColumn()
  child1Id: string;
  @ViewColumn()
  orgChild1ShortName: string;
  @ViewColumn()
  orgChild1Order: string;
  @ViewColumn()
  orgChild1Name: string;
  @ViewColumn()
  child2Id: string;
  @ViewColumn()
  orgChild2ShortName: string;
  @ViewColumn()
  orgChild2Order: string;
  @ViewColumn()
  orgChild2Name: string;
  @ViewColumn()
  child3Id: string;
  @ViewColumn()
  orgChild3ShortName: string;
  @ViewColumn()
  orgChild3Order: string;
  @ViewColumn()
  orgChild3Name: string;
  @ViewColumn()
  child4Id: string;
  @ViewColumn()
  orgChild4ShortName: string;
  @ViewColumn()
  orgChild4Order: string;
  @ViewColumn()
  orgChild4Name: string;
  @ViewColumn()
  positionId: string;
  @ViewColumn()
  positionIsSelected: boolean;
  @ViewColumn()
  positionPosExecutiveId: string;
  @ViewColumn()
  isSpecial: boolean;
  @ViewColumn()
  posExecutiveId: string;
  @ViewColumn()
  posExecutiveName: string;
  @ViewColumn()
  profileDisciplineId: string;
  @ViewColumn()
  disCriplineDate: Date;
  @ViewColumn()
  profileLeaveId: string;
  @ViewColumn()
  profileAssessmentId: string;
  @ViewColumn()
  pointSum: number;
  @ViewColumn()
  posLevelId: string;
  @ViewColumn()
  posLevelName: string;
  @ViewColumn()
  posTypeId: string;
  @ViewColumn()
  posTypeName: string;
  @ViewColumn()
  salaryLevel: number;
  @ViewColumn()
  group: number;
  @ViewColumn()
  posTypeShortName: string;
}
