package org.jeecg.common.util.shumai;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;

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

@Slf4j
public class ShumaiUtil {

    private static String appId;

    private static String appSecurity;

    /**
     * 模糊查询企业工商信息
     * @param keyword
     * @return
     */
    public static Result<JSONArray> fuzzySearchBusInfo(String keyword) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        //参数
        String pageNo= "1";
        String pageSize= "10";

        //接口地址
        String url = "https://api.shumaidata.com/v4/business3/get";

        //MD5Utils文件请联系工作人员索取
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("keyword",keyword);
        params.put("pageNo",pageNo);
        params.put("pageSize",pageSize);

        //HttpUtils文件请联系工作人员索取
        String resultStr = HttpUtils.get(url,null, params);
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        if(resultObj.getBoolean("success")) {
            JSONObject dataObj = resultObj.getJSONObject("data");
            JSONArray result = dataObj.getJSONArray("data");
            return Result.ok(result);
        }else {
            return Result.ok(new JSONArray());
        }
    }

    public static Result<JSONObject> getBusInfo(String keyword) {
        String timestamp = String.valueOf(System.currentTimeMillis());

        //接口地址
        String url = "https://api.shumaidata.com/v4/business2/get";
        //MD5Utils文件请联系工作人员索取
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("keyword",keyword);

        //HttpUtils文件请联系工作人员索取
        String resultStr = HttpUtils.get(url,null,params);
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        if(resultObj.getBoolean("success")) {
            JSONObject dataObj = resultObj.getJSONObject("data");
            JSONObject result = dataObj.getJSONObject("data");
            return Result.ok(result);
        }else {
            return Result.ok(new JSONObject());
        }
    }

    public static String getAppId() {
        return appId;
    }

    public static void setAppId(String appId) {
        ShumaiUtil.appId = appId;
    }

    public static String getAppSecurity() {
        return appSecurity;
    }

    public static void setAppSecurity(String appSecurity) {
        ShumaiUtil.appSecurity = appSecurity;
    }
}
