<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-6-19
 * Time: 上午10:02
 * To change this template use File | Settings | File Templates.
 */
require_once ('config.php');
$httpURL = "http://api.sinob2b.com/rs/oms/swapOrder/update";
$postHeader = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
$appSercet = "1747dfbf884ac612e050007f01000ac5";
$appKey = "1747dfbf884ac612e050007f01000ac5";
$actionUrl = "/rs/oms/swapOrder/update";
$total = Service_SwapOrder::getByCondition(array('swo_status'=>5,'has_send'=>'0'),"count(*)");
$pageSize = 20;
$totalPage = ceil($total/$pageSize);
for($page=1;$page<=$totalPage;$page++){
    $swapOrderList = Service_SwapOrder::getByCondition(array('swo_status'=>5,'has_send'=>'0'),"*",$pageSize,$page);
    foreach($swapOrderList as $swapRow){
        $timestamp = strtotime($swapRow['last_update_time']);
        $order_code = $swapRow['swo_code'];
        $status = "S";
        $message = "易货成功";
        $finishTime = $swapRow['last_update_time'];
        $shalstr = $actionUrl."appKey{$appKey}finishTime{$finishTime}message{$message}order_code{$order_code}status{$status}timestamp{$timestamp}{$appSercet}";
        $sign = sha1($shalstr);
        $ch = curl_init($httpURL);
        curl_setopt($ch, CURLOPT_URL, $httpURL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $postHeader);
        curl_setopt($ch, CURLOPT_POST, 1);
        $param = array(
            'appKey'=>$appKey,
            'timestamp'=>$timestamp,
            'order_code'=>$order_code,
            'status'=>$status,
            'message'=>$message,
            'finishTime'=>$finishTime,
            'sign'=>$sign,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($param));
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            print curl_error($ch);
        }
        echo $order_code.":".$response."</br>";
        $obj = json_decode($response);
        $result = Common_Common::objectToArray($obj);
		echo $swapRow['swo_code']." ".$result['ack']."</br>";
        if($result['ack']=="Success"){
            $update = array(
                'has_send'=>1,
            );
            Service_SwapOrder::update($update,$order_code,'swo_code');
        }else{
            $update = array(
                'remark'=>$result['data'],
            );
            Service_SwapOrder::update($update,$order_code,'swo_code');
        }
    }
}