
<?php


$DEBUG=0;
$sysTypeNo=$_REQUEST['sysTypeNo'];
$bufferNo=$_REQUEST['bufferNo'];
$keyValue=$_REQUEST['keyValue'];
$isTryCreate=$_REQUEST['isTryCreate'];


global $OMC_server,$pubConn;

if(isset($sysId)){
	$sysIdArr	= explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];
}elseif(isset($sysTypeNo)){
	//����vlrû��heartbeat,��Ҫ����MSC��״̬��ȷ�������ĸ�ϵͳ
	if($sysTypeNo == '325'){
		$getSysIdSql = "SELECT sysNo,subSysNo FROM OMC_PUB.sysInfo 
				WHERE sysTypeNo='325' 
				ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo";
	}else{
		$getSysIdSql = "SELECT sysNo,subSysNo FROM OMC_PUB.sysInfo 
				WHERE sysTypeNo='$sysTypeNo' 
				ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo";	
	}
	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
		//debug_log("/tmp/debug.txt",$getSysIdSql);
	$getSysIdResult=mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);
	if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-1");
	if(mysqli_num_rows($getSysIdResult) > 0){
		$getSysIdRows=mysqli_fetch_array($getSysIdResult);
		$sysNo		= $getSysIdRows[sysNo];
		$subSysNo	= $getSysIdRows[subSysNo];
		$sysId = $sysTypeNo.'_'.$sysNo.'_'.$subSysNo;
	}else{
		echo "<br><font size=3 color=red>Can not find this system's IP in system List,";
		echo "Please add it!</font>";
		exit(0);
	}
}else{
	echo "Can not find sysTYpeNo!!";
	exit(0);
}

if(($isBatchCreate>0&&$isCreate>0)&&(!isset($batchNum)||$batchNum>100||$batchNum<=0)){
	echo "<SCRIPT LANGUAGE=\"JavaScript\">";
	echo "alert(\"The batch amount should be >0 and <= 100. \")";
	echo "</SCRIPT>";
	echo " <meta http-equiv=\"refresh\" content=\"0; url=$PHP_SELF?sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo&isTryCreate=1&isBatchCreate=1\">";
	exit();
}
$objDb='OBJ_'.$sysTypeNo;
$objConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], $objDb);

$dataTypeNo	= (strlen($dataTypeNo) > 0)?$dataTypeNo:0;
$confTable	= 'subsParamConf_'.$dataTypeNo;
if($sysTypeNo==390 || $sysTypeNo == 365 || $sysTypeNo == 366){
	$getConfSql="SELECT sysTypeName,dataTypeNo,indexFieldName,subsDataTable,tmpDataTable  
				FROM $sysSubsConfTable,$systemConfTable 
				WHERE $sysSubsConfTable.sysTypeNo='$sysTypeNo' 
				AND $systemConfTable.sysTypeNo='$sysTypeNo'
				AND $sysSubsConfTable.dataTypeNo='$dataTypeNo'
				AND $sysSubsConfTable.sysNo='$sysNo'
				";
}else{
	$getConfSql="SELECT sysTypeName,dataTypeNo,indexFieldName,subsDataTable,tmpDataTable  
				FROM $sysSubsConfTable,$systemConfTable 
				WHERE $sysSubsConfTable.sysTypeNo='$sysTypeNo' 
				AND $systemConfTable.sysTypeNo='$sysTypeNo'
				AND $sysSubsConfTable.dataTypeNo='$dataTypeNo' 
				";
}
//if($DEBUG) 
//echo "<br>getConfSql=$getConfSql";
//exit();
$getConfResult=mysqli_query($pubConn,$getConfSql);
echo mysqli_error($pubConn);

if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-2");
$getConfRows=mysqli_fetch_array($getConfResult);
$sysTypeName	=$getConfRows['sysTypeName'];
$indexFieldName	=$getConfRows['indexFieldName'];
$srcDataTable	=$getConfRows['subsDataTable'];
$tmpDataTable	=$getConfRows['tmpDataTable'];
global $userName;
 
