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

    <sql id="queryCommon">
        (
        select
        tbeiu.room_code as roomCode, tbeiu.room_name as roomName, tbeiu.user_id as userId,tee.id as examId,
        tee.name as examName, (
        select
        tbu.name
        from
        t_b_user tbu
        where
        tbu.id = tbeiu.user_id) as name
        from
        t_b_exam_invigilate_user tbeiu
        left join t_e_exam tee on
        tee.id = tbeiu.exam_id
        <where>
            <if test="examId != null and examId != ''">
                and tbeiu.exam_id = #{examId}
            </if>
            <if test="userId != null and userId != ''">
                and tbeiu.user_id = #{userId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and tbeiu.room_code like concat('%', #{roomCode}, '%')
            </if>
            <if test="orgId != null and orgId != ''">
                and tbeiu.org_id = #{orgId}
            </if>
        </where>
        ) t
    </sql>

    <select id="examInvigilateUserQuery" resultType="com.qmth.themis.business.dto.response.TBExamInvigilateUserDto">
        select
        t.examId,
        t.examName,
        t.roomCode,
        t.roomName,
        GROUP_CONCAT(t.userId) as userId,
        GROUP_CONCAT(t.name) as name
        from
        <include refid="queryCommon"/>
        group by
        t.examId,
        t.examName,
        t.roomCode,
        t.roomName
        order by t.examName,t.roomCode
    </select>

    <select id="examInvigilateUserExport" resultType="com.qmth.themis.business.dto.RoomCodeExportDto">
        select
        t.examId,
        t.examName,
        t.roomCode,
        t.roomName,
        GROUP_CONCAT(t.name) as name
        from
        <include refid="queryCommon"/>
        group by
        t.examId,
        t.examName,
        t.roomCode,
        t.roomName
    </select>

    <select id="getInvigilateUsers" resultType="java.util.Map">
	    select f.login_name loginName,f.`name` from 
		t_b_exam_invigilate_user t left join t_b_user f on t.user_id=f.id
		where t.exam_id=#{examId} and t.room_code=#{roomCode}
    </select>

    <select id="examInvigilateUserLoginTimeExport" resultType="com.qmth.themis.business.dto.InvigilateUserExportDto">
        select
            t.*
        from
            (
                select
                    distinct tbo.name as 'schoolName',
                        tee.name as 'examName',
                        teea.code as 'code',
                        tbu.login_name as 'name',
                        teul.`type`,
                        DATE_FORMAT(FROM_UNIXTIME(teul.create_time / 1000), '%Y-%m-%d %H:%i:%s') as 'createTime'
                from
                    t_b_exam_invigilate_user tbeiu
                        join t_e_exam tee on
                        tee.id = tbeiu.exam_id
                        join t_e_exam_activity teea on
                        teea.exam_id = tee.id
                        join t_b_user tbu on
                        tbu.id = tbeiu.user_id
                        join t_b_org tbo on
                        tbo.id = tbeiu.org_id
                        join t_e_user_log teul on
                        teul.user_id = tbu.id
                where
                    tee.id = #{examId}
                  and (teul.`type` = 'LOGIN'
                    or teul.`type` = 'LOGOUT')) t
        order by
            t.code,t.name,t.createTime
    </select>
</mapper>
