package cn.ey.biz.module.system.service.user;

import cn.ey.biz.framework.third.auth.AuthRequestFactory;
import cn.ey.biz.framework.third.auth.oauth.request.AbstractAuthMicrosoftRequest;
import cn.ey.biz.module.system.dal.redis.RedisKeyConstants;
import cn.ey.biz.module.system.enums.social.SocialTypeEnum;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AadServiceImpl implements AadService {

    @Resource
    private AuthRequestFactory authRequestFactory;

    @Override
    @Cacheable(value = RedisKeyConstants.SYSTEM_USER_GROUP_KEY, key = "#root.methodName+'-'+#identity" ,sync = true)
    public List<String> getMemberOfGroups(String identity) {
        // 获得对应的 AuthRequest 实现
        AbstractAuthMicrosoftRequest authRequest = (AbstractAuthMicrosoftRequest)authRequestFactory.get(SocialTypeEnum.valueOfType(SocialTypeEnum.MICROSOFT_EXT.getType()).getSource());
        List<String> groups = authRequest.getMemberOfGroups(identity)
                .stream()
                .map(item -> item.getId())
                .collect(Collectors.toList());
        return groups;
    }

}
