import e from 'express';
import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user }) {
  const datas = await dataSource.query(`
	select min(c.times) emin,avg(c.times) eavg,max(c.times) emax,sum(times) total
from 
(select count(soe.id) as times,soe.exam_id 
from student_online_exam soe 
where soe.creator = '${user.userId}' 
group by soe.exam_id )
c
  `);
  const keys = Object.keys(datas[0]);
  const formatDatas = keys.map((item) => {
    return {
      type: { emax: '最高', emin: '最低', eavg: '平均', total: '累计' }[item],
      value: parseFloat(datas[0][item]),
    };
  });
  return formatDatas;
}
