import { DataSource } from 'typeorm';
export async function handler(dataSource: DataSource, { user, query }) {
  console.log(query, '====================>');

  const data = await dataSource.query(`
  select
	sum(lqa.score) score,lqa.creator,u.name
from
	live_question_answer lqa
	left join user u on u.id = lqa.creator 
where
	lqa.del_flag = 0
	and
	lqa.record_id = ${query.id}
group by
	lqa.creator
	order by sum(lqa.score) desc
        `);
  const total = data.reduce((prev, item) => {
    return prev + +item.score;
  }, 0);
  const a = [
    {
      type: '最高成绩',
      value: +data[0]?.score,
      name: data[0]?.name,
    },
    {
      type: '人均成绩',
      value: total / data.length,
      name: '平均成绩',
    },
    {
      type: '最低成绩',
      value: +data[data.length - 1]?.score,
      name: data[data.length - 1]?.name,
    },
  ];
  return a;
}
