<?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.BladeRegionMapper">
    
    <resultMap type="com.ruoyi.system.domain.BladeRegion" id="BladeRegionResult">
        <result property="code"    column="code"    />
        <result property="parentCode"    column="parent_code"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="name"    column="name"    />
        <result property="zipCode"    column="zip_code"    />
        <result property="provinceCode"    column="province_code"    />
        <result property="provinceName"    column="province_name"    />
        <result property="cityCode"    column="city_code"    />
        <result property="cityName"    column="city_name"    />
        <result property="districtCode"    column="district_code"    />
        <result property="districtName"    column="district_name"    />
        <result property="townCode"    column="town_code"    />
        <result property="townName"    column="town_name"    />
        <result property="villageCode"    column="village_code"    />
        <result property="villageName"    column="village_name"    />
        <result property="regionLevel"    column="region_level"    />
        <result property="sort"    column="sort"    />
        <result property="remark"    column="remark"    />
        <result property="districtShortName"    column="district_short_name"    />
        <result property="cityShortName"    column="city_short_name"    />
    </resultMap>

    <sql id="selectBladeRegionVo">
        select code, parent_code, ancestors, name, zip_code, province_code, province_name, city_code, city_name, district_code, district_name, town_code, town_name, village_code, village_name, region_level, sort, remark, district_short_name, city_short_name from blade_region
    </sql>
    
    <select id="selectAll" parameterType="com.ruoyi.system.domain.BladeRegion" resultMap="BladeRegionResult">
<include refid="selectBladeRegionVo"/> order by sort
    </select>


