package com.xkrs.dao;

import com.xkrs.model.entity.SysAuthorityEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * SysAuthorityDao
 * @author tajochen
 */
public interface SysAuthorityDao extends JpaRepository<SysAuthorityEntity,Integer> {
    /**
     * 查询权限实体列表根据用户名 Object[]
     * @param userName 用户名
     * @return 用户实体
     */
    @Query(value = "SELECT a.id, a.authority_name, a.authority_name_zh, a.authority_desc " +
            "FROM sys_authority a,rel_role_authority ra,sys_role r WHERE r.id = ra.role_id AND ra.authority_id = a.id AND r.id " +
            "IN (SELECT r.id FROM sys_user u,sys_role r,rel_user_role ur " +
            "    WHERE u.user_name = :userName AND u.id = ur.user_id AND ur.role_id = r.id)", nativeQuery = true)
    List<SysAuthorityEntity> selectByUserName(@Param("userName") String userName);

    /**
     * 根据id批量查询权限信息
     * @param id
     * @return
     */
    List<SysAuthorityEntity> findAllByIdIn(List<Integer> id);
}
