<?php
/**
 * @author william-fan
 * @todo  向招宝提交出区单数据(调用招宝webservice)
 */
//发送出区报文
set_time_limit(7200);
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/shipBatchSubmit.lock';
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";
try {
	$ftProcess = new Service_FtpReceiveProcess();
	//$receivingList = Service_Receiving::getByCondition(array('receiving_status'=>3));
	$receivingCondition = array('fmt_code'=>'MCK', 'fml_status'=>1);
	$counts = Service_FtpMessageSendlist::getByCondition($receivingCondition,"count(*)");
	$pageSize = 20;
	if($counts>0){
		$ftProcess = new Service_FtpReceiveProcess();
		
		$cmblApi=new Service_CMBLApiProcess();
		
		$pages = ceil($counts/$pageSize);
		for($page=1;$page<=$pages;$page++){
			$sendList = Service_FtpMessageSendlist::getByCondition($receivingCondition,'*',$pageSize,$page,'fml_id desc');
			//print_r($sendList);
			foreach($sendList as $sendRow){
				//print_r($sendList);
				//uploadRCustomerOrder($sendRow);
				$cmblApi->UploadCCustomOrder($sendRow);
			}
		}

	}
	echo "[发送出区报文]全部处理完成\r\n";
} catch (Exception $e) {
	@unlink($flagFile);
	echo '[' . date('Y-m-d H:i:s') . '] Fail Exception:' . $e->getMessage() ."\r\n";
}
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n\r\n";
@unlink($flagFile);