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.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;

/**
 * 快手广告主分天数据
 *
 * @author syh
 */
@Component
public class KuaishouDailyAccountReportLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    static ExecutorService executorService = Executors.newFixedThreadPool(3);

    @XxlJob("kuaishouDailyAccountReport")
    public void execute() throws Exception {
        Date getDate = DateUtils.addDay(new Date(), -1);
        Date getDate2 = DateUtils.addDay(new Date(), -2);
        Date getDate3 = DateUtils.addDay(new Date(), -3);
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("定时获取快手数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return ;
        }
        tokens.forEach(token -> executorService.submit(() ->{
                    kuaishouInterfaceService.getAdvertiserReportDaily(token, getDate2, getDate2);
                    kuaishouInterfaceService.getAdvertiserReportDaily(token, getDate, getDate);
                    kuaishouInterfaceService.getAdvertiserReportDaily(token, getDate3, getDate3);
                    kuaishouInterfaceService.getAdvertiserReportHourly(token, getDate3, getDate3);

        }));
    }
}
