package com.iformall.utils.car;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxCouponOrder;
import com.iformall.utils.MapUtil;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 停简单 停车管理
 */
@Component
public class TJDUtil {

    public static final String TJD_URL = "url";
    public static final String TJD_ACCOUNT = "partner";
    public static final String TJD_ACCOUNT_KEY = "key";
    public static final String TJD_VERSION = "version";

    public static final String TJD_CHARSET = "charset";
    public static final String TJD_UTF_8 = "utf-8";

    public static final String TJD_RETURN_CODE = "returnCode";
    public static final String TJD_RETURN_SUCC = "T";
    public static final String TJD_RETURN_FAIL = "F";

    public static final String TJD_IS_SUC = "isSuccess";
    public static final String TJD_ERR_MSG = "errorMSG";
    public static final String TJD_SUC = "0";
    public static final String TJD_ERR_1 = "1"; // 业务级失败(不进行重发)
    public static final String TJD_ERR_2 = "2"; // 系统级失败(有重发机制)
    public static final String TJD_PARK_ID = "parkId";
    public static final String TJD_PARK_NAME = "parkName";
    public static final String TJD_CAR_NUMBER = "carNum";
    public static final String TJD_TRADE_ID = "tradeId";
    public static final String TJD_IN_DT = "inDt";
    public static final String TJD_OUT_DT = "outDt";
    public static final String TJD_PARK_AMT = "parkAmount";
    public static final String TJD_PAY_AMT = "payAmount";
    public static final String TJD_PAY_DT = "payDt";
    public static final String TJD_FREE_MINS = "freeMins";
    public static final String TJD_ACCOUNT_ID = "accountId";
    public static final String TJD_PREPAY_TYPE = "prePayType";
    public static final String TJD_PREPAY_TYPE_3 = "3";    // 三方合作方支付
    public static final String TJD_PREPAY_TYPE_20 = "20";  // 手机场内支付
    public static final String TJD_DETAIL_LIST = "detailList";
    public static final String TJD_OUT_TRADE_NO = "outTradeNo";
    public static final String TJD_TYPE = "type";
    public static final String TJD_TYPE_DISCOUNT = "0";
    public static final String TJD_TYPE_COUPON = "1";
    public static final String TJD_COUPON_TYPE = "couponType";
    public static final String TJD_CHANNEL = "channel";
    public static final String TJD_AMOUNT = "amount";
    public static final String TJD_MINUTES = "minutes";
    public static final String TJD_CREDIT = "integral";
    public static final String TJD_MEMO = "memo";

    public static final SimpleDateFormat dateInFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat dateOutFormat = new SimpleDateFormat("yyyyMMddHHmmss");


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

    private static String baseurl = "http://prep.tingjiandan.com/openapi/gateway";

    private static String partner = "fb21a5b2f7064ff5afb8288d5bb48ca8";
    private static String key     = "bc2f09be8f5b4227898346cf6fe451c2";
    private static String version = "1.0";

    //
    // {"url":"http://prep.tingjiandan.com/openapi/gateway","partnerId":"fb21a5b2f7064ff5afb8288d5bb48ca8","key":"bc2f09be8f5b4227898346cf6fe451c2","parkId":"7715622dff834b34a44448b801c27607", "version":"1.0.0"}
    //

    public static void main(String[] args) throws Exception {
        String carNumber = "京XAZMF1";

        String result = infoForFreeMins(baseurl, partner, key, version, carNumber);

        JSONObject obj = JSON.parseObject(result);
        /*

        Map<String, String> paramMap1 = MapUtil.getOrderMap();
        paramMap1.put("tradeId", obj.getString("tradeId"));
        paramMap1.put("deductionAmount", obj.getString("totalAmount"));
        paramMap1.put("outTradeNo", "utf-8");
        paramMap1.put("accountId", obj.getString("accountId"));
        paramMap1.put(TJD_CHARSET, "utf-8");
        paramMap1.put(TJD_ACCOUNT, partner);
        paramMap1.put(TJD_VERSION, version);

        deductionNotSettle(baseurl, key, paramMap1);
        */
    }

    /**
     * md5算法
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */

