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

    <select id="regionSummary" resultType="com.qmth.themis.business.entity.TERegionSummary">
        select tir.country,
        tir.province,
        count(toer.id) as onlineCount
        from t_ip_region tir
        join t_oe_exam_record toer on tir.exam_record_id = toer.id
        WHERE toer.client_websocket_status = 'ON_LINE'
            <if test="examId != null and examId != ''">
                and toer.exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and toer.exam_activity_id = #{examActivityId}
            </if>
        group by
        tir.country,
        tir.province
    </select>

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

    <update id="updateRegionSummary">
        update t_e_region_summary
        set ${fieldValue}
        <where> 1 = 1
            <if test="country != null and country != ''">
                and country = #{country}
            </if>
            <if test="province != null and province != ''">
                and province = #{province}
            </if>
        </where>
    </update>

    <select id="selectRegionSummaryCount" resultType="java.lang.Integer">
        select count(1) from t_e_region_summary
        <where> 1 = 1
            <if test="country != null and country != ''">
                and country = #{country}
            </if>
            <if test="province != null and province != ''">
                and province = #{province}
            </if>
        </where>
    </select>
</mapper>
