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

/**
 * 头条数据异步报表  获取前一天绑定的账户，跑从2020-01-01到前一天的数据；目的是防止新绑定账户数据缺失；
 *
 * @author sunzhen
 */
@Slf4j
public class BytedanceDailyAsyncJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IBytedanceReportService bytedanceReportService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String startDate = "2020-01-01";
        Date getDate = DateUtils.addDay(new Date(), -1);
        String endDate = DateUtils.formatDate(getDate);

        log.info("头条数据异步报表  获取前一天绑定的账户，跑从2020-01-01到前一天的数据,时间为：" + startDate + "~" + endDate + "的数据");

        List<CtopOauthToken> tokens = tokenService.getToutiaoTokenByCreateTime(endDate);
        if (null == tokens || tokens.size() <= 0) {
            log.info("头条获取异步报表数据任务执行失败:未获取到可用的token");
            return;
        }

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

    }
}
