package com.ffii.fhsmsc.modules.User_group.repository;

import com.ffii.core.support.AbstractRepository;
import com.ffii.fhsmsc.modules.User_group.enity.User_authority;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.jpa.repository.Modifying;

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

@Repository
public interface User_authorityRepository extends AbstractRepository<User_authority, Long> {
    
    @Query("SELECT ua FROM User_authority ua WHERE ua.userId = :userId")
    List<User_authority> findByUserId(@Param("userId") Long userId);

    @Query("SELECT ua FROM User_authority ua WHERE ua.userId = :userId AND ua.authId = :authId")
    Optional<User_authority> findByUserIdAndAuthId(
        @Param("userId") Long userId,
        @Param("authId") Long authId
    );

    @Modifying
    @Transactional
    @Query("DELETE FROM User_authority ua WHERE ua.userId = :userId")
    void deleteByUserId(@Param("userId") Long userId);

    @Modifying
    @Transactional
    @Query("DELETE FROM User_authority ua WHERE ua.userId = :userId AND ua.authId = :authId")
    void deleteByUserIdAndAuthId(
        @Param("userId") Long userId,
        @Param("authId") Long authId
    );
} 