package com.zhgd.xmgl.util;

import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @program: wisdomSite
 * @description: 外部环境设备接口
 * @author: Mr.Peng
 * @create: 2021-07-12 09:54
 **/
@Slf4j
public class EnvironmentUtils {

    //账户
    public static final String loginName = "t210711qxz";
    //密码
    public static final String password = "t210711qxz";
    public static final String url = "http://iot.0531yun.cn/wsjc";
    private static String userId = "";

    public static String getLoginUserId() {
        Map<String, String> param = new HashMap<>();
        param.put("loginName", loginName);
        param.put("password", password);
        String result = HttpUtil.post(url + "/app/Login", JSONUtil.toJsonStr(param));
        if (StringUtils.isNotEmpty(result)) {
            JSONObject object = JSONObject.parseObject(result);
            String code = String.valueOf(object.get("code").toString());
            if ("1000".equals(code)) {
                Map<String, Object> map = com.zhgd.jeecg.common.util.JSONUtil.readValueToMap(object.get("data").toString());
                return MapUtils.getString(map, "userId");
            }
        }
        return null;
    }

    public static JSONArray getUserDeviceGroups() {
        if (StringUtils.isEmpty(userId)) {
            userId = getLoginUserId();
        }
        Map<String, Object> param = new HashMap<>();
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("userId", userId);
        String result = get(url + "/app/GetUserDeviceGroups", param, headerMap);
        log.info("result:" + result);
        if (StringUtils.isNotEmpty(result)) {
            JSONObject object = JSONObject.parseObject(result);
            String code = String.valueOf(object.get("code").toString());
            if ("1000".equals(code)) {
                JSONArray array = object.getJSONArray("data");
                return array;
            }
        }
        return null;
    }

    public static JSONArray getDeviceData(String groupId) {
        if (StringUtils.isEmpty(userId)) {
            userId = getLoginUserId();
        }
        Map<String, Object> param = new HashMap<>();
        param.put("groupId", groupId);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("userId", userId);
        String result = get(url + "/app/GetDeviceData", param, headerMap);
        log.info("result:" + result);
        if (StringUtils.isNotEmpty(result)) {
            JSONObject object = JSONObject.parseObject(result);
            String code = String.valueOf(object.get("code").toString());
            if ("1000".equals(code)) {
                JSONArray array = object.getJSONArray("data");
                return array;
            }
        }
        return null;
    }


    public static String get(String urlString, Map<String, Object> paramMap, Map<String, String> headerMap) {
        return HttpRequest.get(urlString).addHeaders(headerMap).form(paramMap).timeout(HttpGlobalConfig.getTimeout()).execute().body();
    }

    public static void main(String[] args) {
        //log.info(getLoginUserId());
        //log.info(getUserDeviceGroups());

        /*JSONArray array=getUserDeviceGroups();
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.getJSONObject(i);
                log.info(getDeviceData(obj.getString("groupId")).toJSONString());
            }
        }*/
        log.info(getDeviceData("").toJSONString());
    }
}
