import { ViewColumn, ViewEntity } from "typeorm";

@ViewEntity({
  expression: `SELECT 
      \`profileEmployeeId\`,
      \`period\`,
      \`year\`,
      \`employeePosMaster\`.\`orgRootId\`,
      \`employeePosMaster\`.\`orgChild1Id\`,
      \`employeePosMaster\`.\`orgChild2Id\`,
      \`employeePosMaster\`.\`orgChild3Id\`,
      \`employeePosMaster\`.\`orgChild4Id\`, 
      COUNT(*) AS recordCount, 
      SUM(pointSum) AS totalPointSum, 
      SUM(pointSum) / COUNT(*) AS result
    FROM \`profileAssessment\` 
    LEFT JOIN \`profileEmployee\` ON \`profileAssessment\`.\`profileEmployeeId\` = \`profileEmployee\`.\`id\`
    LEFT JOIN \`employeePosMaster\` ON \`profileEmployee\`.\`id\` = \`employeePosMaster\`.\`current_holderId\`
    LEFT JOIN \`orgRevision\` ON \`employeePosMaster\`.\`orgRevisionId\` = \`orgRevision\`.\`id\`
    WHERE \`period\` IS NOT NULL
    AND \`orgRevision\`.\`orgRevisionIsCurrent\` = TRUE
    GROUP BY \`profileEmployeeId\`, \`period\`, \`year\`, 
            \`employeePosMaster\`.\`orgRootId\`, 
            \`employeePosMaster\`.\`orgChild1Id\`, 
            \`employeePosMaster\`.\`orgChild2Id\`, 
            \`employeePosMaster\`.\`orgChild3Id\`, 
            \`employeePosMaster\`.\`orgChild4Id\`
    ORDER BY \`year\` DESC, \`period\` ASC;
    `,
})

export class viewProfileEmployeeEvaluation {
  @ViewColumn()
  profileEmployeeId: string;
  @ViewColumn()
  period: string;
  @ViewColumn()
  year: number;
  @ViewColumn()
  orgRootId: string;
  @ViewColumn()
  orgChild1Id: string;
  @ViewColumn()
  orgChild2Id: string;
  @ViewColumn()
  orgChild3Id: string;
  @ViewColumn()
  orgChild4Id: string;
  @ViewColumn()
  recordCount: number;
  @ViewColumn()
  totalPointSum: number;
  @ViewColumn()
  result: number;
}
