<?php 
/*********************************************************      
 程序说明：         
    功能说明：download current tariff or tariff plan form PPS
              
    调用关系：调用: header.inc   
              被调用:        
 
    变量说明：

    作者：
      
修改注释：      
NO.1      
姓名：       
时间：    
修改说明：
*********************************************************/
require("../../inc/header.inc"); 
?>
<body leftmargin="15" rightmargin="10" onload="javascript:adjust();" onresize="javascript:adjust();" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head();
adjust_title_head();
$helpId = "2.49";
$DEBUG=0;
global $pubConn,$OMC_server;
//==========================================================
//变量定义
//==========================================================
$db='PPS_DB';
$dbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], "PPS_DB");

$sysTypeNo = 360;
$pagePath=$strTitleAdvanced.$strTitleSpace.$strRateAdmin.$strTitleSpace.$strRefresh;

//==========================================================
//输入变量
//==========================================================
$downloadCurrent = $_REQUEST['downloadCurrent'];
$downloadPlan = $_REQUEST['downloadPlan'];

if($DEBUG) echo "<br>downloadCurrent = ".$_REQUEST['downloadCurrent'];
if($DEBUG) echo "<br>downloadPlan	 = ".$_REQUEST['downloadPlan'];



//==========================================================
//数据处理
//==========================================================
$ftpInfo=getMSSFtpAccount();
if($DEBUG) print_r($ftpInfo);

