package com.ruoyi.business.util;

import cn.hutool.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SwtToken {

    private static final String appid = "1048576-3350528";

    private static final String secret = "9922B9D6638BC6F1824D1DA461B1DED0";

    private static final String grantType = "client_credential";

    private static final String orgId = "1048576";

    private static final String idType = "mobile";

    private static final String appTokenUrl = "http://50.16.172.231:80/open/apigw/v1/apptoken/create";

    private static final String staffIdUrl = "http://50.16.172.231:80/open/apigw/v2/staffs/id_mapping/fetch";

    private static final Logger logger = LoggerFactory.getLogger(SwtToken.class);





    public static String getSwtAppToken(){
        String url =   appTokenUrl + "?grant_type=" + grantType + "&appid=" + appid + "&secret=" + secret;
        String resText = JsoupUtils.getJsoupDocGet(url);
        logger.info("苏网通" + resText);
        JSONObject jsonObject = new JSONObject(resText);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        String appToken = dataObject.getStr("app_token");
        return appToken;
    }


    public static String getStaffIdByPhone(String phone,String appToken){
        String url =  staffIdUrl + "?app_token=" + appToken +
                "&org_id=" + orgId + "&id_type=" + idType + "&id_value=86-" + phone;
        String resText = JsoupUtils.getJsoupDocGet(url);
        JSONObject jsonObject = new JSONObject(resText);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        if (dataObject != null) {
            String staffId = dataObject.getStr("staffId");
            return staffId;
        }
        return "";
    }
}
