<?php
function subsCommSend($sysId,$dataTypeNo,$indexValueList,$fieldNameList,$commType,$newIndexValue=0){
	//global $pubDb,$commTable;
	$DEBUG = 0;
	$pubDb = 'OMC_PUB';
	$commTable = 'subscriberComm';
	$ipTable = 'sysInfo';
	global $pubConn;

	$nReadComm		= 1;
	$nChangeComm	= 2;
	$nCreateComm	= 3;
	$nDeleteComm	= 4;
	$nLoadIndexComm	= 5;
	$nReplaceIndexComm=6;

	$sysIdArr = explode('_',$sysId);
	$sysTypeNo = $sysIdArr[0];
	$sysNo	   = $sysIdArr[1];
	$subSysNo  = $sysIdArr[2];
	
	//step 1: delete overtime and imcompetent command
	$overTime = date("Y-m-d H:i:s", mktime(date("H"),date("i")-1,date("s"),date("m"),date("d"),date("Y")));
	$delSql = "DELETE FROM $commTable WHERE (commState ='0' OR commState ='3') AND sendTime < '$overTime'";
	if($DEBUG) echo"<br>delete command sql = $delSql";
	mysqli_query($pubConn,$delSql);
	echo mysqli_error($pubConn);

	$overTime = date("Y-m-d H:i:s", mktime(date("H"),date("i")-30,date("s"),date("m"),date("d"),date("Y")));
	$delSql = "DELETE FROM $commTable WHERE sendTime < '$overTime'";
	if($DEBUG) echo"<br>delete command sql = $delSql";
	mysqli_query($pubConn,$delSql);
	echo mysqli_error($pubConn);
	
	/*
	//step 2: check the system's ip
	$chkIpSql = "SELECT ip FROM $ipTable
					WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo'
					";
	if($DEBUG) echo"<br>chkIpSql = $chkIpSql";
	$chkIpResult = mysqli_query($pubConn,$chkIpSql);
	echo mysqli_error($pubConn);
	$rowsNum = @mysqli_num_rows($chkIpResult);
	if($rowsNum > 0){
		echo "<script language=\"javascript\"> ";
		echo "alert('Can not find this system's ip address in system infp list,\n Please add it first!'); ";
		echo "</script>";
		return(-1);
	} 
	*/

	//step 3: check the command(����ͬʱ����ϵͳ���ͣ��������ͱ�ţ������������ͬ�Ĳ���)
	$chkSql = "SELECT commId FROM $commTable
					WHERE sysTypeNo='$sysTypeNo' AND indexValueList='$indexValueList'
					";
	if($DEBUG) echo"<br>check command sql = $chkSql";
	$chkResult = mysqli_query($pubConn,$chkSql);
	echo mysqli_error($pubConn);
	$rowsNum = @mysqli_num_rows($chkResult);
	if($rowsNum <= 0){
			
		//step 4:insert commands into command table
		if($commType == $nReplaceIndexComm){
			$sendSql="INSERT INTO $commTable 	
				(commType,dataTypeNo,sysTypeNo,sysNo,subSysNo,indexValueList,	fieldNameList,paramType,commState,sendTime,remark)
				VALUES
				('$commType','$dataTypeNo','$sysTypeNo','$sysNo','$subSysNo','$indexValueList',
				'$fieldNameList','0','1',CURRENT_TIMESTAMP,'$newIndexValue')
				";
		}else{
			$sendSql="INSERT INTO $commTable 	
				(commType,dataTypeNo,sysTypeNo,sysNo,subSysNo,indexValueList,fieldNameList,paramType,commState,sendTime)
				VALUES
				('$commType','$dataTypeNo','$sysTypeNo','$sysNo','$subSysNo','$indexValueList',
				'$fieldNameList','0','1',CURRENT_TIMESTAMP)
				";
			
		}
		if($DEBUG)echo"<br>send command sql = $sendSql";
		$sendResult=mysqli_query($pubConn,$sendSql);
		echo mysqli_error($pubConn);
	}
	
	//step 5:Return commIdStr
	$selSql = "SELECT commId FROM $commTable 
			WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo' 
				AND indexValueList='$indexValueList'
			";
	if($DEBUG) 
	echo"<br>get command id sql = $selSql";
	$selResult=mysqli_query($pubConn,$selSql);
	echo mysqli_error($pubConn);
	$commIdStr='';
	while($selRows = mysqli_fetch_array($selResult)){
		$commIdStr .= $selRows[commId].'-';
	}
	if($DEBUG)echo "<br>commIdStr = $commIdStr";
	if($DEBUG) exit();
	return($commIdStr);
}//end of function sendComm()
?>
