package com.xxl.job.executor.jobhandler;

import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.common.entity.CtopOauthToken;
import com.xxl.job.executor.common.service.ICtopOauthTokenService;
import com.xxl.job.executor.job.entity.JobDistributeInfo;
import com.xxl.job.executor.job.service.IJobDistributeInfoService;
import com.xxl.job.executor.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class BytedanceDistributeJob {
    @Autowired
    private IJobDistributeInfoService jobDistributeInfoService;
//    String URLPREFIX = "http://139.186.29.76:8090/xxl-job-admin/jobinfo/trigger?id=";
String URLPREFIX = "http://127.0.0.1:8090/xxl-job-admin/jobinfo/trigger?id=";
    String EXECUTORPARAM = "&executorParam=";
    @Autowired
    private ICtopOauthTokenService tokenService;
    @XxlJob("kuaishouDistributeDailyJob")
    public void kuaishouDistributeDailyJob(){
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if(null!=tokens&&!tokens.isEmpty()){
            List<JobDistributeInfo>distributeInfos = jobDistributeInfoService.selectByParams("DAILY","KUAISHOU");
            if(null!=distributeInfos&&!distributeInfos.isEmpty()){
                tokens.forEach(token->
                    distributeInfos.forEach(distributeInfo->{
                        String url = URLPREFIX+distributeInfo.getJobId()+EXECUTORPARAM+token.getAccountId();
                        HttpUtils.httpGet(url,new HashMap<>(),new HashMap<>());
                    }));
            }
        }
    }

    @XxlJob("kuaishouDistributeHourlyJob")
    public void kuaishouDistributeHourlyJob(){
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if(null!=tokens&&!tokens.isEmpty()){
            List<JobDistributeInfo>distributeInfos = jobDistributeInfoService.selectByParams("HOURLY","KUAISHOU");
            if(null!=distributeInfos&&!distributeInfos.isEmpty()){
                for (CtopOauthToken token:tokens) {
                    for (JobDistributeInfo job : distributeInfos) {
                        String url = URLPREFIX + job.getJobId() + EXECUTORPARAM + token.getAccountId();
                        HttpUtils.httpGet(url, new HashMap<>(), new HashMap<>());
                    }
                }
            }
        }
    }
}
