<?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 id="BaseResultMap" type="com.cool.store.entity.UserRegionMappingDO">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="region_id" jdbcType="VARCHAR" property="regionId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="deleted" jdbcType="BIT" property="deleted"/>
        <result column="create_id" jdbcType="VARCHAR" property="createId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_id" jdbcType="VARCHAR" property="updateId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, region_id, user_id, type, deleted, create_id, create_time, update_id, update_time
    </sql>
    <insert id="batchInsertOrUpdateUserRegion">
        <foreach collection="insertOrUpdateList" item="record" separator=";">
            insert into user_region_mapping
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="record.regionId != null">
                  region_id,
                </if>
                <if test="record.userId != null">
                  user_id,
                </if>
                <if test="record.type != null">
                  type,
                </if>
                <if test="record.deleted != null">
                    deleted,
                </if>
                <if test="record.createId != null">
                  create_id,
                </if>
                <if test="record.createTime != null">
                  create_time,
                </if>
                <if test="record.updateId != null">
                  update_id,
                </if>
                <if test="record.updateTime != null">
                  update_time,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="record.regionId != null">
                  #{record.regionId},
                </if>
                <if test="record.userId != null">
                  #{record.userId},
                </if>
                <if test="record.type != null">
                  #{record.type},
                </if>
                <if test="record.deleted != null">
                    #{record.deleted},
                </if>
                <if test="record.createId != null">
                  #{record.createId},
                </if>
                <if test="record.createTime != null">
                  #{record.createTime},
                </if>
                <if test="record.updateId != null">
                  #{record.updateId},
                </if>
                <if test="record.updateTime != null">
                  #{record.updateTime},
                </if>
            </trim>
          ON DUPLICATE KEY UPDATE type = values(type), deleted = values(deleted)
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective">
        update user_region_mapping
        <set>
            <if test="regionId != null">
                region_id = #{regionId},
            </if>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createId != null">
                create_id = #{createId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateId != null">
                update_id = #{updateId},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="deleteUserRegion">
        update user_region_mapping set deleted = 1 where region_id = #{regionId} and type = #{type}
        <if test="excludeUserIds != null and excludeUserIds.size() > 0">
          and user_id not in <foreach collection="excludeUserIds" separator="," open="(" close=")" item="userId" >#{userId}</foreach>
        </if>
    </update>

    <update id="deleteUserRegionByExcludeRegionIds">
        update user_region_mapping set deleted = 1 where user_id = #{userId} and type = #{type}
        <if test="excludeRegionIds != null and excludeRegionIds.size() > 0">
            and region_id not in <foreach collection="excludeRegionIds" separator="," open="(" close=")" item="regionId" >#{regionId}</foreach>
        </if>
    </update>

    <update id="deleteRegionUserByExcludeRegionIds">
        update user_region_mapping set deleted = 1 where region_id not in <foreach collection="excludeRegionIds" open="(" close=")" separator="," item="regionId">#{regionId}</foreach>
    </update>

    <update id="deleteRegionUserByExcludeUserIds">
        update user_region_mapping set deleted = 1 where user_id not in <foreach collection="excludeUserIds" open="(" close=")" separator="," item="userId">#{userId}</foreach>
    </update>

    <update id="deleteUserRegionByUserId">
        update user_region_mapping set deleted = 1 where user_id = #{userId}
    </update>

    <select id="getUserListByRegionId" resultType="string">
        select user_id from user_region_mapping where region_id = #{regionId} and deleted = '0'
    </select>

    <select id="getRegionIdsByUserId" resultType="string">
        select region_id from user_region_mapping where user_id = #{userId} and deleted = '0'
    </select>

    <select id="getUserListByRegionIds" resultType="string">
        select user_id from user_region_mapping where deleted = '0' and region_id in <foreach collection="regionIds" item="regionId" open="(" close=")" separator=",">#{regionId}</foreach>
    </select>

</mapper>