<?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.themis.business.dao.TIeInvigilateWarnInfoMapper">

    <select id="warningCount" resultType="java.lang.Integer">
        select count(1) as count
        from t_ie_invigilate_warn_info tiiwi
        left join t_e_exam_student tees on tees.id = tiiwi.exam_student_id
        inner join (select toer.id from t_oe_exam_record toer where EXISTS(select tees.id from t_e_exam_student tees where EXISTS (select distinct tbeiu.room_code from t_b_exam_invigilate_user tbeiu
        where
        <if test="userId != null and userId != ''">
            tbeiu.user_id = #{userId} and
        </if>
        tbeiu.room_code = tees.room_code and toer.exam_student_id = tees.id))) t on t.id = tiiwi.exam_record_id
        <where>
            <if test="examId != null and examId != ''">
                and tiiwi.exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and tiiwi.exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and tees.room_code = #{roomCode}
            </if>
            and tiiwi.approve_status = 0
        </where>
    </select>

    <select id="warningMessage" resultType="com.qmth.themis.business.dto.response.TIeWarningNotifyDto">
        select
            tees.id as examStudentId,
            tees.name,
            tiiwi.info,
            tiiwi.`level`,
            tiiwi.id as warningId,
            tiiwi.remark,
            tiiwi.exam_id as examId,
            tiiwi.exam_record_id as examRecordId
        from
            t_ie_invigilate_warn_info tiiwi
        left join t_e_exam_student tees on
            tees.id = tiiwi.exam_student_id
        left join t_b_exam_invigilate_user tbeiu on
            tbeiu.exam_id = tiiwi.exam_id
            and tbeiu.room_code = tees.room_code
        <where>
            <if test="examId != null and examId != ''">
                and tiiwi.exam_id = #{examId}
            </if>
            <if test="userId != null and userId != ''">
                and tbeiu.user_id = #{userId}
            </if>
            <if test="orgId != null and orgId != ''">
                and tbeiu.org_id = #{orgId}
            </if>
            and tiiwi.approve_status = 0
        </where>
    </select>
    <select id="findLastMsg" resultType="com.qmth.themis.business.bean.backend.WarningNotifyBean">
    	SELECT
			f.identity,
			f.name,
			f.room_code roomCode,
			tem.*
		FROM
			(
				SELECT
					t.exam_student_id AS examStudentId,
					t.info,
					t.`level`,
					t.id AS warningId,
					t.remark,
					t.exam_id AS examId,
					t.exam_record_id AS examRecordId,
					t.create_time createTime
				FROM
					t_ie_invigilate_warn_info t
				ORDER BY
					t.id DESC
				limit #{conut}
			) tem
		LEFT JOIN t_e_exam_student f ON tem.examStudentId = f.id
    </select>
</mapper>
