package com.zhgd.xmgl.util;

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

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

/**
 * @program: wisdomSite
 * @description: 儋州安全帽平台对接接口
 * @author: Mr.Peng
 * @create: 2021-10-21 11:13
 **/
@Slf4j
public class GovDanzhouSafeHatUtil {

    public static String getToken(String url) {
        String token = null;
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("grant_type", "password");
            param.put("client_id", "scodx");
            param.put("client_secret", "scodx");
            param.put("scope", "gettoken");
            param.put("username", "dz_account");
            param.put("password", "dz_account");
            log.info("安全帽请求路径--------:" + url + "/oauth/token");
            String result = HttpUtil.post(url + "/oauth/token", param);
            log.info("---获取token--" + result);
            if (StringUtils.isNotEmpty(result)) {
                JSONObject object = JSONObject.parseObject(result);
                String code = String.valueOf(object.get("code").toString());
                if ("0".equals(code)) {
                    JSONObject data = object.getJSONObject("data");
                    token = data.getString("access_token");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 发生账号信息
     *
     * @param url
     * @param projectSn
     * @param account
     * @param userName
     * @param token
     * @return
     */
    public static boolean sendUserInfo(String url, String projectSn, String account, String userName, String token) {
        boolean flag = false;
        Map<String, Object> param = new HashMap<>();
        param.put("project_id", projectSn);
        param.put("name", userName);
        param.put("account", account);
        //log.info(JSONUtil.toJsonStr(param));
        String result = HttpUtil.post(url + "/async/danzhou/manager/detail?access_token=" + token, JSONUtil.toJsonStr(param));
        log.info("---上传账号信息--" + result);
        if (StringUtils.isNotEmpty(result)) {
            JSONObject object = JSONObject.parseObject(result);
            String code = String.valueOf(object.get("code").toString());
            if ("0".equals(code)) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 发生项目信息
     *
     * @param url
     * @param projectSn
     * @param projectname
     * @param areaCode
     * @param headPerson
     * @param headMobile
     * @param longitude
     * @param latitude
     * @param token
     * @return
     */
    public static boolean sendProjectInfo(String url, String projectSn, String projectname, String areaCode, String headPerson,
                                          String headMobile, String longitude, String latitude, String token) {
        boolean flag = false;
        Map<String, Object> param = new HashMap<>();
        param.put("project_id", projectSn);
        param.put("name", projectname);
        param.put("area_code", areaCode);
        param.put("head_person", headPerson);
        param.put("head_mobile", headMobile);
        JSONObject coordinate = new JSONObject();
        coordinate.put("lat", latitude);
        coordinate.put("lnt", longitude);
        param.put("coordinate", coordinate);
        //log.info(JSONUtil.toJsonStr(param));
        String result = HttpUtil.post(url + "/async/danzhou/project/detail?access_token=" + token, JSONUtil.toJsonStr(param));
        log.info("---上传项目信息--" + result);
        if (StringUtils.isNotEmpty(result)) {
            JSONObject object = JSONObject.parseObject(result);
            String code = String.valueOf(object.get("code").toString());
            if ("0".equals(code)) {
                flag = true;
            }
        }
        return flag;
    }


    public static void main(String[] args) {
        log.info(getToken("http://122.193.33.115:8102"));
    }
}
