package com.xxl.job.executor.jobhandler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.common.entity.CtopOauthToken;
import com.xxl.job.executor.common.service.ICtopOauthTokenService;
import com.xxl.job.executor.report.service.IBytedanceFundDailyService;
import com.xxl.job.executor.utils.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");
        }
        tokens.forEach(token -> executorService.submit(() -> bytedanceFundDailyService.loadFundDataByPage(token, DateUtils.formatDate(getDate), DateUtils.formatDate(getDate),1)));
        XxlJobHelper.log("头条账户流水数据获取结束");
    }
}