    public static String md5(String data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data.getBytes());
        StringBuffer buf = new StringBuffer();
        byte[] bits = md.digest();
        for(int i=0;i<bits.length;i++){
            int a = bits[i];
            if(a<0) a+=256;
            if(a<16) buf.append("0");
            buf.append(Integer.toHexString(a));
        }
        return buf.toString();
    }

    /**
     * getCurrentDate 获取当前时间
     */
    public static String getCurrentDate() {
        Date currentDate = new Date();
        String  timestamp = dateInFormat.format(currentDate);
        return timestamp;
    }

    /**
     * 签名
     * @param map
     * @param keysign
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getSign(Map<String,String> map, String keysign) throws NoSuchAlgorithmException {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue() != null || entry.getValue() != "") {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        sb.deleteCharAt(sb.length()-1);// ?
        sb.append(keysign);
        logger.debug("签名前的字符串输出："+sb.toString());
        String signValue = md5(sb.toString()).toUpperCase();
        logger.debug("sign: " + signValue);
        return signValue;
    }

    /**
     * 注册车牌
     * @param url
     * @param partner
     * @param key     // 密钥
     * @param version
     *
     *  {
     *    "carNum": "京A45781",
     *    "carNumColor": "blue",
     *    "phone": "15210501514",
     *    "outCarId": "45454545454",
     *    "partner": "5836b8b52ada463ebc6199579f029566",
     *    "timestamp": "2016-05-26 11:30:10",
     *    "version": "1.0"
     *  }
     * @throws Exception
     */
    public static String registerCar(String url, String partner, String key, String version,
                                     String carNum, String carNumColor, String phone, String outCarId) {
        String service = "parkhub.car.register";

        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put(TJD_CAR_NUMBER, carNum);
        paramMap.put("carNumColor", carNumColor);
        if (phone != null) {
            paramMap.put("phone", phone);
        }
        if (outCarId != null) {
            paramMap.put("outCardId", outCarId);
        }
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);

        /*
        {
            "timestamp": "1464235239561",
            "returnCode": "T",
            "errorMsg": "",
            "returnMsg": "OK",
            "isSuccess": "0",
            "carId": "5836b8b52ada463ebc6199579f029561"
         }*/
        return result;
    }

    /**
     * 注销车牌
     * @param url
     * @param partner
     * @param key     // 密钥
     * @param version
     *
     *  {
     *    "service": "parkhub.car.writeOff",
     *    "version":"1.0",
     *    "sign":"3347b109a1e44f3fd5baa78b74a84948",
     *    "partner":"5836b8b52ada463ebc6199579f029566",
     *    "carId":"3347b109a1e44f3fd5baa78b74a84948",
     *    "timestamp":"2016-05-26 11:30:10",
     *    "charset":"utf-8",
     *    "signType":"md5"
     *  }
     * @throws Exception
     */
    public static String writeOffCar(String url, String partner, String key, String version,
                                     String carId) {
        String service = "parkhub.car.writeOff";

        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put("carId", carId);
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);

        /*
        {
            "timestamp": "1464235239561",
            "returnCode": "T",
            "errorMsg": "",
            "returnMsg": "OK",
            "isSuccess": "0",
         }*/
        return result;
    }


    /**
     * 在场订单查询
     * @param url
     * @param partner
     * @param key     // 密钥
     * @param version
     *
     *  {
     *    "carNum": "京A45781",
     *    "freeMins": "0",
     *    "partner": "5836b8b52ada463ebc6199579f029566",
     *    "pmParkId": "5836b8b52ada463ebc6199579f029565",
     *    "timestamp": "2016-05-26 11:30:10",
     *    "tradeId": "5836b8b52ada463ebc6199579f029565",
     *    "version": "1.0"
     *  }
     * @throws Exception
     */
    public static String infoForFreeMins(String url, String partner, String key, String version, String carNum) {
        String service = "parkhub.order.infoForFreeMins";

        String freeMins = "0";

        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put(TJD_CAR_NUMBER, carNum);
        //paramMap.put("carNumColor", "");
        //paramMap.put("pmParkId", "");
        paramMap.put(TJD_FREE_MINS, freeMins);
        //paramMap.put("tradeId", "");
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);

        /*
        {
          "wLon":"116.310335",
          "returnCode":"T",
          "lon":"116.316813",
          "accountId":"818a6ac865e841119ec9aab21c5e5a24",
          "freeThroughTime":"10",
          "customerServicePhone":"400-001-0606",
          "tradeId":"2ae003431584495081014428c90173ab",
          "returnMsg":"OK",
          "outDt":"20190920110443",
          "isSuccess":"0",
          "miniProgram":"{\"path\": \"pages/pre_pay/index/main\",\"extraData\": {\"prePayType\": \"16\",\"channel\": \"30166\",\"isShowDetail\": \"true\",\"partnerId\": \"956ca31ac8464cf7b6a876e8887912f3\",\"orderId\": \"2ae003431584495081014428c90173ab\"}}","pmParkId":"7715622dff834b34a44448b801c27607","unPayAmount":"88.12","timestamp":"1568948681580","errorMSG":"","parkTime":"81629","wLat":"39.93609","totalAmount":"88.12","paidAmount":"0.00","freeMinsAmount":"0.00","payUrl":"https://prep.tingjiandan.com/tcweixin/letter/prePay/pagePayInPark?prePayType=16&channel=30166&isShowDetail=true&partnerId=&orderId=2ae003431584495081014428c90173ab&returnUrl=",
          "lat":"39.942078",
          "inDt":"20190919122414"}
        */
        return result;
    }

    //// TODO
    /**
     * deductionNotSettle 停车费批量抵扣接口
     * @param url
     * @param partner
     * @param key     // 密钥
     * @param version
     * type 可选值:0=抵扣、1=优惠券
     * couponType type=1时必填，可选值:0=金额、1=时长、2=全免、3=时间区间
     * channel 可选值: type=0时:5013=微信、5014=支付宝、5015=抵扣其他
     *                type=1时:2202=会员等级、2203=会员积分、2204=会员卡券、2205=会员补贴、2206=会员其他
     * amount: type=0时、couponType=0必填，单位(元)，保留小数点后两位，金额类型使用
     * minutes: couponType=1时必填，单位(分钟)，时长类型使用
     * integral: 大于等于0的整数值，积分类型使用，如果使用了积分兑换，可填写积分值
     * expireDt: type=1时选填，格式:yyyyMMddHHmmss，如果不填，则永不过期，若填写，则出场时间在此 时间之后，认为此记录已经过期，不再使用
     * membershipGrade: 调用方系统会员等级标识，如:LV1、VIP2、金卡等，长度不超过64位
     * identityType: identityId非空时，此值必填，可选值:01=手机号、02=会员卡号、03=车牌号、04=身份证号
     * identityId: identityType非空时，此值必填，对应身份标识类型的唯一值，长度不超过100位
     *  {
          "service": "parkhub.order.deductionForDetail",
          "partner": "120a565de377427184de35ca0f320764",
          "sign": "3347b109a1e44f3fd5baa78b74a84948",
          "signType": "md5",
          "charset": "utf-8",
          "version": "1.0",
          "timestamp": "2016-05-26 11:30:10",
          "tradeId": "c39d3aac49d04e12a187681e6189f841",
          "accountId": "a9b2963c98344bb292e0fa3f7dd70946",
          "prePayType": "3",
          "giveFreeTimeOut": "false",
          "detailList":"[{
             \"outTradeNo\":\"PLDKY01\",
             \"identityType\":\"01\",
             \"identityId\":\"17710111111\",
             \"membershipGrade\":\"LV1\",
             \"amount\":\"12.50\",
             \"type\":\"0\",
             \"channel\":\"5013\",
             \"memo\":\"批量抵扣-金额-微信\"
          },{
             \"outTradeNo\":\"PLDKY02\",
             \"identityType\":\"01\",
             \"identityId\":\"17710111111\",
             \"membershipGrade\":\"LV1\",
             \"couponType\":\"1\",
             \"minutes\":\"60\",
             \"type\":\"1\",
             \"channel\":\"2204\",
             \"memo\":\"批量抵扣-时长-会员卡券\"
          },{
             \"outTradeNo\":\"PLDKY03\",
             \"identityType\":\"01\",
             \"identityId\":\"17710111111\",
             \"membershipGrade\":\"LV1\",
             \"couponType\":\"3\",
             \"startDt\":\"20180901110000\",
             \"endDt\":\"20180901170000\",
             \"integral\":\"500\",
             \"type\":\"1\",
             \"channel\":\"2205\", \"memo\":\"批量抵扣-区间-积分-会员补贴\"
          }]"
        }
     * @throws Exception
     */
    public static String deductionForDetail(
            String url, String partner, String key, String version,
            String tradeId, String accountId,
            WxCouponOrder couponOrder, WxCoupon coupon) {
        // 为指定订单抵扣停车费，如果此时车辆已经出场，返回isSuccess错误码为3，停简单系统不再接收此笔抵扣信息，对方系统 需要给用户发起退款。
        String service = "parkhub.order.deductionForDetail";
        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put(TJD_TRADE_ID, tradeId);
        paramMap.put(TJD_ACCOUNT_ID, accountId);
        paramMap.put(TJD_PREPAY_TYPE, TJD_PREPAY_TYPE_3);

        JSONArray detailArr = new JSONArray();
        JSONObject detailObj = new JSONObject();
        detailObj.put(TJD_OUT_TRADE_NO, String.valueOf(couponOrder.getId()));
        detailObj.put(TJD_TYPE, TJD_TYPE_COUPON);
        detailObj.put(TJD_CHANNEL, "2204");
        String memo = "会员优惠券";
        if (coupon.getUnit().equals(0)) {
            detailObj.put(TJD_COUPON_TYPE, "0");
            detailObj.put(TJD_AMOUNT, coupon.getPriceStr());
            memo += "-金额";
        } else {
            detailObj.put(TJD_COUPON_TYPE, "1");
            detailObj.put(TJD_MINUTES, String.valueOf(coupon.getPrice()*60));
            memo += "-时长";
        }
        detailObj.put(TJD_MEMO, memo);
        detailArr.add(detailObj);
        paramMap.put(TJD_DETAIL_LIST, JSON.toJSONString(detailArr));

        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);
        /*
        "{\"timestamp\":\"1528534986636\",
        \"returnCode\":\"T\",
        \"prePayId\":\"7a1d52aa77ca487db4e61130394285bd\",
        \"errorMSG\":\"\",
        \"returnMsg\":\"OK\",
        \"isSuccess\":\"0\"}[\\r][\\n]"
         */
        return result;
    }

    /**
     * deductionNotSettle 停车费无结算抵扣
     * @param url
     * @param partner
     * @param key     // 密钥
     * @param version
     *  {
          "service": "",
          "version":"1.0",
          "sign":"3347b109a1e44f3fd5baa78b74a84948",
          "partner":"5836b8b52ada463ebc6199579f029566",
          "timestamp":"2016-05-26 11:30:10",
          "charset":"utf-8",
          "signType":"md5",
          "tradeId": "224478781859452794f2a68a756fe461",
          "deductionAmount": "0.1",
          "outTradeNo":"4738473847834378",
          "accountId":"3347b109a1e44f3fd5baa78b74a84941",
        }
     * @throws Exception
     */
    public static String deductionNotSettle(String url, String partner, String key, String version,
                                            String tradeId, String outTradeNo, String accountId, String deductionAmount) {
        // TODO
        // 为指定订单抵扣停车费，如果此时车辆已经出场，返回isSuccess错误码为3，
        // 停简单系统不再接收此笔抵扣信息，对方系统需要给用户发起退款(后续业务中无结算)
        String service = "parkhub.order.deductionNotSettle";
        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put("tradeId", tradeId);
        paramMap.put("deductionAmount", deductionAmount);
        paramMap.put("outTradeNo", outTradeNo);
        paramMap.put("accountId", accountId);
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);
        /*
        "{\"timestamp\":\"1528534986636\",
        \"returnCode\":\"T\",
        \"prePayId\":\"7a1d52aa77ca487db4e61130394285bd\",
        \"errorMSG\":\"\",
        \"returnMsg\":\"OK\",
        \"isSuccess\":\"0\"}[\\r][\\n]"
         */
        return result;
    }

    /**
     * bindTime 绑定时长优惠券
     * @param url
     * @param partner //
     * @param key     // 密钥
     * @param version // 1.0
     *
     *  {
     *      "service": "parkhub.ticket.bindTime",
     *      "version":"1.0",
     *      "sign":"3347b109a1e44f3fd5baa78b74a84948",
     *      "partner":"5836b8b52ada463ebc6199579f029566",
     *      "timestamp":"2016-05-26 11:30:10",
     *      "charset":"utf-8",
     *      "signType":"md5",
     *      "tradeId": "224478781859452794f2a68a756fe461",
     *      "outTicketNo": "224478781859452794f2a68a756fe461",
     *      "accountId":"3347b109a1e44f3fd5baa78b74a84941",
     *      "prePayType": "3",
     *      "minutes": "30",
     *      "memo": "测试绑定时长优惠券"
     *  }
     * @throws Exception
     */
    public static String bindTime(String url, String partner, String key, String version,
                                  String tradeId, String outTicketNo, String accountId, String prePayType, String minutes, String memo) throws Exception {
        String service = "parkhub.ticket.bindTime";

        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put("tradeId", tradeId);
        paramMap.put("outTicketNo", outTicketNo);
        paramMap.put("accountId", accountId);
        paramMap.put("prePayType", prePayType);
        paramMap.put("minutes", minutes);
        paramMap.put("memo", memo);
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);
        /*{
                "timestamp": "1464247094636",
                "returnCode": "T",
                "errorMsg": "",
                "returnMsg": "OK",
                "isSuccess": "0",
                "ticketId": "224478781859452794f2a68a756fe441"
          }*/
        return result;
    }

    /**
     * 绑定区间优惠券
     * @param url
     * @param partner
     * @param key     // 密钥
     * @param version
     *
     *  {
     *      "service": "parkhub.ticket.bindPeriod",
     *      "version": "1.0",
     *      "sign": "3347b109a1e44f3fd5baa78b74a84948",
     *      "partner": "5836b8b52ada463ebc6199579f029566",
     *      "timestamp": "2016-05-26 11:30:10",
     *      "charset": "utf-8",
     *      "signType": "md5",
     *      "tradeId": "224478781859452794f2a68a756fe461",
     *      "outTicketNo": "224478781859452794f2a68a756fe461",
     *      "accountId": "3347b109a1e44f3fd5baa78b74a84941",
     *      "prePayType": "3",
     *      "startDt": "20160526113010",
     *      "endDt": "20160526123010",
     *      "expireDt": "20160527113010",
     *      "memo": "测试绑定时长优惠券"
     *  }
     * @throws Exception
     */
    public static String bindPeriod(String url, String partner, String key, String version,
                                    String tradeId, String outTicketNo, String accountId, String prePayType,
                                    String startDt, String EndDt, String expireDt, String memo) throws Exception {
        String service = "parkhub.ticket.bindPeriod";

        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put("tradeId", tradeId);
        paramMap.put("outTicketNo", outTicketNo);
        paramMap.put("accountId", accountId);
        paramMap.put("prePayType", prePayType);
        paramMap.put("startDt", startDt);
        paramMap.put("EndDt", EndDt);
        paramMap.put("expireDt", expireDt);
        paramMap.put("memo", memo);
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);
        /*{
                "timestamp": "1464247094636",
                "returnCode": "T",
                "errorMsg": "",
                "returnMsg": "OK",
                "isSuccess": "0",
                "ticketId": "224478781859452794f2a68a756fe441"
          }*/
        return result;
    }

    /**
     * 绑定区间优惠券
     * @param url
     * @param partner     // 密钥
     * @param key     // 密钥
     * @param version
     *
     *  {
     *        "service": "parkhub.ticket.unbind",
     *        "version": "1.0",
     *        "sign": "3347b109a1e44f3fd5baa78b74a84948",
     *        "partner": "5836b8b52ada463ebc6199579f029566",
     *        "timestamp": "2016-05-26 11:30:10",
     *        "charset": "utf-8",
     *        "signType": "md5",
     *        "tradeId ": "3347b109a1e44f3fd5baa78b74a84948",
     *        "ticketId": "224478781859452794f2a68a756fe461"
     *  }
     * @throws Exception
     */
    public static String unbind(String url, String partner, String key, String version,
                                String tradeId, String ticketid) throws Exception {
        String service = "parkhub.ticket.unbind";

        Map<String, String> paramMap = MapUtil.getOrderMap();
        paramMap.put("tradeId", tradeId);
        paramMap.put("ticketid", ticketid);
        paramMap.put(TJD_CHARSET, TJD_UTF_8);
        paramMap.put(TJD_ACCOUNT, partner);
        paramMap.put(TJD_VERSION, version);

        String result = Proc(url, key, service, paramMap);
        /*{
                "timestamp": "1464247094636",
                "returnCode": "T",
                "errorMsg": "",
                "returnMsg": "OK",
                "isSuccess": "0",
          }*/
        return result;
    }

    private static String Proc(String url, String key, String service, Map<String, String> paramMap) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Accept-Encoding", "UTF-8");

        String timestamp = getCurrentDate();
        paramMap.put("timestamp", timestamp);

        paramMap.put("service", service);

        String sign = null;
        try{
            sign = getSign(paramMap, key);
        }catch(NoSuchAlgorithmException e) {
            logger.error(e.getLocalizedMessage());
        }

        paramMap.put("sign", sign);
        paramMap.put("signType", "md5");

        String jsonstr = JSON.toJSONString(paramMap);
        logger.info(jsonstr);

        try {
            StringEntity se = new StringEntity(jsonstr, Consts.UTF_8);
            se.setContentType("application/json");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
            httpPost.setEntity(se);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        HttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }

        String result = null;

        //打印StatusLine
        logger.debug("StatusLine: " + response.getStatusLine());
        try{
            //获取实体
            HttpEntity httpEntity= response.getEntity();
            result = EntityUtils.toString(httpEntity, "UTF-8");
            logger.debug(result);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }

        try {                //关闭流并释放资源
            httpClient.close();
        } catch (IOException e) {
            logger.error(e.getLocalizedMessage());
        }

        return result;
    }

}

