package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ctop.service.IRefreshTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RefreshTokenServiceImpl implements IRefreshTokenService {
    @Autowired
    private CtopOauthTokenMapper oauthTokenMapper;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;

    /**
     * 获取所有token
     * @return
     */
    @Override
    public void getRefresh() {
        List<CtopOauthToken> oauthTokens = oauthTokenMapper.selectAll();
        log.info("需要刷新token列表长度:{}", oauthTokens.size());
        if (!Check.isNull(oauthTokens)) {
            for (CtopOauthToken oauthToken : oauthTokens) {
                //头条
                if (CtopAdConstant.PLATFORM_TYPE_BYTEDANCE.equals(oauthToken.getMediaId().trim())) {
                    oauthTokenService.getByteDanceAccessToken(String.valueOf(oauthToken.getAccountId()));
                }
                //快手
                if (CtopAdConstant.PLATFORM_TYPE_KUAISHOU.equals(oauthToken.getMediaId().trim())) {
                    oauthTokenService.getKuaiShouRefreshToken(oauthToken.getAccountId(), oauthToken.getRefreshToken());
                }
            }
        } else {
            log.error("----------------------刷新token获取数据返回结果为空----------------------------------");
        }
    }
}
