<?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/sendETPShipBatch.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_ShipBatchEtpXmlSendlist::getByCondition( $condition, '*', $pageSize, $page, 'sbexs_id') ) {

    $condition['sbexs_id_gt'] = $sendList[ count( $sendList ) - 1 ]['sbexs_id'];
    foreach( $sendList as $value ) {

        $obj = new Service_ShipBatchEtpXmlProcess();
       if( true === $obj->sendXmlToEtp( $value['sub_sb_code'] ) )
            echo '出区单sub_sb_code: '. $value['sub_sb_code'] ." 发送ETP备案清单成功\n";
        else
            echo '出区单sub_sb_code: '. $value['sub_sb_code'] .' 发送ETP备案清单失败,'.implode( ',', $obj->getError() ) ."\n";
    }
}
echo "[发送出区报文]全部处理完成\n";
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n\r\n";
@unlink($flagFile);