package com.mosquito.project.permission;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 权限Repository
 */
@Repository
public interface PermissionRepository extends JpaRepository<SysPermission, Long> {

    /**
     * 根据权限代码查询
     */
    Optional<SysPermission> findByPermissionCode(String permissionCode);

    /**
     * 根据模块代码查询
     */
    List<SysPermission> findByModuleCode(String moduleCode);

    /**
     * 检查权限代码是否存在
     */
    boolean existsByPermissionCode(String permissionCode);

    /**
     * 根据角色ID列表查询权限
     */
    @Query("SELECT DISTINCT p FROM SysPermission p JOIN SysRolePermission rp ON p.id = rp.permissionId WHERE rp.roleId IN :roleIds")
    List<SysPermission> findByRoleIds(@Param("roleIds") List<Long> roleIds);

    /**
     * 根据权限代码列表查询
     */
    List<SysPermission> findByPermissionCodeIn(List<String> permissionCodes);
}
