<?php
/*********************************************************      
 程序说明:         
    功能说明:本程序实现对数据表中的参数的初始化，根据参数类型的定义，
              在参数表中填入缺省参数值和参数对象编号。

    调用关系:调用: header.inc     
              被调用:paramConf.php     
 
    变量说明:

    返回值:

 作者:      
      
修改注释:      
NO.1      
姓名	:            
时间	:           
修改说明:      
*********************************************************/ 
@set_time_limit(600);
require("../../inc/header.inc");

$DEBUG = 0;

function omcGetObjDb($sysTypeNo){
	$dbName = 'OBJ_'.$sysTypeNo;
	return($dbName);
}

function omcGetObjTbl($tableType='paramConf',$sysTypeNo,$sysNo=0,$subSysNo=0){
	$dbName = 'OBJ_'.$sysTypeNo;
	switch($tableType){
		case 'paramConf':
			$tableName = $dbName.'.paramConf';
			break;
		case 'param':
			$tableName = $dbName.'.param_'.$sysNo;
			break;
		default:
			echo "<br><b>function omcGetObjTbl(): Unknown table Type:$tableType</b>";
			exit();
	}
	return($tableName);
}
function initParbyConf($sysTypeNo)
{
	$DEBUG=0;
	$pubDb = "OMC_PUB";
	global $PlatTypeDefArr;
	$objDb = omcGetObjDb($sysTypeNo);
	$paramConfTable = omcGetObjTbl('paramConf',$sysTypeNo);  //得到参数定义表的名称
	
	$nPubFlag = 99;
	$nSubSysParam		= 0;
	$nGlobalSubSysParam = 1;
	$nGlobalSysParam	= 2;
	
	//********************************取出设备类型定义*******************************
	$sysConfTable = 'OMC_PUB.sysConf';
	$confSql	  = "SELECT maxSysNum,maxSubSysNum,needGlobalSysParam,snmpInstanceBgn 
					FROM $sysConfTable 
					WHERE sysTypeNo='$sysTypeNo'
					";
	$confResult	  = mysqli_query($pubConn ,$confSql);
	$confRows	  = mysqli_fetch_array($confResult);
	$maxSysNum	  = $confRows[maxSysNum];
	$maxSubSysNum = $confRows[maxSubSysNum];
	$needGlobalSysParam = $confRows[needGlobalSysParam];
	$snmpInstanceBgn = $confRows[snmpInstanceBgn];
	if($DEBUG) echo "<br>confSql = $confSql";
	if($DEBUG) echo "<br>sysTypeNo = $sysTypeNo";
	if($DEBUG) echo "<br>maxSysNum = $maxSysNum";
	if($DEBUG) echo "<br>maxSubSysNum = $maxSubSysNum";
	if($DEBUG) echo "<br>needGlobalSysParam = $needGlobalSysParam";
	
	//********************************取出参数类型定义**********************************
	$getConfSql = "SELECT paramType,number,length,valueType,operType,initValue,
				level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,level_10 
				FROM $paramConfTable 
				WHERE number > '0' AND LENGTH(initValue) > 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>getConfSql = $getConfSql";
	$getConfResult = mysqli_query($pubConn,$getConfSql);
	echo mysqli_error($pubConn);
	if(mysqli_num_rows($getConfResult) < 0){
		echo "Cannot get vailed record form paramConfTable:$paramConfTable";
	}
	//********************************循环填充每一个参数类型****************************
	$confRowsNum = mysqli_num_rows($getConfResult);
	while($confRows = mysqli_fetch_array($getConfResult)){
		//取出该类型参数信息
	    $paramType	= $confRows[paramType];
		$number		= $confRows[number];
		$length		= $confRows[length];
		$valueType	= $confRows[valueType];
		$operType	= $confRows[operType];
		$initValue	= $confRows[initValue];
		
		$levelWhereStr = '';
		for($i=1; $i<=10; $i++){
			$level_i = "level_$i";
			$levelWhereStr .= " $level_i=" . $confRows[$level_i] . " and";
		}
		$levelWhereStr = rtrim($levelWhereStr,'and');
		
		if($paramType == $nGlobalSysParam){
			$paramTable = omcGetObjTbl('param',$sysTypeNo,$nPubFlag);
			$subSysNo = $nPubFlag;
			
			$updSql = "update $paramTable set initValue='$initValue', readValue='' where $levelWhereStr";
			if($DEBUG)echo "<br>updSql = $updSql";
			mysqli_query($pubConn,$updSql);
			
			if($sysTypeNo==222||$sysTypeNo==223){//platform system
				for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){ //每个系统编号
					if($sysNo!=8&&$sysNo!=11)//目前只有VMS AAS用到了平台参数
						continue;
					$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);
					$updSql = "update $paramTable set initValue='$initValue', readValue='' where $levelWhereStr";
					if($DEBUG)echo "<br>updSql = $updSql";
					mysqli_query($pubConn,$updSql);
				}
			}
		}else if($paramType == $nGlobalSubSysParam){
			for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){ //每个系统编号
				$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);
				$subSysNo = $nPubFlag;
				$updSql = "update $paramTable set initValue='$initValue', readValue='' where $levelWhereStr";
				if($DEBUG)echo "<br>updSql = $updSql";
				mysqli_query($pubConn,$updSql);
			}
		}else{//子系统参数
			for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){ //每个系统编号
				$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);
				$updSql = "update $paramTable set initValue='$initValue', readValue='' where $levelWhereStr";
				if($DEBUG)echo "<br>updSql = $updSql";
				mysqli_query($pubConn,$updSql);
			}
		}//end of ($paramType)
		
		if($confRowsNum > 20){
			print("&gt;");
		}else{
			print("&gt;&gt;");
		}
		flush();
		//if($DEBUG) exit();
	}//end of while
}