if($sysTypeName=="VSS") $sysTypeName="VMS";
$srcDataConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], $sysTypeName."_DB");

if($DEBUG)echo "<br>srcDataTable:$srcDataTable<br>tmpDataTable:$tmpDataTable";
//�����û��Ƿ������˲���
$editFlag = false;
if($editFlag < 0){
	$chkFlagSql = "SELECT editFlag	FROM $tmpDataTable WHERE $indexFieldName = '$keyValue'";
	if($DEBUG) echo "<br>chkFlagSql=$chkFlagSql";
	$result=mysqli_query($objConn,$chkFlagSql);
	echo mysqli_error($objConn);
	if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-3");
	$flagRow=mysqli_fetch_array($result);
	$editFlag = $flagRow[0];
	if($editFlag > 0){
		echo "This subscriber is be other one editing!!,please wait and try again";
	}else{
		$setFlagSql = "UPDATE $tmpDataTable SET editFlag = '1' WHERE $indexFieldName = '$keyValue'";
		mysqli_query($objConn,$setFlagSql);
	}
}

$needSaveEditValue	= 0;
$needSendComm		= 0;
$indexValueList		= $keyValue;

//*********��ȡһ���û�*************************** 
if($isRead > 0){//$getFromSys
	$commType = $nReadComm;
	$needSendComm = 1;
}
//*********�޸�һ���û�*************************
if($isChange > 0){
	$needSaveEditValue = 1;
	$commType = $nChangeComm;
	$needSendComm = 1;
}
//*********����һ���û�*************************** 
if($isCreate > 0){
	if($isCreate == 2){//create in omc
		$needSaveEditValue = 1;
	}else{//$isCreate == 2,create in server
		$commType = $nCreateComm;
		$needSendComm = 1;
		$needSaveEditValue = 1;
	}
}

//*********ɾ��һ���û�*************************** 
if($isDelete > 0){
	$commType = $nDeleteComm;
	$needSendComm = 1;
}

//*********���������û�*************************** 
if($isLoadIndex > 0){
	$commType = $nLoadIndexComm;
	$needSendComm = 1;
}

//*********�滻�û�index����*************************** 
if($isReplaceIndex > 0){
	$chkIndexPass = 0;
	if($isConfirmed > 0){
		$chkIndexPass=1;
		if($DEBUG) echo "<br>newIndexValue=$newIndexValue";
		//���newIndexValue����Ч��
		switch($sysTypeNo){
			case '330':
				//���㳤��
				for($i=strlen($newIndexValue);$i<16;$i++){
					$imsi='0'.$newIndexValue;
				}
				$oldMccMnc=substr($keyValue,0,6);
				$newMccMnc=substr($newIndexValue,0,6);
				/*
				if($oldMccMnc != $newMccMnc){
					echo "<font color=red>Please Check new IMSI's MCC AND MNC!<font>";
					$chkIndexPass=0;
				}*/
				if($newIndexValue == $keyValue){
					echo "<font color=red>New IMSI Number same as old's!<font>";
					$chkIndexPass=0;
				}
				break;
			default:
				echo "Unknown sysTypeNo($sysTypeNo)!";
				exit();
		}
		if($chkIndexPass > 0){
			$commType = $nReplaceIndexComm;
			$needSendComm = 1;
		}
	}
	if($chkIndexPass == 0){	
		if($DEBUG) echo "<br>";
		echo "\n <form method=\"POST\" name=\"update\" action=\"$PHP_SELF\" >";
		echo "\n <table border=\"1\" width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"  bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\"> ";
		$showIndexName=strtoupper($indexFieldName);
		echo "\n <tr bgcolor=\"#E6E6E6\"><th colspan=\"2\">Replace $showIndexName</th></tr>";
		echo "\n <tr><td>Old $showIndexName:</td><td>$keyValue</td></tr>";
		echo "\n <tr><td>New $showIndexName:</td><td>";
		echo "\n <input type=\"text\" name=\"newIndexValue\" maxlength=\"16\" size=\"18\" value=\"$newIndexValue\" class=\"text\">";
		echo "\n </td></tr></table>";
		echo "<br> &nbsp;&nbsp;&nbsp;&nbsp; <input type=\"submit\" value=\"Replace\" class=\"button\">";
		echo "\n <input type=\"hidden\" name=\"sysId\"			value=\"$sysId\">";
		echo "\n <input type=\"hidden\" name=\"dataTypeNo\"		value=\"$dataTypeNo\">";
		echo "\n <input type=\"hidden\" name=\"indexFieldName\"	value=\"$indexFieldName\">";
		echo "\n <input type=\"hidden\" name=\"keyValue\"		value=\"$keyValue\">";
		echo "\n <input type=\"hidden\" name=\"isReplaceIndex\"	value=\"1\">";
		echo "\n <input type=\"hidden\" name=\"isConfirmed\"	value=\"1\">";
		echo "</form>";
		exit();
	}
}

