<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.pluss.platform.mapper.AreaCityMapper">

    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.AreaCity">
        <id column="id" property="id" />
        <result column="type" property="type" />
        <result column="areaName" property="areaName" />
        <result column="parentAreaCode" property="parentAreaCode" />
        <result column="areaCode" property="areaCode" />
        <collection property="areaCityList" column="areaCode" ofType="java.util.HashMap" select="queryCity"/>
    </resultMap>

    <resultMap id="cityMap" type="cn.pluss.platform.entity.AreaCity">
        <id column="id" property="id" />
        <result column="type" property="type" />
        <result column="areaName" property="areaName" />
        <result column="parentAreaCode" property="parentAreaCode" />
        <result column="areaCode" property="areaCode" />
        <collection property="areaCityList" column="areaCode" ofType="java.util.HashMap" select="queryCountry"/>
    </resultMap>


    <sql id="ref_column">
        id,type,areaName,parentAreaCode,areaCode
    </sql>
    <!--查询所有省 -->
    <select id="queryProvince" resultMap="BaseResultMap">
        select  <include refid="ref_column" />  from tb_pluss_area_city where type = 1
    </select>

    <select id="queryOnlyProvince" resultType="cn.pluss.platform.entity.AreaCity">
        select  <include refid="ref_column" />  from tb_pluss_area_city where type = 1
    </select>

    <!--查询所有市 -->
    <select id="queryCity" resultMap="cityMap">
        select <include refid="ref_column" /> from tb_pluss_area_city where parentAreaCode=#{areaCode}
    </select>

    <!--查询所有区 -->
    <select id="queryCountry" resultType="cn.pluss.platform.entity.AreaCity">
        select <include refid="ref_column" /> from tb_pluss_area_city  where parentAreaCode = #{areaCode}
    </select>

    <select id="getAreaCityByProvince" resultMap="BaseResultMap">
        select  <include refid="ref_column" />  from tb_pluss_area_city where areaCode = #{areaCode}
    </select>

    <select id="getAreaCodeByName" parameterType="java.lang.String" resultType="java.util.Map">
        SELECT a.areaCode AS provinceCode
        <choose>
            <when test="type == 1">
                ,b.areaCode AS cityCode
            </when>
            <otherwise>
                ,a.areaCode AS cityCode
            </otherwise>
        </choose>
        <choose>
            <when test="type == 1">
                ,c.areaCode as areaCode
            </when>
            <otherwise>
                ,b.areaCode AS areaCode
            </otherwise>
        </choose>
        FROM tb_pluss_area_city a
                 LEFT JOIN tb_pluss_area_city b  on a.areaCode = b.parentAreaCode
                <if test="type == 1">
                    LEFT JOIN tb_pluss_area_city c ON b.areaCode = c.parentAreaCode
                </if>
        WHERE a.areaName = #{provinceName}
          and b.areaName = #{cityName}
        <if test="type == 1">
          AND c.areaName = #{areaName}
        </if>
    </select>


</mapper>