<?php
/**
 * @author william-fan
 * @todo 向招宝提交备案数据（调用招宝webservice）
 */
set_time_limit(7200);
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/productbeian.lock';
if (@file_exists($flagFile)) {
    $lockTime = filectime($flagFile);
    $nowTime = date("Y-m-d H:i:s");
    $minutes = (strtotime($nowTime) - strtotime($lockTime))/60;
    if($minutes>5){
        @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);
}
//E0659 分销平台测试帐号 E0721只走区外仓
$aTestAccount = array('E0659','E0110', 'E0207','E0346','E0370','E0412','E0431','E0441','E0444','E0483','E0493','E0497','E0519','E0537','E0581','E0585','E0637','E641','E0659','E660','E0533','E0653','E654','E0692','E0693','E0697','E0721','E0751','E0750','E0756','E0757','E0760','E0763','E0610','E0786','E0796','E0800','E0806','E0436','E0822','E0833','E0837','E0846','E0898','E0896','E0842','E0897','E0900','E0903','E0909','E0916','E0924','E0923','E0931','E0918','E0929','E0943');
define('RUNTIME', '[' . date('Y-m-d H:i:s') . '] ');
echo RUNTIME . "Starting.\r\n\r\n";

try {
	$counts = Service_ProductGoods::getByCondition(array('pg_status'=>2,'fml_status'=>'-1'),'count(*)');
	$pageSize = 20;
	if($counts>0){
		$cmblApi=new Service_CMBLApiProcess();
		
		$pages = ceil($counts/$pageSize);
		for($page=1;$page<=$pages;$page++){
			$goodsList = Service_ProductGoods::getByCondition(array('pg_status'=>2,'fml_status'=>'-1'),'*',$pageSize,$page,'product_id desc');
			foreach($goodsList as $goodsRow){
				//beian($goodsRow);
                $product = Service_Product::getByField($goodsRow['product_id'],'product_id');
                if (empty($product) || in_array($product['customer_code'], $aTestAccount)) {
                    //echo "测试账号的单[" . $sendRow['refercence_no'] . "]，不能发送\r\n";
                    continue;
                }else{
                    $cmblApi->beian($goodsRow);
                }
			}
		}
	}
} catch (Exception $e) {
	@unlink($flagFile);
	echo '[' . date('Y-m-d H:i:s') . '] Fail Exception:' . $e->getMessage() ."\r\n";
}

echo "\r\n[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flagFile);