<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 17-3-24
 * Time: 下午5:34
 * To change this template use File | Settings | File Templates.
 */
class Service_ParcelOprWebService extends Common_Service
{
    public $companyID;
    public $pwd;
    public $client;
    public $webServerUrl = "http://post.8dt.com:8880/wb_lc/cxf/ParcelOprWebService?wsdl"; //WebServer地址;

    public function __construct($uid, $password)
    {
        $this->companyID = $uid;
        $this->pwd = $password;
        $this->client = new SoapClient($this->webServerUrl);
        $this->client->soap_defencoding = 'UTF-8';
        $this->client->decode_utf8 = false;
        $this->client->xml_encoding = 'UTF-8';
    }

    /*
     * 访问接口 addParcelService（新增包裹）
    */
    public function addParcel(array $parcelList)
    {

        //构建传入参数
        $args = array(
            'companyID' => $this->companyID,
            'pwd' => $this->pwd,
            'parcelList' => $parcelList
        );

        $ret = array(
            'ask'=>0,
            'message'=>'',
            'vsnumber'=>'',
            'errorCode'=>'',
            'trackingNo'=>'',
        );
        //调用接口addParcelService
        try {

            $result = $this->client->addParcelService($args);
            print_r($result);
            if (!isset($result->return)) {
                throw new Exception("无数据");
            }

            //$return = $this->obj2array($result->return);
            $return = Common_Common::objectToArray($result->return);
            if ($return['success']) {
                $ret['ask'] = 1;
                $ret['message'] = '添加成功';
                $ret['vsnumber'] = $return['vsnumber'];
                $ret['trackingNo'] = $return['trackingNo'];
                //echo '添加成功';
                //echo "<br>";
                //echo '包裹条码:' . $return['vsnumber'];
                //echo '-----客户订单号:' . $return['refNo'];
            } else {
                $ret['errorCode'] = $return['errorCode'];
                //echo "errorCode:" . $return['errorCode'];
                if (isset($return['errorMsg'])) {
                    //echo "<br>" . "errorMsg:" . $return['errorMsg'];
                    throw new Exception($return['errorMsg']);
                }

            }

        } catch (Exception $e) {
            //die($e->getMessage());
            $ret['message'] = $e->getMessage();
        }
        return $ret;
    }


    /*
     *  访问接口forecastService（按内部条码预报）
     *
     */

    public function forecast(array $vsnumbers)
    {
        $args = array(
            'companyID' => $this->companyID,
            'pwd' => $this->pwd,
            'vsnumbers' => $vsnumbers
        );

        try {
            $result = $this->client->forecastService($args);
            $return = $this->obj2array($result->return);
            foreach ($return as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k1 => $v1) {
                        echo $k1 . '=' . $v1;
                        echo "<br>";
                    }
                    echo "<hr>";
                } else {
                    echo $key . '=' . $value;
                    echo "<br>";
                }
            }

        } catch (Exception $e) {
            die($e->getMessage());
        }
    }


    /*
     *
     * 访问接口forecastByRefNoService（按客方单号预报）
     *
     */

    public function forecastByRefNo(array $refNos){
        $args = array(
            'companyID' => $this->companyID,
            'pwd' => $this->pwd,
            'refNos' => $refNos
        );

        try {
            $result = $this->client->forecastByRefNoService($args);
            $return = $this->obj2array($result->return);
            foreach ($return as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k1 => $v1) {
                        echo $k1 . '=' . $v1;
                        echo "<br>";
                    }
                    echo "<hr>";
                } else {
                    echo $key . '=' . $value;
                    echo "<br>";
                }
            }

        } catch (Exception $e) {
            die($e->getMessage());
        }
    }


    /*
     * 访问接口queryParcelByRefNoService（按客户参考号获取订单信息）
     *
     */

    public function queryParcelByRefNo(array $refNos)
    {


        $args = array(
            'companyID' => $this->companyID,
            'pwd' => $this->pwd,
            'refNos' => $refNos
        );

        //调用queryParcelByRefNoService
        try {
            $result = $this->client->queryParcelByRefNoService($args);
            if (!isset($result->return)) {
                die('无数据');
            }

            $return = $this->obj2array($result->return);

            foreach ($return as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k1 => $v1) {
                        echo $k1 . '=' . $v1;
                        echo "<br>";
                    }
                    echo "<hr>";
                } else {
                    echo $key . '=' . $value;
                    echo "<br>";
                }
            }
        } catch (Exception $e) {
            die($e->getMessage());
        }

    }


    /*
     * obj to array
     */

    public function obj2array($obj)
    {
        if (is_object($obj)) {
            $array = (array)$obj;
        }
        if (is_array($obj)) {
            foreach ($obj as $key => $value) {
                $array[$key] = $this->obj2array($value);
            }
        }
        return $array;
    }
}
