package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.OauthAgentToken;
import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IOauthAgentTokenService;
import cn.com.ctop.common.module.service.IOauthConfigService;
import cn.com.ctop.common.module.service.IRefreshTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

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

    /**
     * 获取所有token
     *
     * @return
     */
    @Override
    public void getKuaiShouRefresh() {


        List<CtopOauthToken> oauthTokens = oauthTokenMapper.selectKuaiShouTokens(CtopAdConstant.PLATFORM_TYPE_KUAISHOU);
        if (!Check.isNull(oauthTokens)) {
            Map<Long, String> configMap = configService.getConfigMapByMediaId(CtopAdConstant.PLATFORM_TYPE_KUAISHOU_INT);
            for (CtopOauthToken oauthToken : oauthTokens) {
                XxlJobHelper.log("刷新token,accountId:{}", oauthToken.getAccountId());
                //快手
                if (CtopAdConstant.PLATFORM_TYPE_KUAISHOU.equals(oauthToken.getMediaId().trim())) {
                    String secret = configMap.get(oauthToken.getAppId());
                    oauthTokenService.getKuaiShouRefreshToken(oauthToken.getAccountId(), oauthToken.getAccessToken(), oauthToken.getRefreshToken(), oauthToken.getAppId(), secret);
                }
            }
        } else {
            XxlJobHelper.log("----------------------刷新token获取数据返回结果为空----------------------------------");
        }
    }

    @Override
    public void byteDanceRefresh(String accountId) {
        if (null == accountId || "".equals(accountId.trim()) || "null".equalsIgnoreCase(accountId)) {
            List<CtopOauthToken> byteDanceOauthTokens = oauthTokenMapper.selectByteDanceTokens(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
            if (!Check.isNull(byteDanceOauthTokens)) {
                for (CtopOauthToken oauthToken : byteDanceOauthTokens) {
                    oauthTokenService.getByteDanceAccessToken(oauthToken);
                }
            }
        } else {
            CtopOauthToken token = oauthTokenMapper.selectByAccountId(Long.parseLong(accountId));
            if (token == null) {
                XxlJobHelper.log("未获取到账户数据,accountId:{}", accountId);
                return;
            }
            oauthTokenService.getByteDanceAccessToken(token);
        }
    }

    @Override
    public void getKuaiShouAgentRefresh() {
        QueryWrapper<OauthAgentToken> agentTokenQueryWrapper = new QueryWrapper<>();
        agentTokenQueryWrapper.eq("media_id", 2);
        List<OauthAgentToken> list = agentTokenService.list(agentTokenQueryWrapper);
        if (!Check.isNull(list)) {
            for (OauthAgentToken agentToken : list) {
                oauthTokenService.getKuaiShouAgentRefreshToken(agentToken.getAgentId(), agentToken.getAccessToken(), agentToken.getRefreshToken());

            }

        }


    }
}
