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.kuaishou.modules.batch.service.IKuaishouInterfaceService;
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.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 获取广告计划分时报表
 *
 * @author syh
 */
@Slf4j
public class KuaishouAppListJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    static ExecutorService executorService = null;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Thread thread = new Thread() {
            @Override
            public void run() {
                List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE);
                executorService = Executors.newFixedThreadPool(3);
                tokens.forEach(token -> {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                kuaishouInterfaceService.getAppList(token.getAccountId(), token.getAccessToken());
                                log.info("快手应用信息获取完成完成");

                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                            }
                        }
                    });
                });

            }

        };
        thread.start();


    }
}
