package cn.novalon.gym.manage.db.dao;

import cn.novalon.gym.manage.db.entity.SysRolePermissionEntity;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Repository
public interface SysRolePermissionDao extends R2dbcRepository<SysRolePermissionEntity, Long> {

    Flux<SysRolePermissionEntity> findByRoleId(Long roleId);

    Flux<SysRolePermissionEntity> findByPermissionId(Long permissionId);

    Flux<Long> findPermissionIdsByRoleId(Long roleId);

    Flux<Long> findRoleIdsByPermissionId(Long permissionId);

    @Modifying
    @org.springframework.data.r2dbc.repository.Query("""
        DELETE FROM sys_role_permission 
        WHERE role_id = :roleId AND permission_id IN (:permissionIds)
        """)
    Mono<Void> deleteByRoleIdAndPermissionIds(Long roleId, java.util.List<Long> permissionIds);

    @Modifying
    @org.springframework.data.r2dbc.repository.Query("""
        DELETE FROM sys_role_permission 
        WHERE permission_id = :permissionId AND role_id IN (:roleIds)
        """)
    Mono<Void> deleteByPermissionIdAndRoleIds(Long permissionId, java.util.List<Long> roleIds);

    @Modifying
    @org.springframework.data.r2dbc.repository.Query("""
        DELETE FROM sys_role_permission WHERE role_id = :roleId
        """)
    Mono<Void> deleteByRoleId(Long roleId);

    @Modifying
    @org.springframework.data.r2dbc.repository.Query("""
        DELETE FROM sys_role_permission WHERE permission_id = :permissionId
        """)
    Mono<Void> deleteByPermissionId(Long permissionId);
}