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.material.service.IByteDanceAdvertiserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class BytedancePlanLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;
    static ExecutorService executorService = Executors.newFixedThreadPool(4);
    static CountDownLatch countDownLatch = null;

    @XxlJob("bytedancePlanLoadJob")
    public void execute() throws Exception {
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        countDownLatch = new CountDownLatch(tokens.size());
        tokens.forEach(token -> executorService.submit(() -> {
            try {
                advertiserDataService.getAdvertiserPlan(token, "", null, null);
            }catch (Exception e){
            }finally {
                countDownLatch.countDown();
            }
        }));
        countDownLatch.await();
        XxlJobHelper.log("头条全量计划数据获取完成");
    }
}