//---------�������------------------------------
if($needSaveEditValue == 1){	
	if($isBatchCreate>0&&$isCreate>0){
		$stopFlag=0;
		for($tmpNum=0;$tmpNum<$batchNum;$tmpNum++){
			$counter=0;
			for($i=$bgnNo;$i<$endNo;$i++){
				if(${'checkBox_'.$i} > 0){
					$editValue=${'editValue_'.$i};
					$tmpFieldName = ${'fieldName_'.$i};
					if($tmpFieldName == $indexFieldName){
						if($tmpNum==0)
							$indexValueList = $editValue;
						$tmpLen=strlen($editValue);
						$tmpEditValue=$editValue[$tmpLen-4].$editValue[$tmpLen-3].$editValue[$tmpLen-2].$editValue[$tmpLen-1];
						$tmpEditValue = $tmpEditValue+$tmpNum;
						if($tmpEditValue%10000==0 && $tmpNum!=0){
							$batchNum=$tmpNum;
							$stopFlag=1;
						}
						$tmpEditValue=''.$tmpEditValue;//ȷ��tmpEditValueΪ�ַ���
						$tmpEditValue=omc_keep_length($tmpEditValue,'0',4);
						$editValue[$tmpLen-4]= $tmpEditValue[0];
						$editValue[$tmpLen-3]= $tmpEditValue[1];
						$editValue[$tmpLen-2]= $tmpEditValue[2];
						$editValue[$tmpLen-1]= $tmpEditValue[3];
						
						//echo "<br>editValue=$editValue";
						
					}
					
					if($sysTypeNo==330&&$tmpFieldName == 'inter_msisdn'){
						$tmpEditValueArr=explode("E",$editValue);
						$editValue=$tmpEditValueArr[0];
						$tmpLen=strlen($editValue);
						$tmpEditValue=$editValue[$tmpLen-4].$editValue[$tmpLen-3].$editValue[$tmpLen-2].$editValue[$tmpLen-1];
						$tmpEditValue = $tmpEditValue+$tmpNum;
						if($tmpEditValue%10000==0 && $tmpNum!=0){
							$batchNum=$tmpNum;
							$stopFlag=1;
						}
						$tmpEditValue=''.$tmpEditValue;//ȷ��tmpEditValueΪ�ַ���
						$tmpEditValue=omc_keep_length($tmpEditValue,'0',4);
						$editValue[$tmpLen-4]=$tmpEditValue[0];
						$editValue[$tmpLen-3]=$tmpEditValue[1];
						$editValue[$tmpLen-2]=$tmpEditValue[2];
						$editValue[$tmpLen-1]=$tmpEditValue[3];
						$editValue=omc_keep_length($editValue,'E',18,0);
						//echo "<br>editValue=$editValue";
					}
					
					if($counter == 0){
						$setValueStr	= "$tmpFieldName = '$editValue'";
						$fieldNameList	= "$tmpFieldName";
					}else{
						$setValueStr	.= ", $tmpFieldName = '$editValue'";
						$fieldNameList	.= ",$tmpFieldName";
					}
					$counter++;	
				}
			}
			if($stopFlag==1)
				break;
			if($isCreate == 2){//��omc�ϴ���
				$saveSql = "REPLACE $srcDataTable SET $setValueStr";
				$saveResult = mysqli_query($srcDataConn,$saveSql);

			}else{
				$saveSql = "REPLACE $tmpDataTable SET $setValueStr";
				$saveResult = mysqli_query($objConn,$saveSql);
				echo mysqli_error($objConn);

			}
			
			if($DEBUG) 
			echo "<br>saveSql = $saveSql <br>objDb=$objDb";
			echo mysqli_error($pubConn);
			if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-4");
		}
		$keyValue=$indexValueList;

		if($isCreate == 2){//��omc�ϴ���
			echo "<script LANGUAGE=\"JavaScript\">";
			echo "\n window.location.href=\"./subscriberListDown.php?sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo\";";
			echo "\n </script>";
			exit();
		}
	}else{
		$counter=0;
		//echo "bgnNo=$endNo<br>";
		for($i=$bgnNo;$i<$endNo;$i++){
			//echo "checkBox_$i=".${'checkBox_'.$i}."<br>";

			if(${'checkBox_'.$i} > 0){
				$editValue=${'editValue_'.$i};
				$tmpFieldName = ${'fieldName_'.$i};
				if($isCreate > 0){
					if($tmpFieldName == $indexFieldName){
						$indexValueList = $editValue;
					}
				}else{
					if($tmpFieldName == $indexFieldName){
						continue;
					}
				}
				if($counter == 0){
					$setValueStr	= "$tmpFieldName = '$editValue'";
					$fieldNameList	= "$tmpFieldName";
				}else{
					$setValueStr	.= ", $tmpFieldName = '$editValue'";
					$fieldNameList	.= ",$tmpFieldName";
				}
				$counter++;	
			}
		}
		
		if($isCreate){
			$keyValue=$indexValueList;
			if($isCreate == 2){//��omc�ϴ���
				$saveSql = "REPLACE $srcDataTable SET $setValueStr";
				$saveResult = mysqli_query($srcDataConn,$saveSql);

			}else{
				$saveSql = "REPLACE $tmpDataTable SET $setValueStr";
				$saveResult = mysqli_query($objConn,$saveSql);
				echo mysqli_error($objConn);

			}
		}else{
				$saveSql = "UPDATE $tmpDataTable SET $setValueStr
				 WHERE $indexFieldName = '$keyValue'
				";
				$saveResult = mysqli_query($objConn,$saveSql);
				echo mysqli_error($objConn);

		}

		if($DEBUG) echo "<br>saveSql = $saveSql <br>objDb=$objDb";
		//if(mysql_errno())
		//debug_log("/tmp/debug.txt","remark-5:$saveSql,tmpFieldName=$tmpFieldName,editValue'=$editValue'");
		echo mysqli_error($pubConn);
	}
}