function initParam($sysTypeNo)
{
	global $PlatTypeDefArr;
	$objDb = omcGetObjDb($sysTypeNo);
	$paramConfTable = omcGetObjTbl('paramConf',$sysTypeNo);  //得到参数定义表的名称
	$maxFieldLen = 128;
	
	$nPubFlag = 99;
	$nSubSysParam		= 0;
	$nGlobalSubSysParam = 1;
	$nGlobalSysParam	= 2;
	
	//********************************取出设备类型定义*******************************
	$sysConfTable = 'OMC_PUB.sysConf';
	$confSql	  = "SELECT maxSysNum,maxSubSysNum,needGlobalSysParam,snmpInstanceBgn 
					FROM $sysConfTable 
					WHERE sysTypeNo='$sysTypeNo'
					";
	$confResult	  = mysqli_query($pubConn ,$confSql);
	$confRows	  = mysqli_fetch_array($confResult);
	$maxSysNum	  = $confRows[maxSysNum];
	$maxSubSysNum = $confRows[maxSubSysNum];
	$needGlobalSysParam = $confRows[needGlobalSysParam];
	$snmpInstanceBgn = $confRows[snmpInstanceBgn];
	
	if($DEBUG) echo "<br>sysTypeNo = $sysTypeNo";
	if($DEBUG) echo "<br>maxSysNum = $maxSysNum";
	if($DEBUG) echo "<br>maxSubSysNum = $maxSubSysNum";
	if($DEBUG) echo "<br>needGlobalSysParam = $needGlobalSysParam";
	
	//************删去每个系统原有的参数表，并创建新的表************************************
	if($needGlobalSysParam > '0'){
		$GlobalSysParamTable = 'param_'.$nPubFlag;
		$dropSql = " DROP TABLE $GlobalSysParamTable ";
		if($DEBUG) echo "<br>---->dropSql = $dropSql";
		mysqli_query($pubConn,$dropSql);
		//echo mysqli_error($pubConn);
		sleep(1);
		$createTableSql="CREATE TABLE $GlobalSysParamTable ( 
				subSysNo tinyint(3) unsigned DEFAULT '0' NOT NULL,
				paramType tinyint(1) unsigned DEFAULT '0' NOT NULL,
				level_1 int(5) unsigned DEFAULT '0' NOT NULL,
				level_2 int(5) unsigned DEFAULT '0' NOT NULL,
				level_3 int(5) unsigned DEFAULT '0' NOT NULL,
				level_4 int(5) unsigned DEFAULT '0' NOT NULL,
				level_5 int(5) unsigned DEFAULT '0' NOT NULL,
				level_6 int(5) unsigned DEFAULT '0' NOT NULL,
				level_7 int(5) unsigned DEFAULT '0' NOT NULL,
				level_8 int(5) unsigned DEFAULT '0' NOT NULL,
				level_9 int(5) unsigned DEFAULT '0' NOT NULL,
				level_10 int(5) unsigned DEFAULT '0' NOT NULL,
				instanceNo int(5) unsigned DEFAULT '0' NOT NULL,
				sortNo tinyint(2) unsigned DEFAULT '0' NOT NULL,
				valueType tinyint(1) unsigned DEFAULT '0' NOT NULL,
				operType tinyint(1) unsigned DEFAULT '0' NOT NULL,
				length smallint(3) unsigned DEFAULT '0' NOT NULL,
				initValue varchar(255) binary NOT NULL,
				readValue varchar(255) binary NOT NULL,
				UNIQUE KEY keyList( subSysNo ,level_1,level_2,level_3,level_4,level_5,
				level_6, level_7 ,level_8,level_9,level_10,instanceNo),
				KEY subSysNo (subSysNo),
				KEY level_1 (level_1),
				KEY level_2 (level_2),
				KEY level_3 (level_3),
				KEY level_4 (level_4),
				KEY level_5 (level_5),
				KEY level_6 (level_6),
				KEY level_7 (level_7),
				KEY level_8 (level_8),
				KEY level_9 (level_9),
				KEY level_10 (level_10),
				KEY instanceNo (instanceNo)
				)";
		if($DEBUG) echo "<br>---->createTableSql = $createTableSql";
		mysqli_query($pubConn,$createTableSql);
		echo mysqli_error($pubConn);
	}
	
	for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){
		$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);  //得到参数定义表的名称
		if($DEBUG) echo "<br>---->paramTable = $paramTable";
		
		$dropSql = " DROP TABLE $paramTable ";
		if($DEBUG) echo "<br>---->dropSql = $dropSql";
		mysqli_query($pubConn,$dropSql);
		echo mysqli_error($pubConn);
	    sleep(1);
		$createTableSql="CREATE TABLE $paramTable ( 
				subSysNo tinyint(3) unsigned DEFAULT '0' NOT NULL,
				paramType tinyint(1) unsigned DEFAULT '0' NOT NULL,
				level_1 int(5) unsigned DEFAULT '0' NOT NULL,
				level_2 int(5) unsigned DEFAULT '0' NOT NULL,
				level_3 int(5) unsigned DEFAULT '0' NOT NULL,
				level_4 int(5) unsigned DEFAULT '0' NOT NULL,
				level_5 int(5) unsigned DEFAULT '0' NOT NULL,
				level_6 int(5) unsigned DEFAULT '0' NOT NULL,
				level_7 int(5) unsigned DEFAULT '0' NOT NULL,
				level_8 int(5) unsigned DEFAULT '0' NOT NULL,
				level_9 int(5) unsigned DEFAULT '0' NOT NULL,
				level_10 int(5) unsigned DEFAULT '0' NOT NULL,
				instanceNo int(5) unsigned DEFAULT '0' NOT NULL,
				sortNo tinyint(2) unsigned DEFAULT '0' NOT NULL,
				valueType tinyint(1) unsigned DEFAULT '0' NOT NULL,
				operType tinyint(1) unsigned DEFAULT '0' NOT NULL,
				length smallint(3) unsigned DEFAULT '0' NOT NULL,
				initValue varchar(255) binary NOT NULL,
				readValue varchar(255) binary NOT NULL,
				UNIQUE KEY keyList( subSysNo ,level_1,level_2,level_3,level_4,level_5,
				level_6, level_7 ,level_8,level_9,level_10,instanceNo),
				KEY subSysNo (subSysNo),
				KEY level_1 (level_1),
				KEY level_2 (level_2),
				KEY level_3 (level_3),
				KEY level_4 (level_4),
				KEY level_5 (level_5),
				KEY level_6 (level_6),
				KEY level_7 (level_7),
				KEY level_8 (level_8),
				KEY level_9 (level_9),
				KEY level_10 (level_10),
				KEY instanceNo (instanceNo)
				)";
		if($DEBUG) echo "<br>---->createTableSql = $createTableSql";
		mysqli_query($pubConn,$createTableSql);
		echo mysqli_error($pubConn);
	}//end of for()
	
	//********************************取出参数类型定义**********************************
	$getConfSql = "SELECT paramType,number,length,valueType,operType,initValue,
				level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,level_10 
				FROM $paramConfTable 
				WHERE number > '0' AND LENGTH(initValue) > 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>getConfSql = $getConfSql";
	$getConfResult = mysqli_query($pubConn,$getConfSql);
	echo mysqli_error($pubConn);

	if(mysqli_num_rows($getConfResult) < 0){
		echo "Cannot get vailed record form paramConfTable:$paramConfTable";
	}
	//********************************循环填充每一个参数类型****************************
	$confRowsNum = mysqli_num_rows($getConfResult);
	while($confRows = mysqli_fetch_array($getConfResult)){
		//取出该类型参数信息
	    $paramType	= $confRows[paramType];
		$number		= $confRows[number];
		$length		= $confRows[length];
		$valueType	= $confRows[valueType];
		$operType	= $confRows[operType];
		$initValue	= $confRows[initValue];
		$sortNo		= 0;
		$level_1	= $confRows[level_1];
		$level_2	= $confRows[level_2];
		$level_3	= $confRows[level_3];
		$level_4	= $confRows[level_4];
		$level_5	= $confRows[level_5];
		$level_6	= $confRows[level_6];
		$level_7	= $confRows[level_7];
		$level_8	= $confRows[level_8];
		$level_9	= $confRows[level_9];
		$level_10	= $confRows[level_10];
		for($instanceNo=$snmpInstanceBgn;$instanceNo<$number+$snmpInstanceBgn;$instanceNo++){//每个实例编号
			if($paramType == $nGlobalSysParam){
				$paramTable = omcGetObjTbl('param',$sysTypeNo,$nPubFlag);
				$subSysNo = $nPubFlag;
				$insertSql="INSERT INTO $paramTable
						(subSysNo,paramType,instanceNo,sortNo,valueType,operType,length,initValue,
						   level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,Level_10)
						 VALUES
						 ('$subSysNo','$paramType','$instanceNo','$sortNo','$valueType','$operType','$length',
							 '$initValue','$level_1','$level_2','$level_3','$level_4','$level_5',
								'$level_6','$level_7','$level_8','$level_9','$level_10')
						  ";
				if($DEBUG) echo "<br> $insertSql";
				mysqli_query($pubConn,$insertSql);
				echo mysqli_error($pubConn);
				
				if($sysTypeNo==222||$sysTypeNo==223){//platform system
					for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){ //每个系统编号
						if($sysNo!=8&&$sysNo!=11)//目前只有VMS AAS用到了平台参数
							continue;
						$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);
						
						for($subSysNo=0; $subSysNo < $PlatTypeDefArr[$sysNo][maxSysNum]; $subSysNo++){//每个子系统
							$insertSql="INSERT INTO $paramTable
									(subSysNo,paramType,instanceNo,sortNo,valueType,operType,length,initValue,
									   level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,Level_10)
								 	VALUES
								 	('$subSysNo','$nSubSysParam','$instanceNo','$sortNo','$valueType','$operType','$length',
										 '$initValue','$level_1','$level_2','$level_3','$level_4','$level_5',
											'$level_6','$level_7','$level_8','$level_9','$level_10')
								  	";
							if($DEBUG) 
							echo "<br> $insertSql";
							mysqli_query($pubConn,$insertSql);
							echo mysqli_error($pubConn);
						}//end for
					}
				}
			}else if($paramType == $nGlobalSubSysParam){
				for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){ //每个系统编号
					$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);
					$subSysNo = $nPubFlag;
					$insertSql="INSERT INTO $paramTable
							(subSysNo,paramType,instanceNo,sortNo,valueType,operType,length,initValue,
							   level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,Level_10)
						 	VALUES
						 	('$subSysNo','$paramType','$instanceNo','$sortNo','$valueType','$operType','$length',
								 '$initValue','$level_1','$level_2','$level_3','$level_4','$level_5',
									'$level_6','$level_7','$level_8','$level_9','$level_10')
						  	";
					if($DEBUG) echo "<br> $insertSql";
					mysqli_query($pubConn,$insertSql);
					echo mysqli_error($pubConn);
				}
			}else{//子系统参数
				for($sysNo=0;$sysNo<$maxSysNum;$sysNo++){ //每个系统编号
					$paramTable = omcGetObjTbl('param',$sysTypeNo,$sysNo);
					for($subSysNo=0; $subSysNo < $maxSubSysNum; $subSysNo++){//每个子系统
						$insertSql="INSERT INTO $paramTable
							(subSysNo,paramType,instanceNo,sortNo,valueType,operType,length,initValue,
							   level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,Level_10)
							 VALUES
							 ('$subSysNo','$paramType','$instanceNo','$sortNo','$valueType','$operType','$length',
								'$initValue','$level_1','$level_2','$level_3','$level_4','$level_5',
									'$level_6','$level_7','$level_8','$level_9','$level_10')
							  ";
						if($DEBUG) echo "<br> $insertSql";
						mysqli_query($pubConn,$insertSql);
						echo mysqli_error($pubConn);
					}
				}
			}//end of for($paramType)
		}//end of for($instanceNo..)
		if($confRowsNum > 20){
			print("&gt;");
		}else{
			print("&gt;&gt;");
		}
		flush();
		//if($DEBUG) exit();
	}//end of while
}
echo "<br>";
echo "<br><font size=3><b>Please wait a while for the operation to be finished...</b></font><br>";

