import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  const sql = `
  
	((
	-- 直播后
	select
		lqa.update_time ct,
		lt.title title,
		1 type,
		concat(round((unix_timestamp(max(lqa.update_time)) - unix_timestamp(min(lqa.create_time)))/ 60, 2), '分钟') duration,
		lqa.record_id rid,
		lqa.record_id id,
		-- 	count(lqa.score >= lqa.question_score or null) as passCount,
		-- 	lqx.count qcount,
		concat(round(count(lqa.score >= lqa.question_score or null)/ lqx.count * 100, 2), '%') as percent,
		lqa.creator creator
	from
		live_question_answer lqa
	left join live_teaching lt on
		lt.id = lqa.record_id
	left join (
		select
			count(1) count,
			lq.live_id lid
		from
			live_question lq
		where
			lq.del_flag = 0
		group by
			lq.live_id
		) lqx on
		lqx.lid = lt.id
	where
		lqa.creator = '${query.userId}'
		and lqa.del_flag = 0
		and lt.del_flag = 0
	group by
		lqa.record_id
	order by
		lqa.create_time desc 
)
union all	
(
-- 课程后
select
	cse.update_time ct,
	cl.name title,
	2 type,
	--     min(cse.create_time) 	duration,
	--     max(cse.update_time) 	duration2,
	concat(round((unix_timestamp(max(cse.update_time)) - unix_timestamp(min(cse.create_time)))/ 60, 2), '分钟') duration,
	cse.record_id rid,
	cl.id id,
	-- 	count(cse.score >= cse.question_score or null) a,
	-- 	qfcx.count b,
	concat(round(count(cse.score >= cse.question_score or null)/ qfcx.count * 100, 2), '%') as percent,
	cse.creator creator
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
left join (
	select
		count(1) count,
		qfc.course_id cid
	from
		questions_for_course qfc
	where
		qfc.del_flag = 0
	group by
		qfc.course_id
	) qfcx on
	qfcx.cid = cl.id
where
	cse.creator = '${query.userId}'
	and cse.del_flag = 0
	and cl.del_flag = 0
group by
	cse.record_id
order by
	cse.update_time desc	
)
union all
(
-- 模拟训练
select
	oeh.create_time ct,
	ep.title title,
	3 type,
	concat(round((oeh.report_time / 1000 - unix_timestamp(oeh.create_time))/ 60, 2), '分钟') duration,
	e.id rid,
	oeh.id id,
	concat(round(count(eqr.score >= eqr.question_score or null)/ qfpx.count * 100, 2), '%') as percent,
	eqr.creator creator
from
	exam_questions_result eqr
left join online_exam_history oeh on
	oeh.id = eqr.history_id
left join exam e on
	e.id = oeh.exam_id
left join exam_paper ep on
	ep.id = oeh.exam_paper_id
left join (
select
		count(1) count,
		qfp.exam_paper_id pid
from
		questions_for_paper qfp
where
		qfp.del_flag = 0
group by
		qfp.exam_paper_id
) qfpx on
	qfpx.pid = oeh.exam_paper_id
where
	eqr.creator = '${query.userId}'
and eqr.del_flag = 0
and ep.del_flag = 0
and oeh.del_flag = 0
and e.del_flag = -1
and e.classify_id =-1
group by
	eqr.history_id
order by
	eqr.history_id desc	
)
union all
(
-- 	错题巩固
select
	eqr.update_time ct,
	qc.name title,
	4 type,
	'无' duration,
	min(eqr.update_time) rid,
	max(eqr.update_time) id,
	concat(round(count(eqr.mistake_score >= eqr.question_score or null)/ eqrx.count * 100, 2), '%') as percent,
	eqr.creator creator
from
	exam_questions_result eqr
left join online_exam_history oeh on
	oeh.id = eqr.history_id
left join question q on
	q.id = eqr.question_id
left join question_classify qc on
	q.classify_id = qc.id
left join (
select
		count(1) count,
		round(unix_timestamp(eqr.update_time)) ut
from
		exam_questions_result eqr
left join online_exam_history oeh on
		oeh.id = eqr.history_id
where
		eqr.creator = '${query.userId}'
and eqr.del_flag = 0
and eqr.update_time <> eqr.create_time
group by
		round(unix_timestamp(eqr.update_time))
) eqrx on
	eqrx.ut = round(unix_timestamp(eqr.update_time))
where
	eqr.creator = '${query.userId}'
and eqr.del_flag = 0
and eqr.update_time <> eqr.create_time
and qc.name is not null
group by
	round(unix_timestamp(eqr.update_time))
order by
eqr.update_time desc 
)) cc

  `;
  const datas = await dataSource.query(
    `select
    *
  from` +
      sql +
      ` order by cc.ct desc limit ${(query.currentPage - 1) * query.pageSize},${
        query.pageSize
      }`,
  );
  const total = await dataSource.query(
    `select
  count(1) total
from` + sql,
  );
  return {
    currentPage: +query.currentPage,
    pageSize: +query.pageSize,
    total: +total[0].total,
    data: datas,
  };
}
