<?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(distinct tiiwi.exam_record_id) as count
        from t_ie_invigilate_warn_info tiiwi
        left join t_e_exam_student tees on tees.id = tiiwi.exam_student_id
        left join t_e_exam tee on tee.id = tees.exam_id
        <if test="userId != null and userId != ''">
            left join t_b_exam_invigilate_user tbeiu on tbeiu.exam_id = tees.exam_id and tbeiu.room_code = tees.room_code
        </if>
        <where> 1 = 1
            and tee.monitor_status <![CDATA[ <> ]]> 'FINISHED'
            <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 tee.org_id = #{orgId}
            </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
            and tiiwi.remind_status = 1
        </where>
    </select>

    <select id="warningCountByList" resultType="com.qmth.themis.business.dto.response.TEExamQueryDto">
        select
           tiiwi.exam_id as id,
           count(distinct tiiwi.exam_record_id) as warnCount
        from t_ie_invigilate_warn_info tiiwi
        left join t_e_exam_student tees on tees.id = tiiwi.exam_student_id
        left join t_e_exam tee on tee.id = tees.exam_id
        <if test="userId != null and userId != ''">
            left join t_b_exam_invigilate_user tbeiu on tbeiu.exam_id = tees.exam_id and tbeiu.room_code = tees.room_code
        </if>
        <where> 1 = 1
            and tee.monitor_status <![CDATA[ <> ]]> 'FINISHED'
            <if test="examIdList != null and examIdList != '' and examIdList.size > 0">
                and tiiwi.exam_id in
                <foreach collection="examIdList" item="examId" index="index" open="(" close=")" separator=",">
                    #{examId}
                </foreach>
            </if>
            <if test="userId != null and userId != ''">
                and tbeiu.user_id = #{userId}
            </if>
            <if test="orgId != null and orgId != ''">
                and tee.org_id = #{orgId}
            </if>
            and tiiwi.approve_status = 0
            and tiiwi.remind_status = 1
            group by tiiwi.exam_id
        </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_e_exam_student tees
        left join t_ie_invigilate_warn_info tiiwi on tees.id = tiiwi.exam_student_id
        <!--2022/09/03新增 start
        join
        (
        select
        distinct tiiwi.exam_record_id as examRecordId,
        max(tiiwi.create_time) as createTime,
        max(tiiwi.id) as warningId
        from
        t_ie_invigilate_warn_info tiiwi
        group by
        tiiwi.exam_record_id) temp on
        temp.warningId = tiiwi.id
        2022/09/03新增 end-->
        left join t_oe_exam_record toer on toer.id = tiiwi.exam_record_id
        left join t_b_exam_invigilate_user tbeiu on tbeiu.exam_id = tiiwi.exam_id and tbeiu.room_code = tees.room_code
        join t_e_exam tee on tee.id = tiiwi.exam_id
        <where> 1 = 1
            and tee.monitor_status <![CDATA[ <> ]]> 'FINISHED'
            <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
            and tiiwi.remind_status = 1
        </where>
    </select>

    <select id="findLastMsg" resultType="com.qmth.themis.business.bean.admin.WarningNotifyBean">
        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,
        IF(t.approve_status = 0,'未处理','已处理') as approveStatus,
        INSERT(rpad(substring(f.identity, 1, 2), char_length(f.identity), '*'),char_length(f.identity)-1,char_length(f.identity),substring(f.identity, char_length(f.identity) - 1 , char_length(f.identity) - 2)) as identity,
        f.name,
        f.room_code roomCode
        FROM
        t_ie_invigilate_warn_info t
        left join t_e_exam h on t.exam_id=h.id
        LEFT JOIN t_e_exam_student f ON t.exam_student_id = f.id
        where 1=1
        <if test="orgId != null">
            and h.org_id=#{orgId}
        </if>
        and h.monitor_status <![CDATA[ <> ]]> 'FINISHED'
        order by t.create_time desc limit #{count}
    </select>

    <select id="findWarnAndExceptionInfo" resultType="java.lang.Integer">
        select
            count(1) as c
        from
            t_ie_invigilate_warn_info t
        where
            t.exam_record_id = #{examRecordId}
        union all
        select
            count(1) as c
        from
            t_ie_invigilate_exception_info t
        where
            t.exam_record_id = #{examRecordId}
        union all
        select
            count(1) as c
        from
            t_ie_invigilate_warn_info t
        where
            t.exam_record_id = #{examRecordId}
            <if test="type != null and type != ''">
                and t.`type` = #{type}
            </if>
            <if test="level != null and level != ''">
                and t.`level` = #{level}
            </if>
    </select>

</mapper>