<?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.ruoyi.system.mapper.TAdministrativeDivisionsMapper">

    <resultMap type="TAdministrativeDivisions" id="TAdministrativeDivisionsResult">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="streetCode" column="streetCode"/>
        <result property="areaCode" column="areaCode"/>
        <result property="cityCode" column="cityCode"/>
        <result property="provinceCode" column="provinceCode"/>
    </resultMap>

    <sql id="selectTAdministrativeDivisionsVo">
        select id, code, name, streetCode, areaCode, cityCode, provinceCode
        from t_administrative_divisions
    </sql>

    <select id="selectTAdministrativeDivisionsList" parameterType="TAdministrativeDivisions"
            resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        <where>
            <if test="code != null ">and code = #{code}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="streetCode != null ">and streetCode = #{streetCode}</if>
            <if test="areaCode != null ">and areaCode = #{areaCode}</if>
            <if test="cityCode != null ">and cityCode = #{cityCode}</if>
            <if test="provinceCode != null ">and provinceCode = #{provinceCode}</if>
        </where>
    </select>

    <select id="selectTAdministrativeDivisionsById" parameterType="Long" resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        where id = #{id}
    </select>
    <!-- 查询所有省级级（省） -->
    <select id="selectTAdministrativeDivisionsProvinceAllList" resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        where provinceCode is null
    </select>
    <!-- 查询所有地级（市） -->
    <select id="selectTAdministrativeDivisionsListByProvinceCode" parameterType="Long"
            resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        where cityCode is null
        AND
        provinceCode = #{provinceCode}
    </select>
    <!-- 查询所有县级（区） -->
    <select id="selectTAdministrativeDivisionsListByCityCode" parameterType="Long"
            resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        where areaCode is null
        AND
        cityCode = #{cityCode}
    </select>
    <!-- 查询所有乡级（镇） -->
    <select id="selectTAdministrativeDivisionsListByAreaCode" parameterType="Long"
            resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        where streetCode is null
        AND
        areaCode = #{areaCode}
    </select>
    <!-- 查询所有居委会 -->
    <select id="selectTAdministrativeDivisionsListByStreetCode" parameterType="Long"
            resultMap="TAdministrativeDivisionsResult">
        <include refid="selectTAdministrativeDivisionsVo"/>
        where streetCode = #{streetCode}
    </select>

    <insert id="insertTAdministrativeDivisions" parameterType="TAdministrativeDivisions" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_administrative_divisions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="streetCode != null">streetCode,</if>
            <if test="areaCode != null">areaCode,</if>
            <if test="cityCode != null">cityCode,</if>
            <if test="provinceCode != null">provinceCode,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="streetCode != null">#{streetCode},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="cityCode != null">#{cityCode},</if>
            <if test="provinceCode != null">#{provinceCode},</if>
        </trim>
    </insert>

    <update id="updateTAdministrativeDivisions" parameterType="TAdministrativeDivisions">
        update t_administrative_divisions
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="streetCode != null">streetCode = #{streetCode},</if>
            <if test="areaCode != null">areaCode = #{areaCode},</if>
            <if test="cityCode != null">cityCode = #{cityCode},</if>
            <if test="provinceCode != null">provinceCode = #{provinceCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTAdministrativeDivisionsById" parameterType="Long">
        delete
        from t_administrative_divisions
        where id = #{id}
    </delete>

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