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.Check;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCampaignService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONArray;
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.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 获取创意分天报表
 *
 * @author syh
 */
@Slf4j
public class KuaishouDailyCreativeReportLoadJob implements Job {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    static ExecutorService executorService = null;

    @Autowired
    private IKuaiShouCampaignService campaignService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Date getDate = DateUtils.addDay(new Date(), -1);
                //1:查询当日数据
                executorService = Executors.newFixedThreadPool(4);
                List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
                if (null == tokens || tokens.size() <= 0) {
                    log.info("定时获取快手数据异常：未获取到可用的token");
                    return;
                }
                for (CtopOauthToken token : tokens) {
                    try {
                        log.info("获取创意分天报表开始,accountId:{}", token.getAccountId());
                        kuaishouInterfaceService.getCampaignList(token, null, null);
                        sleep(5 * 100);
                        List<JSONArray> campaigns = campaignService.getJsonArrByAccount(token.getAccountId(), 50);
                        if (!Check.isNull(campaigns) && campaigns.size() > 0) {
                            for (int i = 0; i < campaigns.size(); i++) {
                                JSONArray campaignIds = (JSONArray) JSONArray.parse(campaigns.get(i).toJSONString());
                                executorService.submit(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            kuaishouInterfaceService.getAdvertiserCreativeReportDaily(token, getDate, getDate, campaignIds, null);
                                        } catch (Exception e) {
                                            log.info("获取创意日报失败,accountId:{}", token.getAccountId());
                                        }

                                    }
                                });

                            }

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


                }
            }
        };
        thread.start();

    }

}
