<?php
/*********************************************************      
 程序说明：         
    功能说明：统计用户资料
    调用关系：调用: header.inc    
              被调用:        
    变量说明：
    作者：LiLong   2004-04-14      
      
修改注释：      
NO.1      
姓名：Cui Ticun          
时间：2004-04-15
修改说明：修改了充值卡的统计功能
*********************************************************/
$DEBUG=0;
global $pubConn;

$sysTypeNo=$_REQUEST['sysTypeNo'];
$overview=$_REQUEST['overview'];
$searchInServer=$_REQUEST['searchInServer'];
$dataTypeNo=$_REQUEST['dataTypeNo'];
$keyValue=$_REQUEST['keyValue'];
$canSetNum=$_REQUEST['canSetNum'];
$isRead=$_REQUEST['isRead'];
$searchInServer=$_REQUEST['searchInServer'];

if ($_REQUEST['sysId'])
{
	$sysId =   $_REQUEST['sysId'];
	if($DEBUG) echo "<br>sysId=$sysId";
	$sysIdArr=explode('_',$sysId);
	$sysTypeNo=$sysIdArr[0];
	$sysNo=$sysIdArr[1];
	//	echo "sysTypeNo=$sysTypeNo,sysNo=$sysNo";
}


//overview statistic webpage
if(isset($overview)){
	if($DEBUG) echo "******************     Overview    **********************<br>";
	switch($sysTypeNo){
	case '330':  //HLR
		//select HLR subscriber count
		$selSql = "SELECT count(*) FROM $tableName";
		if($DEBUG) echo "<br>selSql=$selSql";
		$result=@mysqli_query($pubConn,$selSql);
		echo mysqli_error($pubConn);
		$record = @mysqli_fetch_array($result);
		$nHlrSubscriber = $record[0];
		if($DEBUG) echo "<br>nHlrSubscriber=$nHlrSubscriber";
		
		//select postpaid subscriber total
		//camel_flag: 
		//highest bit=1 => Enable Prepaid; highest bit=0 => Disable Postpaid
		$nPrepaidTotal = 0;
		$nPostTotal = 0;
		$nGprs = 0;
		$nNoGprs = 0;
		$nBoth = 0;
		$selSql = "SELECT camel_flag,nam FROM $tableName";
		if($DEBUG) echo "<br>selSql=$selSql";
		$result=@mysqli_query($pubConn,$selSql);
		echo mysqli_error($pubConn);
		$record = @mysqli_fetch_array($result);
		do{
			$flag = substr($record[camel_flag],0,1);
			//prepaid subscriber
			if('8'==$flag){
				$nPrepaidTotal++;
			}
			//postpaid subscriber
			elseif('0'==$flag){
				$nPostTotal++;
			}

			$nam = $record[nam] - 0;
			if (1 == $nam){
				$nNoGprs++;
			}elseif (2 == $nam){
				$nGprs++;
			}elseif (0 == $nam){
				$nBoth++;
			}
		}while($record=@mysqli_fetch_array($result));
		if($DEBUG) echo "<br>nPrepaidTotal=$nPrepaidTotal,nPostTotal=$nPostTotal";
		if($DEBUG) echo "<br>nNoGprs=$nNoGprs,nGprs=$nGprs,nBoth=$nBoth";
		echo "<b><font size=3><p align=center>HLR subscriber statistics</p></font></b>";
		echo "<TABLE  border=1 width=100% cellpadding=2 cellspacing=0 bordercolor=#666666 bordercolordark=#FFFFFF bgcolor=#FFFFFF>";
		echo "<TR>";
			echo "<TD width=40% colspan=2 bgcolor=#E6E6E6>HLR subscriber</TD>";
			echo "<TD colspan=3 align=center>$nHlrSubscriber</TD>";
		echo "</TR>";
		echo "<TR>";
			echo "<TD width=20% bgcolor=#E6E6E6>Postpay subscriber</TD>";
			echo "<TD width=20% bgcolor=#E6E6E6>Prepay subscriber</TD>";
			echo "<TD width=20% bgcolor=#E6E6E6>Only non-GPRS subscriber</TD>";
			echo "<TD width=20% bgcolor=#E6E6E6>Only GPRS subscriber</TD>";
			echo "<TD width=20% bgcolor=#E6E6E6>Both GPRS and non-GPRS subscriber</TD>";
		echo "</TR>";
		echo "<TR>";
			echo "<TD width=20% align=center>$nPostTotal</TD>";
			echo "<TD width=20% align=center>$nPrepaidTotal</TD>";
			echo "<TD width=20% align=center>$nNoGprs</TD>";
			echo "<TD width=20% align=center>$nGprs</TD>";
			echo "<TD width=20% align=center>$nBoth</TD>";
		echo "</TR>";
		echo "</TABLE>";
		break;
	case '360':  //PPS
	case '395':
		//total pps subscriber
		$selSql = "SELECT count(*) FROM $tableName";
		if($DEBUG) echo "<br>selSql=$selSql";
		$result=@mysqli_query($pubConn,$selSql);
		echo mysqli_error($pubConn);
		$record = @mysqli_fetch_array($result);
		$nPpsAmount = $record[0];
		if($DEBUG) echo "<br>nPpsAmount=$nPpsAmount";
		
		//statistics by all kinds of types
		$nFresh=0;
		$nUsed=0;
		$nSuspend=0;
		$nBlacklist=0;
		$nRelease=0;
		$nLowbalance=0;
		if(0==$dataTypeNo){
			$selSql = "SELECT status FROM $tableName";
			if($DEBUG) echo "<br>selSql=$selSql";
			$result=@mysqli_query($pubConn,$selSql);
			echo mysqli_error($pubConn);
			$record = @mysqli_fetch_array($result);
			do{
				switch($record[0]){
				case "00":
					$nFresh++;
					break;
				case "01":
					$nUsed++;
					break;
				case "02":
					$nSuspend++;
					break;
				case "03":
					$nBlacklist++;
					break;
				case "04":
					$nRelease++;
					break;
				case "05":
					$nLowbalance++;
					break;
				default:
					break;
				}
			}while( $record=@mysqli_fetch_array($result) );
			if($DEBUG) echo "<br>nFresh=$nFresh,nSuspend=$nSuspend,nBlacklist=$nBlacklist,nRelease=$nRelease,nLowbalance";

			//display
			echo "<b><font size=3><p align=center>PPS subscriber statistics</p></font></b>";
			echo "<TABLE  border=1 width=100% cellpadding=2 cellspacing=0 bordercolor=#666666 bordercolordark=#FFFFFF bgcolor=#FFFFFF>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Prepaid subscriber</TD>";
				echo "<TD align=center>$nPpsAmount</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Fresh</TD>";
				echo "<TD align=center>$nFresh</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Normal</TD>";
				echo "<TD align=center>$nUsed</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Suspend</TD>";
				echo "<TD align=center>$nSuspend</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Blacklist</TD>";
				echo "<TD align=center>$nBlacklist</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Release</TD>";
				echo "<TD align=center>$nRelease</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Low balance</TD>";
				echo "<TD align=center>$nLowbalance</TD>";
			echo "</TR>";
			echo "</TABLE>";
		}
		else{
			$selSql = "SELECT card_status FROM $tableName";
			if($DEBUG) echo "<br>selSql=$selSql";
			$result=@mysqli_query($pubConn,$selSql);
			echo mysqli_error($pubConn);
			$record = @mysqli_fetch_array($result);
			do{
				if(0==$record[0]){
					$nFresh++;
				}
				else{
					$nUsed++;
				}
			}while( $record=@mysqli_fetch_array($result) );
			if($DEBUG) echo "<br>nFresh=$nFresh,nUsed=$nUsed";

			//display
			echo "<b><font size=3><p align=center>PPS Recharge card statistics</p></font></b>";
			echo "<TABLE  border=1 width=100% cellpadding=2 cellspacing=0 bordercolor=#666666 bordercolordark=#FFFFFF bgcolor=#FFFFFF>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Recharge card</TD>";
				echo "<TD align=center>$nPpsAmount</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Fresh</TD>";
				echo "<TD align=center>$nFresh</TD>";
			echo "</TR>";
			echo "<TR>";
				echo "<TD width=50% bgcolor=#E6E6E6>Used</TD>";
				echo "<TD align=center>$nUsed</TD>";
			echo "</TR>";
			echo "</TABLE>";
		}
		break;
	default:
		break;
	}

	exit(0);
}

if ( isset($searchInServer) ) {
	if ($DEBUG) {
		echo "******************     Search in server    **********************<br>";
		echo "$targetPage?sysId=$sysId&dataTypeNo=$dataTypeNo&keyValue=$selKeyValue&canSetNum=$canSetNum&isRead=1<br>";
	}
?>
	<script language=javascript>
		var url = '<?php echo "$targetPage?sysId=$sysId&dataTypeNo=$dataTypeNo&keyValue=$selKeyValue&canSetNum=$canSetNum&isRead=1&searchInServer=1"; ?>';
		window.parent.main.location = url;
		//window.alert(url);
	</script>
<?php
}
?>
