package com.iamberry.wechat.utils;

import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by Administrator on 2017/12/26.
 */
@Component
public class KuaiDi100 {

    private static String key = "29833628d495d7a5";//授权密匙key    百度的
  /*private static String key = "06d6c2ea1bdb4ee9";//授权密匙key  自己的*/
                    /*<option value="sto">申通快递</option>
					<option value="yto">圆通快递</option>
					<option value="sf">顺丰快递</option>
					<option value="ems">邮政EMS</option>
					<option value="zto">中通快递</option>
					<option value="zjs">宅急送</option>
					<option value="yunda">韵达快递</option>
					<option value="cces">cces快递</option>
					<option value="pick">上门提货</option>
					<option value="htky">汇通快递</option>
					<option value="ttkdex">天天快递</option>
					<option value="stars">星晨急便</option>
					<option value="jd">京东快递</option>
					<option value="01">其他</option>
					<option value="02">上门送货</option>*/

    public String logisticsConversions(String code){
        if(code == null || code.equals("")){
            return null;
        }
        switch (code){
            case "sto":
                code = "shentong";
                break;
            case "yto":
                code = "yuantong";
                break;
            case "sf":
                code = "shunfeng";
                break;
            case "ems":
                code = "ems";
                break;
            case "eyb":
                code = "ems";
                break;
            case "zto":
                code = "zhongtong";
                break;
            case "zjs":
                code = "zhaijisong";
                break;
            case "yunda":
                code = "yunda";
                break;
            case "cces":
                code = "cces";
                break;
            case "pick":
                code = "pick";
                break;
            case "htky":
                code = "huitongkuaidi";
                break;
            case "ttkdex":
                code = "tiantian";
                break;
            case "stars":
                code = "xingchenjibian";
                break;
            case "jd":
                code = "jd";
                break;
            case "01":
                code = "01";
                break;
            case "02":
                code = "01";
                break;
        }
        return code;
    }
    /**
     *  快递对应代码
     *  EMS 		ems/eyb
     * 	申通快递  	shentong
     * 	顺丰速运 	shunfeng
     * 	圆通速递 	yuantong
     * 	韵达快运		yunda
     *	中通快递		zhongtong
     *	百世快递		huitongkuaidi
     *	天天快递		tiantian
     *	宅急送		zhaijisong
     *	邮政国内包裹	youzhengguonei
     *	邮政国际包裹	youzhengguoji
     *	EMS国际快递	emsguoji
     */
    public static void main(String[] agrs){
        KuaiDi100 kuaidi = new KuaiDi100();
        System.out.println(kuaidi.getExpressInfo("ems","9754210807242"));
    }
    /**
     * 查询快递信息
     * @param com 快递公司代码
     * @param nu 快递单号
     * @return
     */
    public JSONObject getExpressInfo(String com , String nu){
        JSONObject ret = null;
        try
        {
            com = logisticsConversions(com);
            StringBuilder sb = new StringBuilder();
            sb.append("http://api.kuaidi100.com/api?id=");
            sb.append(KuaiDi100.key);
            sb.append("&com=").append(com);
            sb.append("&nu=").append(nu);
            sb.append("&show=0&muti=1&order=desc");
            URL url= new URL(sb.toString());
            URLConnection con=url.openConnection();
            con.setAllowUserInteraction(false);
            InputStream urlStream = url.openStream();
            String type = con.guessContentTypeFromStream(urlStream);
            String charSet=null;
            if (type == null)
                type = con.getContentType();

            if (type == null || type.trim().length() == 0)
                return null;

            if(type.indexOf("charset=") > 0)
                charSet = type.substring(type.indexOf("charset=") + 8);

            byte b[] = new byte[10000];
            int numRead = urlStream.read(b);
            String content = new String(b, 0, numRead, "UTF-8");
            while (numRead != -1) {
                numRead = urlStream.read(b);
                if (numRead != -1) {
                    //String newContent = new String(b, 0, numRead);
                    String newContent = new String(b, 0, numRead, "UTF-8");
                    content += newContent;
                }
            }
            ret = JSONObject.fromObject(content);
            System.out.println(ret);
            urlStream.close();
        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return ret;
    }
    /**
     * 该接口使用与收费的快递公司接口查询
     * @param com 快递公司代码
     * @param nu 快递单号
     * @return
     */
    public static String searchkuaiDiInfo(String com, String nu)
    {
        String content = "";
        try
        {
            StringBuilder sb = new StringBuilder();
            sb.append("http://www.kuaidi100.com/applyurl?key=").append(KuaiDi100.key);
            sb.append("&com=").append(com);
            sb.append("&nu=").append(nu);
            URL url = new URL(sb.toString());
            URLConnection con = url.openConnection();
            con.setAllowUserInteraction(false);
            InputStream urlStream = url.openStream();
            byte b[] = new byte[10000];
            int numRead = urlStream.read(b);
            content = new String(b, 0, numRead);
            while (numRead != -1)
            {
                numRead = urlStream.read(b);
                if (numRead != -1)
                {
                    // String newContent = new String(b, 0, numRead);
                    String newContent = new String(b, 0, numRead, "UTF-8");
                    content += newContent;
                }
            }
            urlStream.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("快递查询错误");
        }
        return content;
    }
}
