<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/3/30
 * Time: 下午5:10
 */

require_once (BASE_CORE_PATH . "/framework/function/http.php");
class kdn_helper
{
    const req_url = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';

//1256766   22c88d88-3565-4023-b5ac-0201792869ed
//1256767   e02cd836-3146-4958-b1dc-c1585fa05b9e
//1256768   e7eb7d2f-0525-484e-80ba-c8f5b5a90af3

    static public function query($shipperCode, $logisticCode)
    {
        $requestData = json_encode(array('ShipperCode' => $shipperCode,'LogisticCode' => $logisticCode));

        $account = self::get_account();
        Log::record("EBusinessID={$account}",Log::DEBUG);
        $datas = array(
            'EBusinessID' => $account['EBusinessID'],
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
            'DataSign' => self::encrypt($requestData, $account['AppID'])
        );
        $result = http_post_data(self::req_url,$datas);
        return $result;
    }
    static public function cur_businessid() {
        $account = self::get_account();
        return $account['EBusinessID'];
    }

    static public function get_account()
    {
        if(defined('MOBILE_PUBLISH') && MOBILE_PUBLISH == true) {
            return array('EBusinessID' => '1256766','AppID' => '22c88d88-3565-4023-b5ac-0201792869ed');
        } else {
            return array('EBusinessID' => '1256051','AppID' => '6718d260-e2b6-4329-ad78-daff173309ac');
        }
    }

    static private function encrypt($data, $appkey)
    {
        return urlencode(base64_encode(md5($data . $appkey)));
    }

    static public function subscribe($order_sn)
    {
        $model_order = Model('order');
        $condition['order_sn'] = $order_sn;
        $order_info = $model_order->getOrderInfo($condition, array('order_common', 'order_goods'));
        // 发送快递鸟订阅回调
        $express = rkcache('express', true);
        $e_code = $express[$order_info['extend_order_common']['shipping_express_id']]['e_kdn_code'];
        if(empty($e_code)) {
            Log::record("找不到该单号的快递公司编码—— ordersn={$order_sn}",Log::ERR);
            return;
        }
        // 通知订阅, 快递鸟定时回调
        $ret = self::req_subscribe($e_code, $order_info['shipping_code'], $order_sn);
        if($ret === false) {
            Log::record("kdn_helper::subscribe order_sn={$order_sn} error.",Log::ERR);
        } else {
            Log::record("kdn_helper::subscribe ret={$ret}",Log::DEBUG);
        }
    }

    static public function req_subscribe($e_code, $logisticCode, $order_sn)
    {
        $datas = array(array('Code' => $e_code,'Item' => array(array('No' => $logisticCode,'Bk' => $order_sn))));
        $requestData = json_encode($datas);

        $account = self::get_account();
        $datas = array(
            'EBusinessID' => $account['EBusinessID'],
            'RequestType' => '1005',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
            'DataSign' => self::encrypt($requestData, $account['AppID'])
        );
        $result = http_post_data(self::req_url, $datas);
        return $result;
    }

    static public function onCallback($data)
    {
        $order_sn = $data['CallBack'];
        if(empty($order_sn)) return;

        $e_code = $data['ShipperCode'];
        $shipping_code = $data['LogisticCode'];
        $key = "express_{$e_code}_{$shipping_code}";
        wkcache($key, $data);
        // 数据解析
        switch ($data['State'])
        {
            case '3':	// 已签收, 设置为已收货
                Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 已签收.",Log::ERR);
                break;
            case '4':	// 问题件
                Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 问题件.",Log::ERR);
                break;
            default: {
                Log::record(__METHOD__ . ' query_status false');
                break;
            }
        }
    }
}

