<?php

class Service_LgstBeiAnProductNotify{
    private $apiurl = 'http://www.lightstrade.com/Services/GoodsAttributeService.asmx/AcceptGoodsBeiAnNotify';
    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(
                    'id'=>$row['product_id'],
                    'productSku'=>$row['product_sku'],
                    'beiAnStatus'=>$row['product_status'],
                    'customerCode'=>$row['customer_code'],
                );
                $data[]=$arr;
            }
        }

        $gsRow['data']=$data;
        
        $this->gsData=$gsRow;

    }
    public function notifyLgstProductBeiAn($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)
        {
            if($xdata['Success'] == 'true')
            {
                //请求成功并且数据校验成功
                $handlerData = json_decode($xdata['Info'],true);

                if(!empty($handlerData)){
                   
                    foreach ($handlerData as $key => $value) {
                        $condition = array("product_id"=>$value['id']);
                        $productIds = Service_NotifyProductBeianLightstradeLog::getByCondition($condition);
                        if(!empty($productIds)){
                            $upRow = array(
                                    'status'=>$value['status'],
                                    'message'=>$value['info'],
                                    'update_time'=>date("Y-m-d H:i:s",time())
                                );
                            Service_NotifyProductBeianLightstradeLog::update($upRow,$value['id'],'product_id');
                        }else{
                            $row = array(
                                    'product_id'=>$value['id'],
                                    'product_sku'=>$value['productSku'],
                                    'customer_code'=>$value['customerCode'],
                                    'status'=>$value['status'],
                                    'message'=>$value['info'],
                                    'add_time'=>date("Y-m-d H:i:s",time())
                                );
                            Service_NotifyProductBeianLightstradeLog::add($row);
                        }
                    }
                }
            }
            // else{
            //    // 请求成功并且数据校验失败

            // }
        }
        exit;
    }

    /**
     * @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));   
       }   
   }
}