package com.weiqi.mis.contorller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weiqi.mis.HttpUtil;
import com.weiqi.vo.WightVo;


import java.util.concurrent.Callable;


/**
 * @Description 回调方法
 * @author yyy
 * @date 2019/8/1 下午3:16
 */
public class CallableTask implements Callable<WightVo> {
    String url;
    Integer id;
    String des;
    int type;
    String host;


    public CallableTask(String url, int id, String des,int type, String host) {
        super();
        this.url = url;
        this.id = id;
        this.des = des;
        this.type = type;
        this.host = host;

    }

    @Override
    public WightVo call() throws Exception {

            WightVo v = getwight(url,  id,  des,type,host);
            return v;

    }


    /**
     *
     * @param url
     * @param id
     * @param des
     * @param type 1 权重  2 流量
     * @return
     */

    public WightVo getwight(String url, int id, String des,int type,String host){
        String result = "";
        WightVo wightVo =null;
        try{
            if(type==1){
                result = HttpUtil.sendPostRequest("http://" + url + "/api/sms/getlb?_appid=test", null,host);
                JSONArray jsa =   JSONArray.parseArray(result);
                wightVo = init(url,id,des,jsa);
            }else  if(type==2){
                //调用权重接口
                result = HttpUtil.sendPostRequest("http://" + url + "/api/sms/getFlow?_appid=test", null,host);
                JSONObject jsa =   JSONObject.parseObject(result);
                wightVo = initflow(url,id,des,jsa);
            }else  if(type==3){
                //调用权重接口
                result = HttpUtil.sendPostRequest("http://" + url + "/api/sms/getKafkaCluster?_appid=test", null,host);
                JSONObject jsa =   JSONObject.parseObject(result);
                wightVo = initCluster(url,id,des,jsa);
            }


        }catch (Exception e){
            wightVo = initerror(url,id,des,e.getMessage());
        }

        return wightVo;
    }

    /**初始化权重列表
     *
     * @param url
     * @param id
     * @param name
     * @param jsa
     * @return
     */
    public WightVo init(String url,int id,String name, JSONArray jsa ) {

        WightVo wightVo = new WightVo();
        for(int i =0 ;i<jsa.size();i++){
            switch (jsa.getJSONObject(i).getString("name")){
                case "sp116": wightVo.setSp116( jsa.getJSONObject(i).getString("weight")); break;
                case "sp206": wightVo.setSp206( jsa.getJSONObject(i).getString("weight")); break;
            }
        }
        wightVo.setIp(url.replace(":5000",""));
        wightVo.setId(id);
        wightVo.setDes(name);

        return wightVo;
    }

    /**初始化流量图表
     *
     * @param url
     * @param id
     * @param name
     * @param jsa
     * @return
     */
    public WightVo initflow(String url,int id,String name, JSONObject jsa ) {
        WightVo wightVo = new WightVo();
        wightVo.setFlow(jsa.getString("weight"));

        wightVo.setIp(url.replace(":5000",""));
        wightVo.setId(id);
        wightVo.setDes(name);

        return wightVo;
    }


    /**初始化流量图表
     *
     * @param url
     * @param id
     * @param name
     * @param jsa
     * @return
     */
    public WightVo initCluster(String url,int id,String name, JSONObject jsa ) {
        WightVo wightVo = new WightVo();
        wightVo.setCluster(jsa.getString("cluster"));

        wightVo.setIp(url.replace(":5000","").replace(":4000",""));
        wightVo.setId(id);
        wightVo.setDes(name);

        return wightVo;
    }

    /**
     * 错误初始化
     *
     * @param url
     * @param id
     * @param name
     * @param error
     * @return
     */
    public WightVo initerror(String url,int id,String name,String error ) {
        WightVo wightVo = new WightVo();

        wightVo.setSp116( "error");
        wightVo.setSp206( "error");
        wightVo.setFlow( error);
        wightVo.setIp(url.replace(":5000",""));
        wightVo.setId(id);
        wightVo.setDes(name);
        wightVo.setCluster(error);

        return wightVo;
    }
}