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

    <sql id="fields">
        id                     ,
            id          as regionId,
            name                   ,
            parent_id   as parentId,
            group_id    as groupId,
            store_id    as storeId,
            create_time as createTime,
            create_name as createName,
            update_time as updateTime,
            update_name as updateName,
            syn_ding_dept_id as synDingDeptId,
            region_type as regionType,
            region_path as regionPath,
            deleted as deleted,
            third_dept_id as thirdDeptId,
            third_region_type as thirdRegionType
    </sql>

    <select id="getRegionByRegionIdsForMap" resultType="com.cool.store.entity.RegionDO">
        select
        id          ,
        id          as regionId,
        name
        from region_${enterpriseId} where id in
        <foreach collection="regionIds" index="index" item="regionId"
                 separator="," open="(" close=")">
            #{regionId, jdbcType=BIGINT}
        </foreach>
    </select>

    <select id="getRegionByRegionId" resultType="com.cool.store.dto.RegionNode">
        select
            id          ,
            id          as regionId,
            name        as name ,
            parent_id   as parentId,
            group_id    as groupId,
            create_time as createTime,
            create_name as createName,
            update_time as updateTime,
            update_name as updateName,
            syn_ding_dept_id as synDingDeptId,
            region_type as regionType,
            region_path as regionPath,
            store_num   as storeCount
        from region_${enterpriseId} where id = #{regionId, jdbcType=BIGINT} and deleted = 0
    </select>

    <select id="getRegionByRegionIds" resultType="com.cool.store.entity.RegionDO">
        select
            id          ,
            id          as regionId,
            name        ,
            parent_id   as parentId,
            group_id    as groupId,
            create_time as createTime,
            create_name as createName,
            syn_ding_dept_id as synDingDeptId,
            update_time as updateTime,
            update_name as updateName,
            region_path as regionPath,
            store_num   as storeNum,
            store_id   as storeId,
            region_type as regionType,
            is_external_node as isExternalNode,
        third_dept_id as thirdDeptId,
        third_region_type as thirdRegionType
        from region_${enterpriseId} where id in
        <foreach collection="regionIds" index="index" item="regionId"
                 separator="," open="(" close=")">
            #{regionId, jdbcType=BIGINT}
        </foreach>
        and deleted = 0
    </select>

    <select id="getByIds" resultType="com.cool.store.entity.RegionDO">
        select
        id          ,
        id   as regionId,
        name        ,
        parent_id   as parentId,
        group_id    as groupId,
        create_time as createTime,
        create_name as createName,
        update_time as updateTime,
        update_name as updateName,
        store_id as storeId,
        order_num   as orderNum,
        deleted     as deleted,
        region_path as regionPath
        from region_${enterpriseId}
        where id in (
        <foreach collection="list" item="item" separator=",">
            #{item}
        </foreach>
        )
    </select>

    <select id="getByRegionId" resultType="com.cool.store.entity.RegionDO">
        select
            id          ,
            id          as regionId,
            name        as name ,
            parent_id   as parentId,
            group_id    as groupId,
            create_time as createTime,
            create_name as createName,
            update_time as updateTime,
            update_name as updateName,
            syn_ding_dept_id as synDingDeptId,
            store_id as storeId,
            region_type as regionType,
            region_path as regionPath,
            deleted,
            store_num   as storeNum,
            third_region_type as thirdRegionType
        from region_${enterpriseId}
        where id = #{regionId}
    </select>

    <select id="getRegionsByParentId" resultType="com.cool.store.entity.RegionDO">
        select
            id          ,
            region_id    as regionId,
            name        ,
            parent_id   as parentId,
            group_id    as groupId,
            create_time as createTime,
            create_name as createName,
            update_time as updateTime,
            update_name as updateName,
            region_path as regionPath,
            region_type as regionType,
            store_num   as storeNum,
            store_id   as storeId
        from region_${enterpriseId}
        where parent_id = #{parentId} and deleted = 0
        order by region_type
    </select>

    <select id="listRegionByIds" resultType="com.cool.store.entity.RegionDO">
        select
        id          ,
        region_id   as regionId,
        name        as name ,
        parent_id   as parentId,
        group_id    as groupId,
        create_time as createTime,
        create_name as createName,
        update_time as updateTime,
        update_name as updateName,
        vds_group_corp_id as vdsGroupCorpId,
        syn_ding_dept_id as synDingDeptId,
        region_type as regionType,
        deleted as deleted,
        region_path as regionPath,
        store_num as storeNum
        from region_${enterpriseId}
        where deleted = 0
        and id in (
        <foreach collection="regionIds" item="regionId" separator=",">
            #{regionId}
        </foreach>
        )
    </select>

    <select id="listRegionByRegionPath" resultType="com.cool.store.entity.RegionDO">
        select
        id          ,
        region_id   as regionId,
        name        as name ,
        parent_id   as parentId,
        group_id    as groupId,
        create_time as createTime,
        create_name as createName,
        update_time as updateTime,
        update_name as updateName,
        vds_group_corp_id as vdsGroupCorpId,
        syn_ding_dept_id as synDingDeptId,
        region_type as regionType,
        deleted as deleted
        from region_${enterpriseId}
        where deleted = 0
        and region_path like concat(#{regionPath},'%')
    </select>

    <select id="getRegionPathByIds" resultType="com.cool.store.entity.RegionDO">
        select
        id,
        name,
        region_path
        from region_${enterpriseId}
        where id in (
        <foreach collection="list" item="item" separator=",">
            #{item}
        </foreach>
        )
    </select>


    <select id="getRegionsByEid" resultType="com.cool.store.entity.RegionDO">
        select
            id          ,
            region_id   as regionId,
            name        ,
            parent_id   as parentId,
            create_time as createTime,
            update_time as updateTime,
            order_num   as orderNum,
            deleted     as deleted
        from region_${enterpriseId} where id != 1
        <if test="regionId!=null">
            and region_path like concat('%/',#{regionId},'/%')
        </if>
        order by id
    </select>


    <select id="getSubRegion" resultType="com.cool.store.entity.RegionDO">
        select
            id          ,
            id          as regionId,
            name        ,
            parent_id   as parentId,
            group_id    as groupId,
            create_time as createTime,
            create_name as createName,
            syn_ding_dept_id as synDingDeptId,
            update_time as updateTime,
            update_name as updateName,
            region_path as regionPath,
            store_num   as storeNum,
            store_id   as storeId,
            region_type as regionType,
            syn_ding_dept_id as synDingDeptId
        from region_${enterpriseId}
        where deleted = 0 and parent_id = #{parentId}
    </select>

    <select id="getByRegionIdExcludeDeleted" resultType="com.cool.store.entity.RegionDO">
        select
        id          ,
        id          as regionId,
        name        as name ,
        parent_id   as parentId,
        group_id    as groupId,
        create_time as createTime,
        create_name as createName,
        update_time as updateTime,
        update_name as updateName,
        syn_ding_dept_id as synDingDeptId,
        store_id as storeId,
        region_type as regionType,
        region_path as regionPath,
        deleted,
        store_num   as storeNum
        from region_${enterpriseId}
        where id = #{regionId} and deleted = 0;
    </select>

    <select id="getSubIdsByRegionIds" resultType="string">
        select
            id
        from
            region_${enterpriseId}
        where
            deleted = 0
        and
            <foreach collection="regionPaths" separator=" or " open="(" close=")" item="region" > region_path like concat(#{region}, "%")</foreach>
    </select>

    <select id="getSubRegionIdsByRegionIds" resultType="string">
        select
            id
        from
            region_${enterpriseId}
        where
            deleted = 0
        and
        <foreach collection="regionIds" separator=" or " open="(" close=")" item="region" > region_path like concat("%", #{region}, "%")</foreach>
    </select>

    <select id="getStoreIdsByRegionIds" resultType="string">
        select
        store_id
        from
        region_${enterpriseId}
        where
        deleted = 0
        and
        <foreach collection="regionIds" separator=" or " open="(" close=")" item="region" > region_path like concat("%", #{region}, "%")</foreach>
    </select>

    <select id="getSubIdsByRegionpaths" resultType="string">
        select
        id
        from
        region_${enterpriseId}
        where
        deleted = 0
        and region_type = 'path'
        and
        <foreach collection="regionPaths" separator=" or " open="(" close=")" item="region" > region_path like concat('%',#{region}, "%")</foreach>
    </select>

    <select id="getRegionPathByRegionIds" resultType="string">
        select
            region_path
        from
            region_${enterpriseId}
        where
            deleted = 0
        and
            id in <foreach collection="regionIds" separator="," open="(" close=")" item="region" > #{region}</foreach>
    </select>


    <select id="getCompRegionByRegionIds" resultType="com.cool.store.entity.RegionDO">
        select
        <include refid="fields"></include>
        from region_${enterpriseId}
        where region_type = 'path'
        and deleted = 0
        and (
        region_path like
        <foreach collection="stringCompParentIdList" item="id" separator="or region_path like" close="">
            concat('%/',#{id},'/%')
        </foreach>
        )
    </select>
    <select id="countByRegionIdList" resultType="java.lang.Integer">
        select
        count(0)
        from region_${enterpriseId}
        where id in (
        <foreach collection="regionIdList" separator="," item="item">
            #{item}
        </foreach>
        )
    </select>

    <select id="getRegionByParentIds" resultType="com.cool.store.entity.RegionDO">
        select <include refid="fields"/>
        from region_${enterpriseId}
        WHERE
        deleted = 0
        AND parent_id in (
        <foreach collection="regionIdList" separator="," item="id">
            #{id}
        </foreach>
        )
    </select>


    <select id="listByThirdRegionType" resultType="com.cool.store.entity.RegionDO">
        select <include refid="fields"/>
        from region_${enterpriseId}
        where deleted = 0
        <if test="parentId != null">
            and parent_id= #{parentId}
        </if>
        <if test="thirdRegionType != null and thirdRegionType != ''">
            and third_region_type = #{thirdRegionType}
        </if>
    </select>
    <select id="getRegionByStoreId" resultType="com.cool.store.entity.RegionDO">
        select <include refid="fields"/>
        from region_${enterpriseId}
        where deleted = 0
        and store_id = #{storeId}
        and region_type = 'store'
    </select>
    <select id="getBigRegionByRegionId" resultType="com.cool.store.entity.RegionDO">
        select <include refid="fields"/>
        from region_${enterpriseId}
        where deleted = 0
        and id = (
        SELECT parent_id
        FROM region_${enterpriseId}
        WHERE id = #{regionId}
        )
    </select>
    <select id="getFightRegionByRegionIds" resultType="com.cool.store.entity.RegionDO">
        select <include refid="fields"/>
        from region_${enterpriseId}
        where deleted = 0
        and third_region_type = '2'
        <if test="regionIds != null">
            AND id in (
            <foreach collection="regionIds" separator="," item="id">
                #{id}
            </foreach>
            )
        </if>

    </select>
    <select id="getRegionInfoBySynDingDeptId" resultType="com.cool.store.entity.RegionDO">
        select <include refid="fields"/>
        from region_${enterpriseId}
        where deleted = 0
        and syn_ding_dept_id = #{synDingDeptId}
    </select>


</mapper>