<?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.RegionMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.RegionDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="region_id" jdbcType="VARCHAR" property="regionId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
        <result column="region_path" jdbcType="VARCHAR" property="regionPath"/>
        <result column="unclassified_flag" jdbcType="TINYINT" property="unclassifiedFlag"/>
        <result column="leader_user_id" jdbcType="VARCHAR" property="leaderUserId"/>
        <result column="order_num" jdbcType="INTEGER" property="orderNum"/>
        <result column="third_dept_id" jdbcType="VARCHAR" property="thirdDeptId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_name" jdbcType="VARCHAR" property="createName"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_name" jdbcType="VARCHAR" property="updateName"/>
        <result column="deleted" jdbcType="BIT" property="deleted"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, region_id, name, parent_id, region_path, unclassified_flag, leader_user_id, order_num,
        third_dept_id, create_time, create_name, update_time, update_name, deleted
    </sql>
    <insert id="batchInsertOrUpdate">
        <foreach collection="insertOrUpdateList" item="record" separator=";">
            insert into region
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="record.regionId != null">
                    region_id,
                </if>
                <if test="record.name != null">
                    name,
                </if>
                <if test="record.parentId != null">
                    parent_id,
                </if>
                <if test="record.regionPath != null">
                    region_path,
                </if>
                <if test="record.unclassifiedFlag != null">
                    unclassified_flag,
                </if>
                <if test="record.leaderUserId != null">
                    leader_user_id,
                </if>
                <if test="record.orderNum != null">
                    order_num,
                </if>
                <if test="record.thirdDeptId != null">
                    third_dept_id,
                </if>
                <if test="record.createTime != null">
                    create_time,
                </if>
                <if test="record.createName != null">
                    create_name,
                </if>
                <if test="record.updateTime != null">
                    update_time,
                </if>
                <if test="record.updateName != null">
                    update_name,
                </if>
                <if test="record.deleted != null">
                    deleted,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="record.regionId != null">
                    #{record.regionId},
                </if>
                <if test="record.name != null">
                    #{record.name},
                </if>
                <if test="record.parentId != null">
                    #{record.parentId},
                </if>
                <if test="record.regionPath != null">
                    #{record.regionPath},
                </if>
                <if test="record.unclassifiedFlag != null">
                    #{record.unclassifiedFlag},
                </if>
                <if test="record.leaderUserId != null">
                    #{record.leaderUserId},
                </if>
                <if test="record.orderNum != null">
                    #{record.orderNum},
                </if>
                <if test="record.thirdDeptId != null">
                    #{record.thirdDeptId},
                </if>
                <if test="record.createTime != null">
                    #{record.createTime},
                </if>
                <if test="record.createName != null">
                    #{record.createName},
                </if>
                <if test="record.updateTime != null">
                    #{record.updateTime},
                </if>
                <if test="record.updateName != null">
                    #{record.updateName},
                </if>
                <if test="record.deleted != null">
                    #{record.deleted},
                </if>
            </trim>
          ON DUPLICATE KEY UPDATE region_id = values(region_id), name = values(name), parent_id = values(parent_id), region_path = values(region_path), leader_user_id = values(leader_user_id), order_num = values(order_num), third_dept_id = values(third_dept_id), update_time = now(), deleted = values(deleted)
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective">
        update region
        <set>
            <if test="regionId != null">
                region_id = #{regionId},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="regionPath != null">
                region_path = #{regionPath},
            </if>
            <if test="unclassifiedFlag != null">
                unclassified_flag = #{unclassifiedFlag},
            </if>
            <if test="leaderUserId != null">
                leader_user_id = #{leaderUserId},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="thirdDeptId != null">
                third_dept_id = #{thirdDeptId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="createName != null">
                create_name = #{createName},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateName != null">
                update_name = #{updateName},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="deleteNotExistRegion">
        update region set deleted = 1 , update_time = now() where region_id not in <foreach collection="regionIds" separator="," item="regionId" open="(" close=")"> #{regionId}</foreach>
    </update>
    
    <select id="getRegionByRegionIds" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            region
        where
            deleted ='0' and region_id in <foreach collection="regionIds" item="regionId" separator="," open="(" close=")">#{regionId}</foreach>

    </select>

    <select id="getRegionNameByRegionIds" resultMap="BaseResultMap">
        select
            region_id, name
        from
            region
        where
            deleted = 0 and region_id in <foreach collection="regionIds" item="regionId" separator="," open="(" close=")">#{regionId}</foreach>

    </select>

    <select id="getRegionBaseInfoList" resultMap="BaseResultMap">
        select
            region_id, name, parent_id
        from
            region
        where
            deleted = 0
    </select>
    
    <select id="getRegionInfoByRegionId" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            region
        where
            deleted = 0 and region_id= #{regionId}
    </select>
    
    <select id="getSubNodeCountByRegionId" resultType="integer">
        select count(1) from region where deleted = 0 and parent_id = #{regionId}
    </select>

    <update id="deleteRegionByRegionId">
        update region set deleted = 1 where region_id = #{regionId}
    </update>
    
    <select id="getSubRegionIds" resultType="string">
        select region_id from region where deleted = 0 and <foreach collection="regionPathList" item="regionPath" open="(" close=")" separator="or"> region_path like concat(#{regionPath}, '%')</foreach>
    </select>
    
    <select id="getSubRegion" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            region
        where
            deleted = 0 and parent_id = #{regionId}
    </select>
</mapper>