import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user }) {
  let datas = await dataSource.query(`
  select
	oeh.id,
	max(t_score.score) score,
	oeh.exam_id,
	e.title,
	date_format(oeh.create_time, '%Y-%m-%d') as time
from
	online_exam_history oeh
left join (
	select
		sum(eqr.score) score,
		eqr.history_id hid
	from
		exam_questions_result eqr
	group by
		eqr.history_id
) as t_score on
	t_score.hid = oeh.id
left join exam e on
	e.id = oeh.exam_id
where
	oeh.creator = '${user.userId}'
	and t_score.score is not null
	group by date_format(oeh.create_time, '%Y-%m-%d')
order by
	oeh.create_time desc
 limit 0,7
  `);
  datas = datas.map((item) => {
    return { ...item, score: +item.score };
  });
  return datas;
}
