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.entity.BytedanceReportMaterialRetry;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 头条素材报表错误重试定时任务
 *
 * @author sunzhen
 */
@Slf4j
public class BytedanceDailyMaterialReportRetryJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IBytedanceReportService bytedanceReportService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("头条素材报表重试定时任务开始");
        List<BytedanceReportMaterialRetry> retryList = bytedanceReportService.getRetryList();
        //
        //多线程
        final ExecutorService executorService = Executors.newFixedThreadPool(3);
        retryList.forEach(retry -> {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        CtopOauthToken token = tokenService.getOauthTokenByAccountId(String.valueOf(retry.getAccountId()));
                        log.info("头条素材报表重试定时任务，当前accountId为：" + token.getAccountId());
                        bytedanceReportService.bytedanceMaterialReportRetry(token, retry.getStartDate(), retry.getEndDate());
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    }
                }
            });
        });
    }
}
