<?php
require_once ('config.php');
$user="1072581API";
$password="1072581";
$flagFile = APPLICATION_PATH . '/../data/log/batchGetTracking.lock';
if (@file_exists($flagFile)) {
    echo 'Another process is running.'."\r\n";
    echo $flagFile."\r\n";
    exit();
}
$link = fopen($flagFile, 'wb');
fclose($link);
define('RUNTIME', '[' . date('Y-m-d H:i:s') . '] ');
echo RUNTIME . "Starting.\r\n\r\n";
date_default_timezone_set('Asia/Shanghai');
$date	= date("Y-m-d H:i:s");
set_time_limit(0);
$orderInfo	= Service_LogisticsOrder::getByCondition(
	array(
		'lo_status'=>1,
		'ref_tracking_number_is_null'=>1,
		'sm_code_arr'=>array('BH-HKECM','BH-JE-POST','BH-US'),
	)
);
if(!empty($orderInfo)){
	foreach($orderInfo as $key=>$val){
		try{
			$pieces = array();
			$pieces[] = array(
				'Weight' => $val['lo_weight'],
				'Width'  => $val['size_width'],
				'Length' => $val['size_length'],
				'Height' => $val['size_height'],
			);
			$obj = new Service_BHHKDHLApiProcess($val['logistics_code'],'',$pieces,0);
			$result = $obj->runAPI();
			$logisticsOrderLogParam	= array(
				'logistics_code'		=> $val['logistics_code'],
				'lol_comment'			=> '运单号获取失败',
				'lol_create_time'		=> date('Y-m-d H:i:s'),
				'lol_operator'			=> 0,
				'lol_ip'				=> '127.0.0.1',
				'order_status_from'		=> 1,
				'order_status_to'		=> 1,
			);
			if(1 == $result['ask']){
				Service_LogisticsOrder::update(array('ref_tracking_number'=>$result['AirwayBillNumber']),$val['lo_id']);
				$logisticsOrderLogParam['lol_comment'] = '运单号获取成功';
			}
			Service_LogisticsOrderLog::add($logisticsOrderLogParam);
		} catch (Exception $e) {
			@unlink($flagFile);
			echo $e->getMessage();
		}
	}
}

echo "\r\n[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flagFile);

