import { DataSource } from 'typeorm';

export async function handler(dataSource: DataSource, { user, query }) {
  // const datas = await dataSource.query(`
  // `);
  /**
   * 返回的试题列表要求试题格式为：
   * {
    "id": 4057,
    "typeId": 1,
    "difficultyLevelId": 2,
    "difficultyLevel": "较易",
    "classifyId": 2,
    "classify": "量子力学",
    "type": "单选题",
    "element": "radio",
    "knowledgeId": 4,
    "title": "单选题例子，导入前请先删除",
    "options": "[\"选项1\",\"选项2\",\"选项3\"]",
    "answer": "[\"A\"]",
    "analysis": "答案解析XXX",
    "score": 3,
    "userScore": 3,
    "userAnswer": "[\"A\"]",
    "teacherComment": null,
    "resultId": 334
    }   
   */
  console.log(query);
  let datas = [];
  if (query.type === '1') {
    datas = await dataSource.query(`select
    q.id id,
    q.type typeId,
    q.difficulty_level difficultyLevelId,
    qdl.name difficultyLevel,
    q.classify_id classifyId,
    qc.name classify,
    qt.name type,
    qt.element element,
    q.knowledge_id knowledgeId,
    q.title title,
    q.options options,
    q.answer answer,
    q.analysis analysis,
  -- 	以下是可变参数
    lqa.question_score score,
    lqa.score userScore,
    lqa.user_answer userAnswer,
    '' teacherComment,
    lqa.id resultId
  from
    live_question_answer lqa
  left join question q on q.id = lqa.question_id 
  left join question_difficulty_level qdl on qdl.id = q.difficulty_level 
  left join question_classify qc on qc.id = q.classify_id 
  left join question_type qt  on qt.id = q.type
  where
    lqa.record_id = ${query.id}
    and lqa.del_flag = 0
    and lqa.creator = '${query.userId}'
  `);
  } else if (query.type == '2') {
    datas = await dataSource.query(`select
    q.id id,
    q.type typeId,
    q.difficulty_level difficultyLevelId,
    qdl.name difficultyLevel,
    q.classify_id classifyId,
    qc.name classify,
    qt.name type,
    qt.element element,
    q.knowledge_id knowledgeId,
    q.title title,
    q.options options,
    q.answer answer,
    q.analysis analysis,
  -- 	以下是可变参数
    cse.question_score score,
    cse.score userScore,
    cse.user_answer userAnswer,
    '' teacherComment,
    cse.id resultId
  from
    course_study_exercise cse  
    left join course_study_record csr on csr.id = cse.record_id 
  left join question q on q.id = cse.question_id 
  left join question_difficulty_level qdl on qdl.id = q.difficulty_level 
  left join question_classify qc on qc.id = q.classify_id 
  left join question_type qt  on qt.id = q.type
  where
    csr.course_id  = ${query.id}
    and cse.del_flag = 0
    and cse.creator = '${query.userId}'
  `);
  } else if (query.type === '3') {
    datas = await dataSource.query(`	select
    q.id id,
    q.type typeId,
    q.difficulty_level difficultyLevelId,
    qdl.name difficultyLevel,
    q.classify_id classifyId,
    qc.name classify,
    qt.name type,
    qt.element element,
    q.knowledge_id knowledgeId,
    q.title title,
    q.options options,
    q.answer answer,
    q.analysis analysis,
  -- 	以下是可变参数
    eqr.question_score score,
    eqr.score userScore,
    eqr.user_answer userAnswer,
    '' teacherComment,
    eqr.id resultId
  from
    exam_questions_result eqr  
  left join question q on q.id = eqr.question_id 
  left join question_difficulty_level qdl on qdl.id = q.difficulty_level 
  left join question_classify qc on qc.id = q.classify_id 
  left join question_type qt  on qt.id = q.type
  where
    eqr.history_id  = ${query.id}
    and eqr.del_flag = 0
    and eqr.creator = '${query.userId}'
  `);
  } else if (query.type === '4') {
    datas = await dataSource.query(`		select
    q.id id,
    q.type typeId,
    q.difficulty_level difficultyLevelId,
    qdl.name difficultyLevel,
    q.classify_id classifyId,
    qc.name classify,
    qt.name type,
    qt.element element,
    q.knowledge_id knowledgeId,
    q.title title,
    q.options options,
    q.answer answer,
    q.analysis analysis,
  -- 	以下是可变参数
    eqr.question_score score,
    eqr.mistake_score  userScore,
    (case when eqr.mistake_score = eqr.mistake_score then q.answer else eqr.user_answer end) userAnswer,
    '' teacherComment,
    eqr.id resultId
  from
    exam_questions_result eqr  
  left join question q on q.id = eqr.question_id 
  left join question_difficulty_level qdl on qdl.id = q.difficulty_level 
  left join question_classify qc on qc.id = q.classify_id 
  left join question_type qt  on qt.id = q.type
  where
    eqr.update_time  >= '${query.rid}' and eqr.update_time <='${query.id}'
    and eqr.creator = '${query.userId}'
  `);
  }
  return {
    title: query.title,
    questions: datas,
  };
}
