package com.jkcredit.invoice.util;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

/**
 * @author mashengyi
 */
public class QueryDemoTest1 {

    /**
     * 测试地址
     */
    private static final String URL = "http://api.jkcredit.com:58080/gateway?api=credit.sec.data";

    /**
     * 分配的appKey
     */
    private static final String APP_KEY = "dzka";

    /**
     * 分配的appSecret
     */
    private static final String APP_SECRET = "0551954004ED4EED99B40E0E5023E7966AC084DB";

    /**
     * 企业编号
     */
    private static final String COMPANY_NUM = "11194852";

    /**
     * 方法入口
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        QueryDemoTest1 demo = new QueryDemoTest1();
        demo.runMainService();
    }

    /**
     * 流转字符串
     *
     * @param is
     * @return
     */
    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }

    /**
     * 调用主接口
     */
    public void runMainService() {
        try {
            /**
             * 实名demo
             */
            JSONObject paramJsonObj = initParamJsonObjRealname();
            String ret = postClient(URL, paramJsonObj);
            System.out.println(ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 整合参数
     *
     * @return
     */
    public JSONObject initParamJsonObjRealname() {
        /**
         * 基本参数
         */
        JSONObject paramJsonObj = new JSONObject();
        //CMCC_MOBILE_CHECK_V8//CMCC_3RD_V2//CMCC_3RD_DETAIL_V1//
        paramJsonObj.put("api", "B2B_QUERY_TRADE_APPLY_V1");
        paramJsonObj.put("appKey", APP_KEY);
        paramJsonObj.put("appSecret", APP_SECRET);


        /**
         * 业务参数
         */
        JSONObject dataJson = initMainServiceParamJsonObjRealname();
        paramJsonObj.put("data", dataJson);
        return paramJsonObj;
    }

    public JSONObject initMainServiceParamJsonObjRealname() {
        /**
         * 具体业务参数放在data中
         */
        JSONObject dataJson = new JSONObject();
        dataJson.put("companyNum", COMPANY_NUM);
        dataJson.put("cardId", "33012308230220077349");
        dataJson.put("tradeId", "G150331002002040100302024090308000210");

        return dataJson;
    }

    /**
     * 实现http post请求 注意编码 UTF-8
     *
     * @param url
     * @param paramObj
     * @return
     */
    public String postClient(String url, JSONObject paramObj) {
        String str = "";
        HttpClient client = null;
        PostMethod method = null;
        try {
            client = new HttpClient();
            method = new PostMethod(url);
            method.setRequestHeader("Content-Type", "application/json;charset=utf-8");
            NameValuePair[] param = new NameValuePair[paramObj.size()];
            @SuppressWarnings("unchecked")
            Iterator<String> keys = paramObj.keys();
            int i = 0;
            while (keys.hasNext()) {
                String key = keys.next();
                String value = paramObj.getString(key);
                param[i] = new NameValuePair(key, value);
                i++;
            }
            method.setRequestBody(param);
            client.executeMethod(method);
            str = convertStreamToString(method.getResponseBodyAsStream());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
        return str;
    }

}
