import { DataSource } from 'typeorm';
/**
 * 学员端-我的成绩-详情分析-分析-成绩题型占比
 * @param dataSource mysql执行体
 * @param param1 请求参数
 * @returns 数据
 */
export async function handler(dataSource: DataSource, { user, query }) {
  if (!query?.historyId) {
    return [];
  }
  let datas = await dataSource.query(`
	select
	*,
	sum(jj.score) over(partition by 1 = 1) total,
	jj.score/(sum(jj.score) over(partition by 1 = 1))*100 percent
from
	(
	select
		qt.name,
		sum(eqr.score) score,
		'c' k
	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.history_id = ${query.historyId}
	group by
		q.type
)jj
  `);
  datas = datas.map((item) => {
    return {
      ...item,
      percent: +item.percent,
      score: +item.score,
      total: +item.total,
    };
  });
  return datas;
}
