import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  if (!query?.exam) {
    return [];
  }
  const datas = await dataSource.query(`
	select
	(
	select
		count(1)
	from
		student_online_exam soe
	where
		soe.exam_id = ${query.exam}) - (
	select
		count(distinct oeh.creator)
	from
		online_exam_history oeh
	where
		oeh.exam_id = ${query.exam} ) as unJoin,
	(
    select
		count(distinct oeh.creator)
	from
		online_exam_history oeh
	where
		oeh.exam_id = ${query.exam} ) as isJoin
  `);
  const allCount = parseInt(datas?.[0]?.isJoin) + parseInt(datas?.[0]?.unJoin);
  const formatDatas = [
    {
      name: '已考',
      count: datas?.[0]?.isJoin,
      all: allCount,
      percent: datas?.[0]?.isJoin / allCount,
    },
    {
      name: '缺考',
      count: datas?.[0]?.unJoin,
      all: allCount,
      percent: datas?.[0]?.unJoin / allCount,
    },
  ];

  return formatDatas;
}
