import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  if (!query?.examId) {
    return [];
  }
  const datas = await dataSource.query(`
  select min(jj.score) as emin,avg(jj.score) as eavg,max(jj.score) as emax from (select
    sum(eqr.score) score,
    eqr.creator ,
    oeh.exam_id ,
    eqr.history_id
  from
    exam_questions_result eqr
  left join online_exam_history oeh on
    oeh.id = eqr.history_id
  where
    oeh.exam_id = ${query.examId}
  group by
    eqr.creator,
    eqr.history_id) jj
  `);
  const keys = Object.keys(datas[0]);
  const formatDatas = keys.map((item) => {
    return {
      type: { emax: '最高', emin: '最低', eavg: '平均', total: '累计' }[item],
      value: +datas[0][item],
    };
  });
  return formatDatas;
}
