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 cn.com.ctop.toutiao.modules.material.service.IByteDanceCampaignService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 素材数据获取
 */
@Slf4j
public class BytedanceAdvertiserDataLoadJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = Executors.newFixedThreadPool(5);
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;
    @Autowired
    private IByteDanceCreativeService creativeService;
    @Autowired
    private IByteDanceCampaignService byteDanceCampaignService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date getDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(getDate);
        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
        tokens.forEach(token -> executorService.submit(() -> {
            //1：获取当日广告组数据
            byteDanceCampaignService.getAdvertiserCampaign(token, null, dateString);
            //2：获取当日广告计划数据
            advertiserDataService.getAdvertiserPlan(token, "", dateString, null);
            //3：获取当日创意数据
            creativeService.getAdvertiserCreative(token, "", dateString);
        }));
        log.info("物料数据同步完成");
    }
}
