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

/**
 * 头条当日计划素材获取任务
 */

@Component
public class BytedancePlanRealTimeLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    private ExecutorService executorService = Executors.newFixedThreadPool(4);
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;

    @XxlJob("bytedanceRealTimePlanInfo")
    public void execute() throws Exception {
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
        String nowDate = DateUtils.formatDate();
        tokens.forEach(token -> executorService.submit(() -> advertiserDataService.getAdvertiserPlan(token, "", nowDate, nowDate)));
        XxlJobHelper.log("头条全量计划数据获取完成");
    }


}
