<?php
/**
 * @author william-fan
 * @todo 检查产品备案情况(调用招宝接口,备案成功更新产品为备案状态，就是1)
 */
set_time_limit(7200);
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/productbeianCheck.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);
}
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_lt'=>'-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_lt'=>'-1'),'*',$pageSize,$page,'pg_id desc');
			foreach($goodsList as $goodsRow){
				//beian($goodsRow);
				$cmblApi->checkbeianStatus($goodsRow);
			}
		}
	}
	echo '[' . date('Y-m-d H:i:s') . ']'."备案更新状态全部处理完成"."\r\n";
} 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);