<?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="pwc.taxtech.atms.dao.AreaRegionMapper">

    <!-- User defined -->
    <resultMap id="ResultMapWithAreaAndRegion" type="pwc.taxtech.atms.entity.AreaRegion">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="AREA_REGION_AREA_ID" jdbcType="VARCHAR" property="areaId"/>
        <result column="AREA_REGION_REGION_ID" jdbcType="VARCHAR" property="regionId"/>
        <association column="AREA_ID" javaType="pwc.taxtech.atms.entity.Area" property="area">
            <id column="AREA_ID" property="id"/>
            <result column="AREA_PARENT_ID" property="parentId"/>
            <result column="AREA_NAME" property="name"/>
            <result column="AREA_IS_ACTIVE" property="isActive"/>
        </association>
        <association column="REGION_ID" javaType="pwc.taxtech.atms.entity.Region" property="region">
            <id column="REGION_ID" property="id"/>
            <result column="REGION_PARENT_ID" property="parentId"/>
            <result column="REGION_NAME" property="name"/>
            <result column="REGION_SHORT_NAME" property="shortName"/>
            <result column="REGION_MERGER_NAME" property="mergerName"/>
            <result column="REGION_LEVEL_TYPE" property="levelType"/>
            <result column="REGION_TEL_CODE" property="telCode"/>
            <result column="REGION_ZIP_CODE" property="zipCode"/>
            <result column="REGION_PIN_YIN" property="pinYin"/>
            <result column="REGION_LONGITUDE" property="longitude"/>
            <result column="REGION_LATITUDE" property="latitude"/>
            <result column="REGION_IS_ACTIVE" property="isActive"/>
        </association>
    </resultMap>

    <select id="selectAreaRegionByAreaId" parameterType="string" resultMap="ResultMapWithAreaAndRegion">
        SELECT
        area_region.id,
        area_region.area_id AS AREA_REGION_AREA_ID,
        area_region.region_id AS AREA_REGION_REGION_ID,
        area.id AS AREA_ID,
        area.parent_id AS AREA_PARENT_ID,
        area.name AS AREA_NAME,
        area.is_active AS AREA_IS_ACTIVE,
        region.id AS REGION_ID,
        region.parent_id AS REGION_PARENT_ID,
        region.name AS REGION_NAME,
        region.short_name AS REGION_SHORT_NAME,
        region.merger_name AS REGION_MERGER_NAME,
        region.level_type AS REGION_LEVEL_TYPE,
        region.tel_code AS REGION_TEL_CODE,
        region.zip_code AS REGION_ZIP_CODE,
        region.pin_yin AS REGION_PIN_YIN,
        region.latitude AS REGION_LATITUDE,
        region.is_active AS REGION_IS_ACTIVE
        FROM
        area_region,
        area,
        region
        WHERE
        area_region.area_id = area.ID
        AND area_region.region_id = region.ID
        AND area_region.area_id = #{areaId}
        ORDER BY
        region_id
    </select>

    <resultMap id="resultMapAreaRegionDto" type="pwc.taxtech.atms.dto.arearegion.AreaRegionDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="AREA_ID" property="areaId"/>
        <result column="REGION_NAME" property="regionName"/>
        <result column="REGION_ID" property="regionId"/>
    </resultMap>

    <select id="selectByRegionLevelType" parameterType="map" resultMap="resultMapAreaRegionDto">
        SELECT
        area_region_real.area_id AS AREA_ID,
        P.name AS REGION_NAME,
        P.id AS REGION_ID
        FROM
        region  P
        LEFT JOIN
        area_region  AreaRegionReal
        ON
        P.id = AreaRegionReal.region_id
        WHERE
        P.level_type = #{regionLevelType}
    </select>

</mapper>