package cn.com.ctop.job.bytedance.handler;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.toutiao.modules.report.service.IReportService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 头条计划日报数据任务
 */
@Component
public class BytedancePlanDailyReportLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IReportService reportService;

    /**
     * 头条计划日报
     * @throws Exception
     */
    @XxlJob("bytedancePlanDailyReport")
    public void execute() throws Exception {
        Date getDate = DateUtils.addDay(new Date(), -1);
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("定时获取头条数据异常：为获取到可用的token");
            XxlJobHelper.handleFail();
            return ;
        }
        tokens.forEach(token -> reportService.getAdvertiserPlanReport(token, getDate, getDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY));
        //跑次留
        Date getDate2 = DateUtils.addDay(new Date(), -2);
        tokens.forEach(token -> reportService.getAdvertiserPlanReport(token, getDate2, getDate2, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY));
    }
}
