import { DataSource } from 'typeorm';
/**
 * 教员端-练习分析-练习正确率占比统计图
 * @param dataSource mysql执行体
 * @param param1 请求参数
 * @returns 数据
 */
export async function handler(dataSource: DataSource, { user, query }) {
  const datas = await dataSource.query(`
	(
		select
			*
		from
			(
			select
				moni2.count /(sum(moni2.count) over(partition by moni2.p))* 100 percent,
				moni2.isPass,
				'模拟训练' type
			from
				(
				select
					count(1) count,
					moni.isPass,
					'a' p
				from
					(
					select
						eqr.id id,
						(case
							when eqr.mistake_score >= eqr.question_score
							or eqr.score >= eqr.question_score then 1
							else 0
						end) isPass
					from
						exam_questions_result eqr
					left join online_exam_history oeh on
						eqr.history_id = oeh.id
					left join exam e on
						e.id = oeh.exam_id
					where
						e.classify_id = -1	
						and eqr.creator='${query.userId}'
		) moni
				group by
					moni.isPass) moni2
			group by
				moni2.isPass) moni3
		where
			moni3.isPass = 1)
		union all (
		select
		*
		from
		(
		select
			moni2.count /(sum(moni2.count) over(partition by moni2.p))* 100 percent,
			moni2.isPass,
			'错题巩固' type
		from
			(
			select
				count(1) count,
				moni.isPass,
				'a' p
			from
				(
				select
					eqr.id id,
					(case
						when eqr.mistake_score >= eqr.question_score
							or eqr.score >= eqr.question_score then 1
							else 0
						end) isPass
				from
					exam_questions_result eqr
				left join online_exam_history oeh on
					eqr.history_id = oeh.id
				left join exam e on
					e.id = oeh.exam_id
				where
				eqr.creator = '${query.userId}'
				) moni
			group by
				moni.isPass) moni2
		group by
			moni2.isPass) moni4
		where
		moni4.isPass = 1)
		union all
		(select
			*
		from
			(
			select
				moni2.count /(sum(moni2.count) over(partition by moni2.p))* 100 percent,
				moni2.isPass,
				'课后练习' type
			from
				(
				select
					count(1) count,
					moni.isPass,
					'a' p
				from
					(
					select
						cse.id id,
						(case
							when cse.mistake_score >= cse.question_score
							or cse.score >= cse.question_score then 1
							else 0
						end) isPass
					from
						course_study_exercise cse
					left join course_study_record csr on
						csr.id = cse.record_id
					left join course_list cl on
						cl.id = csr.course_id
					where
						csr.student_id ='${query.userId}'
			) moni
				group by
					moni.isPass) moni2
			group by
				moni2.isPass) moni4
		where
			moni4.isPass = 1)
			
		union all
		(select
			*
		from
			(
			select
				moni2.count /(sum(moni2.count) over(partition by moni2.p))* 100 percent,
				moni2.isPass,
				'直播练习' type
			from
				(
				select
					count(1) count,
					moni.isPass,
					'a' p
				from
					(
					select
						lqa.id id,
						(case
							when lqa.mistake_score >= lqa.question_score
							or lqa.score >= lqa.question_score then 1
							else 0
						end) isPass
					from
						live_question_answer lqa 
					where lqa.creator  = '${query.userId}'
					
			) moni
				group by
					moni.isPass) moni2
			group by
				moni2.isPass) moni4
		where
			moni4.isPass = 1)
		
		
  `);
  const totalDuration = datas.reduce((p, v) => (p += +v.percent), 0);
  const formatdata = datas.map((v) => ({
    item: v.type,
    originPercent: +v.percent,
    total: totalDuration,
    percent: +(v.percent / totalDuration).toFixed(4),
  }));
  return formatdata;
}
