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 QueryDemoTest_opera {

    /**
     * 测试地址
     */
    private static final String URL = "http://127.0.0.1:18081/api/rest";

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

    /**
     * 分配的appSecret
     */
    private static final String APP_SECRET = "7697CE9BB1D5AC46CFDDA238328AD34EA33E483G";


    /**
     * 方法入口
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        QueryDemoTest_opera demo = new QueryDemoTest_opera();
        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", "WAYBILL_OPERATORVALIDCODE");
        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("taxplayerCode", "91111205MA01BCJJ8U");
        dataJson.put("validCode", "122222");

        dataJson.put("operatorName", "张三");
        dataJson.put("operatorMobile", "19910751229");
        dataJson.put("identificationId", "410327199311279656");
        dataJson.put("contractFileName", "测试msy.pdf");
        //dataJson.put("base64Str", Base64Util.getPdfBinary(new File("/Users/mashengyi/Downloads/b10852e4-430a-4222-b088-41f12d41b941.pdf")));

        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;
    }

}
