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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author 宋英豪
 */
@Component
public class BytedanceTodayPlaneLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;
    static ExecutorService executorService = Executors.newFixedThreadPool(15);
    @XxlJob("bytedanceTodayPlaneLoadJob")
    public void execute() throws Exception {
        Date getDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(getDate);
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        tokens.forEach(token -> executorService.submit(() -> {
            try {
                advertiserDataService.getAdvertiserPlan(token, "", null, dateString);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
        XxlJobHelper.log("当日计划数据同步完成完成");
    }
}
