package cn.edu.whu.boot.user.mapper;

import cn.edu.whu.boot.user.entities.Permission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PermissionMapper {
    @Select({"<script>",
            "SELECT id, description",
            "FROM user_permission",
            "WHERE id IN",
            "<foreach item='id' index='index' collection='list'",
            "open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"})
    List<Permission> findByIdIn(List<Integer> list);

    @Select("SELECT description FROM user_permission")
    List<String> findAllDescriptions();

    @Select("SELECT description FROM user_permission WHERE id = #{id}")
    String findDescriptionById(@Param("id") int id);

    @Select("SELECT description FROM user_permission " +
            "WHERE id IN (SELECT permission FROM user_role_permission WHERE role = #{role})")
    List<String> findDescriptionsByRole(@Param("role") int role);
}
