<?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.TIeExamInvigilateCallMapper">

    <select id="examInvigilateCallQuery" resultType="com.qmth.themis.business.dto.response.TIeExamInvigilateCallDto">
        select
            tieic.*,
            tees.name as examStudentName,
            tes.base_photo_path as basePhotoPath
        from
            t_ie_exam_invigilate_call tieic
        left join t_e_exam_student tees on
            tees.id = tieic.exam_student_id
        left join t_e_student tes on
            tes.id = tees.student_id
        left join t_b_exam_invigilate_user tbeiu on
            tbeiu.exam_id = tieic.exam_id
        and tbeiu.room_code = tees.room_code
        left join t_oe_exam_record toer on
            toer.id = tieic.exam_record_id
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and tieic.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>
            <if test="status != null and status != ''">
                and tieic.status = #{status}
            </if>
            <if test="callStatusList != null and callStatusList != ''">
                and tieic.call_status in
                <foreach collection="callStatusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            <if test="roomCode != null and roomCode != ''">
                and tees.room_code = #{roomCode}
            </if>
        </where>
        <if test="callStatus != null and callStatus != '' and (callStatus == 'START' or callStatus == 'START,CANCEL')">
            and (toer.status = 'FIRST_PREPARE' or toer.status = 'ANSWERING' or toer.status = 'BREAK_OFF' or toer.status = 'RESUME_PREPARE')
        </if>
        order by tieic.start_time desc
    </select>

    <select id="examInvigilateCallQueryCount" resultType="java.lang.Integer">
        select
        count(1)
        from
            t_ie_exam_invigilate_call tieic
        left join t_e_exam_student tees on
            tees.id = tieic.exam_student_id
        left join t_e_student tes on
            tes.id = tees.student_id
        left join t_b_exam_invigilate_user tbeiu on
            tbeiu.exam_id = tieic.exam_id
        and tbeiu.room_code = tees.room_code
        left join t_oe_exam_record toer on
        toer.id = tieic.exam_record_id
        <where>
            <if test="examId != null and examId != ''">
                and tieic.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>
            <if test="status != null and status != ''">
                and tieic.status = #{status}
            </if>
            <if test="callStatusList != null and callStatusList != ''">
                and tieic.call_status in
                <foreach collection="callStatusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            <if test="roomCode != null and roomCode != ''">
                and tees.room_code = #{roomCode}
            </if>
        </where>
        <if test="callStatus != null and callStatus != '' and (callStatus == 'START' or callStatus == 'START,CANCEL')">
            and (toer.status = 'FIRST_PREPARE' or toer.status = 'ANSWERING' or toer.status = 'BREAK_OFF' or toer.status = 'RESUME_PREPARE')
        </if>
    </select>
</mapper>
