package com.philisense.www.service.impl;

import com.philisense.www.dao.JwtPermissionDAO;
import com.philisense.www.service.JwtPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@Service
//@CacheConfig(cacheNames = "role")
public class JwtPermissionServiceImpl implements JwtPermissionService {
    @Autowired
    private JwtPermissionDAO jwtPermissionDAO;

    /**
     * key的名称如有修改，请同步修改 UserServiceImpl 中的 update 方法
     * @param userid
     * @return
     */
    //@Cacheable(key = "'loadPermissionByUser:' + #userid")
    public Collection<GrantedAuthority> mapToGrantedAuthorities(String userid) {
        Set<Map<String,Object>> roles = jwtPermissionDAO.getAuthoritiesByUserId(userid);
        return roles.stream().map(role -> new SimpleGrantedAuthority(role.get("AUTH_TYPE")+""))
                .collect(Collectors.toList());

    }
}
