package com.fdkanfang.dao.backend;

import com.fdkanfang.domain.backend.ResourceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;


@Component
@Mapper
public interface ResourceMapper extends IBaseMapper<ResourceEntity, Long> {

    @Select(value = "select * from tb_role_resource z left join tb_resource r ON z.resource_id = r.id where z.role_id= #{roleId}")
    List<ResourceEntity> findRoleResourceByRoleId(Long roleId);


    /**
     * 根据用户id查询权限
     * 没有left ，直接用join，就不会前后表关联了
     */
    @Select(value = "select b.id, b.name, b.parent_id, b.resource_type, b.resource_key from " +
            "((tb_user u left join tb_user_role ur on u.id = ur.user_id) " +
            " join tb_role r on r.id = ur.role_id) " +
            " join tb_role_resource a on r.id = a.role_id " +
            " join tb_resource b on b.id = a.resource_id WHERE u.id = #{userId}")
//    List<ResourceEntity> findResourceByUserId(Long userId);
    Set<ResourceEntity> findResourceByUserId(Long userId);

    /**
     * 根据用户id查询权限resource_key
     */
    @Select(value = "select b.resource_key from " +
            "((tb_user u left join tb_user_role ur on u.id = ur.user_id) " +
            "left join tb_role r on r.id = ur.role_id) " +
            "left join tb_role_resource a on r.id = a.role_id " +
            "left join tb_resource b on b.id = a.resource_id WHERE u.id = #{userId}")
    Set<String> findResourceKeyByUserId(Long userId);
}