<?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.mark.mapper.MarkArbitrateHistoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.mark.entity.MarkArbitrateHistory">
        <id column="id" property="id" />
        <result column="exam_id" property="examId" />
        <result column="paper_number" property="paperNumber" />
        <result column="group_number" property="groupNumber" />
        <result column="student_code" property="studentCode" />
        <result column="secret_number" property="secretNumber" />
        <result column="student_id" property="studentId" />
        <result column="status" property="status" />
        <result column="update_user_id" property="updateUserId" />
        <result column="total_score" property="totalScore" />
        <result column="score_list" property="scoreList" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>
    <select id="pageArbitrate" resultType="com.qmth.teachcloud.mark.dto.mark.manage.MarkArbitrateDto">
        SELECT
            mah.id,
            mah.exam_id examId,
            mah.paper_number paperNumber,
            mah.group_number groupNumber,
            mah.student_id studentId,
            mah.status,
            mah.update_user_id updateUserId,
            su.login_name updateUserLoginName,
            su.real_name updateUserName,
            mah.create_time createTime,
            mah.update_time updateTime
        FROM
            mark_arbitrate_history mah
                LEFT JOIN
            sys_user su ON mah.update_user_id = su.id
        where mah.exam_id = #{examId}
            AND mah.paper_number = #{paperNumber}
            <if test="groupNumber != null and groupNumber != ''">
                AND group_number = #{groupNumber}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="className != null and className != ''">
                AND EXISTS( SELECT
                            1
                        FROM
                            mark_user_class muc
                                join
                            mark_user_group mug ON muc.exam_id = mug.exam_id AND muc.paper_number = mug.paper_number AND muc.user_id = mug.user_id
                        WHERE
                            mah.exam_id = muc.exam_id
                            AND mah.paper_number = muc.paper_number
                            AND mah.group_number = mug.group_number
                            AND muc.class_name = #{className}
                            AND EXISTS( SELECT
                                    1
                                FROM
                                    mark_student ms
                                WHERE
                                    mah.student_id = ms.id
                                    AND muc.class_name = ms.class_name))
            </if>
            ORDER BY mah.status desc, mah.create_time asc
    </select>
    <select id="getArbitrateWaitingOne" resultType="com.qmth.teachcloud.mark.entity.MarkArbitrateHistory">
        SELECT
            *
        FROM
            mark_arbitrate_history
        WHERE
            exam_id = #{examId} AND paper_number = #{paperNumber}
          AND group_number = #{groupNumber}
          AND status = 'WAITING'
            ORDER BY id
            LIMIT 1
    </select>
    <select id="getHistory" resultType="com.qmth.teachcloud.mark.entity.MarkArbitrateHistory">
        SELECT
            *
        FROM
            mark_arbitrate_history t
        WHERE
            t.exam_id = #{examId} AND t.paper_number = #{paperNumber}
          AND t.group_number = #{groupNumber}
          AND t.status = #{status}
        ORDER BY t.update_time DESC
    </select>
    <select id="waitArbitrateCount" resultType="java.lang.Integer">
        SELECT
            count(1)
        FROM
            mark_arbitrate_history mah
        WHERE
            mah.exam_id = #{examId}
          AND mah.paper_number = #{paperNumber}
          AND mah.group_number = #{groupNumber}
          AND mah.status = #{status}
          <if test="className != null and className != ''">
              AND EXISTS( SELECT
              1
              FROM
              (select id, exam_id, paper_number, class_name
              from mark_student
              where exam_id = #{examId}
              and paper_number = #{paperNumber}
              and class_name = #{className}) ms
              WHERE
              mah.student_id = ms.id)
          </if>
    </select>

</mapper>
