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.toutiao.modules.report.service.IBytedancePlanDailyReportService;
import cn.com.ctop.toutiao.modules.report.service.IReportService;
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.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;

/**
 * done
 * 头条计划时报数据任务
 */
@Slf4j
public class BytedancePlanHourlyReportLoadJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private IBytedancePlanDailyReportService planDailyReportService;
    static ExecutorService executorService = null;
    //线程计数器
    static CountDownLatch countDownLatch = null;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("头条时报数据获取开始");
        Date getDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        String hour = simpleDateFormat.format(getDate);
        //1:查询当日计划时报数据
        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
        executorService = Executors.newFixedThreadPool(5);
        countDownLatch = new CountDownLatch(tokens.size());
        tokens.forEach(token -> {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    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) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //清洗缘多多数据渠道号
        if (null != hour && "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);

        log.info("头条计划时报数据获取完成");
        executorService.shutdown();
    }
}
