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.batch.service.IKuaishouInterfaceService;
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.text.SimpleDateFormat;
import java.util.Date;

/**
 * 获取广告计划分时报表
 *
 * @author syh
 */
@Component
public class KuaishouHourlyCampaignReportLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    @XxlJob("kuaishouHourlyCampaignReport")
    public void execute(){
        String param = XxlJobHelper.getJobParam();
        if (param==null || param.trim().length()==0) {
            XxlJobHelper.log("param["+ param +"] invalid.");
            XxlJobHelper.handleFail();
            return;
        }
        CtopOauthToken token = tokenService.getOauthTokenByAccountId(param);
        if (null == token) {
            XxlJobHelper.log("获取头条token数据异常：账户{}token不存在",param);
            XxlJobHelper.handleFail();
            return;
        }

        Date getDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        String hour = simpleDateFormat.format(getDate);
        if ("00".equals(hour)) {
            getDate = DateUtils.addDay(getDate, -1);
        }
        kuaishouInterfaceService.getAdvertiserCampaignReportHourly(token, getDate, getDate);
    }
}
