<?php
/*********************************************************      
 程序说明：         
    功能说明：话务统计，各个BTS的erlang列表和柱状图显示

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

     返回值：无

 作者：   
      
修改注释：      
NO.1      
姓名：            
时间：      
修改说明：      
*********************************************************/  
require("../../inc/header.inc");
$DEBUG=0;
$db=$bss_db;
$csta_table = 'OMCR_BtsMeasurements';
$bts_table = 'OMCR_Bts';
//$btsBgnNo=0;
//$btsEndNo=12;
if(!isset($bss_id)){
	$bss_id= 0;
}
$sqlstring = "select BsPlus,BsPlusMaxExtBtsMgr from OMCR_BssFunc where bssid=$bss_id";
	//echo $sqlstring . "<BR>";
	$result = mysqli_query($pubConn,$sqlstring) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
	$row = mysqli_fetch_row($result);
	$isbsplus = $row[0];

if($isbsplus == 0)
$sqlstring = "select ObjectId from OMCR_BSSTREE where ObjectId like 'Bts.%' and ObjectId not like 'Bts.0%' and Presence=1 and bssid=$bss_id";
else
$sqlstring = "select ObjectId from OMCR_BSSTREE where ObjectId like 'Bts.%' and Presence=1 and bssid=$bss_id";
//echo $sqlstring . "<BR>";
$result = mysqli_query($pubConn,$sqlstring) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
while($row = mysqli_fetch_row($result))
{
	list($cell_object[]) =$row;
}
$no_of_cell = count($cell_object);

$btsBgnNo=0;
$btsEndNo=$no_of_cell;

//----------------------get BssId <--> CellIdentity----------------------
//---init this bsc's bts array---
for($i=$btsBgnNo; $i <= $btsEndNo; $i++){
		//array(cellid,0,...,23);
	$btsArray[$i]=array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);//25个元素
}
/*
$sel_sql="SELECT ObjectId,CellIdentity FROM OMCR_Bts WHERE CellIdentity >='0' AND bssid='$bss_id'";
$sel_result=mysqli_query($pubConn,$sel_sql);
$result_rows=mysqli_fetch_array($sel_result);
if($DEBUG) echo "<br>db=$db<br>sel_sql=$sel_sql";
do{
	$temp_bts_id = $result_rows[ObjectId]; //like"Bts.1.0"
	$temp_bts_id = explode('.',$temp_bts_id);
	$temp_bts_id = $temp_bts_id[1];
	$temp_cell_id= $result_rows[CellIdentity];
	if($temp_cell_id > 0){
		$btsArray[$temp_bts_id][0]= $temp_cell_id;
	}
}while($result_rows=mysqli_fetch_array($sel_result));
*/
//-----------get where---------------

$where=" bssid='$bss_id' ";
//data
if(isset($sel_date)){
	$bgn_date=$sel_date.' '.$hou_1.':'.$mid_1.':'.$sec_1;
	$end_date=$sel_date.' '.$hou_2.':'.$mid_2.':'.$sec_2;
}else{
	$bgn_date =date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d"),date("Y"))); 
	$end_date =date("Y-m-d H:i:s",mktime()); 
}
$where = $where." AND startTime >=UNIX_TIMESTAMP('$bgn_date') AND EndTime <=UNIX_TIMESTAMP('$end_date')";

?>

<body leftmargin=12 topmargin=0>    
<?php
//------------------------------显示标题，日期--------------------------------------
?>
<TABLE  border="0" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">   
	<TR>
		<TD>
		<?php 
		$bscNum=(strlen($bss_id)<2)?'0'.$bss_id:$bss_id;
		echo "<font color=\"#0000ff\" size=\"2\"><b>BSS"."$bscNum $strErlang $strStatistics </b></font>";
		?>
		</TD>
		<TD align="right">
		<?php echo "$strFrom<font color=\"#0000ff\"> $bgn_date</font> &nbsp;&nbsp;$strTo&nbsp;&nbsp; <font color=\"#0000ff\">$end_date</font>";?>
		</TD>
	</TR>
