<?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.zehong.system.mapper.TTownLevelRegionMapper">
    
    <resultMap type="TTownLevelRegion" id="TTownLevelRegionResult">
        <result property="fId"    column="f_id"    />
        <result property="fCountyId"    column="f_county_id"    />
        <result property="fTownCode"    column="f_town_code"    />
        <result property="fName"    column="f_name"    />
    </resultMap>

    <sql id="selectTTownLevelRegionVo">
        select f_id, f_county_id, f_town_code, f_name from t_town_level_region
    </sql>

    <select id="selectTTownLevelRegionList" parameterType="TTownLevelRegion" resultMap="TTownLevelRegionResult">
        <include refid="selectTTownLevelRegionVo"/>
        <where>  
            <if test="fCountyId != null "> and f_county_id = #{fCountyId}</if>
            <if test="fTownCode != null  and fTownCode != ''"> and f_town_code = #{fTownCode}</if>
            <if test="fName != null  and fName != ''"> and f_name like concat('%', #{fName}, '%')</if>
        </where>
    </select>
    
    <select id="selectTTownLevelRegionById" parameterType="Long" resultMap="TTownLevelRegionResult">
        <include refid="selectTTownLevelRegionVo"/>
        where f_id = #{fId}
    </select>
        
    <insert id="insertTTownLevelRegion" parameterType="TTownLevelRegion">
        insert into t_town_level_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fId != null">f_id,</if>
            <if test="fCountyId != null">f_county_id,</if>
            <if test="fTownCode != null and fTownCode != ''">f_town_code,</if>
            <if test="fName != null and fName != ''">f_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fId != null">#{fId},</if>
            <if test="fCountyId != null">#{fCountyId},</if>
            <if test="fTownCode != null and fTownCode != ''">#{fTownCode},</if>
            <if test="fName != null and fName != ''">#{fName},</if>
         </trim>
    </insert>

    <update id="updateTTownLevelRegion" parameterType="TTownLevelRegion">
        update t_town_level_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="fCountyId != null">f_county_id = #{fCountyId},</if>
            <if test="fTownCode != null and fTownCode != ''">f_town_code = #{fTownCode},</if>
            <if test="fName != null and fName != ''">f_name = #{fName},</if>
        </trim>
        where f_id = #{fId}
    </update>

    <delete id="deleteTTownLevelRegionById" parameterType="Long">
        delete from t_town_level_region where f_id = #{fId}
    </delete>

    <delete id="deleteTTownLevelRegionByIds" parameterType="String">
        delete from t_town_level_region where f_id in 
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>

    <insert id="batchInsertTTownLevelRegion" parameterType="TTownLevelRegion">
        insert into t_town_level_region(
            f_id,
            f_county_id,
            f_town_code,
            f_name
        )VALUES
        <foreach collection="list" separator="," item="item">
            (
                #{fId},
                #{fCountyId},
                #{fTownCode},
                #{fName}
            )
        </foreach>
    </insert>
</mapper>