//if($DEBUG && $_SERVER['REQUEST_METHOD'] == "POST") exit();
//echo "$sysId";
//exit();
//---------���������תҳ��----------------------------

global $objConn, $srcDataConn;
if($needSendComm == 1 && isset($commType)){
	if($commType == $nLoadIndexComm || $commType == $nDeleteComm){
		if($commType == $nLoadIndexComm){
			$okURL	  ="./subscriberListDown.php?sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo&sysNo=$sysNo&sysId=$sysId";
			$failURL  ="./subscriberListDown.php?sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo&sysNo=$sysNo&sysId=$sysId";
		}else{
			$okURL	  ="./subscriberListDown.php?sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo&sysNo=$sysNo&sysId=$sysId&commType=$nDeleteComm&keyValue=$keyValue";
			$failURL  = "$PHP_SELF?sysId=$sysId&bgnNo=$bgnNo&keyValue=$keyValue&canSetNum=$canSetNum&bufferNo=$bufferNo&dataTypeNo=$dataTypeNo";
		}
	}else{
		if($commType == $nReplaceIndexComm){
			$okURL	  = "$PHP_SELF?sysId=$sysId&bgnNo=$bgnNo&keyValue=$newIndexValue&canSetNum=$canSetNum&bufferNo=$bufferNo&dataTypeNo=$dataTypeNo&isRead=1";
		}elseif($commType == $nCreateComm){
			if($isBatchCreate==1)
				$okURL	  = "$PHP_SELF?sysId=$sysId&bgnNo=$bgnNo&keyValue=$keyValue&canSetNum=$canSetNum&bufferNo=$bufferNo&dataTypeNo=$dataTypeNo&isBatchCreate=$isBatchCreate";
			else
				$okURL	  = "$PHP_SELF?sysId=$sysId&bgnNo=$bgnNo&keyValue=$keyValue&canSetNum=$canSetNum&bufferNo=$bufferNo&dataTypeNo=$dataTypeNo&isRead=1";
				
		}else{
			$okURL	  = "$PHP_SELF?sysId=$sysId&bgnNo=$bgnNo&keyValue=$keyValue&canSetNum=$canSetNum&bufferNo=$bufferNo&dataTypeNo=$dataTypeNo";
		}
		$failURL  = "$PHP_SELF?sysId=$sysId&bgnNo=$bgnNo&keyValue=$keyValue&canSetNum=$canSetNum&bufferNo=$bufferNo&dataTypeNo=$dataTypeNo&isBatchCreate=$isBatchCreate";
	}
	if($DEBUG) echo "<br>okURL =$okURL ";	
	sleep(1);
		
	if($commType == $nReplaceIndexComm){
		if($DEBUG) echo "<br>subsCommSend(sysId,dataTypeNo,indexValueList,fieldNameList,commType,newIndexValue)";
		if($DEBUG) echo "<br>subsCommSend($sysId,$dataTypeNo,$indexValueList,$fieldNameList,$commType,$newIndexValue)";
		$commIdStr = subsCommSend($sysId,$dataTypeNo,"$indexValueList","$fieldNameList",$commType,$newIndexValue);
	}else{
		if($DEBUG) echo "<br>subsCommSend(sysId,dataTypeNo,indexValueList,fieldNameList,commType)";
		if($DEBUG) echo "<br>subsCommSend($sysId,$dataTypeNo,$indexValueList,$fieldNameList,$commType)";
		if($isBatchCreate==1){
			for($tmpNum=0;$tmpNum<$batchNum;$tmpNum++){
				$tmpCommIdStr = subsCommSend($sysId,$dataTypeNo,"$indexValueList","$fieldNameList",$commType);
				$tmpLen=strlen($indexValueList);
				$tmpIndexValueList=$indexValueList[$tmpLen-4].$indexValueList[$tmpLen-3].$indexValueList[$tmpLen-2].$indexValueList[$tmpLen-1];
				$tmpIndexValueList = $tmpIndexValueList+1;
				$tmpIndexValueList=''.$tmpIndexValueList;//ȷ��tmpIndexValueListΪ�ַ���
				$tmpIndexValueList=omc_keep_length($tmpIndexValueList,'0',4);
				$indexValueList[$tmpLen-4]=$tmpIndexValueList[0];
				$indexValueList[$tmpLen-3]=$tmpIndexValueList[1];
				$indexValueList[$tmpLen-2]=$tmpIndexValueList[2];
				$indexValueList[$tmpLen-1]=$tmpIndexValueList[3];
				//echo "<br>commIdStr=subsCommSend($sysId,$dataTypeNo,\"$indexValueList\",\"$fieldNameList\",$commType)";
				$commIdStr=$commIdStr.$tmpCommIdStr;
			}
		}else
			$commIdStr = subsCommSend($sysId,$dataTypeNo,"$indexValueList","$fieldNameList",$commType);
		//exit();
	}
	if($DEBUG) {
		echo "<br>commIdStr=subsCommSend($sysId,$dataTypeNo,\"$indexValueList\",\"$fieldNameList\",$commType)";
	}
	if(isset($commIdStr) && $commIdStr != -1){
		if($commType == $nLoadIndexComm){
			$srcDataTableArr=explode('.',$srcDataTable);
			$srcDataDb=$srcDataTableArr[0];
			$clearDataSql="DELETE FROM $srcDataTable";
			mysqli_query($srcDataConn,$clearDataSql);
			if($DEBUG)
				echo "mysqli_query($srcDataConn,$clearDataSql);<br>";
			$clearDataSql="DELETE FROM $tmpDataTable";
			if($DEBUG)
			echo "mysqli_query($srcDataConn,$clearDataSql);<br>";
			mysqli_query($objConn,$clearDataSql);
			
			mysqli_error($srcDataConn);
			mysqli_error($objConn);

		}
		$failURL = urlencode($failURL);
		$okURL	 = urlencode($okURL);
		$scanURL = "commIdStr=$commIdStr&okURL=$okURL&failURL=$failURL";
		if($DEBUG) echo "<br>scanURL = $scanURL";
if($DEBUG) exit();
		echo " <meta http-equiv=\"refresh\" content=\"0; url=./commScan.php?$scanURL&sysId=$sysId&sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo&userType=$userType&searchInServer=$searchInServer&isLoadIndex=$isLoadIndex&isBatchCreate=$isBatchCreate\"> ";  
		exit();
	}else{
		echo "<br><font size=3 color=red>SEND COMMAND ERROR!<br></font>";
	}
}


