<?php

class Service_LgstNotifyProcess{
    private $apiurl="https://psc.api.globalsources.com/pscapi/globex/updateProductInventory.api";
    private $gsData = array();
    private $notifyRows = array();
    private $gsIds = array();
    public function __construct(){

    }
    /**
     * 获取lightstrade数据
     */
    public function getGsData($rows){
        $data = array();
        $ids = array();
        $gsRow = array(
            'token'=>array(
                'key'=>'6f4286dc93166bdfbfb3942eead236cd',
                'token'=>'EA7FD500EFF20F6C',
                'code'=>'E0843',
            )
        );
        if(!empty($rows)){
            foreach($rows as $k=>$row){
                $arr = array(
                    'productId'=>$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 notifyGS($rows){
        include_once 'JWT.php';

        $this->getGsData($rows);
        $jsonStr = json_encode($this->gsData);
        //echo $jsonStr;
        $jwtStr=JWT::encode($jsonStr, 'EA7FD500EFF20F6C');

        $postData = array(
            'postfield'=>urlencode($jwtStr)
        );
        //$data = Common_Common::http_post($this->apiurl,500,$postData);
        //echo 'sss';

        $this->apiurl=$this->apiurl."?postfield=".urlencode($jwtStr);
        //echo $this->apiurl;

        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $this->apiurl); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

        //curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        /*if ($postData != ''){
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // Post提交的数据包
        }*/
        //curl_setopt($curl, CURLOPT_POSTFIELDS, "postfield=".urlencode($jwtStr));
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        //var_dump($curl);
        curl_close($curl); // 关闭CURL会话
        file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory555.txt",date("Y-m-d H:i:s").var_export($rows,true)."\r\n",FILE_APPEND);
        file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory555.txt",var_export($jsonStr,true)."\r\n",FILE_APPEND);
        file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory555.txt",var_export($jwtStr,true)."\r\n",FILE_APPEND);
        file_put_contents(APPLICATION_PATH . "/../data/log/lgstinventory555.txt",var_export($tmpInfo,true)."\r\n",FILE_APPEND);
        //var_dump($tmpInfo);
        $this->updatenotify($tmpInfo,$rows);
        //return $tmpInfo;
    }

    public function updatenotify($tmpInfo,$rows){
        $gsnotify = json_decode($tmpInfo,true);
        if(!empty($gsnotify)){
            $upRow = array(
                'response_message'=>$tmpInfo,
                'update_time'=>date("Y-m-d H:i:s",time()),
            );
            if($gsnotify['status']=='success'){
                $upRow['status'] ='1';
            }
        }else{
            $upRow = array(
                'response_message'=>$tmpInfo,
                '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( '插入环球库存队列失败' );
    }
}