<?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.fastbee.system.mapper.SysDistrictMapper">

    <resultMap type="SysDistrict" id="SysDistrictResult">
        <result property="id"    column="id"    />
        <result property="adcode"    column="adcode"    />
        <result property="level"    column="level"    />
        <result property="parentId"    column="parent_id"    />
        <result property="name"    column="name"    />
        <result property="fullName"    column="full_name"    />
        <result property="lng"    column="lng"    />
        <result property="lat"    column="lat"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="mapOutline"    column="map_outline"    />
        <result property="mapOutlineUrl"    column="map_outline_url"    />
    </resultMap>

    <sql id="selectSysDistrictVo">
        select id, adcode, level, parent_id, name, full_name, lng, lat, del_flag, map_outline, map_outline_url from sys_district
    </sql>

    <select id="selectSysDistrictList" parameterType="SysDistrict" resultMap="SysDistrictResult">
        <include refid="selectSysDistrictVo"/>
        <where>
            <if test="adcode != null "> and adcode = #{adcode}</if>
            <if test="level != null "> and level = #{level}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="fullName != null  and fullName != ''"> and full_name like concat('%', #{fullName}, '%')</if>
            <if test="lng != null "> and lng = #{lng}</if>
            <if test="lat != null "> and lat = #{lat}</if>
            <if test="mapOutline != null  and mapOutline != ''"> and map_outline = #{mapOutline}</if>
            <if test="mapOutlineUrl != null  and mapOutlineUrl != ''"> and map_outline_url = #{mapOutlineUrl}</if>
        </where>
    </select>

    <select id="selectSysDistrictById" parameterType="Long" resultMap="SysDistrictResult">
        <include refid="selectSysDistrictVo"/>
        where id = #{id}
    </select>
    <select id="selectSysDistrictByCode" resultType="com.fastbee.system.domain.SysDistrict">
        <include refid="selectSysDistrictVo"/> where adcode = #{code}
    </select>

    <insert id="insertSysDistrict" parameterType="SysDistrict" useGeneratedKeys="true" keyProperty="id">
        insert into sys_district
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="adcode != null">adcode,</if>
            <if test="level != null">level,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="name != null">name,</if>
            <if test="fullName != null">full_name,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="mapOutline != null">map_outline,</if>
            <if test="mapOutlineUrl != null">map_outline_url,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="adcode != null">#{adcode},</if>
            <if test="level != null">#{level},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="name != null">#{name},</if>
            <if test="fullName != null">#{fullName},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="mapOutline != null">#{mapOutline},</if>
            <if test="mapOutlineUrl != null">#{mapOutlineUrl},</if>
        </trim>
    </insert>

    <update id="updateSysDistrict" parameterType="SysDistrict">
        update sys_district
        <trim prefix="SET" suffixOverrides=",">
            <if test="adcode != null">adcode = #{adcode},</if>
            <if test="level != null">level = #{level},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="fullName != null">full_name = #{fullName},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="mapOutline != null">map_outline = #{mapOutline},</if>
            <if test="mapOutlineUrl != null">map_outline_url = #{mapOutlineUrl},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysDistrictById" parameterType="Long">
        delete from sys_district where id = #{id}
    </delete>

    <delete id="deleteSysDistrictByIds" parameterType="String">
        delete from sys_district where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>