<?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.nantian.cloud.jljc.mapper.DictRegionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.DictRegion">
        <id column="id" property="id" />
        <result column="pid" property="pid" />
        <result column="meaning" property="meaning" />
        <result column="meaning2" property="meaning2" />
        <result column="version" property="version" />
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
    </resultMap>

    <resultMap id="BaseResultMapVo" type="com.nantian.cloud.jljc.vo.TreeVo">
        <id column="value" property="value" />
        <result column="pvalue" property="pvalue" />
        <result column="label" property="label" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, pid, meaning, meaning2, version, userid, update_time, del_flag, dept_id
    </sql>
    <select id="getNameByCode" resultType="com.nantian.cloud.jljc.entity.DictRegion">
        select <include refid="Base_Column_List"></include>
        from dict_region
        where del_flag = "1"
        <if test="id!=null and id!=''">
            and id = #{id}
        </if>
    </select>

    <select id="getDictRegionTreee" resultType="com.nantian.cloud.jljc.vo.TreeVo">
        select id as value, pid as pvalue, meaning as label
        from dict_region  where del_flag = "1"
    </select>

    <select id="getDictRegion" resultType="com.nantian.cloud.jljc.entity.DictRegion">
        SELECT DISTINCT
	    concat( LEFT ( dd.area, 4 ), "00" ) area,
	    dd.meaning
        FROM(
        SELECT LEFT ( d.id, 4 ) area, meaning
        FROM dict_region d WHERE pid ="530000" and del_flag = "1") dd
        GROUP BY dd.area
    </select>

    <select id="getCityRegion" resultType="com.nantian.cloud.jljc.entity.DictRegion">
        select <include refid="Base_Column_List"></include>
        from dict_region
        where del_flag = "1" and pid like '530000'
    </select>

    <select id="getDictRegionTreeeScope" resultType="com.nantian.cloud.jljc.vo.TreeVo">
        <if test="query.area.length()==2">
            select id as value, pid as pvalue, meaning as label
            from dict_region  where del_flag = "1"
            <if test="query.type!=null and query.type!=''">
                and version = #{query.type}
            </if>
        </if>
        <if test="query.area.length()==4">
            select id as value, null as pvalue, meaning as label
            from dict_region  where id like concat(#{query.area},'00')
            <if test="query.type!=null and query.type!=''">
                and version = #{query.type}
            </if>
            union
            select id as value, pid as pvalue, meaning as label
            from dict_region  where pid like concat(#{query.area},'00')
            <if test="query.type!=null and query.type!=''">
                and version = #{query.type}
            </if>
        </if>
        <if test="query.area.length()==6">
            select id as value, pid as pvalue, meaning as label
            from dict_region  where id = #{query.area}
            <if test="query.type!=null and query.type!=''">
                and version = #{query.type}
            </if>
        </if>
    </select>
    <select id="getDictRegionTreeDataScope" resultType="com.nantian.cloud.jljc.vo.TreeVo">
        select id as value, case when #{query.area} != '53' and pid = '530000' then '' else pid end pvalue, concat(meaning2,'_',id) as label
        from dict_region  where id like concat(#{query.area},'%')
        <if test="query.type!=null and query.type!=''">
            and version = #{query.type}
        </if>
    </select>
    <select id="getAreaTmp" resultType="java.lang.String">
        select distinct concat(meaning2,'_',id) from dict_region where id = concat(left(right(#{area},6),2),'0000') or id = concat(left(right(#{area},6),4),'00') or id = right(#{area},6) order by id
    </select>
    <select id="selectLRegion" resultType="java.lang.String">
        select id from dict_region where meaning like concat ('%', #{area}, '%' )
    </select>

</mapper>
