<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hotwj.platform.securityManagement.trainingAnswer.mapper.HotTrainingAnswerRecordMapper">

    <select id="selectStudyDetailByTrainingAndUser"
            resultType="com.hotwj.platform.securityManagement.trainingAnswer.domain.vo.HotTrainingAnswerRecordVo">
        SELECT
            ar.id,
            ar.company_id          AS companyId,
            ar.training_id         AS trainingId,
            ar.course_id           AS courseId,
            ar.exam_id             AS examId,
            ar.user_id             AS userId,
            ar.user_name           AS userName,
            ar.question_id         AS questionId,
            ar.paper_question_id   AS paperQuestionId,
            ar.question_type       AS questionType,
            ar.question_score      AS questionScore,
            ar.question_order      AS questionOrder,
            ar.correct_answers     AS correctAnswers,
            ar.selected_answers    AS selectedAnswers,
            ar.is_correct          AS isCorrect,
            ar.gained_score        AS gainedScore,
            ar.answer_duration     AS answerDuration,
            ar.answer_time         AS answerTime,
            ar.create_by_name      AS createByName,
            ar.update_by_name      AS updateByName,
            ar.is_deleted          AS isDeleted,
            t.plan_name            AS trainingName,
            c.name                 AS courseName,
            p.name                 AS examName,
            p.total_score          AS paperTotalScore,
            p.pass_score           AS passScore,
            r.exam_score           AS examScore,
            r.learn_duration_min   AS learnDurationMin,
            r.complete_time        AS completeTime,
            qb.question_desc       AS questionDesc,
            qb.option_a            AS optionA,
            qb.option_b            AS optionB,
            qb.option_c            AS optionC,
            qb.option_d            AS optionD,
            qb.option_e            AS optionE,
            qb.option_f            AS optionF,
            qb.difficulty          AS difficulty,
            qb.score               AS questionOriginalScore,
            SUM(CASE WHEN ar.question_type = 1 THEN 1 ELSE 0 END) OVER (PARTITION BY ar.company_id, ar.training_id, ar.course_id, ar.exam_id, ar.user_id) AS singleChoiceCount,
            SUM(CASE WHEN ar.question_type = 3 THEN 1 ELSE 0 END) OVER (PARTITION BY ar.company_id, ar.training_id, ar.course_id, ar.exam_id, ar.user_id) AS judgeCount,
            SUM(ar.question_score) OVER (PARTITION BY ar.company_id, ar.training_id, ar.course_id, ar.exam_id, ar.user_id) AS totalQuestionScore
        FROM hot_training_answer_record ar
                 LEFT JOIN hot_training_course_record r
                           ON r.company_id = ar.company_id
                               AND r.training_id = ar.training_id
                               AND r.user_id = ar.user_id
                               AND r.training_course_id = ar.course_id
                               AND r.exam_id = ar.exam_id
                               AND r.is_deleted = 0
                 LEFT JOIN hot_training t
                           ON t.id = ar.training_id
                               AND t.is_deleted = 0
                 LEFT JOIN hot_course c
                           ON c.id = ar.course_id
                               AND c.is_deleted = 0
                 LEFT JOIN hot_exam_paper p
                           ON p.id = ar.exam_id
                               AND p.is_deleted = 0
                 LEFT JOIN hot_question_bank qb
                           ON qb.id = ar.question_id
                               AND qb.is_deleted = 0
        WHERE ar.company_id = #{companyId}
          AND ar.training_id = #{trainingId}
          AND ar.user_id = #{userId}
          AND ar.scene_type = 'PLAN'
          AND ar.is_deleted = 0
        ORDER BY ar.question_order ASC, ar.id ASC
    </select>

    <select id="selectPlanStudyDetailByTrainingAndUser"
            resultType="com.hotwj.platform.securityManagement.trainingAnswer.domain.vo.HotTrainingAnswerRecordVo">
        SELECT
            ar.id,
            ar.company_id          AS companyId,
            ar.training_id         AS trainingId,
            ar.exam_id             AS examId,
            ar.user_id             AS userId,
            ar.user_name           AS userName,
            ar.question_id         AS questionId,
            ar.paper_question_id   AS paperQuestionId,
            ar.question_type       AS questionType,
            ar.question_score      AS questionScore,
            ar.question_order      AS questionOrder,
            ar.correct_answers     AS correctAnswers,
            ar.selected_answers    AS selectedAnswers,
            ar.is_correct          AS isCorrect,
            ar.gained_score        AS gainedScore,
            ar.answer_duration     AS answerDuration,
            ar.answer_time         AS answerTime,
            ar.create_by_name      AS createByName,
            ar.update_by_name      AS updateByName,
            ar.is_deleted          AS isDeleted,
            t.plan_name            AS trainingName,
            p.name                 AS examName,
            p.total_score          AS paperTotalScore,
            p.pass_score           AS passScore,
            qb.question_desc       AS questionDesc,
            qb.option_a            AS optionA,
            qb.option_b            AS optionB,
            qb.option_c            AS optionC,
            qb.option_d            AS optionD,
            qb.option_e            AS optionE,
            qb.option_f            AS optionF,
            qb.difficulty          AS difficulty,
            qb.score               AS questionOriginalScore,
            SUM(CASE WHEN ar.question_type = 1 THEN 1 ELSE 0 END)
                OVER (PARTITION BY ar.company_id, ar.training_id, ar.exam_id, ar.user_id) AS singleChoiceCount,
            SUM(CASE WHEN ar.question_type = 3 THEN 1 ELSE 0 END)
                OVER (PARTITION BY ar.company_id, ar.training_id, ar.exam_id, ar.user_id) AS judgeCount,
            SUM(ar.question_score)
                OVER (PARTITION BY ar.company_id, ar.training_id, ar.exam_id, ar.user_id) AS totalQuestionScore
        FROM hot_training_answer_record ar
                 LEFT JOIN hot_training t
                           ON t.id = ar.training_id
                               AND t.is_deleted = 0
                 LEFT JOIN hot_exam_paper p
                           ON p.id = ar.exam_id
                               AND p.is_deleted = 0
                 LEFT JOIN hot_question_bank qb
                           ON qb.id = ar.question_id
                               AND qb.is_deleted = 0
        WHERE ar.company_id = #{companyId}
          AND ar.training_id = #{trainingId}
          AND ar.user_id = #{userId}
          AND ar.scene_type = 'PLAN'
          AND ar.is_deleted = 0
        ORDER BY ar.question_order ASC, ar.id ASC
    </select>

    <!-- 错题回顾 -->
    <select id="selectWrongQuestionReviewList"
            resultType="com.hotwj.platform.securityManagement.trainingAnswer.domain.vo.HotTrainingWrongQuestionReviewVo">
        SELECT ar.question_id        AS questionId,
               SUM(ar.error_count)   AS errorCount
        FROM hot_training_answer_record ar
        WHERE ar.is_deleted = 0
        AND ar.company_id = #{companyId}
        AND ar.user_id = #{userId}
          AND ar.scene_type = 'PLAN'
                AND ar.training_id = #{bo.trainingId}
                AND ar.exam_id = #{bo.examId}
          AND ar.error_count > 0
        GROUP BY ar.question_id
    </select>
</mapper>
