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.common.module.utils.CtopAdConstant;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouHistoryReportTaskService;
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;

@Slf4j
public class KuaishouDailyReportTaskJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaiShouHistoryReportTaskService reportTaskService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("快手获取日报数据任务执行开始");
        Long start = System.currentTimeMillis();
        Date getDate = DateUtils.addDay(new Date(), -1);
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.size() <= 0) {
            log.info("快手获取日报数据任务执行失败:未获取到可用的token");
            return;
        }
        String getDateStr = DateUtils.formatDate(getDate);
        for (CtopOauthToken token : tokens) {
            reportTaskService.createTask(token.getAccountId(), token.getAccessToken(), getDateStr, getDateStr, CtopAdConstant.KUAISHOU_LOAD_JOB_TYPE_DAILY);
        }
        Long end = System.currentTimeMillis();
        log.info("快手获取日报数据任务执行结束,执行耗时：{}秒", (end - start) / 1000);
    }
}
