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.material.service.IByteDanceAdvertiserDataService;
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.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 BytedanceSameDayCreativeLoadJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = null;
    //线程计数器
    static CountDownLatch countDownLatch = null;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
        executorService = Executors.newFixedThreadPool(6);
        countDownLatch = new CountDownLatch(tokens.size());
        tokens.forEach(token -> {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        String getDateString = DateUtils.formatDate(new Date());
//                        advertiserDataService.getAdvertiserCreative(token, getDateString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("创意物料数据同步完成");
    }
}
