package com.zhgd.xmgl.util;

import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.zhgd.jeecg.common.execption.OpenAlertException;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @program: wisdomSite
 * @description: 配电箱锁
 * @author: Mr.Peng
 * @create: 2021-04-08 18:22
 **/
@Slf4j
public class ElectricBoxLockUtils {

    private static String company = "JUCE";
    private static String apiKey = "iu07phDozQvL9DzD/iu0lg==";
    private static String url = "https://saas.mglok.com";

    public static boolean unlock(String imei, String deviceId, String userId, String devName) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("company", company);
        headerMap.put("apiKey", apiKey);
        Map<String, String> data = new HashMap<>();
        data.put("username", "admin");
        data.put("siteId", "1");
        data.put("userId", userId);
        data.put("deviceName", devName);
        data.put("siteName", "机柜");
        data.put("deviceId", deviceId);
        data.put("imei", imei);
        data.put("index", "1");
        String resJson = post(url + "/sdmp/device/unlock", JSONUtil.toJsonStr(data), headerMap);
        log.info("--" + resJson);
        if (resJson != null && resJson.length() > 0) {
            JSONObject obj = JSONObject.parseObject(resJson);
            if ("0".equals(obj.get("code").toString())) {
                return true;
            } else {
                throw new OpenAlertException(MessageUtil.get("failErr"));
            }
        } else {
            throw new OpenAlertException(MessageUtil.get("failErr"));
        }
    }


    public static boolean setCallbackUrl(String callbackUrl) {
        String oldCallbackUrl = selectCallbackUrl();
        if (oldCallbackUrl == null || "null".equals(oldCallbackUrl) || "".equals(oldCallbackUrl)) {
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("company", company);
            headerMap.put("apiKey", apiKey);
            JSONObject data = new JSONObject();
            data.put("callbackUrl", callbackUrl);
            String resJson = post(url + "/sdmp/api/subscriptions", data.toJSONString(), headerMap);
            log.info(resJson);
            if (resJson != null && resJson.length() > 0) {
                JSONObject obj = JSONObject.parseObject(resJson);
                if ("0".equals(obj.get("code").toString())) {
                    return true;
                } else {
                    throw new OpenAlertException(MessageUtil.get("failErr"));
                }
            } else {
                throw new OpenAlertException(MessageUtil.get("failErr"));
            }
        } else {
            return true;
        }
    }

    public static String selectCallbackUrl() {
        String callbackUrl = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("company", company);
        headerMap.put("apiKey", apiKey);
        Map<String, Object> data = new HashMap<>();
        log.info(url + "/sdmp/api/subscriptions");
        String resJson = get(url + "/sdmp/api/subscriptions", data, headerMap);
        log.info(resJson);
        if (resJson != null && resJson.length() > 0) {
            JSONObject obj = JSONObject.parseObject(resJson);
            if ("0".equals(obj.get("code").toString())) {
                JSONObject jsonObject = (JSONObject) obj.get("data");
                callbackUrl = jsonObject.getString("callbackUrl");
            }
        }
        return callbackUrl;
    }

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

    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(selectCallbackUrl());
        //log.info(setCallbackUrl("http://36.137.53.203:9090/xmgl/lockCallback/electricBoxLockCallbackUrl"));
        //log.info(selectCallbackUrl());
        /*Map<String, String> data=new HashMap<>();
        data.put("username","admin");
        data.put("siteId","1");
        data.put("userId","1");
        data.put("deviceName","测试");
        data.put("siteName","机柜");
        data.put("deviceId","1111111111");
        data.put("imei","864162046410679");
        data.put("index","1");*/
        boolean flag = unlock("864162046410679", "1111111111", "1", "测试");
        log.info("{}", flag);
        if (flag) {
            log.info("开锁成功");
        } else {
            log.info("开锁失败");
        }

    }
}
