<?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.pangu.base.mapper.RegionMapper">

    <resultMap id="RegionResult" type="com.pangu.base.domain.Region">
        <id property="regionId" column="region_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="regionName" column="region_name"/>
        <result property="regionCode" column="region_code"/>
        <result property="level" column="level"/>
        <result property="ancestors" column="ancestors"/>
        <result property="orderNum" column="order_num"/>
        <result property="status" column="status"/>
        <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>

    <sql id="selectRegionVo">
        SELECT region_id, parent_id, region_name, region_code, level, ancestors,
               order_num, status, create_by, create_time, update_by, update_time
        FROM pg_region
        WHERE del_flag = '0'
    </sql>

    <select id="selectRegionList" parameterType="com.pangu.base.domain.Region" resultMap="RegionResult">
        <include refid="selectRegionVo"/>
        <if test="regionName != null and regionName != ''">
            AND region_name LIKE CONCAT('%', #{regionName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ORDER BY order_num
    </select>

    <select id="selectRegionById" parameterType="Long" resultMap="RegionResult">
        <include refid="selectRegionVo"/>
        AND region_id = #{regionId}
    </select>

    <insert id="insertRegion" parameterType="com.pangu.base.domain.Region" useGeneratedKeys="true" keyProperty="regionId">
        INSERT INTO pg_region (
            parent_id, region_name, region_code, level, ancestors,
            order_num, status, create_by, create_time, del_flag
        ) VALUES (
            #{parentId}, #{regionName}, #{regionCode}, #{level}, #{ancestors},
            #{orderNum}, #{status}, #{createBy}, #{createTime}, '0'
        )
    </insert>

    <update id="updateRegion" parameterType="com.pangu.base.domain.Region">
        UPDATE pg_region
        <set>
            <if test="regionName != null and regionName != ''">region_name = #{regionName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            update_by = #{updateBy},
            update_time = #{updateTime}
        </set>
        WHERE region_id = #{regionId}
    </update>

    <update id="deleteRegionById" parameterType="Long">
        UPDATE pg_region SET del_flag = '1' WHERE region_id = #{regionId}
    </update>

    <!-- 获取区域路径：通过ancestors字段递归查询 -->
    <select id="selectRegionPath" parameterType="Long" resultType="String">
        SELECT GROUP_CONCAT(region_name ORDER BY level SEPARATOR '-')
        FROM pg_region
        WHERE del_flag = '0'
        AND FIND_IN_SET(region_id, (
            SELECT CONCAT(ancestors, ',', region_id)
            FROM pg_region
            WHERE region_id = #{regionId} AND del_flag = '0'
        ))
    </select>

</mapper>
