import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  const studyTime = await dataSource.query(`
  SELECT
  SUM(csc.study_progress/100*c_sc.study_time) as studyTime
FROM
	course_study_courseware AS csc
	INNER JOIN
	course_section_courseware AS c_sc
	ON 
		csc.courseware_id = c_sc.id
	INNER JOIN
	course_study_record
	ON 
		csc.record_id = course_study_record.id
WHERE
	csc.creator = '${query.id}' AND
	course_study_record.del_flag = 0 
  `);
  const resourceCount = await dataSource.query(`
  SELECT 
  COUNT(DISTINCT course_section_courseware.id)  AS resourceCount
 FROM
   course_section_courseware
   INNER JOIN
   course_study_courseware
   ON 
     course_section_courseware.id = course_study_courseware.courseware_id
 WHERE
   course_study_courseware.creator = '${query.id}'
  `);
  const formatDatas = {
    studyTime: studyTime[0].studyTime,
    resourceCount: resourceCount[0].resourceCount,
  };
  return formatDatas;
}
