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.common.entity.CtopOauthToken;
import com.xxl.job.executor.common.service.ICtopOauthTokenService;
import com.xxl.job.executor.report.service.IByteDanceVideoReportDailyService;
import com.xxl.job.executor.report.service.IBytedanceReportService;
import com.xxl.job.executor.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class BytedanceDailyMaterialReportJob {

    static ExecutorService executorService = Executors.newFixedThreadPool(4);
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IBytedanceReportService bytedanceReportService;
    @Autowired
    private IByteDanceVideoReportDailyService videoReportDailyService;


    @XxlJob("bytedanceDailyMaterialReportJob")
    public void execute() throws Exception {
        Date getDate2 = DateUtils.addDay(new Date(), -2);
        String date2 = DateUtils.formatDate(getDate2);
        Long start = System.currentTimeMillis();
        Date getDate = DateUtils.addDay(new Date(), -1);
        String date = DateUtils.formatDate(getDate);
        XxlJobHelper.log("头条获取素材报表数据任务开始，任务时间：" + date2 + "~" + date);

        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        CountDownLatch countDownLatch = new CountDownLatch(tokens.size());

        tokens.forEach(token -> executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        //获取头条素材报表两天前的数据
                        XxlJobHelper.log("账户"+token.getAccountId() + "素材报表数据任务开始，任务时间：" + date + "~" + date2);
                        bytedanceReportService.bytedanceMaterialReport(token, date2, date2);
                        bytedanceReportService.bytedanceMaterialReport(token, date, date);
                        //获取头条素材报表数据
                        bytedanceReportService.bytedanceVideoMaterialReport(token, date2, date2);
                        bytedanceReportService.bytedanceVideoMaterialReport(token, date, date);
                        XxlJobHelper.log("账户"+token.getAccountId() + "素材报表数据任务完成，任务时间：" + date + "~" + date2);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            }));

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Long end = System.currentTimeMillis();
        XxlJobHelper.log("头条素材数据获取耗时：{}毫秒", end - start);

        videoReportDailyService.videoInfoList(date, date);
        videoReportDailyService.videoInfoList(date2, date2);
    }
}
