<?php
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/checkProductRecord.lock';
if (@file_exists($flagFile)) {
    echo 'Anather 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";

try {
	$number = 0;
	$agrList = Service_ApiGoodsResult::getByCondition(array('agr_status'=>1));
	foreach($agrList as $agrRow) {
		$pgList =  Service_ProductGoods::getByCondition(array('hs_key'=>$agrRow['agr_hs_key']));
		if(empty($pgList)) {
			echo $agrRow['agr_id']."\r\n";
			echo $agrRow['agr_hs_key']."\r\n";
		}
		foreach($pgList as $pgRow) {
			if($agrRow['STATUS'] == '2') {
				Service_ProductGoods::update(array('pg_status'=>1), $pgRow['pg_id']);
				Service_Product::update(array('product_status'=>1), $pgRow['goods_code'], 'goods_id');
				echo '['.(++$number).']'.$pgRow['goods_code']."备案成功\r\n";
			} else {
				Service_ProductGoods::update(array('pg_status'=>3), $pgRow['pg_id']);
				echo '['.(++$number).']'.$pgRow['goods_code']."备案失败\r\n";
			}
		}
		Service_ApiGoodsResult::update(array('agr_status'=>2), $agrRow['agr_id']);
	}
	echo "全部处理完成\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);