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

import cn.ey.biz.common.enums.UserTypeEnum;
import cn.ey.biz.module.system.controller.admin.oauth2.vo.open.OAuth2OpenAuthorizeInfoRespVO;
import cn.ey.biz.module.system.convert.oauth2.OAuth2OpenConvert;
import cn.ey.biz.module.system.dal.dataobject.oauth2.OAuth2ApproveDO;
import cn.ey.biz.module.system.dal.dataobject.oauth2.OAuth2ClientDO;
import org.springframework.stereotype.Service;

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

import static cn.ey.biz.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Service
public class OAuth2OpenCommonServiceImpl implements OAuth2OpenCommonService{
    @Resource
    private OAuth2ClientService oauth2ClientService;

    @Resource
    private OAuth2ApproveService oauth2ApproveService;
    @Override
    public OAuth2OpenAuthorizeInfoRespVO getOAuth2OpenAuthorizeInfo(String clientId) {
        // 0. 校验用户已经登录。通过 Spring Security 实现

        // 1. 获得 Client 客户端的信息
        OAuth2ClientDO client = oauth2ClientService.validOAuthClientFromCache(clientId);
        // 2. 获得用户已经授权的信息
        List<OAuth2ApproveDO> approves = oauth2ApproveService.getApproveList(getLoginUserId(), getUserType(), clientId);

        return OAuth2OpenConvert.INSTANCE.convert(client, approves);
    }

    private Integer getUserType() {
        return UserTypeEnum.ADMIN.getValue();
    }
}
