import { DataSource, Repository } from 'typeorm';
export async function handler(dataSource: DataSource, { user, query: { id } }) {
  const data = await dataSource.query(`
  SELECT
	* 
FROM
	(
	SELECT
		course_hours AS courseHours,
		section_count AS sectionCount,
		-- 查出用户数量等
		(
		SELECT
			COUNT( USER.id ) 
		FROM
			course_list
			INNER JOIN user ON course_list.study_org_id = user.org_id 
		WHERE
			course_list.id = ${id} 
			AND user.del_flag = 0 
		) AS userCount,
		-- 四表联查-平均时间
		(
		SELECT
			SUM( csc.study_progress / 100 * cscc.study_time ) / userCount 
		FROM
			course_study_record AS csr
			INNER JOIN course_list AS cl ON csr.course_id = cl.id
			INNER JOIN course_study_courseware AS csc ON csr.id = csc.record_id
			INNER JOIN course_section_courseware AS cscc ON csc.courseware_id = cscc.id 
		WHERE
			csr.course_id = ${id} 
		) AS avgTime
	FROM
		course_list 
	WHERE
		course_list.id = ${id}
	) AS xxx
  `);
  return data[0];
}
