<?php
require_once ('config.php');
$debug = false; //是否为测试
$flag = APPLICATION_PATH . '/../data/log/autoGetDHLLabelToCj.lock';

if (@file_exists($flag)) {
    echo "Another process is running.\r\n";
    echo "{$flag}\r\n";
    exit();
}
set_time_limit(0);
ini_set('memory_limit', '512M');
echo "[" . date('Y-m-d H:i:s') . "] Starting run\r\n";
$handler = fopen($flag, 'wb');
fclose($handler);
//备货模式订单
$conditionBh = array('sync_status'=>'0','order_status'=>4,'sm_code_Arr'=>array('CJ-DGMG','CJ-DGMP','DGM-US'),'empty_ref_tracking_number'=>'EMPTY');
$condition = array('sync_status'=>'0','order_status'=>4,'sm_code_Arr'=>array('CJ-DGMG','CJ-DGMP','DGM-US'),'empty_ref_tracking_number'=>'EMPTY');
$count = Service_Orders::getByCondition($condition,"count(*)");
$objService = new Service_GetDHLInfoProcess();
if($count){
    $pageSize =20;
    $totalPage = ceil($count/$pageSize);
    for($page=1;$page<=$totalPage;$page++){
        $orderList = Service_Orders::getByCondition($condition, '*', $pageSize, $page, 'order_id desc');
        foreach($orderList as  $key=>$order){
            $dealOrderList = array();
            if($order['customer_code']!='E0207') $dealOrderList[] = $order;
            if(!empty($dealOrderList)){
                var_dump($dealOrderList);
                $response = Service_GetDHLInfoProcess::getLabelDataService($dealOrderList,$debug);
                //print_r($response);
                if($response['status'] == 1){
                    echo $response['message']."\n";
                }else{
                    echo $response['message']."\n";
                }
            }
        }
    }
}
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flag);
exit;
if($debug){
    $orderBh = ' update_time DESC';
}
$ordersInfo = Service_Orders::getByCondition($conditionBh,"*","","",$orderBh);
//集货模式订单
$coditionJh = array('receive_model_type'=>1,'receiving_status_arr'=>array('2','3','4','5','6'));
$receivingCodeInfo = Service_Receiving::getByCondition($coditionJh);
$orderCodes = array();
foreach($receivingCodeInfo as $value){
    $orderCodes = Service_ReceivingOrderDetail::getOrderCodes($value['receiving_code']);
}
if($debug){
    $orderJh = ' update_time DESC';
}
$ordersInfoJh = Service_Orders::getByCondition(array('order_code_arr'=>$orderCodes,'sm_code_Arr'=>array('CJ-DGMG','CJ-DGMP'),'empty_ref_tracking_number'=>'EMPTY'),"*","","",$orderJh);
foreach($ordersInfoJh as $v){
    array_push($ordersInfo, $v);
}

$counts = count($ordersInfo);
if($debug){
    $pageSize = 1;
}else{
    $pageSize = 1;
}

if($counts > 0){
    $pages = ceil($counts / $pageSize);
    var_dump($ordersInfo);
    for ($page = 1; $page <= $pages; $page++){
        //通过接口发送订单信息发送服务商，并取回跟踪号和标签
        if($debug){
            $array = array_slice($ordersInfo,($page-1)*$pageSize,$pageSize);
            print_r(($array));
        }
        $response = Service_GetDHLInfoProcess::getLabelDataService(array_slice($ordersInfo,($page-1)*$pageSize,$pageSize),$debug);
        if($debug){
            print_r($response);
        }
        if($response['status'] == 1){
            echo $response['message']."\n";
        }else{
            echo $response['message']."\n";
        }
        //break;
    }
}
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flag);
exit;