<?php

class Service_LgstNotifyProcess{
    private $apiurl = 'http://www.lightstrade.com/Services/OrderService.asmx/GetOperationStock';
    private $gsData = array();
    private $notifyRows = array();
    private $token = "EA7FD500EFF20F6CDKXE45D90XKDX91";
    private $secret = 'globex12';
    private $gsIds = array();
    private $code  = 'E2018GLOBEX';
    public function __construct(){

    }
    /**
     * 获取lightstrade数据
     */
    public function getGsData($rows){
        $data = array();
        $ids = array();
        $gsRow = array(
            'token'=>array(
                'token'=>$this->token,
                'code'=>$this->code
            )
        );
        if(!empty($rows)){
            foreach($rows as $k=>$row){
                $arr = array(
                    'productSku'=>$row['product_sku'],
                    'updateType'=>$row['gstype'],
                    'updateReason'=>$row['reason'],
                    'updateQty'=>$row['quantity'],
                    'customerCode'=>$row['customer_code']
                );
                $data[]=$arr;
                $ids[] = $row['gin_id'];
            }
        }
        $gsRow['data']=$data;
        
        $this->gsData=$gsRow;
        $this->gsIds = $ids;

    }
    public function notifyLgst($rows){

        $this->getGsData($rows);
        $jsonStr = json_encode($this->gsData);
        $contents = Common_SecretUtilTools::encryptForDES($jsonStr,$this->secret);

        $this->apiurl=$this->apiurl;
        $postData = array(
            'contents'=>$contents
        );
        $data = Common_Common::http_post($this->apiurl,500,$postData);
        $xdata = $this->xml_parser($data);
        if($xdata)
        {
            file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory.txt",date("Y-m-d H:i:s").var_export($rows,true)."\r\n",FILE_APPEND);
            file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory.txt",var_export($jsonStr,true)."\r\n",FILE_APPEND);
            file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory.txt",var_export($contents,true)."\r\n",FILE_APPEND);
            file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory.txt",var_export($data,true)."\r\n",FILE_APPEND);
            $this->updatenotify($xdata,$rows);
        }
    }

    public function updatenotify($tmpInfo,$rows){
        if($tmpInfo['Success'] == 'true'){
            $upRow = array(
                'response_message'=>$tmpInfo['Info'],
                'update_time'=>date("Y-m-d H:i:s",time()),
                'status'=>1
            );
        }else{
            $upRow = array(
                'response_message'=>$tmpInfo['Info'],
                'update_time'=>date("Y-m-d H:i:s",time()),
            );
        }
        if(!empty($rows)){
            foreach($rows as $k=>$v){
                Service_LgstInventoryNotify::update($upRow,$v['gin_id']);
            }
        }
    }
    /* @todo:插入推送队列
     * @param array $notify 一条notify数据
     * @return none;
     */
    public static function addNotify( $notify ) {
        $result = Service_LgstInventoryNotify::add( $notify );
        if( !$result )
            throw new Exception( '插入Lightstrade库存队列失败' );
    }

    /**
     * @todo:解析xml
     * 
     */
    public function xml_parser($str){   
       $xml_parser = xml_parser_create();   
       if(!xml_parse($xml_parser,$str,true)){   
           xml_parser_free($xml_parser);   
           return false;   
       }else {   
           return (json_decode(json_encode(simplexml_load_string($str)),true));   
       }   
   }
}