import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user }) {
  const [data] = await dataSource.query(`select 

    -- -------- 试卷批改
    (
      select
      ifnull(count(cc.c),0) count
    from
      (
      
      select
      oeh.exam_paper_id exam_paper_id,
      oeh.creator ctr,
        count(1) c,
        max(oeh.is_grade_paper) gp
      from
        online_exam_history oeh
        left join student_online_exam soe on oeh.exam_paper_id = soe.id
        left join exam e on e.id = oeh.exam_id
      where
         oeh.del_flag =0 and e.del_flag  =0 and e.grade_paper_mode =1 and soe.creator = '${user.userId}'
      group by
        oeh.exam_id,oeh.creator 
        
        )cc
        where cc.gp = 0
		
    ) as paper_marking

    -- -------- 直播授课
    ,(
      SELECT
      COUNT(1)
      FROM
      live_teaching AS lt
      WHERE
      lt.creator = '${user.userId}' and lt.del_flag=0
    ) as live_teach

    -- -------- 提问答疑
   ,(
      SELECT
      COUNT(1) 
      FROM
      online_issues_answer oia
      WHERE oia.answer_person_id = '${user.userId}'  AND status = 0
   ) as question_and_answer

    -- -------- 学员分析
    ,(
      SELECT
      COUNT(1)
      FROM
      teacher_student AS ts
      INNER JOIN
      user
      ON 
      ts.student = user.id
      INNER JOIN
      role
      ON 
      user.role_id = role.id
      WHERE
      ts.teacher = '${user.userId}' AND
      role.base = 3 
    ) as trainee

    -- -------- 课程分析
    ,(
      SELECT
      COUNT(1) 
      FROM
      course_list cl
      WHERE cl.creator = '${user.userId}'   AND del_flag = 0   
    ) as course

    -- -------- 考试分析
    ,(
      SELECT
      COUNT(1) 
      FROM
      exam e
      WHERE e.creator = '${user.userId}' AND status = 0
    ) as examination

    -- -------- 试题分析
    ,(
      SELECT
      COUNT(1) 
      FROM
      question as q
      WHERE q.creator = '${user.userId}'   AND del_flag = 0
    ) as test_question

    -- -------- 练习分析
    ,(
      select sum(xx.c) from ((select
        count(1) c
      from
        (
        select
          count(1) as times
        from
          online_exam_history oeh
        left join exam e on
          oeh.exam_id = e.id
        where
          e.classify_id = -1
          and oeh.del_flag = 0
        group by
          oeh.id) practice_test)
      union all
      (select
        count(1) c
      from
        (
        select
          count(1) mistakes
        from
          exam_questions_result eqr
        where
          eqr.mistake_score <> 0
        group by
          round(unix_timestamp(eqr.update_time)/ 1000),
          eqr.creator ) mistakes)
      union all
      (select
        count(1) c
      from
        (
        select
          count(1)
        from
          course_study_exercise cse
        group by
          cse.record_id ,
          cse.creator )cc)
      union all
      (select
        count(1) c
      from
        (
        select
          count(1)
        from
          live_question_answer lqa
        group by
          lqa.record_id ,
          lqa.creator)jj))xx
    ) as exercise

    -- -------- 直播分析
    ,(
      SELECT
      COUNT(1) 
      FROM
      live_teaching 
      WHERE creator = '${user.userId}' AND del_flag = 0
    ) as live_broadcast

    -- -------- 资源分析
    ,(
      SELECT
      COUNT(1)
      FROM
      resource
      WHERE creator = '${user.userId}' AND del_flag = 0
    ) as resources
  `);

  return data;
}