</TABLE>
<?php
//------------------------------显示列表--------------------------------------
?>
<table border="1" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF" > 
<?php	
echo "<TR>";
echo "<TD>BTS ID</TD>";
echo "<TD>$strCellID</TD>";
for($clock=0;$clock<24;$clock++){
	echo "<TD>$clock</TD>";
}
echo "</TR>";
//$bss_id = (strlen($bss_id)<2)?'0'.$bss_id:$bss_id;
if($DEBUG) echo "<br>btsBgnNo=$btsBgnNo,btsEndNo=$btsEndNo ";
for($i=$btsBgnNo; $i < $btsEndNo; $i++){
	//$bts_num=($i<10)?'0'.$i:$i;
	$cellcont = explode(".",$cell_object[$i]);
	
	$sel_sql="SELECT CellIdentity FROM OMCR_Bts WHERE CellIdentity >=0 AND ObjectId like '$cell_object[$i]%' AND bssid=$bss_id";
		
		$sel_result=mysqli_query($pubConn,$sel_sql);
		//echo "<br>sel_sql=$sel_sql<br>";
		echo mysqli_error($pubConn);
		$result_row=@mysqli_fetch_row($sel_result);
		$temp_cell_id = $result_row[0];
		
		if($temp_cell_id > 0){
		$btsArray[$i][0]= $temp_cell_id;
		//echo "<br>btsArray[$temp_bts_id][0]= $temp_cell_id";
	    }
		$sum_sql="SELECT 
		CellIdentity as CellIdentity,
		hour(from_unixtime(StartTime)) as hours,
		(meanTCHBusyTime * nbrOfAvailableTCHs)/(100 * (EndTime - StartTime)) as erlang
		FROM $csta_table 
		WHERE $where AND CellIdentity = '$temp_cell_id' 
		Group By hours,CellIdentity
		";
$sel_result=mysqli_query($pubConn,$sum_sql);
if($DEBUG) echo "<br>sel_sql=$sum_sql<br> ";
echo mysqli_error($pubConn);
while($rows=mysqli_fetch_array($sel_result)){
		    $btsArray[$i][0] == $rows[CellIdentity];
			$hourse=$rows[hours]+1;
			$btsArray[$i][$hourse] += $rows[erlang];
			if($btsArray[$i][$hourse] > $maxNum)
				$maxNum = $btsArray[$i][$hourse];
}


		if($btsArray[$i][0] > 0){
		echo "<TR>";
		echo "<TD>Bts.$cellcont[1].$cellcont[2]</TD>";
		echo "<TD>{$btsArray[$i][0]}</TD>";
			for($clock=1;$clock<25;$clock++){
			echo "<TD>{$btsArray[$i][$clock]}</TD>";
			}
		echo "</TR>";
		}
		else
		{
		echo "<TR>";
		echo "<TD>Bts.$cellcont[1].$cellcont[2]</TD>";
		echo "<TD><nobr>--</nobr></TD>";
		for($clock=1;$clock<25;$clock++){
			echo "<TD><nobr>--</nobr></TD>";
		}
		echo "</TR>";
		}
}
$y_max = ceil($maxNum);
if($y_max < 1){
	$y_max = 1;
}

?>
</table>
<br><br>
<?php
if(1)
{
$colorArr = array("00FFFF","000000","FF00FF","008000","00FF00","800000",
				  "000080","808000","800080","FF0000","008080","00FF00");
for($i=$btsBgnNo;$i< $btsEndNo;$i++)
{
	//$formatNum = $i < 10 ? '0'.$i : $i;
	$btsName = $cell_object[$i];//"BTS.".$$cell_object[1].$cell_object[2];
	$color = $colorArr[$i];
	$legendArr[$i] = array($btsName, $color, 0);
}
$title = ucfirst("Service erlang statistic graphic");

$maxYNum=$y_max;
//-----图片背景和坐标，标示等基本元素，请勿修改这部分
$sizex=800;
$sizey=320;
$bgcolor=F8F8F8;
$fgcolor=000000;
$dayStr=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$i,date("Y")));

$im = ImageCreate ($sizex, $sizey);

$background_color = ImageColorAllocate ($im, 
			hexdec(substr($bgcolor, 0, 2)), 
			hexdec(substr($bgcolor, 2, 2)),
			hexdec(substr($bgcolor, 4, 2)));