<select id="selectList" parameterType="com.ruoyi.system.domain.BladeRegion" resultMap="BladeRegionResult">
        select
          code, parent_code, ancestors, name, zip_code, province_code, province_name, city_code, city_name,
          district_code, district_name, town_code, town_name, village_code, village_name, region_level, sort, remark, district_short_name,
           city_short_name
        from blade_region
        <where> 
        <if test="code != null  and code != ''"> and code = #{code}</if>
        <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
        <if test="name != null  and name != ''"> and name =  #{name}</if>
        <if test="zipCode != null  and zipCode != ''"> and zip_code = #{zipCode}</if>
        <if test="provinceCode != null  and provinceCode != ''"> and province_code = #{provinceCode}</if>
        <if test="provinceName != null  and provinceName != ''"> and province_name =  #{provinceName}</if>
        <if test="cityName != null  and cityName != ''"> and city_name = #{cityName}</if>
        <if test="cityShortName != null  and cityShortName != ''"> and city_short_name = #{cityShortName}</if>
        <if test="districtCode != null  and districtCode != ''"> and district_code = #{districtCode}</if>
       </where>
        
        order by code
    </select>
    
    
      <select id="selectAllAreaList"   resultMap="BladeRegionResult">
     select
          code, parent_code, ancestors, name, zip_code, province_code, province_name, city_code, city_name,
          district_code, district_name, town_code, town_name, village_code, village_name, region_level, sort, remark, district_short_name,
            city_short_name
        from blade_region
        where region_level=3 
    </select>
    
    <select id="selectAllCityList"   resultMap="BladeRegionResult">
     select
          code, parent_code, ancestors, name, zip_code, province_code, province_name, city_code, city_name,
          district_code, district_name, town_code, town_name, village_code, village_name, region_level, sort, remark, district_short_name,
            city_short_name
        from blade_region
        where region_level=2  order by code
    </select>
    
    <select id="selectBladeRegionList" parameterType="com.ruoyi.system.domain.BladeRegion" resultMap="BladeRegionResult">
        select
          code, parent_code, ancestors, name, zip_code, province_code, province_name, city_code, city_name,
          district_code, district_name, town_code, town_name, village_code, village_name, region_level, sort, remark, district_short_name,
          (case when parent_code=53 then CONCAT('-',city_short_name) else city_short_name end) city_short_name
        from blade_region
        where 1=1 
        
         <if test="parentCode != null  and parentCode != ''"> and parent_code =  #{parentCode}</if>
          <if test="parentCode == null  or parentCode == ''"> and (parent_code='00' or parent_code='53')  </if>
         
        order by code
        <!--<where>  -->
            <!--<if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>-->
            <!--<if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>-->
            <!--<if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>-->
            <!--<if test="zipCode != null  and zipCode != ''"> and zip_code = #{zipCode}</if>-->
            <!--<if test="provinceCode != null  and provinceCode != ''"> and province_code = #{provinceCode}</if>-->
            <!--<if test="provinceName != null  and provinceName != ''"> and province_name like concat('%', #{provinceName}, '%')</if>-->
            <!--<if test="cityCode != null  and cityCode != ''"> and city_code = #{cityCode}</if>-->
            <!--<if test="cityName != null  and cityName != ''"> and city_name like concat('%', #{cityName}, '%')</if>-->
            <!--<if test="districtCode != null  and districtCode != ''"> and district_code = #{districtCode}</if>-->
            <!--<if test="districtName != null  and districtName != ''"> and district_name like concat('%', #{districtName}, '%')</if>-->
            <!--<if test="townCode != null  and townCode != ''"> and town_code = #{townCode}</if>-->
            <!--<if test="townName != null  and townName != ''"> and town_name like concat('%', #{townName}, '%')</if>-->
            <!--<if test="villageCode != null  and villageCode != ''"> and village_code = #{villageCode}</if>-->
            <!--<if test="villageName != null  and villageName != ''"> and village_name like concat('%', #{villageName}, '%')</if>-->
            <!--<if test="regionLevel != null "> and region_level = #{regionLevel}</if>-->
            <!--<if test="sort != null "> and sort = #{sort}</if>-->
            <!--<if test="districtShortName != null  and districtShortName != ''"> and district_short_name like concat('%', #{districtShortName}, '%')</if>-->
            <!--<if test="cityShortName != null  and cityShortName != ''"> and city_short_name like concat('%', #{cityShortName}, '%')</if>-->
        <!--</where>-->
    </select>
    
    <select id="selectBladeRegionByCode" parameterType="java.lang.String" resultMap="BladeRegionResult">
        <include refid="selectBladeRegionVo"/>
        where code = #{code}
    </select>
        
    <insert id="insertBladeRegion" parameterType="com.ruoyi.system.domain.BladeRegion">
        insert into blade_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="parentCode != null">parent_code,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="name != null">name,</if>
            <if test="zipCode != null">zip_code,</if>
            <if test="provinceCode != null">province_code,</if>
            <if test="provinceName != null">province_name,</if>
            <if test="cityCode != null">city_code,</if>
            <if test="cityName != null">city_name,</if>
            <if test="districtCode != null">district_code,</if>
            <if test="districtName != null">district_name,</if>
            <if test="townCode != null">town_code,</if>
            <if test="townName != null">town_name,</if>
            <if test="villageCode != null">village_code,</if>
            <if test="villageName != null">village_name,</if>
            <if test="regionLevel != null">region_level,</if>
            <if test="sort != null">sort,</if>
            <if test="remark != null">remark,</if>
            <if test="districtShortName != null">district_short_name,</if>
            <if test="cityShortName != null">city_short_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="parentCode != null">#{parentCode},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="name != null">#{name},</if>
            <if test="zipCode != null">#{zipCode},</if>
            <if test="provinceCode != null">#{provinceCode},</if>
            <if test="provinceName != null">#{provinceName},</if>
            <if test="cityCode != null">#{cityCode},</if>
            <if test="cityName != null">#{cityName},</if>
            <if test="districtCode != null">#{districtCode},</if>
            <if test="districtName != null">#{districtName},</if>
            <if test="townCode != null">#{townCode},</if>
            <if test="townName != null">#{townName},</if>
            <if test="villageCode != null">#{villageCode},</if>
            <if test="villageName != null">#{villageName},</if>
            <if test="regionLevel != null">#{regionLevel},</if>
            <if test="sort != null">#{sort},</if>
            <if test="remark != null">#{remark},</if>
            <if test="districtShortName != null">#{districtShortName},</if>
            <if test="cityShortName != null">#{cityShortName},</if>
         </trim>
    </insert>

    <update id="updateBladeRegion" parameterType="com.ruoyi.system.domain.BladeRegion">
        update blade_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentCode != null">parent_code = #{parentCode},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="name != null">name = #{name},</if>
            <if test="zipCode != null">zip_code = #{zipCode},</if>
            <if test="provinceCode != null">province_code = #{provinceCode},</if>
            <if test="provinceName != null">province_name = #{provinceName},</if>
            <if test="cityCode != null">city_code = #{cityCode},</if>
            <if test="cityName != null">city_name = #{cityName},</if>
            <if test="districtCode != null">district_code = #{districtCode},</if>
            <if test="districtName != null">district_name = #{districtName},</if>
            <if test="townCode != null">town_code = #{townCode},</if>
            <if test="townName != null">town_name = #{townName},</if>
            <if test="villageCode != null">village_code = #{villageCode},</if>
            <if test="villageName != null">village_name = #{villageName},</if>
            <if test="regionLevel != null">region_level = #{regionLevel},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="districtShortName != null">district_short_name = #{districtShortName},</if>
            <if test="cityShortName != null">city_short_name = #{cityShortName},</if>
        </trim>
        where code = #{code}
    </update>

    <delete id="deleteBladeRegionByCode" parameterType="java.lang.String">
        delete from blade_region where code = #{code}
    </delete>

    <delete id="deleteBladeRegionByCodes" parameterType="java.lang.String">
        delete from blade_region where code in 
        <foreach item="code" collection="array" open="(" separator="," close=")">
            #{code}
        </foreach>
    </delete>

    <!-- 逻辑删除 -->
    <update id="setDeleteById">
        update blade_region set delete_flag="Y",update_time=sysdate(),update_user_id=#{operId},update_user_name=#{operName} where code = #{code}
    </update>

    <!-- 逻辑删除 -->
    <update id="setDeleteByIds">
        update blade_region set delete_flag="Y",update_time=sysdate(),update_user_id=#{operId},update_user_name=#{operName} where code in
        <foreach item="code" collection="codes" open="(" separator="," close=")">
            #{code}
        </foreach>
    </update>
</mapper>