<?php
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/autoSF.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";

$condition = array(
    'sm_code_Arr'                 => array('SF-RUPR','SF-RUPT'),
    'order_status'=>'4',
    'ref_tracking_number'=>'',
    //'order_mode_type' => '0', // 备货模式
);
$total = Service_Orders::getByCondition($condition,"count(*)");
$pageSize = 20;
$totalPage = ceil($total/$pageSize);
for($page=1;$page<=$totalPage;$page++){
    $orderList = Service_Orders::getByCondition($condition,'*',$pageSize,$page,'order_id desc');
    foreach($orderList as $k=>$v){
        // 数据过滤
        if (!in_array(trim($v['sm_code']),array('SF-RUPR','SF-RUPT'))
            || 4 !== (int) $v['order_status']
            || '' !== trim($v['ref_tracking_number'])) {
          continue ;
        }
        //print_r($v['order_code']);
        $orderXml = Service_SfexpressService::getOrderContent($v['order_code']);
        //print_r($orderXml);
        $SfexpressService = new Service_SfexpressService();
    /*   $res = array(
		'Order' => array(
			'orderid'		=> $v['order_id'],	 //客户订单号
			'j_company'		=> 'baohong',	
			'j_contact'		=> 'li',
			'j_tel'			=> '18818681594',
			'j_mobile'		=> '18818681594',
			'j_country'		=> 'CN',
			'j_province'	=> 'guangdong',
			'j_city'		=> 'gz',
			'j_county'		=> 'py',
			'j_address'		=> 'baoshui',
			'd_contact'		=> 'wang',
			'd_tel'			=> '18818681594',
			'd_mobile'		=> '18818681594',
			'd_country'		=> 'CN',
			'd_province'	=> 'gz',
			'd_city'		=> 'sz',
			'd_county'		=> '',
			'd_address'		=> 'SZ',
			'custid'		=> '',	//顺丰月结卡号
			'pay_method'	=> 1,	//付款方式： 1:寄方付 2:收方付 3:第三方付
			'parcel_quantity'=>1,	//包裹数量
			'express_type'	=> 1,	//业务类型 1.标准快递   2.顺丰特惠   3.电商特惠  7.电商速配
			)
		);*/
	//$req = $SfexpressService->query('OrderService',$res);
        /*$res = array(
		'RouteRequest' => array(
			'tracking_type'		=>1,
			'tracking_number'	=>'994000398211'
			)
		);

	$req = $SfexpressService->query('RouteService',$res);*/
	/*echo '<pre>';
	print_r($req);
	echo '</pre>';exit;*/

	$file=APPLICATION_PATH.'/../data/SF/'.$v['order_code'].".xml";  
	if (!file_exists($file)) {
	  	touch($file,0755);
	  }  
	file_put_contents($file,$orderXml);
        $result = $SfexpressService->validate($orderXml);
        //print_r($result);
            if(isset($result['@attributes']['mailno'])&&$result['@attributes']['mailno']!=""){
            	if ($v['sm_code']=='SF-RUPR') {
            		$updateOrder = array(
	                    'agent_mailno'=>$result['@attributes']['mailno'],
	                    'ref_tracking_number'       =>$result['@attributes']['agent_mailno'],
	                );
            	}else{
	                $updateOrder = array(
	                    'ref_tracking_number'=>$result['@attributes']['mailno'],
	                );
            	}
                $orderLog = array(
                    'order_id'=>$v['order_id'],
                    'order_code'=>$v['order_code'],
                    'ol_type'=>'1',
                    'order_status_from'=>$v['order_status'],
                    'order_status_to'=>$v['order_status'],
                    'ol_add_time'=>date("Y-m-d H:i:s"),
                    'user_id'=>0,
                    'ol_comments'=>'SF自动取单号以及标签->autoSF.php'
                );
                Service_OrderLog::add($orderLog);
                Service_Orders::update($updateOrder,$v['order_id'],"order_id");
               
                echo "订单".$v['order_code']."获取单号".$result['@attributes']['mailno']."成功\r\n";
            }else{
                $errorText="订单".$v['order_code']."提交出错:".$result."\r\n";
                
                $errorLog=array(
                 'order_code'=>$v['order_code'],
                 'sm_code'=>$v['sm_code'],
                 'content'=>$errorText,
                );
             Service_TrackingErrorLog::add($errorLog);
             echo $errorText;
            }
        
    }
}
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flagFile);