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

    <select id="examList" resultType="java.util.HashMap">
        select
        t.id as examId
        from
        t_e_exam t
        <if test="userId != null and userId != ''">
            left join t_b_exam_invigilate_user tbeiu on tbeiu.exam_id = t.id
        </if>
        <where>1 = 1
            and t.monitor_status <![CDATA[ <> ]]> 'FINISHED'
            and t.start_time <![CDATA[ <= ]]> unix_timestamp(current_timestamp()) * 1000
            and t.enable = 1
            <if test="orgId != null and orgId != ''">
                and t.org_id = #{orgId}
            </if>
            <if test="userId != null and userId != ''">
                and tbeiu.user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="examQuery" resultType="com.qmth.themis.business.dto.response.TEExamQueryDto">
        select distinct t.id,
        t.code,
        t.name,
        t.tag,
        t.mode,
        t.enable,
        t.start_time as startTime,
        t.end_time as endTime,
        t.create_time as createTime,
        t.update_time as updateTime,
        t.enable_ip_limit as enableIpLimit,
        t.monitor_video_source as monitorVideoSource,
        t.monitor_record as monitorRecord,
        t.monitor_status as monitorStatus,
        TRUNCATE(t.progress,2) as progress,
        t.score_status as scoreStatus,
        tbu1.name as createName,
        tbu2.name as updateName,
        t.exam_student_call_enable as examStudentCallEnable,
        t.objective_score_push as objectiveScorePush,
        t.exam_student_breach_push as examStudentBreachPush,
        (
        select
        tbth.status
        from
        t_b_task_history tbth
        where
        tbth.exam_id = t.id
        and tbth.`type` = 'CLOUD_MARK_DATA_PUSH'
        and tbth.progress = 100
        order by
        tbth.create_time desc
        limit 1) as cloudMarkPushDataStatus
        <!--<if test="type != null and type != '' and type == 'monitor'">
            ,group_concat(distinct tees.room_code) as roomCode,
            group_concat(distinct tees.room_name) as roomName
        </if>-->
        from t_e_exam t
        left join t_b_user tbu1 on t.create_id = tbu1.id
        left join t_b_user tbu2 on t.update_id = tbu2.id
        <!--<if test="type != null and type != '' and type == 'monitor'">
            left join t_e_exam_student tees on tees.exam_id = t.id
        </if>-->
        <if test="userId != null and userId != ''">
            left join t_b_exam_invigilate_user tbeiu on tbeiu.exam_id = t.id
        </if>
        <where>1 = 1
            <if test="type != null and type != '' and type == 'monitor'">
                and t.monitor_status <![CDATA[ <> ]]> 'FINISHED'
                and t.start_time <![CDATA[ <= ]]> unix_timestamp(current_timestamp()) * 1000
            </if>
            <if test="finished != null and finished != '' and finished == 1">
                and t.monitor_status = 'FINISHED'
            </if>
            <if test="userId != null and userId != ''">
                and tbeiu.user_id = #{userId}
            </if>
            <if test="orgId != null and orgId != ''">
                and t.org_id = #{orgId}
            </if>
            <if test="id != null and id != ''">
                and t.id = #{id}
            </if>
            <if test="code != null and code != ''">
                and t.code = #{code}
            </if>
            <if test="name != null and name != ''">
                and t.name like concat('%', #{name}, '%')
            </if>
            <if test="mode != null and mode != '' ">
                and t.mode = #{mode}
            </if>
            <if test="enable != null and enable != '' or enable == 0">
                and t.enable = #{enable}
            </if>
        </where>
        <!--<if test="type != null and type != '' and type == 'monitor'">
            group by t.id,
            t.code,
            t.name,
            t.tag,
            t.mode,
            t.enable,
            t.start_time,
            t.end_time,
            t.progress,
            tbu2.name,
            t.update_time,
            t.enable_ip_limit,
            t.monitor_video_source,
            t.monitor_record,
            t.monitor_status
        </if>-->
        order by t.create_time desc
    </select>

    <select id="examroomQuery" resultType="java.util.HashMap">
        select
        distinct tees.room_code as roomCode,
        tees.room_name as roomName
        from
        t_e_exam t
        left join t_e_exam_student tees on
        tees.exam_id = t.id
        WHERE 1 = 1
        <if test="code != null and code != ''">
            and t.code = #{code}
        </if>
        and t.monitor_status <![CDATA[ <> ]]> 'FINISHED'
        and t.start_time <![CDATA[ <= ]]> unix_timestamp(current_timestamp()) * 1000
        and t.enable = 1
        order by tees.room_code
    </select>

    <select id="getWaitingExam" resultType="com.qmth.themis.business.dto.response.TEExamWaitDto">
        select
        t.*
        from
        (
        select
        distinct tee.id,
        tee.name,
        tee.code,
        group_concat(distinct teea.id) as examActivityId,
        <!--FLOOR((teea.finish_time - teea.start_time) / (24*60*60*1000)) as diffSum1,
        FLOOR(((teea.finish_time - teea.start_time) / (24*60*60*1000) - (unix_timestamp(current_timestamp()) * 1000 - teea.start_time) / (24*60*60*1000))) as reallyTime1,-->
        datediff(FROM_UNIXTIME(teea.finish_time / 1000,'%Y-%m-%d %H:%i:%s'),FROM_UNIXTIME(teea.start_time /
        1000,'%Y-%m-%d %H:%i:%s')) as diffSum,
        datediff(FROM_UNIXTIME(teea.finish_time / 1000,'%Y-%m-%d %H:%i:%s'),FROM_UNIXTIME(teea.start_time /
        1000,'%Y-%m-%d %H:%i:%s')) -
        datediff(now(),FROM_UNIXTIME(teea.start_time / 1000,'%Y-%m-%d %H:%i:%s')) as reallyTime
        from
        t_e_exam_student tees
        left join t_e_exam tee on
        tee.id = tees.exam_id
        left join t_e_exam_activity teea on
        teea.id = tees.exam_activity_id
        <where>
            <if test="studentId != null and studentId != ''">
                and tees.student_id = #{studentId}
            </if>
            and tee.enable = 1
            and teea.enable = 1
            and tees.enable = 1
            <if test="orgId != null and orgId != ''">
                and tee.org_id = #{orgId}
            </if>
            <if test="examId != null and examId != ''">
                and tee.id = #{examId}
            </if>
            <!--and IF(tee.mode = 'TOGETHER', teea.start_time >= date_sub(now(),
            interval IFNULL(teea.opening_seconds, tee.opening_seconds) second),1 = 1 )-->
            group by tee.id,tee.name,tee.code,diffSum,reallyTime
        </where>
        ) t
        <where>
            t.reallyTime <![CDATA[ <= ]]> diffSum + 15
            and t.reallyTime <![CDATA[ >= ]]> 0
        </where>
    </select>

    <select id="examPrivilegeQuery" resultType="com.qmth.themis.business.entity.TEExam">
        select distinct tee.* from t_e_exam tee
        left join t_b_exam_invigilate_user tbeiu on tee.id = tbeiu.exam_id
        <where> 1 = 1
            <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="type != null and type != '' and type == 'monitor'">
                and tee.monitor_status <![CDATA[ <> ]]> 'FINISHED'
            </if>
        </where>
        order by tee.create_time desc
    </select>

    <update id="updateInvigilateMonitorStatus">
        update t_e_exam
        set monitor_status=#{monitorStatus}
        where id = #{examId}
    </update>

    <update id="updateScoreStatus">
        update t_e_exam
        set score_status=#{scoreStatus}
        where id = #{examId}
    </update>

    <select id="examQueryForOpen" resultType="com.qmth.themis.business.bean.admin.OpenExamBean">
        SELECT
        t.id,
        t. CODE,
        t. NAME,
        t.tag,
        t. MODE,
        t.start_time startTime,
        t.end_time endTime
        FROM
        t_e_exam t
        WHERE t.enable=1
        <if test="id != null">
            AND t.id = #{id}
        </if>
        <if test="code != null and code !='' ">
            AND t.code = #{code}
        </if>
    </select>

    <update id="updateScoreProgress">
        update t_e_exam
        set progress=#{progress}
        where id = #{examId}
    </update>

    <select id="videoQuery" resultType="com.qmth.themis.business.bean.result.VideoQueryResult">
        SELECT
            tbo.id as orgId,
            tbo.code as orgCode,
            tbo.name as orgName,
            tee.id as examId,
            tee.code as examCode,
            tee.name as examName,
            tee.end_time as examEndTime,
            tee.video_delete as videoDelete,
            tee.video_delete_time as videoDeleteTime,
            datediff(now(),FROM_UNIXTIME(tee.end_time / 1000,'%Y-%m-%d %H:%i:%s')) as saveTime
        from
            t_e_exam tee
                join t_b_org tbo on
                tbo.id = tee.org_id
        <where> 1 = 1
            <if test="examName != null and examName !=''">
                and tee.name like concat('%', #{examName}, '%')
            </if>
            <if test="orgName != null and orgName !=''">
                and tbo.name like concat('%', #{orgName}, '%')
            </if>
            <if test="type != null and type != '' and type != 'ALL'">
                <choose>
                    <when test="type == 'TRUE'">
                        and tee.video_delete = 1
                    </when>
                    <otherwise>
                        and tee.video_delete = 0
                    </otherwise>
                </choose>
            </if>
        </where>
        order by tee.end_time
    </select>
</mapper>