import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user }) {
  const datas = await dataSource.query(`
  select count(1) as count,qcount.pass as name from (select
    distinct eqr.question_id qid,
    (case
     when eqr.score < eqr.question_score
     and eqr.mistake_score < eqr.question_score then '当前'
     when eqr.mistake_score >= eqr.question_score then '已解决'
   end) pass
 from
   exam_questions_result eqr
 where
   eqr.creator = '${user.userId}'
   and eqr.score <eqr.question_score) qcount group by qcount.pass
 
`);
  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 },
  ];
	 */
}
