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.service.IByteDanceAdvertiserDataService;
import cn.com.ctop.toutiao.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 BytedanceMaterialsLoadJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = null;
    @Autowired
    private IByteDanceAdvertiserDataService advertiserDataService;
    @Autowired
    private IByteDanceCreativeService creativeService;

    @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);
        executorService = Executors.newFixedThreadPool(10);
        tokens.forEach(token -> {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        //1：获取当日广告组数据
                        advertiserDataService.getAdvertiserCampaign(token.getAccountId() + "", "", dateString);
                        //1：获取当日广告计划数据
                        advertiserDataService.getAdvertiserPlan(token.getAccountId() + "", "", dateString);
                        //1：获取全量创意数据
                        creativeService.getAdvertiserCreative(token.getAccountId() + "", "", dateString);
                        //2：获取全量素材数据
                        advertiserDataService.getMaterialList(token);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    }
                }
            });
        });
        log.info("物料数据同步完成");
    }
}
