import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user }) {
  const datas = await dataSource.query(`
	select
	 count(distinct eqr.question_id) as count,q.type,qt.name
from
	exam_questions_result eqr
	left join question q on q.id = eqr.question_id 
	left join question_type qt on qt.id = q.type
where
	eqr.creator = '${user.userId}'
	and eqr.score <eqr.question_score
	group by q.type
`);
  const alltimes = datas.reduce((prev, item) => {
    return prev + parseInt(item.count);
  }, 0);
  return datas.map((item) => {
    return {
      item: item.name,
      count: item.count,
      percent: +(item.count / alltimes),
    };
  });
  /**
	 *  [
    { item: '及格', count: pass, percent: pass / len },
    { item: '不及格', count: no_pass, percent: no_pass / len },
  ];
	 */
}