//��Ҫ�༭�����ݵ�����ʱ��
$getDataSql="SELECT * FROM $srcDataTable WHERE $indexFieldName='$keyValue'";
if($DEBUG) 
	echo "<br>getDataSql=$getDataSql";

$result=mysqli_query($srcDataConn,$getDataSql);
$fieldNum=mysqli_num_fields($result);
if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-6");
echo mysqli_error($srcDataConn);
if($DEBUG) 
	echo "<br>fieldNum=$fieldNum";
$tmpRow=mysqli_fetch_array($result);
if($DEBUG) echo "<br>tmpRow[mo_expiration_date]=$tmpRow[mo_expiration_date]";

#if($DEBUG) echo "<br>valuestr=$valueStr,bufferNo=$bufferNo";
for($i=0;$i<$fieldNum;$i++){
	$valueStr = "$valueStr ,'$tmpRow[$i]'";
#	if($DEBUG) echo "<br>i:$i,tmpRow:$tmpRow[$i],valuestr=$valueStr";
}
$updateSql = "REPLACE $tmpDataTable VALUES('0' $valueStr)";
mysqli_query($objConn,$updateSql);
#if($DEBUG) echo "<br>bufferNo=$bufferNo";

if($DEBUG) echo "<br>updateSql=$updateSql";
//exit();
if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-7");
echo mysqli_error($objConn);

//	echo"<br>objDb =$isTryCreate <br> ParamSql = $paramSql <br> ";

//����������û�������ȱʡֵ
if($isTryCreate > 0){
	//�ڲ��������ȡ��˵��������ʱ������ȡ����ص�ֵ
	$paramSql = " SELECT * FROM $confTable  
					WHERE $confTable.operType > '0' 
					ORDER BY level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,level_10 
				";
	if($DEBUG) 
		echo"<br>objDb =$objDb <br> ParamSql = $paramSql <br> ";
	$paramResult = mysqli_query($objConn,$paramSql);
	if(mysql_errno())
		debug_log("/tmp/debug.txt","remark-8");
	echo mysqli_error($objConn);
	$i=0;
	while($paramRows = @mysqli_fetch_array($paramResult)){
		$tmpRow[$paramRows[fieldName]]=$paramRows[initValue];
	}  
	if($DEBUG) echo "<br>tmpRow[mo_expiration_date]=$tmpRow[mo_expiration_date]";
}

?>
