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

    <select id="orgSummary" resultType="com.qmth.themis.business.bean.admin.OrgSummaryBean">
        select
            sum(case when (toer.client_websocket_status = 'ON_LINE'
            <if test="examIdSet != null and examIdSet != '' and examIdSet.size > 0">
                and toer.exam_id in
                <foreach collection="examIdSet" item="examId" index="index" open="(" close=")" separator=",">
                    #{examId}
                </foreach>
            </if>) then 1 else 0 end) as onlineCount,
            sum(case when (toer.status = 'ANSWERING' and toer.client_websocket_status = 'ON_LINE') or toer.status = 'RESUME_PREPARE' or toer.status = 'BREAK_OFF'
            <if test="examIdSet != null and examIdSet != '' and examIdSet.size > 0">
                and toer.exam_id in
                <foreach collection="examIdSet" item="examId" index="index" open="(" close=")" separator=",">
                    #{examId}
                </foreach>
            </if> then 1 else 0 end) as examCount
            <if test="all != null and all != '' and all == 1">
                   ,
                sum(case when (toer.status in ('FINISHED', 'PERSISTED')
                <if test="orgId != null and orgId != '' and orgId > 0">
                    <if test="examIdSet != null and examIdSet != '' and examIdSet.size > 0">
                        and toer.exam_id in
                        <foreach collection="examIdSet" item="examId" index="index" open="(" close=")" separator=",">
                            #{examId}
                        </foreach>
                    </if>
                </if>) then 1 else 0 end) as finishCount
            </if>
            from t_oe_exam_record toer
    </select>

    <select id="orgSummaryByFinishStudentCount" resultType="java.lang.Integer">
        select count(tees.id) as c
        from t_e_exam_student tees
        left join t_oe_exam_record toer on toer.exam_student_id = tees.id and tees.current_record_id = toer.id
        where toer.STATUS in ('FINISHED', 'PERSISTED')
        <if test="orgId != null and orgId != ''">
            <if test="examIdSet != null and examIdSet != '' and examIdSet.size > 0">
                and toer.exam_id in
                <foreach collection="examIdSet" item="examId" index="index" open="(" close=")" separator=",">
                    #{examId}
                </foreach>
            </if>
        </if>
    </select>

    <select id="orgSummaryByFinishCount" resultType="java.lang.Integer">
        <include refid="orgSummaryByFinishCountCommon" />
    </select>

    <sql id="orgSummaryByFinishCountCommon">
        select count(1) as finishCount from t_oe_exam_record toer
        where toer.STATUS in ('FINISHED', 'PERSISTED')
        <if test="orgId != null and orgId != '' and orgId > 0">
            <if test="examIdSet != null and examIdSet != '' and examIdSet.size > 0">
                and toer.exam_id in
                <foreach collection="examIdSet" item="examId" index="index" open="(" close=")" separator=",">
                    #{examId}
                </foreach>
            </if>
        </if>
    </sql>

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

    <update id="updateOrgSummary">
        update t_e_org_summary
        set ${fieldValue}
        <where> 1 = 1
            <if test="orgId != null and orgId != ''">
                and org_id = #{orgId}
            </if>
        </where>
    </update>

    <select id="selectOrgSummaryCount" resultType="java.lang.Integer">
        select count(1) from t_e_org_summary
        <where> 1 = 1
            <if test="orgId != null and orgId != ''">
                and org_id = #{orgId}
            </if>
        </where>
    </select>

    <update id="updateOrgZeroSummary">
        update t_e_org_summary set finish_count = finish_count + 1,finish_student_count = finish_student_count + #{finishStudentCount} where org_id = #{orgId}
    </update>
</mapper>
