<?php
exit('exit');

/**
 * @author solar
 * @todo 批量处理产品申报要素
 */
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/batchHsElement.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";

$customer_code = 'E0193';
$hs_code = '8471300000';
try {
	$adapter = Service_Product::getModelInstance()->getAdapter();
	//$elements = $adapter->fetchAll("select * from hs_element as he where he.hs_code='{$hs_code}'");
	//$uoms = $adapter->fetchAll("select * from hs_uom as hu where hu.hs_code='{$hs_code}'");
	$elementDataList = $adapter->fetchAll("select * from tmp_element_data where status=1");
	foreach($elementDataList as $elementData) {
		$sql = "select * from product as p where p.customer_code='{$customer_code}' and p.product_sku='".$elementData['sku']."'";
		$productRow = $adapter->fetchRow($sql);
		if(empty($productRow)) {
			echo $elementData['sku']."不存在\r\n";
			$adapter->query('update tmp_element_data set status=3 where id='.$elementData['id']);
			continue;
		}
		//申报要素
		$elementMap = array();
		$elementMap['he_id'] = '43701';
		$elementMap['product_id'] = $productRow['product_id'];
		$elementMap['hem_detail'] = trim($elementData['type']);
		$elementMap['hem_add_time'] = date('Y-m-d H:i:s');
		Service_HsElementMap::add($elementMap);
		
		$elementMap = array();
		$elementMap['he_id'] = '43702';
		$elementMap['product_id'] = $productRow['product_id'];
		$elementMap['hem_detail'] = trim($elementData['configure']);
		$elementMap['hem_add_time'] = date('Y-m-d H:i:s');
		Service_HsElementMap::add($elementMap);
		
		$elementMap = array();
		$elementMap['he_id'] = '43703';
		$elementMap['product_id'] = $productRow['product_id'];
		$elementMap['hem_detail'] = trim($elementData['brand']);
		$elementMap['hem_add_time'] = date('Y-m-d H:i:s');
		Service_HsElementMap::add($elementMap);
		
		$elementMap = array();
		$elementMap['he_id'] = '43704';
		$elementMap['product_id'] = $productRow['product_id'];
		$elementMap['hem_detail'] = trim($elementData['model']);
		$elementMap['hem_add_time'] = date('Y-m-d H:i:s');
		Service_HsElementMap::add($elementMap);
		
		//法定单位
		$uomsMap = array();
		$uomsMap['hu_id'] = '10285';
		$uomsMap['product_id'] = $productRow['product_id'];
		$uomsMap['hum_quantity_law'] = trim($elementData['unit']);
		$uomsMap['hum_add_time'] = date('Y-m-d H:i:s');
		Service_HsUomMap::add($uomsMap);
				
		//处理料件号
		Service_Product::handleRecord($productRow['product_id']);
		echo '产品['.$productRow['product_id'].']处理成功'."\r\n";
		
		//标记
		$adapter->query('update tmp_element_data set status=2 where id='.$elementData['id']);
		
		//break;
	}
} 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);
