<?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.zhentao.mapper.PermissionMapper">
    
    <!-- 根据角色ID查询权限列表 -->
    <select id="selectPermissionsByRoleId" resultType="com.zhentao.entity.Permission">
        SELECT p.*
        FROM admin_permission p
        INNER JOIN admin_role_permission rp ON p.id = rp.permission_id
        WHERE rp.role_id = #{roleId}
        AND p.status = 1
        ORDER BY p.sort_order, p.create_time
    </select>
    
    <!-- 根据用户ID查询权限列表 -->
    <select id="selectPermissionsByUserId" resultType="com.zhentao.entity.Permission">
        SELECT DISTINCT p.*
        FROM admin_permission p
        INNER JOIN admin_role_permission rp ON p.id = rp.permission_id
        INNER JOIN admin_user_role ur ON rp.role_id = ur.role_id
        WHERE ur.user_id = #{userId}
        AND p.status = 1
        ORDER BY p.sort_order, p.create_time
    </select>
    
    <!-- 根据路径和方法查询权限 -->
    <select id="selectByPathAndMethod" resultType="com.zhentao.entity.Permission">
        SELECT *
        FROM admin_permission
        WHERE path = #{path}
        AND (method = #{method} OR method = 'ALL')
        AND status = 1
        LIMIT 1
    </select>
    
</mapper>

