<?php
class Service_ProductRecordProcess
{	
	/**
	 * 本地文件路径
	 * @author solar
	 * @return string
	 */
	public function getBasePath() {
		return APPLICATION_PATH.'/../data/ftp';
		//return APPLICATION_PATH.'/../data/cmbl';
	}
		
	/**
	 * 添加产品备案记录
	 * @author solar
	 * @param int $product_id
	 * @param string $goods_id
	 * @return true|string
	 */
	public function addGoodsRecord($goodsRow,$type="") {
        $fileName = $goodsRow['goods_code'].'.xml';
		$fmtRow = Service_FtpMessageType::getByField('QHW', 'fmt_code');
		$relativePath = $fmtRow['fmt_folder'].'/'.date('Y/m/d');
		$absolutePath = $this->getBasePath().$relativePath;
		if(!file_exists($absolutePath)) {
			mkdir($absolutePath, 0777, true);
		}
		$xmlProcess = new Service_ProductXmlProcess($goodsRow,$type);
		$result = $xmlProcess->generate($absolutePath.'/'.$fileName);
		if($result===false) return '生成XML文件失败';
		$row['fmt_code'] = 'QHW';
		$row['product_id'] = $goodsRow['product_id'];
		$row['goods_id'] = $type=='ZZC'?"B".$goodsRow['goods_code']:$goodsRow['goods_code'];
		$row['fpr_path'] = $relativePath.'/'.$fileName;
		$row['fpr_add_time'] = date('Y-m-d H:i:s');
		$id = Service_FtpProductRecord::add($row);
		return $id > 0 ? intval($id) : '添加备案记录失败';
	}
	
	/**
	 * 发送消息文件到FTP服务器
	 * @author solar
	 * @param int $fpr_id
	 * @return boolean
	 */
	public function ftpSendRecord($fpr_id) {
		$result = true;
		$fprRow = Service_FtpProductRecord::getByField($fpr_id);
		$fmtRow = Service_FtpMessageType::getByField($fprRow['fmt_code'], 'fmt_code');
		$file_name = $fprRow['goods_id'].'.xml';
		$remote_file = $fmtRow['fmt_folder'].'/'.$file_name;
		$backup_file = $fmtRow['fmt_folder'].'/backup/'.$file_name;
		$local_file = $this->getBasePath().$fprRow['fpr_path'];
		$connect = ftp_connect($fmtRow['fmt_host']);
		$is_login = ftp_login($connect, $fmtRow['fmt_username'], $fmtRow['fmt_password']);
		if(!$is_login) return false;
		while(true) {
			if(ftp_put($connect, $remote_file, $local_file, FTP_BINARY) == false) {
				$result = false;
				break;
			}
			if(ftp_put($connect, $backup_file, $local_file, FTP_BINARY) == false) {
				$result = false;
			}
			break;
		}
		@ftp_close($connect);
		if($result === true) Service_FtpProductRecord::update(array('fpr_status'=>2), $fpr_id);
		else {
			$fprUpdate['fpr_failed_count'] = $fprRow['fpr_failed_count'] + 1;
			$fprUpdate['fpr_status'] = 1;
			Service_FtpProductRecord::update($fprUpdate, $fpr_id);
		}
		return $result;
	}
		
	/**
	 * 检查出库单FTP接收文件夹
	 * @author solar
	 * @return true|string
	 */
	/*public function ftpOutboundReceive() {
		$fmtRow = Service_FtpMessageType::getByField('MCK', 'fmt_code');
		$connect = ftp_connect($fmtRow['fmt_host']);
		$is_login = ftp_login($connect, $fmtRow['fmt_username'], $fmtRow['fmt_password']);
		if(!$is_login) return '登录FTP服务器失败';
		$receivePath = $this->getOutboundReceivePath();
		$paths = ftp_nlist($connect, '/outbound_receive');
		foreach($paths as $remote_path) {
			$path_info = pathinfo($remote_path);
			if(!isset($path_info['extension']) || strtolower($path_info['extension'])!='xml') continue;
			$code = $path_info['filename'];
			$local_path = $receivePath.'/'.$path_info['basename'];
			$is_get = ftp_get($connect, $local_path, $remote_path, FTP_BINARY);
			if(!$is_get) {
				$this->addReceiveMessage($code, $local_path, 1, 1);
				ftp_rename($connect, $remote_path, '/outbound_receive/failed/'.$path_info['basename']);
				continue;				
			}
			$sbRet = $this->updateShipBatch($code);
			if($sbRet!==true) {				
				$this->addReceiveMessage($code, $local_path, 1, 1);
				ftp_rename($connect, $remote_path, '/outbound_receive/failed/'.$path_info['basename']);
				continue;	
			}
			$this->addReceiveMessage($code, $local_path, 2);
			ftp_delete($connect, $remote_path);
		}
		@ftp_close($connect);
		return true;
	}*/
	
}
