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

    <select id="examSummary" resultType="com.qmth.themis.business.entity.TEExamSummary">
        select
            count(1) as totalCount,
            sum(case when t.status = 'FIRST_PREPARE' and t.client_websocket_status = 'ON_LINE' then 1 else 0 end) as prepareCount,
            sum(case when t.client_websocket_status = 'ON_LINE' then 1 else 0 end) as onlineCount,
            sum(case when t.client_websocket_status = 'OFF_LINE' or t.client_websocket_status is null then 1 else 0 end) as offlineCount,
            sum(case when t.camera_monitor_status = 'STOP' or t.screen_monitor_status = 'STOP' or t.mobile_first_monitor_status = 'STOP' or t.mobile_second_monitor_status = 'STOP' then 1 else 0 end) as monitorStopCount,
            sum(case when (t.status = 'ANSWERING' and t.client_websocket_status = 'ON_LINE') or t.status = 'RESUME_PREPARE' or t.status = 'BREAK_OFF' then 1 else 0 end) as examCount,
            sum(t.warning_count) as warningCount,
            sum(t.warning_unread) as warningUnread,
            sum(case when t.warning_unread > 0 then 1 else 0 end) as warningStudentUnread,
            sum(t.warning_multiple_face_count) as warningMultipleFaceCount,
            sum(t.exception_count) as exceptionCount
               <!--,
            sum(case when ((t.status = 'FINISHED' or t.status = 'PERSISTED' and t.first_start_time is not null) or s.already_exam_count > 0) then 1 else 0 end) as finishCount-->
        from t_e_exam_student s
            left join t_oe_exam_record t on t.id = s.current_record_id
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and s.exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and s.exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and s.room_code = #{roomCode}
            </if>
        </where>
    </select>

    <insert id="saveExamSummary">
        INSERT INTO t_e_exam_summary
            (${fieldName})
        VALUES(${fieldValue})
    </insert>

    <update id="updateExamSummaryFinishCount">
        update t_e_exam_summary set finish_count = finish_count + 1
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and room_code = #{roomCode}
            </if>
        </where>
    </update>

    <update id="updateExamSummaryWarningUnread">
        update t_e_exam_summary set warning_unread = 0
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and room_code = #{roomCode}
            </if>
        </where>
    </update>

    <update id="updateExamSummary">
        update t_e_exam_summary
        set ${fieldValue}
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and room_code = #{roomCode}
            </if>
        </where>
    </update>

    <select id="selectExamSummaryCount" resultType="java.lang.Integer">
        select count(1) from t_e_exam_summary
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and room_code = #{roomCode}
            </if>
        </where>
    </select>
</mapper>
