<?php
/**@todo  :发送ETP的备案清单
 * @author: gilbert
 * 
 */
//set_time_limit(7200);
require_once ('config.php');
//header('Content-Type:text/html;charset=utf8');
$flagFile = APPLICATION_PATH . '/../data/log/sendETPConfirmReceivedOrder.lock';
@unlink($flagFile);
if (@file_exists($flagFile)) {
    $lockTime = filectime($flagFile);
    $nowTime = date("Y-m-d H:i:s");
    $minutes = (strtotime($nowTime) - strtotime($lockTime))/60;
    if($minutes>10){
        @unlink($flagFile);
        $link = fopen($flagFile, 'wb');
        fclose($link);
    }else{
        echo 'Anather process is running.' . "\r\n\r\n";
        echo $flagFile . "\r\n";
        exit();
    }
}else{
    $link = fopen($flagFile, 'wb');
    fclose($link);
}
define('RUNTIME', '[' . date('Y-m-d H:i:s') . '] ');
echo RUNTIME . "Starting.\r\n";

$page = 1;
$pageSize = 100;
$condition = array();

while( $sendList = Service_EtpOrderTrackingInfoTmp::getByCondition( $condition, '*', $pageSize, $page, 'id') ) {

    $condition['id_gt'] = $sendList[ count( $sendList ) - 1 ]['id'];
    foreach( $sendList as $value ) {

        $obj = new Service_EtpOrderTrackingProcess();
       if( true === $obj->confirmReceivedOrder( $value['id'] ) )
            echo '订单: '. $value['order_code'] ." 确认收货成功\n";
        else
            echo '订单: '. $value['order_code'] .' 确认收货失败,'.implode( ',', $obj->getError() ) ."\n";
    }
}
echo "[确认订单收货]全部处理完成\n";
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n\r\n";
@unlink($flagFile);