<?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.cool.store.mapper.UserRegionMappingMapper">
    <resultMap type="com.cool.store.entity.UserRegionMappingDO" id="BaseMap">
        <result property="id" column="id"/>
        <result property="regionId" column="region_id"/>
        <result property="userId" column="user_id"/>
        <result property="createId" column="create_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateId" column="update_id"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, region_id, user_id, create_id, create_time, update_id, update_time
    </sql>

    <select id="listUserRegionMappingByUserId" resultMap="BaseMap">
        select 
        <include refid="Base_Column_List"/>
        from user_region_mapping_${enterpriseId}
        <if test="userIds !=null and userIds.size >0">
            where user_id in
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
    </select>

    <select id="selectUserListByRegionIds" resultMap="BaseMap">
        select
        <include refid="Base_Column_List"/>
        from user_region_mapping_${enterpriseId}
        <if test="regionIds !=null and regionIds.size >0">
            where region_id in
            <foreach collection="regionIds" open="(" close=")" separator="," item="regionId">
                #{regionId}
            </foreach>
        </if>
    </select>

    <select id="selectUserCountByRegionIds" resultType="integer">
        select
        count(1) as userNum
        from user_region_mapping_${enterpriseId}
        <if test="regionIds !=null and regionIds.size >0">
            where region_id in
            <foreach collection="regionIds" open="(" close=")" separator="," item="regionId">
                #{regionId}
            </foreach>
        </if>
    </select>

    <select id="getRegionUserCount" resultType="java.util.HashMap">
        select region_id as regionId , count(*) as userCount
         from user_region_mapping_${enterpriseId}
        <if test="regionIds !=null and regionIds.size >0">
            where region_id in
            <foreach collection="regionIds" open="(" close=")" separator="," item="regionId">
                #{regionId}
            </foreach>
        </if>
        group by regionId;
    </select>

    <select id="listByUserIdsAndRegionIds" resultType="com.cool.store.entity.UserRegionMappingDO">
        select
        user_id as userId,
        region_id as regionId
        from
        user_region_mapping_${enterpriseId}
        where 1= 1
        <if test="userIds !=null and userIds.size >0">
            and user_id in
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        <if test="regionIds !=null and regionIds.size >0">
            and region_id in
            <foreach collection="regionIds" open="(" close=")" separator="," item="regionId">
                #{regionId}
            </foreach>
        </if>
    </select>

    <select id="getRegionIdsByUserIds" resultMap="BaseMap">
        select
           id, user_id, region_id
        from
            user_region_mapping_${enterpriseId}
        where
            user_id in
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
    </select>

    <select id="getUserIdsByRegionIds" resultType="string">
        select
            distinct(user_id)
        from
            user_region_mapping_${enterpriseId}
        where
            region_id in
            <foreach collection="regionIds" open="(" close=")" separator="," item="regionId">
                #{regionId}
            </foreach>
    </select>
</mapper>