<?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.TRegionMapper">

    <resultMap type="TRegion" id="TRegionResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="deptName" column="dept_name"/>
        <result property="deptNum" column="dept_num"/>
        <result property="zoneName" column="zone_name"/>
        <result property="zoneNum" column="zone_num"/>
        <result property="kind" column="kind"/>
        <result property="orderNum" column="order_num"/>
        <result property="remarks" column="remarks"/>
        <result property="gpsLng" column="gps_lng"/>
        <result property="gpsLat" column="gps_lat"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="TRegionVo" id="TRegionVoResult">
        <result property="code" column="code"/>
        <result property="name" column="name"/>
    </resultMap>

    <sql id="selectTRegionVo">
        select id,
               parent_id,
               ancestors,
               dept_name,
               dept_num,
               zone_name,
               zone_num,
               kind,
               order_num,
               remarks,
               gps_lng,
               gps_lat,
               create_by,
               create_time,
               update_by,
               update_time
        from t_region
    </sql>

    <select id="selectTRegionList" parameterType="TRegion" resultMap="TRegionResult">
        <include refid="selectTRegionVo"/>
        <where>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="deptName != null  and deptName != ''">and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="deptNum != null  and deptNum != ''">and dept_num = #{deptNum}</if>
            <if test="zoneName != null  and zoneName != ''">and zone_name like concat('%', #{zoneName}, '%')</if>
            <if test="zoneNum != null  and zoneNum != ''">and zone_num = #{zoneNum}</if>
            <if test="kind != null ">and kind = #{kind}</if>
            <if test="orderNum != null ">and order_num = #{orderNum}</if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
            <if test="gpsLng != null ">and gps_lng = #{gpsLng}</if>
            <if test="gpsLat != null ">and gps_lat = #{gpsLat}</if>
        </where>
    </select>

    <select id="selectTRegionById" parameterType="Integer" resultMap="TRegionResult">
        <include refid="selectTRegionVo"/>
        where id = #{id}
    </select>

    <select id="selectTRegionByParentId" parameterType="Integer" resultMap="TRegionResult">
        <include refid="selectTRegionVo"/>
        where parent_id = #{parentId}
    </select>
    <select id="selectTRegionListV1" resultMap="TRegionVoResult">
        SELECT DISTINCT SUBSTRING(province_code, 1, 2) AS code,
                        province_name                  AS name
        FROM position_village;
    </select>
    <select id="selectTRegionListV2" parameterType="string" resultMap="TRegionVoResult">
        SELECT DISTINCT SUBSTRING(city_code, 1, 4) AS code,
                        city_name                  AS name
        FROM position_village
        WHERE SUBSTRING(position_village.city_code, 1, 2) = #{province_code}
    </select>
    <select id="selectTRegionListV3" parameterType="string" resultMap="TRegionVoResult">
        SELECT DISTINCT SUBSTRING(county_code, 1,6) AS code,
                        county_name                  AS name
        FROM position_village
        WHERE SUBSTRING(position_village.city_code, 1, 4) = #{city_code}
    </select>
    <select id="selectTRegionListV4" parameterType="string" resultMap="TRegionVoResult">
        SELECT DISTINCT SUBSTRING(town_code, 1,8) AS code,
                        town_name                  AS name
        FROM position_village
        WHERE SUBSTRING(position_village.county_code, 1, 6) = #{county_code}
    </select>
    <select id="selectTRegionListV5" parameterType="string" resultMap="TRegionVoResult">
        SELECT DISTINCT SUBSTRING(village_code, 1, 10) AS code,
                        village_name                   AS name
        FROM position_village
        WHERE SUBSTRING(position_village.county_code, 1, 8) = #{county_code}
    </select>

    <insert id="insertTRegion" parameterType="TRegion" useGeneratedKeys="true" keyProperty="id">
        insert into t_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="deptNum != null">dept_num,</if>
            <if test="zoneName != null">zone_name,</if>
            <if test="zoneNum != null">zone_num,</if>
            <if test="kind != null">kind,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="remarks != null">remarks,</if>
            <if test="gpsLng != null">gps_lng,</if>
            <if test="gpsLat != null">gps_lat,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="deptNum != null">#{deptNum},</if>
            <if test="zoneName != null">#{zoneName},</if>
            <if test="zoneNum != null">#{zoneNum},</if>
            <if test="kind != null">#{kind},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="gpsLng != null">#{gpsLng},</if>
            <if test="gpsLat != null">#{gpsLat},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateTRegion" parameterType="TRegion">
        update t_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="deptNum != null">dept_num = #{deptNum},</if>
            <if test="zoneName != null">zone_name = #{zoneName},</if>
            <if test="zoneNum != null">zone_num = #{zoneNum},</if>
            <if test="kind != null">kind = #{kind},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="gpsLng != null">gps_lng = #{gpsLng},</if>
            <if test="gpsLat != null">gps_lat = #{gpsLat},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTRegionById" parameterType="Integer">
        delete
        from t_region
        where id = #{id}
    </delete>

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