import { DataSource } from 'typeorm';
/**
 * 教员端-学员分析-练习统计-练习习题占类型统计
 * @param dataSource mysql执行体
 * @param param1 请求参数
 * @returns 数据
 */
export async function handler(dataSource: DataSource, { user, query }) {
  let datas = await dataSource.query(`
	select
	qcount.count times,
	(qcount.count / sum(qcount.count) over(partition by qcount.temp)* 100) percent,
	sum(qcount.count) over(partition by qcount.temp) as total,
	qcount.type item
from
	(
	select
		count(1) count,
		qt.name type,
		'c' temp
	from
		((
		select
			eqr.question_id
		from
			exam_questions_result eqr
		where
			eqr.creator = '${query.userId}')
union all (
	select
		lqa.question_id
	from
		live_question_answer lqa
	where
		lqa.creator = '${query.userId}')
union all (
select
	cse.question_id
from
	course_study_exercise cse
where
	cse.creator = '${query.userId}')) qids
	left join question q on
		q.id = qids.question_id
	left join question_type qt on
		qt.id = q.type
	group by
		q.type) qcount where qcount.type is not null
  `);
  datas = datas.map((item) => {
    return {
      ...item,
      percent: +item.percent,
      times: +item.times,
      total: +item.total,
    };
  });
  return datas;
}
