import { DataSource, Repository } from 'typeorm';
export async function handler(dataSource: DataSource, { user }) {
  const data = await dataSource.query(`
  SELECT
	COUNT( lt.id ) AS count,
	SUM( lt.duration ) AS duration,
	(SELECT SUM(everyUserCountList.everyUserCount) from  (SELECT COUNT( DISTINCT lmd.from_id) AS everyUserCount
	   FROM 
	   live_msg_data lmd
	   INNER JOIN
	   live_teaching lt
	   ON 
		 lmd.room_id = lt.id 
		 WHERE
		 lt.lecturer = '${user.userId}' 
		 GROUP BY
		 lmd.room_id
		 ) AS everyUserCountList  )  as userCount,
		 (SELECT round(SUM(c.timex)/COUNT(DISTINCT c.from_id),2) FROM (
			SELECT
				(MAX(timestamp)-MIN(timestamp))  /1000 /60 /60 as timex,
				from_id
			FROM
				live_msg_data
				GROUP BY  from_id,room_id
			) c
			) as avgTime
FROM
	live_teaching AS lt 
WHERE
	lecturer = '${user.userId}' 
	AND lt.del_flag = 0
      `);
  return data[0];
}
