package com.sifang.modules.gudewei.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.sifang.modules.gudewei.constant.GuDeConstan;
import com.sifang.modules.utils.http.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 固德威接口公共类
 * auth:祁军亮
 */
public class GuDeUtils {
    private static final Logger log = LoggerFactory.getLogger(GuDeUtils.class);

    public static String request(String url,Map body,String account ,String password){
        Map<String,String> headers=new HashMap<>();
        headers.put("token",getToken(account,password));
        String result= HttpUtil.createPost(url).addHeaders(headers).body(JSONUtil.toJsonStr(body)).execute().body();
        return result;
    }

    /**
     * get请求
     */
    public static String requestForGet(String host,String path,Map<String,String> body,String account ,String password){
        Map<String,String> headers=new HashMap<>();
        headers.put("token",getToken(account, password));
        try {
            HttpResponse response = HttpUtils.doGet(host, path, headers, body);
            System.out.println(response.toString());
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                String result = null;
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
                return result;
            }
        } catch (Exception e) {
            log.info("固德威删除设备接口 请求失败：" + e.getMessage());
            throw new RuntimeException(e);
        }
        return null;
    }

    /*@Test
    public void requestA(){

        Map<String,Object> body=new HashMap<>();
        body.put("pw_name","测试电站");//电站名称
        body.put("conn_date",DateUtil.today());//接入时间
        body.put("pw_yieldrate",2.0);//收益率 范围 0~10
        body.put("pw_inventersnum",5);//组件数量
        body.put("pw_capacity",6.0);//电站容量
        body.put("pw_address","安徽省");//所在地区
        body.put("pw_areacode",232000);//区域编码
        body.put("pw_type",0);
        Map m=new HashMap();
        List<Map> list=new ArrayList<>();
        m.put("owner_phone","13771990445");
        m.put("owner_name","测试接口业主");
        m.put("is_first_owner",true);
        list.add(m);
        body.put("list_pwowner",list);
        body.put("list_attachment",null);
        body.put("pw_synopsis","接口创建电站");
        body.put("pw_longitude","116.347482707264");//经度
        body.put("pw_latitude","34.4605003190706");//纬度
        body.put("pw_synopsishide",false);
        List<Map> list2=new ArrayList<>();
        Map ma=new HashMap();
        ma.put("it_name","测试逆变器");//逆变器名称
        ma.put("it_sn","4020KDTF225G1659");//逆变器序列号
        ma.put("it_checkcode","004039");//逆变器校验码
        list2.add(ma);
        body.put("list_inverters",list2);

        Map<String,String> headers=new HashMap<>();
        headers.put("token",getToken());
        String result= HttpUtil.createPost(GuDeConstan.url+GuDeConstan.add_url).addHeaders(headers).body(JSONUtil.toJsonStr(body)).execute().body();
        System.out.println("测试数据----"+result);
    }*/




    private static String getToken(String account ,String password){
        Map<String,String> body=new HashMap<>();
        Map<String,String> headers=new HashMap<>();
        body.put("account",account);//用户名
        body.put("pwd",password);
        String result= HttpUtil.createPost(GuDeConstan.url+GuDeConstan.token_url).addHeaders(headers).body(JSONUtil.toJsonStr(body)).execute().body();
        Map parse=(Map) JSON.parse(result);
        if(parse.containsKey("code")){
                if("0".equals(String.valueOf(parse.get("code"))) && parse.containsKey("data") ){
                    Map tokenData=(Map) JSON.parse(String.valueOf(parse.get("data")));
                    if(tokenData.get("token") != null){
                        return  String.valueOf(tokenData.get("token"));
                    }
                }
        }
        return null;
    }



}
