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.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
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 KuaishouNowDateCreativeJob 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() {
            @SneakyThrows
            @Override
            public void run() {
                //1:查询当日数据
                Date endDate  = new Date();
                String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", DateUtils.formatDate(endDate), -1);
                SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
                Date startDate = sim.parse(anotherDay);
                List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
                executorService = Executors.newFixedThreadPool(5);
                tokens.forEach(token -> {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                kuaishouInterfaceService.getCreativeList(token, startDate, endDate);
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                            }
                        }
                    });
                });
                log.info("快手物料数据同步完成");
            }

        };
        thread.start();


    }

}
