package com.ruixuan.bytedance.service.impl;

import com.doudian.open.api.token.AccessTokenData;
import com.doudian.open.core.AccessToken;
import com.doudian.open.core.GlobalConfig;
import com.ruixuan.bytedance.entity.BytedanceAccessToken;
import com.ruixuan.bytedance.mapper.BytedanceAccessTokenMapper;
import com.ruixuan.bytedance.service.IBytedanceAccessTokenService;
import com.ruixuan.common.utils.Check;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BytedanceAccessTokenServiceImpl implements IBytedanceAccessTokenService {
    @Autowired
    private BytedanceAccessTokenMapper accessTokenMapper;


    @Override
    public AccessToken getAccessToken(Long regId) {
        try {
            BytedanceAccessToken localToken = accessTokenMapper.getAccessToken(regId);
            if (Check.isNotNull(localToken)) {
                GlobalConfig config = new GlobalConfig();
                config.AddAppKeyAndAppSecret(localToken.getAppKey(), localToken.getAppSecret());
                AccessToken accessToken = new AccessToken();
                accessToken.setAppKey(localToken.getAppKey());
                AccessTokenData data = new AccessTokenData();
                data.setAccessToken(localToken.getAccessToken());

                accessToken.setData(data);
                return accessToken;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("头条token获取异常！！！");
        }
        return null;
    }

    @Override
    public List<BytedanceAccessToken> getTokenList() {
        return accessTokenMapper.getTokenList();
    }

}
