package com.mosquito.project.permission;

import com.mosquito.project.permission.entity.SysUserPermissionEntity;
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
public interface UserPermissionRepository extends JpaRepository<SysUserPermissionEntity, Long> {

    /**
     * 查询用户直接拥有的权限ID列表
     */
    @Query("SELECT up.permissionId FROM SysUserPermissionEntity up WHERE up.userId = :userId")
    List<Long> findPermissionIdsByUserId(@Param("userId") Long userId);

    /**
     * 查询用户是否拥有指定权限
     */
    boolean existsByUserIdAndPermissionId(Long userId, Long permissionId);

    /**
     * 删除用户的指定权限
     */
    void deleteByUserIdAndPermissionId(Long userId, Long permissionId);

    /**
     * 删除用户的所有权限
     */
    void deleteByUserId(Long userId);

    /**
     * 查找用户权限关联
     */
    Optional<SysUserPermissionEntity> findByUserIdAndPermissionId(Long userId, Long permissionId);
}
