package cn.com.ctop.job.bytedance.handler;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceFundDailyService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class BytedanceFlowFundJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IBytedanceFundDailyService bytedanceFundDailyService;
    static ExecutorService executorService = Executors.newFixedThreadPool(4);
    @XxlJob("bytedanceFlowFund")
    public void execute() throws Exception {
        XxlJobHelper.log("头条账户流水数据获取开始");
        Date getDate = new Date();
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("头条账户流水数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return ;
        }
        tokens.forEach(token -> executorService.submit(() -> bytedanceFundDailyService.loadFundDataByPage(token, DateUtils.formatDate(getDate), DateUtils.formatDate(getDate),1)));
        XxlJobHelper.log("头条账户流水数据获取结束");
    }
}
