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

    <select id="studentQuery" resultType="com.qmth.themis.business.dto.response.TEStudentDto">
        select
        tes.id,
        tes.name,
        tes.`identity`,
        tbo.name as orgName,
        tes.enable,
        tes.base_photo_path as basePhotoPath,
        IF(tes.base_photo_path is null,0,1) as photo,
        tbu1.name as createName,
        tbu2.name as updateName,
        tes.create_time as createTime,
        tes.update_time as updateTime,
        tes.mobile_number as mobileNumber
        from
        t_e_student tes
        left join t_b_org tbo on tbo.id = tes.org_id
        left join t_b_user tbu1 on tes.create_id = tbu1.id
        left join t_b_user tbu2 on tes.update_id = tbu2.id
        <where>
            <if test="identity != null and identity != ''">
                and tes.identity like concat('%', #{identity}, '%')
            </if>
            <if test="name != null and name != ''">
                and tes.name like concat('%', #{name}, '%')
            </if>
            <if test="enable != null and enable != '' or enable == 0">
                and tes.enable = #{enable}
            </if>
            <if test="orgId != null and orgId != ''">
                and tes.org_id = #{orgId}
            </if>
            <if test="photo != null and photo != '' or photo == 0">
                <choose>
                    <when test="photo == true">
                        and tes.base_photo_path is not null
                    </when>
                    <otherwise>
                        and tes.base_photo_path is null
                    </otherwise>
                </choose>
            </if>
        </where>
        order by tes.id
    </select>

    <select id="studentExamRecordQuery" resultType="com.qmth.themis.business.dto.response.TEStudentExamRecordDto">
        select
            tee.id as examId,
            tee.name as examName,
            tees.id as examStudentId,
            toer.id as examRecordId,
            tees.course_code as courseCode,
            tees.course_name as courseName,
            tees.`identity`,
            tees.name,
            toer.objective_score as objectiveScore,
            toer.status,
            toer.first_start_time as firstStartTime,
            toer.finish_time as finishTime,
            IFNULL(IF(toer.tencent_video_url is not null, 1, null),count(t1.id)) as videoCount,
            toer.monitor_record as monitorRecord,
            GROUP_CONCAT(t2.video_source) as videoSource,
            toer.tencent_video_url as tencentVideoUrl,
            toer.video_status as videoStatus
        from
            t_e_exam_student tees
        join t_e_exam tee on tee.id = tees.exam_id
        join t_oe_exam_record toer on toer.exam_student_id = tees.id
        join t_e_student tes on tes.id = tees.student_id
        left join t_m_tencent_video_message t1 on t1.exam_record_id = toer.id
        left join t_m_tencent_video_message t2 on t2.exam_record_id = toer.id
            <where>
                <if test="studentId != null and studentId != ''">
                    and tes.id = #{studentId}
                </if>
                <if test="examId != null and examId != ''">
                    and tee.id = #{examId}
                </if>
            </where>
        group by
            tee.id,
            tee.name,
            tees.id,
            toer.id,
            tees.course_code,
            tees.course_name ,
            tees.`identity`,
            tees.name,
            toer.objective_score,
            toer.status,
            toer.first_start_time,
            toer.finish_time,
            toer.monitor_record,
            toer.tencent_video_url,
            toer.video_status
        order by toer.first_start_time desc
    </select>

    <select id="examRecordVideoQuery" resultType="com.qmth.themis.business.dto.response.TEStudentExamRecordVideoMessageDto">
        select
            tmtvm.video_source as videoSource,
            tmtvm.start_time as startTime,
            tmtvm.end_time as endTime,
            tmtvm.video_url as videoUrl
        from
            t_m_tencent_video_message tmtvm
                join t_oe_exam_record toer on
                toer.id = tmtvm.exam_record_id
        <where>
            <if test="examRecordId != null and examRecordId != ''">
                and toer.id = #{examRecordId}
            </if>
            <if test="monitorRecord != null and monitorRecord != ''">
                and tmtvm.video_source like concat('%', #{monitorRecord})
            </if>
        </where>
    </select>
</mapper>
