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

    调用关系:调用: connectdb.inc,lib.inc     
              被调用:type_set_down.php     
 
    变量说明:由type_set_down.php 传来$sys_type
              $device_table = OMC_device_Def;

    返回值:无

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

$DEBUG = 0;
$sysDb='OMC_PUB';


/*
if($sureDoIt != 'Yes'){
	if($sureDoIt == 'No'){
		
	}
	echo "";
	echo "<form action=\"sql.php\" method=\"post\">";
	echo "Are you sure to init $sysTypeName 's alarm?";
	echo "<input type="hidden" name="sysTypeName" value="$sysTypeName">";
    echo "<input type="hidden" name="maxSysNum" value="$maxSysNum">";
	echo "<input type="hidden" name="maxSubSysNum" value="$maxSubSysNum">";
 
	echo "<input type="Submit" name="$sureDoIt" value="Yes">";
    echo "<input type="Submit" name="$sureDoIt" value="No">";
    echo "</form>";
	exit();
}
*/


echo "<br><font size=3><b>It's need some minutes,Please wait...</b><br>";
if($initAll > 0){
	$clearSql = "DELETE FROM sysAlarmLog ";
}else{
	$clearSql = "DELETE FROM sysAlarmLog WHERE sysTypeNo = '$sysTypeNo'";
}
if($DEBUG) echo "<br>clearSql=$clearSql";
mysqli_query($pubConn,$clearSql);
echo mysqli_error($pubConn);
if($initAll > 0){
	$getAlarmConfSql="SELECT sysTypeNo,compCode,alarmCode 
					FROM sysAlarmConf 
					WHERE alarmCode > '0'
				";	
}else{
	$getAlarmConfSql="SELECT compCode,alarmCode 
					FROM sysAlarmConf 
					WHERE sysTypeNo= '$sysTypeNo' AND alarmCode > '0'
				";
	$getSysConfSql = "SELECT maxSysNum,maxSubSysNum,sysTypeName FROM sysConf WHERE sysTypeNo= '$sysTypeNo'";
	if($DEBUG) echo "<br>getSysConfSql=$getSysConfSql";
	$sysConfResult=mysqli_query($pubConn,$getSysConfSql);
	echo mysqli_error($pubConn);
	$sysConfRows = mysqli_fetch_array($sysConfResult);
	$maxSysNum	 = $sysConfRows[maxSysNum];
	$maxSubSysNum= $sysConfRows[maxSubSysNum];
	$sysTypeName = $sysConfRows[sysTypeName];
	if($DEBUG) echo "<br>sysTypeNo=$sysTypeNo";
	if($DEBUG) echo "<br>maxSysNum=$maxSysNum";
	if($DEBUG) echo "<br>maxSubSysNum=$maxSubSysNum";
	if($DEBUG) echo "<br>sysTypeName=$sysTypeName";
}

if($DEBUG) echo "<br>getAlarmConfSql=$getAlarmConfSql";
$alarmConfResult=mysqli_query($pubConn,$getAlarmConfSql);
echo mysqli_error($pubConn);
$k=0;
while($alarmConfRows = mysqli_fetch_array($alarmConfResult)){
	if(($initAll > 0) && ($sysTypeNo != $alarmConfRows[sysTypeNo])){
		$sysTypeNo = $alarmConfRows[sysTypeNo];
		$getSysConfSql = "SELECT maxSysNum,maxSubSysNum FROM sysConf WHERE sysTypeNo= '$sysTypeNo'";
		if($DEBUG) echo "<br>getSysConfSql=$getSysConfSql";
		$sysConfResult=mysqli_query($pubConn,$getSysConfSql);
		echo mysqli_error($pubConn);
		$sysConfRows = mysqli_fetch_array($sysConfResult);
		$maxSysNum	 = $sysConfRows[maxSysNum];
		$maxSubSysNum= $sysConfRows[maxSubSysNum];
		if($DEBUG) echo "<br>sysTypeNo=$sysTypeNo";
		if($DEBUG) echo "<br>maxSysNum=$maxSysNum";
		if($DEBUG) echo "<br>maxSubSysNum=$maxSubSysNum";
		if($DEBUG) echo "<br>sysTypeName=$sysTypeName";
	}
	$compCode = $alarmConfRows[compCode];
	$alarmCode = $alarmConfRows[alarmCode];
	for($i=0;$i<$maxSysNum;$i++){
		for($j=0;$j<$maxSubSysNum;$j++){
			$insertSql = "REPLACE sysAlarmLog 
				(sysTypeNo,sysNo,subSysNo,compCode,alarmCode,alarmCount,alarmTime,clearTime) 
				VALUES
				('$sysTypeNo','$i','$j','$compCode','$alarmCode','0','0','0')
				";
			if($DEBUG) echo "<br>insertSql=$insertSql";
			mysqli_query($pubConn,$insertSql);
			echo mysqli_error($pubConn);
		}
	}
	if($initAll > 0){
		if(($k % 10) == 0){
			print(">>");
			flush();
		}
	}else{
		print(">>");
		flush();
	}
	$k++;
}
if($initAll > 0){
	echo "<br>All alarm initialized, number of alarm is $k!!</font>";
}else{
	echo "<br>$sysTypeName 's alarm initialized, number of alarm is $k!!</font>";
}

?>
