<?php
/**
 * 生成商品备案XML 
 * @author solar<solarzheng@cargofe.com.cn>
 */
class Service_ProductXmlProcess
{
	public $version = '1.0';
	public $charset = 'UTF-8';
	public $sendtime = 0;
	public $messageID = '';
	
	private $dom = null;
	private $elEntryRelease = null;
	private $elHead = null;
	
	private $productList = array();
	
	public function __construct($productList) {
		$this->productList = $productList;
		$this->sendtime = time();
	}
	
	/**
	 * 创建根节点EntryRelease
	 */
	private function createMessage() {
		$this->dom = new DOMDocument($this->version, $this->charset);
		$this->elEntryRelease = $this->dom->createElement('EntryRelease');
		$this->dom->appendChild($this->elEntryRelease);
	}
	
	/**
	 * 创建Head节点
	 */
	private function createHead() {
		$this->elHead = $this->dom->createElement('Head');
		$this->messageID = 'QHW035'.date('YmdHis', $this->sendtime).str_pad(rand(1,999999), 6, '0', STR_PAD_LEFT);
		$this->elHead->appendChild($this->dom->createElement('MessageID', $this->messageID));
		$this->elHead->appendChild($this->dom->createElement('FunctionCode', '001'));
		$this->elHead->appendChild($this->dom->createElement('MessageType', 'QHW035'));
		$this->elHead->appendChild($this->dom->createElement('SenderID', 'SZCPORT'));
		$this->elHead->appendChild($this->dom->createElement('ReceiverID', 'SZC.FTPA'));
		$this->elHead->appendChild($this->dom->createElement('SendTime', date('Y-m-d H:i:s', $this->sendtime)));
		$this->elHead->appendChild($this->dom->createElement('Version', $this->version));
		$this->elEntryRelease->appendChild($this->elHead);
	}
			
	/**
	 * 创建BODY节点
	 */
	private function createBody() {
		foreach($this->productList as $i=>$aProduct) {
			$elBody = $this->dom->createElement('Body');
			$elBody->appendChild($this->dom->createElement('SEQ_ID', $i+1));
			$elBody->appendChild($this->dom->createElement('GOODS_ID', $aProduct['goods_id']));
			$elBody->appendChild($this->dom->createElement('CODE_TS', $aProduct['hs_code']));
			$elBody->appendChild($this->dom->createElement('G_NAME', $aProduct['product_title']));
			$elBody->appendChild($this->dom->createElement('G_MODEL', $this->getGModel($aProduct['product_id'])));
			$elBody->appendChild($this->dom->createElement('ENTRY_ID'));
			$elBody->appendChild($this->dom->createElement('G_NO'));
			$elBody->appendChild($this->dom->createElement('AGENT_CODE'));
			$elBody->appendChild($this->dom->createElement('FEEDBACK_FLAG'));
			$elBody->appendChild($this->dom->createElement('FEEDBACK_MESS'));
			$elBody->appendChild($this->dom->createElement('FEEDBACK_DATE'));
			$this->elEntryRelease->appendChild($elBody);
		}		
	}
	
	/**
	 * 规格型号
	 * @return string
	 */
	private function getGModel($product_id) {
		$where['product_id'] = $product_id;
		$list = Service_HsElementMap::listByWhere($where);
		$elements = array();
		foreach($list as $row) {			
			$elements[] = $row['hem_detail'] ? $row['hem_detail'] : '/';
		}
		return implode('|', $elements);
	}
		
	/**
	 * 生成XML文档
	 * @param string $path
	 * @return int|string
	 */
	public function generate($path='') {
		$this->createMessage();
		$this->createHead();
		$this->createBody();
		if($path) {
			$fileName = $this->messageID.'.xml';
			return $this->dom->save($path.'/'.$fileName);
		}
		return $this->dom->saveXML();
	}
	
}