if(isset($initAll))
{
	
	//get all sysTypeNos that need to initial
	$selSql = "select sysTypeNo,sysTypeName from OMC_PUB.sysConf where needParamConf='1' order by sysTypeNo";
	$Result = mysqli_query($pubConn,$selSql);
	while($Rows = mysqli_fetch_array($Result))
	{
		echo "<br><b>Init {$Rows[sysTypeName]} ...</b><br>";
		$sysTypeNo = $Rows[sysTypeNo];
		if($dropAndRecreate==1)
			initParam($sysTypeNo);
		else
			initParbyConf($sysTypeNo);
	}
	
	//init BssOmcDb OMCR_BssFunc
	$updateSql = "UPDATE BssOmcDb.OMCR_BSSTABLE SET ip='',name='',remark='',connected='0',alarmed='0',uploaded='0'";
	mysqli_query($pubConn,$updateSql);
}
else
{
	if($dropAndRecreate==1)
		initParam($sysTypeNo);
	else
		initParbyConf($sysTypeNo);
}
if($confirm==1){
	echo "\n<script language=\"javascript\">";
	echo "\nalert(\"$strParaInitializationComplete\");";
	echo "\nhistory.go(-1);";
	echo "\n</script>";
}
else
	echo "<br><br><br>$strParaInitializationComplete,<a href=./paramConf.php?sysTypeNo=$sysTypeNo>$strClickHereForReturn</a></font>";
?>