import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  const datas = await dataSource.query(`
	select
	(case
		when ad.us<(ad.ts * ad.pp / 100) 
      then 0
		else 1
	end) as is_pass
from
	(
	select
		oehx.hid hid,
		oehx.eid eid,
		scoreSum.userScore us,
		e.total_score ts,
		e.pass_percent pp
	from
		(
		select
			max(oeh.id) as hid ,
			oeh.is_grade_paper as is_grade_paper,
			oeh.online_exam_id as eid,
			soe.user_id 
		from
			online_exam_history oeh
			left join student_online_exam soe  on soe.id = oeh.online_exam_id 
		where soe.user_id ='${query.userId}'
		group by
			oeh.online_exam_id ) oehx
	left join (
		select
			sum(eqr.score) as userScore,
			eqr.history_id as historyId
		from
			exam_questions_result eqr
		group by
			eqr.history_id
	) as scoreSum on
		scoreSum.historyId = oehx.hid
	left join exam e on
		e.id = oehx.eid
where		oehx.is_grade_paper = 1  and e.classify_id <> -1) ad

  `);
  const len = datas.length;
  const pass = datas.filter((item) => item.is_pass == '0').length;
  const no_pass = datas.filter((item) => item.is_pass == '1').length;
  const re = [
    {
      item: '及格',
      count: pass,
      percent: pass / len,
      len,
    },
    {
      item: '不及格',
      count: no_pass,
      percent: no_pass / len,
      len,
    },
  ];
  console.log('请求学生的成绩------------------', query, re);

  return re;
}
