import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  const { current, size } = JSON.parse(query.pageInfo);

  const list = await dataSource.query(`
      select
        count(1) OVER() as total,
        r.id resourceId, dr.create_time createTime, r.name, r.type, r.size,
        UNIX_TIMESTAMP(dr.end_time) - UNIX_TIMESTAMP(dr.create_time) as viewTime
      from report_data dr
      left join resource r
      on dr.field01=r.id
      where dr.type='RESOURCE_VIEW' and dr.creator='${user.userId}'
      limit ${(current - 1) * size},${size}
    `);

  return { list, total: Number(list?.[0]?.total || 0) };
}
