package com.xxl.job.executor.jobhandler;

import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouDailyReportTaskService;
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.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xxl.job.executor.utils.DateUtils.getAnotherDay;

/**
 * 获取文件状态并下载入库
 *
 * @author yumeng
 */
@Component
public class KuaishouDailyLoadFileJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    private ExecutorService executorService = Executors.newFixedThreadPool(3);
    @Autowired
    private IKuaiShouDailyReportTaskService dailyReportTaskService;

    @XxlJob("kuaishouDailyLoadFile")
    public void execute() throws Exception {
        String nowDate = DateUtils.getDate("yyyy-MM-dd");
        String statDate = getAnotherDay("yyyy-MM-dd", nowDate, -1);
        //  查询快手token
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.size() <= 0) {
            XxlJobHelper.log("定时获取快手数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return ;
        }
        tokens.forEach(token -> executorService.submit(() -> dailyReportTaskService.getTaskList(token.getAccountId(), token.getAccessToken(), statDate)));
    }
}
