package org.jeecg.modules.ctop.job;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 头条素材报表按天跑前一天的数据任务
 *
 * @author sunzhen
 */
@Slf4j
public class BytedanceDailyCreativeReportAsyncJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IBytedanceReportService bytedanceReportService;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Date getDate = DateUtils.addDay(new Date(), -1);
        String date = DateUtils.formatDate(getDate);
        log.info("头条创意报表异步任务执行开始.跑的是" + date + "的数据");
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        if (null == tokens || tokens.size() <= 0) {
            log.info("头条获取异步报表数据任务执行失败:未获取到可用的token");
            return;
        }

        for (CtopOauthToken token : tokens) {
            //1.创意 2.广告主 3.广告主 4.广告计划
            bytedanceReportService.bytedanceAsyncTaskCreate(date, date, token, 1);
        }

    }
}
