package cn.com.ctop.job.bytedance.handler;

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 com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
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 BytedancePlanLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = null;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;

    @XxlJob("bytedancePlanLoadJob")
    public ReturnT<String> execute(String param) throws Exception {
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
        executorService = Executors.newFixedThreadPool(4);
        tokens.forEach(token -> executorService.submit(() -> advertiserDataService.getAdvertiserPlan(token, "", null, null)));
        XxlJobLogger.log("头条全量计划数据获取完成");
        return ReturnT.SUCCESS;
    }
}