$text_color = ImageColorAllocate ($im, 
			hexdec(substr($fgcolor, 0, 2)), 
			hexdec(substr($fgcolor, 2, 2)),
			hexdec(substr($fgcolor, 4, 2)));

//背景
ImageFilledRectangle($im,0,0,$sizex,$sizey,ImageColorAllocate ($im, 198, 198, 198));
ImageFilledRectangle($im,2,2,$sizex,$sizey,ImageColorAllocate ($im, 130, 130, 130));
$tmpPointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
Imagefilledpolygon($im, $tmpPointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
$tmpPointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
Imagefilledpolygon($im, $tmpPointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
ImageFilledRectangle($im,2,2,$sizex-3,$sizey-3,$background_color);

//图表名称
Imagestring($im, 2, 300,5, $title,  $text_color);

//边框
$xBgn=60;
$yBgn=25;
$xEnd=780;
$yEnd=275;
$unitXPix=30;
ImageLine($im, $xBgn, $yBgn, $xBgn, $yEnd, $text_color);
ImageLine($im, $xBgn, $yBgn, $xEnd, $yBgn, $text_color);
ImageLine($im, $xBgn, $yEnd, $xEnd, $yEnd, $text_color);
ImageLine($im, $xEnd, $yBgn, $xEnd, $yEnd, $text_color);

//竖网格及标注
for($i=0;$i<24;$i++){
	ImageDashedLine($im, $xBgn + $i*30, $yBgn, $xBgn + $i*30, $yEnd, $text_color);
	Imagestring($im, 2, $xBgn + $i*30 - 4, $yEnd, "$i",  $text_color);
}

//横网格及标注
for($i=0;$i<10;$i++){
	ImageDashedLine($im, $xBgn, $yBgn + $i*25, $xEnd, $yBgn + $i*25, $text_color);
	$scale=$maxYNum / 10 * (10 - $i);
	if($scale>1000*1000){
		$displayMaxData = round($scale/100000)/10;
		$displayMaxData = $displayMaxData."M";	
	}elseif($scale>1000){
		$displayMaxData = round($scale/100)/10;
		$displayMaxData = $displayMaxData."K";	
	}else
		$displayMaxData=$scale;
	Imagestring($im, 2, $xBgn - 50, $yBgn + $i*25 - 5, "$displayMaxData",  $text_color);	
}

//色块图标及说明
$order = 0;

for($i=$btsBgnNo; $i < $btsEndNo; $i++)
{
	
	if($btsArray[$i][0] > 0)
	{
		$tmpName=$legendArr[$i][0];
		$tmpColor=$legendArr[$i][1];
		
		$color = ImageColorAllocate ($im, hexdec(substr($tmpColor, 0, 2)), hexdec(substr($tmpColor, 2, 2)),hexdec(substr($tmpColor, 4, 2)));
		$legendArr[$i][2]=$color;
		ImageFilledRectangle($im,60 + $order* 60,$yEnd + 22,58 + $order* 60 + 10,$yEnd + 30,$legendArr[$i][2]);
		Imagestring($im, 2, 73 + $order* 60,$yEnd + 20, "$tmpName",  $text_color);
		$order++;
	}
}
function getNewXY(&$pointArr) 
{
	global	$xBgn,$yBgn,$xEnd,$yEnd,$maxYNum,$unitXPix;
	$DEBUG=0;
	for($i=0;$i<sizeof($pointArr);$i=$i+2){
		$pointArr[$i]	= $xBgn + $pointArr[$i] * $unitXPix;
		if($DEBUG) echo "<br>pointArr[$i]={$pointArr[$i]}";
		$pointArr[$i+1]	= $yEnd - round(($yEnd - $yBgn) * ($pointArr[$i+1] / $maxYNum));
		if($DEBUG) echo "<br>pointArr[$i+1]={$pointArr[$i+1]}";
	}
	$pointArr[$i]	= $pointArr[$i-2];
	if($DEBUG) echo "<br>pointArr[$i]={$pointArr[$i]}";
	$pointArr[$i+1]	= $yEnd-1;
	if($DEBUG) echo "<br>pointArr[$i+1]={$pointArr[$i+1]}";
}
//get x,y points.
for($i=$btsBgnNo; $i <= $btsEndNo; $i++)
{
	if($btsArray[$i][0] > 0)
	{
		for($j=1;$j<26;$j++)
		{
			$pointArr[$i][$j*2] = $j-1;
			$pointArr[$i][$j*2+1]	= $btsArray[$i][$j];
		}
		getNewXY($pointArr[$i]);
		$pointNum[$i]= sizeof($pointArr[$i]) / 2;
		$tmpColor[$i]=$legendArr[$i][2];
	}
}

for($i=$btsBgnNo; $i <= $btsEndNo; $i++)
{
	if($btsArray[$i][0] > 0)
	{
		for($j=1;$j<26;$j++)
		{
			//画线条
			imageline($im,$pointArr[$i][$j*2],$pointArr[$i][$j*2+1],$pointArr[$i][$j*2+2],$pointArr[$i][$j*2+3],$tmpColor[$i]);
		}
	}
}

//-----输出图形文件，请勿修改
//delete *.png
$handle=opendir('.');
while($file=readdir($handle)){
	$retval="";
	$pt=strrpos($file,".");
	if($pt) $retval=substr($file,$pt+1,strlen($file)-$pt);
	if($retval=='png') unlink($file);
}
$filename=time().".png";
if(!$DEBUG){
	ImagePng ($im,"$filename");  
	ImageDestroy($im);	
}

//-----显示图片
echo "<body topmargin=\"0\" leftmargin=\"5\">";  
echo "<img src=\"$filename\">";
echo "</body>";
exit;
}//end if.
//刻度线图片
$dot_img="<img border=\"0\" src=\"../../images/Icon-0.gif\" width=\"2\" height=\"1\">";

//求图像高度的基数 get the Division by  number
$base_field=10; //$btsArray[bts_num][j]'s j
for($i=$btsBgnNo; $i <= $btsEndNo; $i++){
	$max_number=($btsArray[$i][$base_field] > $max_number)?$btsArray[$i][$base_field]:$max_number;
}
$top_number =(round($max_number/100)+1)*100; //算出最大刻度，该值是个整数
$base_number=round($top_number/100); 
$base_number=($base_number <= 0)?1:$base_number;

//需要显示成柱状图的$btsArray[bts_num][j]元素编号j
$show_field=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
//显示图例，需要和$show_field保持一致
//$legend_def=array(''); //erlang只用一种颜色显示

//显示图例色彩，需要和$show_field保持一致
//$legend_color=array('blue',.....,'blue');//erlang只用一种颜色显示
//------------------------------显示图例--------------------------------------
?>
<!--
<table border="0">
  <tr>
    <td width="60"><b><?php echo "$strLegend"; ?>:</b></td>
<?php
//for($i=0;$i<sizeof($legend_def);$i++){
//	echo "<td width=\"10\">";
//	showImg($i,10,10);
//	echo "</td>";
//	echo "<td width=\"120\">{$legend_def[$i]}</td>";
//}
?>
	<td></td>
  </tr>
</table>
-->
<br>
<script language="JavaScript1.2" type="text/javascript">
<!--
function chgBaseNum(){
	var old_base_number=1.00 * oldBaseNumSet.value;
	var max_first_num = 12;
	var max_second_num = <?php echo sizeof($show_field);?>;
	var new_base_number=1.00 * baseNumSet.value;
	var i=0,j=0,maxHeight=200,maxNum=0;
	if(new_base_number <=0){
		baseNumSet.value=old_base_number;
		return(-1);
	}
	for(i=1;i <= max_first_num;i++){
		for(j=0;j < max_second_num;j++){
			temp_obj_name='img'+i+'_'+j;
			temp_input_name='img'+i+'_'+j+'_org';
			temp_obj=eval(temp_obj_name);
			temp_height=eval(temp_input_name);
			if(temp_obj.height > 0 && new_base_number > 0){
				temp_obj.height = 1.0 * temp_height.value * (old_base_number / new_base_number )+1;
				if(temp_obj.height > maxHeight && temp_obj.alt > maxNum){
					//alert(temp_obj.height+'-'+temp_obj.alt);
					maxNum = temp_obj.alt;
				}
			}
		}
	}
	for(i=1;i<=12;i++){
		tmpobj=eval('topNumber_'+i);
		org_tmpobj=eval('org_topNumber_'+i);
		if(maxNum > 0){
			tmpobj.value = maxNum;
			baseNumSet.value = maxNum;
		}else{
			tmpobj.value = Math.round(org_tmpobj.value * (new_base_number/ old_base_number));
		}
	}
}
-->
</script>
<?php
echo "Erlang (hour):<input type=\"text\" size=\"4\" name=\"baseNumSet\" id=\"baseNumSet\"   value=\"$top_number\" class=\"text\">";
echo "&nbsp;<input  type=\"button\" value=\"$strAdjustHeight\"  name=\"chge\" onclick=\"chgBaseNum()\" class=\"button\"> ";
echo "<input type=\"hidden\" size=\"4\" name=\"oldBaseNumSet\" id=\"oldBaseNumSet\"   value=\"$top_number\">";
echo "<br>";
//-----------------------------显示柱状图-------------------------------------
$bts_num=$btsBgnNo;

for($i=$btsBgnNo; $i <= $btsEndNo; $i++){
	if($DEBUG) echo "<br>btsBgnNo=$btsBgnNo,btsArray[$bts_num][$arrNum] ={$btsArray[$bts_num][$arrNum]}";
	?>
	<br>
	<table border="0" cellPadding="0" cellSpacing="0" height="220"  style="BORDER-BOTTOM: rgb(0,0,0) 1px solid; BORDER-LEFT: rgb(0,0,0) 1px solid; BORDER-RIGHT: medium none; BORDER-TOP: medium none" >
	<tr>
		<td colspan="100" height="20">
		<b>
		<?php 
		$num = $i;
		$num =(strlen($num)<2)?'0'.$num:$num;
		$BTS_ID_1='BTS'."$bss_id"."_$num";
		
		echo"$dot_img&nbsp;";
		echo "<INPUT type=\"text\" size=\"10\" name=\"topNumber_".$i."\" id=\"topNumber\"  value=\"$top_number\" style=\"border-left:medium none;border-right:medium none;border-top:medium none;border-bottom:medium none; solid rgb(0,0,0)\"   readonly>";
		echo "<INPUT type=\"hidden\" size=\"10\" name=\"org_topNumber_".$i."\" id=\"topNumber\"  value=\"$top_number\" >";
		echo "<b>$strErlang $strStatistics</b>";
		?>
		</b>
		</td>
	</tr>
	<tr>
	<td valign="bottom" height="40" width="20"><?php echo"$dot_img";?></td>
	<?php
	//显示每个bts的柱状图
	for($k=0;$k<sizeof($show_field);$k++){
		$arrNum = $show_field[$k];
		$alt = $k.':00 = '.$btsArray[$bts_num][$arrNum].' Erlang';
		$height = $btsArray[$bts_num][$arrNum] / $base_number * 2;
		$width = 19;
		$imgName = 'img'.$j.'_'.$k;
		echo "<td valign=\"bottom\" height=\"200\" width=\"20\" rowspan=\"5\">";
		showImg('blue',$width,$height,$imgName,$alt);
		echo "</td>";
	}
	echo "<td valign=\"bottom\" height=\"200\" width=\"40\" rowspan=\"5\"></td>";
	$bts_num++;
	?>
	<td valign="bottom" height="200" width="100" rowspan="5"></td>
	</tr>
	<tr><td valign="bottom" height="40" width="20"><?php echo"$dot_img";?></td></tr>
	<tr><td valign="bottom" height="40" width="20"><?php echo"$dot_img";?></td></tr>
	<tr><td valign="bottom" height="40" width="20"><?php echo"$dot_img";?></td></tr>
	<tr><td valign="bottom" height="40" width="20"></td></tr>
	</table>
	<table border="0">
	<tr>
	<td valign="bottom" width="20">&nbsp;</td>
	<?php
		$tdWidth=20*sizeof($show_field)+40;
		echo "<td  width=\"$tdWidth\">$BTS_ID_1</td>";
	?>
	</tr>
	</table>
	<?php
}
?>
</body>
</html>
