package com.xxl.job.executor.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.executor.common.entity.CtopOauthToken;
import com.xxl.job.executor.common.entity.OauthAgentToken;
import com.xxl.job.executor.common.mapper.CtopOauthTokenMapper;
import com.xxl.job.executor.common.service.ICtopOauthTokenService;
import com.xxl.job.executor.common.service.IOauthAgentTokenService;
import com.xxl.job.executor.common.service.IRefreshTokenService;
import com.xxl.job.executor.utils.Check;
import com.xxl.job.executor.utils.CtopAdConstant;
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 RefreshTokenServiceImpl implements IRefreshTokenService {
    @Autowired
    private CtopOauthTokenMapper oauthTokenMapper;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IOauthAgentTokenService agentTokenService;

    /**
     * 获取所有token
     *
     * @return
     */
    @Override
    public void getKuaiShouRefresh() {
        List<CtopOauthToken> oauthTokens = oauthTokenMapper.selectKuaiShouTokens(CtopAdConstant.PLATFORM_TYPE_KUAISHOU);
        if (!Check.isNull(oauthTokens)) {
            for (CtopOauthToken oauthToken : oauthTokens) {
                XxlJobHelper.log("刷新token,accountId:{}", oauthToken.getAccountId());
                //快手
                if (CtopAdConstant.PLATFORM_TYPE_KUAISHOU.equals(oauthToken.getMediaId().trim())) {
                    oauthTokenService.getKuaiShouRefreshToken(oauthToken.getAccountId(), oauthToken.getAccessToken(), oauthToken.getRefreshToken(), oauthToken.getAgentType());
                }
            }
        } 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());

            }

        }


    }
}
