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.IBytedancePlanDailyReportService;
import com.xxl.job.executor.report.service.IReportService;
import com.xxl.job.executor.utils.CtopAdConstant;
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;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 宋英豪
 * 头条计划时报数据任务
 */
@Component
public class BytedancePlanHourlyReportLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private IBytedancePlanDailyReportService planDailyReportService;
    static ExecutorService executorService = Executors.newFixedThreadPool(5);
    //线程计数器
    static CountDownLatch countDownLatch = null;


    @XxlJob("bytedancePlanHourlyReport")
    public void execute() {
        XxlJobHelper.log("头条时报数据获取开始");
        Date getDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        String hour = simpleDateFormat.format(getDate);
        //1:查询当日计划时报数据
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        countDownLatch = new CountDownLatch(tokens.size());
        tokens.forEach(token -> executorService.submit(() -> {
                try {
                    if (null != hour && "00".equals(hour)) {
                        Date finalGetDate = DateUtils.addDay(getDate, -1);
                        reportService.getAdvertiserPlanReport(token, finalGetDate, finalGetDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_HOURLY);
                    }
                    reportService.getAdvertiserPlanReport(token, getDate, getDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_HOURLY);
                } catch (Exception e) {
                    XxlJobHelper.log(e.getMessage());
                } finally {
                    countDownLatch.countDown();
                }
        }));

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            XxlJobHelper.log(e.getMessage());
        }
        //清洗缘多多数据渠道号
        if ("00".equals(hour)) {
            Date lastday = DateUtils.addDay(getDate, -1);
            planDailyReportService.cleanYddHourlyChannelCode(113, lastday);
            planDailyReportService.cleanYddHourlyChannelCode(419, lastday);
        }
        planDailyReportService.cleanYddHourlyChannelCode(113, getDate);
        planDailyReportService.cleanYddHourlyChannelCode(419, getDate);
        XxlJobHelper.log("头条时报数据获取结束");
    }
}
