package com.gxwebsoft.apps.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.val;
import org.apache.commons.codec.binary.Base64;

import java.security.MessageDigest;
import java.util.Date;

public class SfExpress {

    //开发者id
    private final Long devId= 1651421896L;
    //开发者密钥
    private final String devKey="2f10570c5057570fe0e488a425a6d813";
    //正式环境
    private final String serverHost="https://openic.sf-express.com";
    //店铺ID
    private final String shopId="3243279847393";




    public static void main(String[] args) {
        try {
            SfExpress api = new SfExpress();
            String postData = api.orderOnlineByJson();
            String sign = generateOpenSign(postData,api.devId, api.devKey);
            String url = api.serverHost.concat("/open/api/external/createorder?sign=").concat(sign);
            String result = HttpUtil.post(url,postData);
            System.out.println("result = " + result);
            val resultObj = JSON.parseObject(result);
            JSONObject resultData = JSON.parseObject(resultObj.getString("result"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String generateOpenSign(String postData, Long appId, String appKey) throws Exception {
      StringBuilder sb =  new StringBuilder();
      sb.append(postData);
      sb.append("&" + appId + "&" + appKey);
      System.out.println("sb = " + sb);
      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] md5 = md.digest(sb.toString().getBytes("utf-8"));
      int i;
      StringBuffer buf = new StringBuffer("");
      for (int offset = 0; offset < md5.length; offset++) {
        i = md5[offset];
        if (i < 0) {
          i += 256;
        }
        if (i < 16) {
          buf.append("0");
        }
        buf.append(Integer.toHexString(i));
      }

      return Base64.encodeBase64String(buf.toString().getBytes("utf-8"));
    }

    //即时查询接口
    public String orderOnlineByJson() throws Exception{
        // 组装应用级参数
      JSONObject json = new JSONObject();
        json.put("dev_id",devId);
        json.put("shop_id",shopId);
        json.put("shop_order_id", RandomUtil.randomNumbers(16));
        json.put("order_source","1");
      long timestamp = new Date().getTime();
        json.put("order_time",timestamp/1000);
        json.put("push_time",timestamp/1000);
        json.put("version",19);
      JSONObject receive = new JSONObject();
        receive.put("user_name","顺丰同城");
        receive.put("user_phone","18978189563");
        receive.put("user_address","北京市海淀区学清嘉创大厦A座15层");
        receive.put("user_lng","116.354787");
        receive.put("user_lat","40.030613");
        json.put("receive",receive);
      JSONObject orderDetail = new JSONObject();
        orderDetail.put("total_price",100);
        orderDetail.put("product_type",1);
        orderDetail.put("weight_gram",100);
        orderDetail.put("product_num",1);
        orderDetail.put("product_type_num",1);
        json.put("order_detail",orderDetail);
        return json.toString();
    }
  //中文转Unicode
  /**
   * @Title: unicodeEncode
   * @Description: unicode编码 将中文字符转换成Unicode字符
   * @param string
   * @return
   */
  public String unicodeEncode(String string) {
    char[] utfBytes = string.toCharArray();
    String unicodeBytes = "";
    for (int i = 0; i < utfBytes.length; i++) {
      String hexB = Integer.toHexString(utfBytes[i]);
      if (hexB.length() <= 2) {
        hexB = "00" + hexB;
      }
      unicodeBytes = unicodeBytes + "\\u" + hexB;
    }
    return unicodeBytes;
  }

}