if($downloadCurrent||$downloadPlan){
	echo "<br><br><br><br><br>";
	adjust_title_tail();
	adjust_content_head();
	//找到适合的子系统
	$getSysIdSql = "SELECT sysNo,subSysNo,ip 
						FROM sysInfo 
						WHERE sysTypeNo='$sysTypeNo' AND baseStat > '0'
						ORDER BY sysNo ASC,subSysNo ASC
					";
	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
	$getSysIdResult= mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);
	if(mysqli_num_rows($getSysIdResult) <= 0)	{
		echo "<br>Sorry, All the PPS's status are abnormal!";
		exit();
	}
	$getSysIdRow=mysqli_fetch_array($getSysIdResult);
	$sysId=$sysTypeNo.'_'.$getSysIdRow[sysNo].'_'.$getSysIdRow[subSysNo];
	$sysName='PPS_'.$getSysIdRow[sysNo].'_'.$getSysIdRow[subSysNo];
	if($DEBUG) echo "<br>sysId = $sysId";
	$dstSeverIp = $getSysIdRow[ip];
	if($DEBUG) echo "<br>dstSeverIp = $dstSeverIp";
	
	//下载数据
	/* close ftp
	$con=@ftp_connect($dstSeverIp);
	*/
	
	$connection=sftp_connect($dstSeverIp);
	
	if($DEBUG)echo "<br>con=$con";

	//login pps server
	$loginName = $ftpInfo[UserName];
	$password = $ftpInfo[Password];
	
	/* close ftp
	$log=@ftp_login($con,$loginName,$password);
	if($DEBUG)echo "<br>log=$log";
	if(!$log){
		echo "<br><font>Fail to connect $dstSeverIp.</font>";
		echo "<br><br><a href=\"./refresh.php\">$strBack</a>"; 
		exit();
	}
	*/
	
	$key=sftp_login($connection,$loginName,$password);
	if(!$key)
	{
		echo "<br><font>Fail to connect $dstSeverIp.</font>";
		echo "<br><br><a href=\"./refresh.php\">$strBack</a>"; 
		exit();
	}

	$mode=FTP_ASCII;
	$remoteFile[0]	='/usr/local/subsManage/rent.csv';
	$remoteFile[1]	='/usr/local/subsManage/bill_sms.csv';
	$remoteFile[2]	='/usr/local/subsManage/bill_discount.csv';
	$remoteFile[3]	='/usr/local/subsManage/bill_holiday.csv';
	$remoteFile[4]	='/usr/local/subsManage/comm_tariff.csv';
	$remoteFile[5]	='/usr/local/subsManage/mo_tariff.csv';
	$remoteFile[6]	='/usr/local/subsManage/mt_tariff.csv';
	$remoteFile[7]	='/usr/local/subsManage/gprs_tariff.csv';
	$remoteFile[8]	='/usr/local/subsManage/vas_mms_email_tariff.csv';
	$remoteFile[9]	='/usr/local/subsManage/vas_mms_retrieve_tariff.csv';
	$remoteFile[10]	='/usr/local/subsManage/vas_mms_send_tariff.csv';
	
	$localFile[0]	='/usr/local/apache/htdocs/ftpFile/rent.csv';
	$localFile[1]	='/usr/local/apache/htdocs/ftpFile/bill_sms.csv';
	$localFile[2]	='/usr/local/apache/htdocs/ftpFile/bill_discount.csv';
	$localFile[3]	='/usr/local/apache/htdocs/ftpFile/bill_holiday.csv';
	$localFile[4]	='/usr/local/apache/htdocs/ftpFile/comm_tariff.csv';
	$localFile[5]	='/usr/local/apache/htdocs/ftpFile/mo_tariff.csv';
	$localFile[6]	='/usr/local/apache/htdocs/ftpFile/mt_tariff.csv';
	$localFile[7]	='/usr/local/apache/htdocs/ftpFile/gprs_tariff.csv';
	$localFile[8]	='/usr/local/apache/htdocs/ftpFile/vas_mms_email_tariff.csv';
	$localFile[9]	='/usr/local/apache/htdocs/ftpFile/vas_mms_retrieve_tariff.csv';
	$localFile[10]	='/usr/local/apache/htdocs/ftpFile/vas_mms_send_tariff.csv';

	if($downloadCurrent){
		$dstTable[0]="rent";
		$dstTable[1]="bill_sms";
		$dstTable[2]="bill_discount";
		$dstTable[3]="bill_holiday";
		$dstTable[4]="comm_tariff";
		$dstTable[5]="mo_tariff";
		$dstTable[6]="mt_tariff";
		$dstTable[7]="gprs_tariff";
		$dstTable[8]="vas_mms_email_tariff";
		$dstTable[9]="vas_mms_retrieve_tariff";
		$dstTable[10]="vas_mms_send_tariff";
	}else{
		$dstTable[0]="pre_rent";
		$dstTable[1]="pre_bill_sms";
		$dstTable[2]="pre_bill_discount";
		$dstTable[3]="pre_bill_holiday";
		$dstTable[4]="pre_comm_tariff";
		$dstTable[5]="pre_mo_tariff";
		$dstTable[6]="pre_mt_tariff";
		$dstTable[7]="pre_gprs_tariff";
		$dstTable[8]="pre_vas_mms_email_tariff";
		$dstTable[9]="pre_vas_mms_retrieve_tariff";
		$dstTable[10]="pre_vas_mms_send_tariff";
	}

	$fileNum = count($remoteFile);
	for($i=0;$i<$fileNum;$i++){
		/* close ftp
		$getRes[$i]=ftp_get($con,$localFile[$i],$remoteFile[$i],$mode);
		*/
		//$getRes[$i]=sftp_get($key,$localFile[$i],$remoteFile[$i],SFTP_ASCII);
		$getRes[$i]=sftp_get($connection,$localFile[$i],$remoteFile[$i],SFTP_ASCII);
		if($DEBUG)echo "<br>getRes[$i]=$getRes[$i]";

		if($getRes[$i]){
			echo "<br>Download $remoteFile[$i] from $sysName($dstSeverIp) ..... <font color=\"#0000FF\" >ok</font>";
			$delSql = " DELETE FROM $dstTable[$i] ";
			$delRes = mysqli_query($dbConn,$delSql);

			$loadSql = "LOAD DATA INFILE '$localFile[$i]' REPLACE 
						INTO TABLE $dstTable[$i] 
						FIELDS 
							TERMINATED BY ',' 
						LINES 
							TERMINATED BY '\n'
					   ";
			//echo "<br>loadSql=$loadSql";
			$loadRes = mysqli_query($dbConn,$loadSql);
			if($loadRes){
				echo "<br>Load $localFile[$i] into OMC $dstTable[$i]..... <font color=\"#0000FF\" >ok</font>";
			}else{
				$noSuccess=1;
				echo "<br>Load $localFile[$i] into OMC $dstTable[$i].....  <font color=\"#FF0000\" >Failed</font>";
			}
		}else{
			$noSuccess=1;
			echo "<br>Download $remoteFile[$i] from $sysName($dstSeverIp) ..... <font color=\"#FF0000\" >Failed</font>";
		}
		echo "<br>";
	}

	if($noSuccess){
		if($downloadCurrent)
			insertLog("Download CURRENT TARIFF FAILURE");
		else
			insertLog("Download TARIFF PLAN FAILURE");
	}else{
		if($downloadCurrent)
			insertLog("Download CURRENT TARIFF SUCCESS");
		else
			insertLog("Download TARIFF PLAN SUCCESS");
	}

	echo "<br><br><br><br><a href=\"$PHP_SELF\"> &nbsp;&nbsp;<< Return </a>";
	adjust_content_tail();
	exit();
}

//==========================================================
//输出并显示数据
//==========================================================
?>

<?php
echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "$pagePath";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";

adjust_title_tail();
adjust_content_head();
echo "<br>";
?>
<form method="POST" action="<?php echo "$PHP_SELF";?>" name="updateTariff">
<?php
echo "<table border=\"1\" width=\"50%\"  cellpadding=\"1\"  cellspacing=\"0\"  bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td>";
if ($selectedLanguage == 'eng')
	echo "<font>Please download current tariff from PPS.</font>";
elseif ($selectedLanguage == 'chn')
	echo "<font>请从PPS下载当前费率.</font>";
echo "</td>";
echo "</tr>";
?>
<tr>
<td>
<input type="submit" name="downloadCurrent" value="<?php echo "$strDownload";?>" class="button">
</td>
</tr>

<tr bgcolor="#E6E6E6">
<td>
<?php
if ($selectedLanguage == 'eng')
	echo "<font>Please download tariff plan from PPS.</font>";
elseif ($selectedLanguage == 'chn')
	echo "<font>请从PPS下载费率计划.</font>";
?>
</td>
</tr>

<tr>
<td>
<input type="submit" name="downloadPlan" value="<?php echo "$strDownload";?>" class="button">
</td>
</tr>
</form>
<?php
adjust_content_tail();
?>
</body>
</html>
