import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  const datas = await dataSource.query(`
  select min(c.duration) emin,avg(c.duration) eavg,max(c.duration) emax from (select
    oeh.report_time - unix_timestamp(oeh.create_time)* 1000 as duration,
    oeh.exam_id as eid
  from
    online_exam_history oeh
    left join student_online_exam soe  on soe.id = oeh.online_exam_id
    where soe.user_id  ='${user.userId}' and oeh.report_time <> unix_timestamp(oeh.create_time)* 1000
  ) c
  `);
  const keys = Object.keys(datas[0]);
  const formatDatas = keys.map((item) => {
    return {
      type: { emax: '最高', emin: '最低', eavg: '平均', total: '累计' }[item],
      value: parseFloat((datas[0][item] / 1000 / 60).toFixed(2)),
    };
  });
  return formatDatas;
}
