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

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysAclDTO">
        <!-- ACL 字段 -->
        <id column="acl_id" property="aclId" jdbcType="VARCHAR"/>
        <result column="object_type" property="objectType" jdbcType="VARCHAR"/>
        <result column="object_id" property="objectId" jdbcType="VARCHAR"/>
        <result column="principal_type" property="principalType" jdbcType="VARCHAR"/>
        <result column="principal_id" property="principalId" jdbcType="VARCHAR"/>
        <result column="principal_dept_id" property="principalDeptId" jdbcType="VARCHAR"/>
        <result column="permission" property="permission" jdbcType="VARCHAR"/>
        <result column="allow" property="allow" jdbcType="BOOLEAN"/>
        <result column="include_descendants" property="includeDescendants" 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="AclVOResultMap" type="org.xyzh.api.system.vo.AclVO">
        <!-- ACL 字段 -->
        <id column="acl_id" property="aclId" jdbcType="VARCHAR"/>
        <result column="object_type" property="objectType" jdbcType="VARCHAR"/>
        <result column="object_id" property="objectId" jdbcType="VARCHAR"/>
        <result column="principal_type" property="principalType" jdbcType="VARCHAR"/>
        <result column="principal_id" property="principalId" jdbcType="VARCHAR"/>
        <result column="principal_dept_id" property="principalDeptId" jdbcType="VARCHAR"/>
        <result column="permission" property="permission" jdbcType="INTEGER"/>
        <result column="allow" property="allow" jdbcType="BOOLEAN"/>
        <result column="include_descendants" property="includeDescendants" 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">
        acl_id, object_type, object_id, principal_type, principal_id, principal_dept_id, permission, allow, include_descendants,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统访问控制列表（必填 + 可选字段动态列，仅修改 insert） -->
    <insert id="insertAcl" parameterType="org.xyzh.common.dto.sys.TbSysAclDTO">
        INSERT INTO sys.tb_sys_acl
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段：ACL 主字段 + optsn -->
            acl_id,
            object_type,
            object_id,
            principal_type,
            principal_id,
            principal_dept_id,
            permission,
            allow,
            include_descendants,
            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=",">
            <!-- 必填字段值 -->
            #{aclId},
            #{objectType},
            #{objectId},
            #{principalType},
            #{principalId},
            #{principalDeptId},
            #{permission},
            #{allow},
            #{includeDescendants},
            #{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="updateAcl" parameterType="org.xyzh.common.dto.sys.TbSysAclDTO">
        UPDATE sys.tb_sys_acl
        <set>
            <if test="objectType != null and objectType != ''">
                object_type = #{objectType},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="principalId != null and principalId != ''">
                principal_id = #{principalId},
            </if>
            <if test="principalDeptId != null and principalDeptId != ''">
                principal_dept_id = #{principalDeptId},
            </if>
            <if test="permission != null and permission != ''">
                permission = #{permission},
            </if>
            <if test="allow != null">
                allow = #{allow},
            </if>
            <if test="includeDescendants != null">
                include_descendants = #{includeDescendants},
            </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 acl_id = #{aclId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统访问控制列表 -->
    <update id="deleteAcl" parameterType="org.xyzh.common.dto.sys.TbSysAclDTO">
        DELETE FROM sys.tb_sys_acl
        WHERE acl_id = #{aclId}
    </update>

    <!-- 根据ID查询系统访问控制列表 -->
    <select id="getAclById" resultMap="AclVOResultMap" parameterType="java.lang.String">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl
        WHERE acl_id = #{aclId}
        AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 根据对象ID查询系统访问控制列表 -->
    <select id="getAclByObjectId" resultMap="AclVOResultMap" parameterType="java.lang.String">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl
        WHERE object_id = #{objectId}
        AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 根据条件查询系统访问控制列表列表 -->
    <select id="getAclByFilter" resultMap="AclVOResultMap" parameterType="org.xyzh.common.dto.sys.TbSysAclDTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl
        <where>
            <if test="filter.aclId != null and filter.aclId != ''">
                AND acl_id = #{filter.aclId}
            </if>
            <if test="filter.objectType != null and filter.objectType != ''">
                AND object_type LIKE CONCAT('%', #{filter.objectType}, '%')
            </if>
            <if test="filter.objectId != null and filter.objectId != ''">
                AND object_id LIKE CONCAT('%', #{filter.objectId}, '%')
            </if>
            <if test="filter.principalType != null and filter.principalType != ''">
                AND principal_type LIKE CONCAT('%', #{filter.principalType}, '%')
            </if>
            <if test="filter.principalId != null and filter.principalId != ''">
                AND principal_id LIKE CONCAT('%', #{filter.principalId}, '%')
            </if>
            <if test="filter.principalDeptId != null and filter.principalDeptId != ''">
                AND principal_dept_id LIKE CONCAT('%', #{filter.principalDeptId}, '%')
            </if>
            <if test="filter.permission != null and filter.permission != ''">
                AND permission LIKE CONCAT('%', #{filter.permission}, '%')
            </if>
            <if test="filter.allow != null">
                AND allow = #{filter.allow}
            </if>
            <if test="filter.includeDescendants != null">
                AND include_descendants = #{filter.includeDescendants}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 根据条件查询系统访问控制列表分页列表 -->
    <select id="getAclPageByFilter" resultMap="AclVOResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_acl
        <where>
            <if test="filter.aclId != null and filter.aclId != ''">
                AND acl_id = #{filter.aclId}
            </if>
            <if test="filter.objectType != null and filter.objectType != ''">
                AND object_type LIKE CONCAT('%', #{filter.objectType}, '%')
            </if>
            <if test="filter.objectId != null and filter.objectId != ''">
                AND object_id LIKE CONCAT('%', #{filter.objectId}, '%')
            </if>
            <if test="filter.principalType != null and filter.principalType != ''">
                AND principal_type LIKE CONCAT('%', #{filter.principalType}, '%')
            </if>
            <if test="filter.principalId != null and filter.principalId != ''">
                AND principal_id LIKE CONCAT('%', #{filter.principalId}, '%')
            </if>
            <if test="filter.principalDeptId != null and filter.principalDeptId != ''">
                AND principal_dept_id LIKE CONCAT('%', #{filter.principalDeptId}, '%')
            </if>
            <if test="filter.permission != null and filter.permission != ''">
                AND permission LIKE CONCAT('%', #{filter.permission}, '%')
            </if>
            <if test="filter.allow != null">
                AND allow = #{filter.allow}
            </if>
            <if test="filter.includeDescendants != null">
                AND include_descendants = #{filter.includeDescendants}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统访问控制列表数量 -->
    <select id="getAclCountByFilter" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysAclDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_acl
        <where>
            <if test="filter.aclId != null and filter.aclId != ''">
                AND acl_id = #{filter.aclId}
            </if>
            <if test="filter.objectType != null and filter.objectType != ''">
                AND object_type LIKE CONCAT('%', #{filter.objectType}, '%')
            </if>
            <if test="filter.objectId != null and filter.objectId != ''">
                AND object_id LIKE CONCAT('%', #{filter.objectId}, '%')
            </if>
            <if test="filter.principalType != null and filter.principalType != ''">
                AND principal_type LIKE CONCAT('%', #{filter.principalType}, '%')
            </if>
            <if test="filter.principalId != null and filter.principalId != ''">
                AND principal_id LIKE CONCAT('%', #{filter.principalId}, '%')
            </if>
            <if test="filter.principalDeptId != null and filter.principalDeptId != ''">
                AND principal_dept_id LIKE CONCAT('%', #{filter.principalDeptId}, '%')
            </if>
            <if test="filter.permission != null and filter.permission != ''">
                AND permission LIKE CONCAT('%', #{filter.permission}, '%')
            </if>
            <if test="filter.allow != null">
                AND allow = #{filter.allow}
            </if>
            <if test="filter.includeDescendants != null">
                AND include_descendants = #{filter.includeDescendants}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

</mapper>