<?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.qmth.teachcloud.report.business.mapper.TBAnswerMapper">

    <select id="findByPaperId" resultType="com.qmth.teachcloud.report.business.bean.dto.query.BasicAnswerDto">
        SELECT
            t0.id AS answerId,
            t0.exam_record_id AS examRecordId,
            t0.number_type AS numberType,
            t0.main_number AS mainNumber,
            t0.sub_number AS subNumber,
            t0.answer,
            t0.score,
            t0.path,
            t0.time,
            t1.exam_id AS examId,
            t1.paper_id AS paperId,
            t1.paper_type AS paperType,
            t1.exam_student_id AS examStudentId,
            t1.subjective_score AS subjectiveScore,
            t1.objective_score AS objectiveScore,
            t1.total_score AS totalScore
        FROM
            t_b_answer t0
                INNER JOIN
            t_b_exam_record t1 ON t0.exam_record_id = t1.id
        WHERE
            t1.paper_id = #{paperId}
    </select>

    <select id="findValid"
            resultType="com.qmth.teachcloud.report.business.bean.dto.query.ValidAnswerDetailDto">
        SELECT
            answer.id AS answerId,
            answer.exam_record_id AS examRecordId,
            answer.number_type AS numberType,
            answer.main_number AS mainNumber,
            answer.sub_number AS subNumber,
            answer.score,
            record.id AS taExamRecordId,
            record.inspect_college_id AS inspectCollegeId,
            record.teacher_id AS teacherId,
            record.percent_grade AS percentGrade,
            record.paper_id AS paperId,
            record.paper_type AS paperType
        FROM
            t_b_answer answer
                INNER JOIN
            t_a_exam_course_record record ON answer.exam_record_id = record.exam_record_id
        WHERE
            record.absent = 0 AND record.student_current = 1
          AND record.exam_id = #{examId}
          AND record.course_code = #{courseCode};
    </select>

    <select id="findValidAnswerDetail" resultType="com.qmth.teachcloud.report.business.bean.result.TBAnswerResult">
        SELECT
            tik.paper_type AS paperType,
            det.score / tbps.full_score AS scoreRate,
            pap.difficult AS difficult,
            (select so.name from sys_org so where so.id = tik.inspect_college_id) AS collegeName,
            tik.inspect_college_id AS collegeId
        FROM
            t_b_answer det
        INNER JOIN t_a_exam_course_record tik ON
            det.exam_record_id = tik.exam_record_id
        INNER JOIN t_a_paper_struct pap ON
            tik.exam_id = pap.exam_id
            AND tik.course_code = pap.course_code
        JOIN t_b_paper_struct tbps on
            tbps.id = pap.paper_struct_id
            AND det.sub_number = tbps.small_question_number
        <where>
            <if test="examId != null and examId != ''">
                and tik.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tik.course_code = #{courseCode}
            </if>
            and tik.absent = 0
        </where>
    </select>

    <select id="findValidAnswerDetailWithPap"
            resultType="com.qmth.teachcloud.report.business.bean.dto.AnswerDetailBean">
        SELECT
            user.id AS teacherId,
            user.real_name AS teacherName,
            college.id AS collegeId,
            struct.paper_id AS paperId,
            record.paper_type AS paperType,
            answer.score / struct.full_score AS scoreRate,
            answer.score AS answerScore,
            struct.full_score AS fullScore,
            struct.difficult AS difficult,
            college.name AS collegeName
        FROM
            t_b_answer answer
                INNER JOIN t_a_exam_course_record record ON answer.exam_record_id = record.exam_record_id
                INNER JOIN sys_user user ON record.teacher_id = user.id
                INNER JOIN sys_org college ON record.inspect_college_id = college.id
                INNER JOIN t_a_paper_struct struct ON record.paper_id = struct.paper_id
                AND answer.number_type = struct.number_type
                AND answer.main_number = struct.big_question_number
                AND answer.sub_number = struct.small_question_number
        WHERE
            record.exam_id = #{examId}
          AND record.course_code = #{courseCode}
          AND record.absent = 0
          AND record.student_current = 1;
    </select>
</mapper>
