<?php

class Service_LgstOrdersTuoTouNotify{
    private $apiurl = 'http://www.lightstrade.com/Services/OrderService.asmx/AcceptOrderLoansNotify';
    private $gsData = array();
    private $notifyRows = array();
    private $token = "FGK34L00EFF20F6CDKX4KK94L990RQLEXK";
    private $secret = 'globex12';
    private $gsIds = array();
    private $code  = 'E2018GLOBEX';
    public function __construct(){

    }
    /**
     * 获取lightstrade数据
     */
    public function getGsData($rows){
        $data = array();
        $ids = array();
        $gsRow = array(
            'token'=>$this->token
        );
        if(!empty($rows)){
            foreach($rows as $k=>$row){
                $arr = array(
                    'orderNo'=>$row['order_code'],
                    'exgoCode'=>$row['customer_code'],
                    'referenceNo'=>$row['reference_no']
                );
                $data[]=$arr;
            }
        }

        $gsRow['data']=$data;
        
        $this->gsData=$gsRow;

    }
    public function notifyLgstTuotou($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("order_code"=>$value['orderNo'],"customer_code"=>$value['exgoCode']);
                        $orderList = Service_NotifyLgstOrderTuotouLog::getByCondition($condition);
                        if(!empty($orderList)){
                            $upRow = array(
                                    'status'=>$value['status'],
                                    'message'=>$value['info'],
                                    'update_time'=>date("Y-m-d H:i:s",time())
                                );
                            Service_NotifyLgstOrderTuotouLog::update($upRow,$value['orderNo'],'order_code');
                        }else{
                            $row = array(
                                    'order_code'=>$value['orderNo'],
                                    'customer_code'=>$value['exgoCode'],
                                    'status'=>$value['status'],
                                    'message'=>$value['info'],
                                    'add_time'=>date("Y-m-d H:i:s",time())
                                );
                            Service_NotifyLgstOrderTuotouLog::add($row);
                        }
                    }
                }
            }else{
                file_put_contents(APPLICATION_PATH . "/../data/log/LgstOrdersTuoTouNotify.txt",date('Y-m-d H:i:s')." --> ".$xdata['Info']."\r\n",FILE_APPEND);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));   
       }   
   }
}