<?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.tourism.webadmin.back.dao.ChinaAreaMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.ChinaArea">
        <id column="code" jdbcType="BIGINT" property="code"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="city_code" jdbcType="BIGINT" property="cityCode"/>
        <result column="city_name" jdbcType="VARCHAR" property="cityName"/>
        <result column="province_code" jdbcType="BIGINT" property="provinceCode"/>
        <result column="province_name" jdbcType="VARCHAR" property="provinceName"/>
        <result column="country" jdbcType="VARCHAR" property="country"/>
        <result column="level" jdbcType="BIGINT" property="level"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_china_area
            (code,
            name,
            city_code,
            city_name,
            province_code,
            province_name,
            country,
            level)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.code},
            #{item.name},
            #{item.cityCode},
            #{item.cityName},
            #{item.provinceCode},
            #{item.provinceName},
            #{item.country},
            #{item.level})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.tourism.webadmin.back.dao.ChinaAreaMapper.inputFilterRef"/>
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="chinaAreaFilter != null">
            <if test="chinaAreaFilter.name != null and chinaAreaFilter.name != ''">
                <bind name = "safeChinaAreaName" value = "'%' + chinaAreaFilter.name + '%'" />
                AND tour_china_area.name LIKE #{safeChinaAreaName}
            </if>
            <if test="chinaAreaFilter.level != null">
                AND tour_china_area.level = #{chinaAreaFilter.level}
            </if>
            <if test="chinaAreaFilter.searchString != null and chinaAreaFilter.searchString != ''">
                <bind name = "safeChinaAreaSearchString" value = "'%' + chinaAreaFilter.searchString + '%'" />
                AND IFNULL(tour_china_area.name,'') LIKE #{safeChinaAreaSearchString}
            </if>
        </if>
    </sql>

    <select id="getChinaAreaList" resultMap="BaseResultMap" parameterType="com.tourism.webadmin.back.model.ChinaArea">
        SELECT * FROM tour_china_area
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
