<?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="org.xyzh.system.mapper.acl.TbSysAclPolicyMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysAclPolicyDTO">
        <!-- ACL 策略字段 -->
        <id column="policy_id" property="policyId" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="object_type" property="objectType" jdbcType="VARCHAR"/>
        <result column="edit_hierarchy_rule" property="editHierarchyRule" jdbcType="VARCHAR"/>
        <result column="view_hierarchy_rule" property="viewHierarchyRule" jdbcType="VARCHAR"/>
        <result column="default_permission" property="defaultPermission" jdbcType="INTEGER"/>
        <result column="default_allow" property="defaultAllow" jdbcType="BOOLEAN"/>
        <result column="apply_to_children" property="applyToChildren" jdbcType="BOOLEAN"/>
        <!-- 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- VO结果映射（用于前端展示） -->
    <resultMap id="AclPolicyVOResultMap" type="org.xyzh.api.system.vo.AclVO">
        <!-- ACL 策略字段 -->
        <id column="policy_id" property="policyId" jdbcType="VARCHAR"/>
        <result column="name" property="policyName" jdbcType="VARCHAR"/>
        <result column="object_type" property="policyObjectType" jdbcType="VARCHAR"/>
        <result column="edit_hierarchy_rule" property="editHierarchyRule" jdbcType="VARCHAR"/>
        <result column="view_hierarchy_rule" property="viewHierarchyRule" jdbcType="VARCHAR"/>
        <result column="default_permission" property="defaultPermission" jdbcType="INTEGER"/>
        <result column="default_allow" property="defaultAllow" jdbcType="BOOLEAN"/>
        <result column="apply_to_children" property="applyToChildren" jdbcType="BOOLEAN"/>
        <!-- BaseVO 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        policy_id, name, object_type, edit_hierarchy_rule, view_hierarchy_rule, default_permission, default_allow, apply_to_children,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统访问控制策略（必填 + 可选字段动态列，仅修改 insert） -->
    <insert id="insertAclPolicy" parameterType="org.xyzh.common.dto.sys.TbSysAclPolicyDTO">
        INSERT INTO sys.tb_sys_acl_policy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段：策略主字段 + optsn -->
            policy_id,
            name,
            object_type,
            edit_hierarchy_rule,
            view_hierarchy_rule,
            default_permission,
            default_allow,
            apply_to_children,
            optsn,
            <!-- 可选字段：根据是否有值动态拼接 -->
            <if test="creator != null and creator != ''">creator,</if>
            <if test="deptPath != null and deptPath != ''">dept_path,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleteTime != null">delete_time,</if>
            <if test="deleted != null">deleted,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段值 -->
            #{policyId},
            #{name},
            #{objectType},
            #{editHierarchyRule},
            #{viewHierarchyRule},
            #{defaultPermission},
            #{defaultAllow},
            #{applyToChildren},
            #{optsn},
            <!-- 可选字段值 -->
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="deptPath != null and deptPath != ''">#{deptPath},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleteTime != null">#{deleteTime},</if>
            <if test="deleted != null">#{deleted},</if>
        </trim>
    </insert>

    <!-- 更新系统访问控制策略 -->
    <update id="updateAclPolicy" parameterType="org.xyzh.common.dto.sys.TbSysAclPolicyDTO">
        UPDATE sys.tb_sys_acl_policy
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="objectType != null and objectType != ''">
                object_type = #{objectType},
            </if>
            <if test="editHierarchyRule != null and editHierarchyRule != ''">
                edit_hierarchy_rule = #{editHierarchyRule},
            </if>
            <if test="viewHierarchyRule != null and viewHierarchyRule != ''">
                view_hierarchy_rule = #{viewHierarchyRule},
            </if>
            <if test="defaultPermission != null">
                default_permission = #{defaultPermission},
            </if>
            <if test="defaultAllow != null">
                default_allow = #{defaultAllow},
            </if>
            <if test="applyToChildren != null">
                apply_to_children = #{applyToChildren},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="deptPath != null and deptPath != ''">
                dept_path = #{deptPath},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE policy_id = #{policyId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统访问控制策略 -->
    <update id="deleteAclPolicy" parameterType="org.xyzh.common.dto.sys.TbSysAclPolicyDTO">
        DELETE FROM sys.tb_sys_acl_policy
        WHERE policy_id = #{policyId}
    </update>

    <!-- 根据ID查询系统访问控制策略 -->
    <select id="getAclPolicyById" resultMap="AclPolicyVOResultMap" parameterType="java.lang.String">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl_policy
        WHERE policy_id = #{policyId}
        AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 根据条件查询系统访问控制策略列表 -->
    <select id="getAclPolicyByFilter" resultMap="AclPolicyVOResultMap" parameterType="org.xyzh.common.dto.sys.TbSysAclPolicyDTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl_policy
        <where>
            <if test="filter.policyId != null and filter.policyId != ''">
                AND policy_id = #{filter.policyId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.objectType != null and filter.objectType != ''">
                AND object_type = #{filter.objectType}
            </if>
            <if test="filter.editHierarchyRule != null and filter.editHierarchyRule != ''">
                AND edit_hierarchy_rule = #{filter.editHierarchyRule}
            </if>
            <if test="filter.viewHierarchyRule != null and filter.viewHierarchyRule != ''">
                AND view_hierarchy_rule = #{filter.viewHierarchyRule}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 根据条件查询系统访问控制策略分页列表 -->
    <select id="getAclPolicyPageByFilter" resultMap="AclPolicyVOResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl_policy
        <where>
            <if test="filter.policyId != null and filter.policyId != ''">
                AND policy_id = #{filter.policyId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.objectType != null and filter.objectType != ''">
                AND object_type = #{filter.objectType}
            </if>
            <if test="filter.editHierarchyRule != null and filter.editHierarchyRule != ''">
                AND edit_hierarchy_rule = #{filter.editHierarchyRule}
            </if>
            <if test="filter.viewHierarchyRule != null and filter.viewHierarchyRule != ''">
                AND view_hierarchy_rule = #{filter.viewHierarchyRule}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统访问控制策略数量 -->
    <select id="getAclPolicyCount" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysAclPolicyDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_acl_policy
        <where>
            <if test="filter.policyId != null and filter.policyId != ''">
                AND policy_id = #{filter.policyId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.objectType != null and filter.objectType != ''">
                AND object_type = #{filter.objectType}
            </if>
            <if test="filter.editHierarchyRule != null and filter.editHierarchyRule != ''">
                AND edit_hierarchy_rule = #{filter.editHierarchyRule}
            </if>
            <if test="filter.viewHierarchyRule != null and filter.viewHierarchyRule != ''">
                AND view_hierarchy_rule = #{filter.viewHierarchyRule}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

</mapper>
