<?php 
/*********************************************************      
程序说明：         
    功能说明：线性图统计
    
    调用关系：调用:"../../inc/header.inc"         
            被调用:       
 作者：Cui Ticun
 Date: 12/28/2005
      
修改注释：      
NO.1
姓名：
时间：
修改说明：
Time: 
Description: 
*********************************************************/
$DEBUG = 0;

if(!isset($selTopVal)) $selTopVal=0;

$legendArr=array(
			array("MS TO SMSC",		'0000FF',0),
			array("VMS TO SMSC",	'00FF00',0),
			array("PPS TO SMSC",	'FF0000',0),
			array("SMPP TO SMSC",	'7D0000',0),
			array("SMSC TO MS",		'007D00',0),
			array("SMSC TO SMPP",	'FF00FF',0)
			);
?>

<script language="javascript">
function selTop(){
	var url,sel_val;
	sel_val=document.selectform.selTopVal.value;
	date = "&sel_date="+"<?php echo $sel_date; ?>";
	url="./dataAnalysisDown.php?selTopVal="+sel_val+date;
	window.parent.main.location.href=url;
}
</script>

<form method="POST" action="$PHP_SELF" name="selectform">
<table border="0" width="100%">
<tr>      
<td>
<select style="font-family: Verdana; font-size: 8pt" name="selTopVal" size=1 onChange="javascript:selTop()">
	<?php     
	for ($i=0; $i<6; $i++){
		if ($i == $selTopVal)
			echo "<OPTION value=\"$i\" selected>{$legendArr[$i][0]}</OPTION>";
		else
			echo "<OPTION value=\"$i\">{$legendArr[$i][0]}</OPTION>";
	}
	?>
</SELECT>
on top
</td>
</tr>
</table>   
<!-- </form> -->   

<?php
$db = $csta_db;
$showDate = date("d",mktime(0,0,0,date("m"),date("d")-$sel_date,date("Y")));
$showDate = omc_keep_length($showDate,0,2);
$table_name	= "smscDetail_".$showDate;
if($DEBUG)	echo "db=$db, table_name=$table_name<br>";
$time_column = "csta_datetime";								//时间列名

$showDate1 = date("d",mktime(0,0,0,date("m"),date("d")-$sel_date+1,date("Y")));
$showDate1 = omc_keep_length($showDate1,0,2);
$table_name1 = "smscDetail_".$showDate1;
if($DEBUG)	echo "table_name1 = $table_name1<br>";
$bgn_time1=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date+1,date("Y")));
$end_time1=date("Y-m-d H:i:s",mktime(1,0,0,date("m"),date("d")-$sel_date+1,date("Y")));

$sel_field_list= @mysql_list_fields($db,$table_name);
$sel_field_list1= @mysql_list_fields($db,$table_name1);
if($DEBUG)	echo "sel_field_list = $sel_field_list, sel_field_list1 = $sel_field_list1<br>";

if((!$sel_field_list)&&(!$sel_field_list1)){
	echo " ";
	exit();
}

//$date = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-$sel_date,date("Y")));
//$time="Time:".$date;//显示的时间

//where语句的时间限制组成部分$w1
//$w1 = " $time_column > '$bgn_time'  AND  $time_column <= '$end_time' ";

//统计每小时
//$group = " GROUP BY key_type, csta_datetime ";
//$order = " ORDER BY key_type ";

//合成查询语句$where
//$where=" WHERE".$w1.$group.$order;
//echo "$where<br>";

if($sel_field_list){
	for ($i=1; $i<=24; $i++){
		$select = " SELECT 
			sum(MS_to_SMSC_success) as MS_to_SMSC_success,
			sum(VMS_to_SMSC_success) as VMS_to_SMSC_success,
			sum(PPS_to_SMSC_success) as PPS_to_SMSC_success,
			sum(SMPP_to_SMSC_success) as SMPP_to_SMSC_success, 
			sum(SMSC_to_MS_success) as SMSC_to_MS_success, 
			sum(SMSC_to_SMPP_success) as SMSC_to_SMPP_success
			FROM $table_name ";
		
		if($sel_date==-1){
			$bgn_time = date("Y-m-d H:i:s", mktime($i-1,0,0,date("m"),date("d")-6,date("Y")));
			$end_time=date("Y-m-d H:i:s", mktime());
		}else{
			$bgn_time=date("Y-m-d H:i:s",mktime($i-1,0,0,date("m"),date("d")-$sel_date,date("Y")));
			$end_time=date("Y-m-d H:i:s",mktime($i,0,0,date("m"),date("d")-$sel_date,date("Y")));
		}
		
		$where = " WHERE $time_column>'$bgn_time' AND $time_column<='$end_time' ";

		$select_sql=$select.$where;//查询语句
		if($DEBUG)	echo "<br>select_sql=$select_sql";
		$select_result=@mysqli_query($pubConn,$select_sql);
		$select_rows=@mysqli_fetch_array($select_result);

		if($DEBUG)	echo "timeslot = $i<br>";
		$smscStat[0][$i] = $select_rows[MS_to_SMSC_success];
		$tValue = $smscStat[0][$i];
		if($DEBUG)	echo "MS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[1][$i] = $select_rows[VMS_to_SMSC_success];
		$tValue = $smscStat[1][$i];
		if($DEBUG)	echo "VMS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[2][$i] = $select_rows[PPS_to_SMSC_success];
		$tValue = $smscStat[2][$i];
		if($DEBUG)	echo "PPS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[3][$i] = $select_rows[SMPP_to_SMSC_success];
		$tValue = $smscStat[3][$i];
		if($DEBUG)	echo "SMPP to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[4][$i] = $select_rows[SMSC_to_MS_success];
		$tValue = $smscStat[4][$i];
		if($DEBUG)	echo "SMSC to MS=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[5][$i] = $select_rows[SMSC_to_SMPP_success];
		$tValue = $smscStat[5][$i];
		if($DEBUG)	echo "SMSC to SMPP=$tValue<br><br>";
		if($tValue > $maxNum)	$maxNum = $tValue;
	}
}

//当天最后一个点的坐标.24:00=NEXT 0:00
//先取23:00~23:45的统计，在smscDetail_(thatDay)表中，
//再取23:45~24:00的统计，在smscDetail_(thatDay+1)表中。
//两者相加
if($sel_field_list1){
	$select = " SELECT 
		sum(MS_to_SMSC_success) as MS_to_SMSC_success,
		sum(VMS_to_SMSC_success) as VMS_to_SMSC_success,
		sum(PPS_to_SMSC_success) as PPS_to_SMSC_success,
		sum(SMPP_to_SMSC_success) as SMPP_to_SMSC_success, 
		sum(SMSC_to_MS_success) as SMSC_to_MS_success, 
		sum(SMSC_to_SMPP_success) as SMSC_to_SMPP_success
		FROM $table_name1 ";

	if($sel_date==-1){
		$bgn_time = date("Y-m-d H:i:s", mktime($i-1,0,0,date("m"),date("d")-6,date("Y")));
		$end_time=date("Y-m-d H:i:s", mktime());
	}else{
		$bgn_time=date("Y-m-d H:i:s",mktime(23,0,0,date("m"),date("d")-$sel_date,date("Y")));
		$end_time=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date+1,date("Y")));
	}

	$where = " WHERE $time_column>'$bgn_time' AND $time_column<='$end_time' ";

	$select_sql=$select.$where;//查询语句
	if($DEBUG)	echo "<br>select_sql=$select_sql";
	$select_result=@mysqli_query($pubConn,$select_sql);
	$select_rows=@mysqli_fetch_array($select_result);
	do{
		$number=explode(" ",$select_rows[csta_datetime]);
		$number_1=explode(":",$number[1]);
		$datetime=$number_1[0];
		if($datetime<10){
			$datetime=substr($datetime,1);
		}
		if(0 == $datetime)	$datetime = 24;
		if($DEBUG)	echo "<br>datetime=$datetime<br>";

		$smscStat[0][$datetime] += $select_rows[MS_to_SMSC_success];
		$tValue = $smscStat[0][$datetime];
		if($DEBUG)	echo "MS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[1][$datetime] += $select_rows[VMS_to_SMSC_success];
		$tValue = $smscStat[1][$datetime];
		if($DEBUG)	echo "VMS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[2][$datetime] += $select_rows[PPS_to_SMSC_success];
		$tValue = $smscStat[2][$datetime];
		if($DEBUG)	echo "PPS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[3][$datetime] += $select_rows[SMPP_to_SMSC_success];
		$tValue = $smscStat[3][$datetime];
		if($DEBUG)	echo "SMPP to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[4][$datetime] += $select_rows[SMSC_to_MS_success];
		$tValue = $smscStat[4][$datetime];
		if($DEBUG)	echo "SMSC to MS=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[5][$datetime] += $select_rows[SMSC_to_SMPP_success];
		$tValue = $smscStat[5][$datetime];
		if($DEBUG)	echo "SMSC to SMPP=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;
	}while($select_rows=@mysqli_fetch_array($select_result));
}

//第二天第一个点的坐标.
if($sel_field_list1){
	$select = " SELECT 
		sum(MS_to_SMSC_success) as MS_to_SMSC_success,
		sum(VMS_to_SMSC_success) as VMS_to_SMSC_success,
		sum(PPS_to_SMSC_success) as PPS_to_SMSC_success,
		sum(SMPP_to_SMSC_success) as SMPP_to_SMSC_success, 
		sum(SMSC_to_MS_success) as SMSC_to_MS_success, 
		sum(SMSC_to_SMPP_success) as SMSC_to_SMPP_success
		FROM $table_name1 ";

	$where = " WHERE $time_column>'$bgn_time1' AND $time_column<='$end_time1' ";

	$select_sql=$select.$where;//查询语句
	if($DEBUG)	echo "<br>select_sql=$select_sql";
	$select_result=@mysqli_query($pubConn,$select_sql);
	$select_rows=@mysqli_fetch_array($select_result);
	do{
		$datetime = 25;
		if($DEBUG)	echo "<br>datetime=$datetime<br>";

		$smscStat[0][$datetime] = $select_rows[MS_to_SMSC_success];
		$tValue = $smscStat[0][$datetime];
		if($DEBUG)	echo "MS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[1][$datetime] = $select_rows[VMS_to_SMSC_success];
		$tValue = $smscStat[1][$datetime];
		if($DEBUG)	echo "VMS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[2][$datetime] = $select_rows[PPS_to_SMSC_success];
		$tValue = $smscStat[2][$datetime];
		if($DEBUG)	echo "PPS to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[3][$datetime] = $select_rows[SMPP_to_SMSC_success];
		$tValue = $smscStat[3][$datetime];
		if($DEBUG)	echo "SMPP to SMSC=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[4][$datetime] = $select_rows[SMSC_to_MS_success];
		$tValue = $smscStat[4][$datetime];
		if($DEBUG)	echo "SMSC to MS=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;

		$smscStat[5][$datetime] = $select_rows[SMSC_to_SMPP_success];
		$tValue = $smscStat[5][$datetime];
		if($DEBUG)	echo "SMSC to SMPP=$tValue<br>";
		if($tValue > $maxNum)	$maxNum = $tValue;
	}while($select_rows=@mysqli_fetch_array($select_result));
}

$y_max = ceil($maxNum/10)*10;
if($y_max < 10)		$y_max = 10;

//insert log
insertLog('BHCA service graphics',2,5);
?>

<br>
<!--*************** Draw service BHCA statistic graphic ********************-->

<?php
$maxYNum=$y_max;
if($DEBUG) echo "maxYNum=$maxYNum<br>";
$title = ucfirst("smsc successful statistic graphic");
if($DEBUG)	echo "title=$title<br>";

//-----图片背景和坐标，标示等基本元素，请勿修改这部分
$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));
$pointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
$pointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
Imagefilledpolygon($im, $pointArr, 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);	
}

//色块图标及说明
for($i=0;$i<sizeof($legendArr);$i++){
	$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,82 + $i* 100,$yEnd + 22,80 + $i* 100 + 10,$yEnd + 30,$legendArr[$i][2]);
	Imagestring($im, 2, 95 + $i* 100,$yEnd + 20, "$tmpName",  $text_color);
}

//坐标转换函数，将数值转换成坐标
function getNewXY($pointArr) {
	global	$xBgn,$yBgn,$xEnd,$yEnd,$maxYNum,$unitXPix;
	$DEBUG=0;
	if($DEBUG) echo "<br>xBgn=$xBgn, yBgn=$yBgn, xEnd=$xEnd, yEnd=$yEnd, maxYNum=$maxYNum, unixXPix=$unitXPix<br>";
	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]}";
	return($pointArr);
}

//MS->SMSC
for($i=1;$i<=25;$i++){
	$pointArr0[$i*2] = $i-1;
	$pointArr0[$i*2+1]	= $smscStat[0][$i];
}
//线条
$pointArr0=getNewXY($pointArr0);
$pointNum0= sizeof($pointArr0) / 2;
$tmpColor0=$legendArr[0][2];
if($DEBUG){
	for($i=0;$i<14;){
		echo "pointArr0[$i]=$pointArr0[$i], ";
		$i++;
		echo "pointArr0[$i]=$pointArr0[$i]<br>";
		$i++;
	}
}

//VMS -> SMSC
for($i=1;$i<=25;$i++){
	$pointArr1[$i*2] = $i-1;
	$pointArr1[$i*2+1]	= $smscStat[1][$i];
}
//线条
$pointArr1=getNewXY($pointArr1);
$pointNum1= sizeof($pointArr1) / 2;
$tmpColor1=$legendArr[1][2];
if($DEBUG){
	for($i=0;$i<14;){
		echo "pointArr1[$i]=$pointArr1[$i], ";
		$i++;
		echo "pointArr1[$i]=$pointArr1[$i]<br>";
		$i++;
	}
}

//PPS->SMSC
for($i=1;$i<=25;$i++){
	$pointArr2[$i*2] = $i-1;
	$pointArr2[$i*2+1]	= $smscStat[2][$i];
}
//线条
$pointArr2=getNewXY($pointArr2);
$pointNum2= sizeof($pointArr2) / 2;
$tmpColor2=$legendArr[2][2];
if($DEBUG){
	for($i=0;$i<14;){
		echo "pointArr2[$i]=$pointArr2[$i], ";
		$i++;
		echo "pointArr2[$i]=$pointArr2[$i]<br>";
		$i++;
	}
}

//SMPP -> SMSC
for($i=1;$i<=25;$i++){
	$pointArr3[$i*2] = $i-1;
	$pointArr3[$i*2+1]	= $smscStat[3][$i];
	if($DEBUG)	echo "smscStat[3][$i]={$smscStat[3][$i]}<br>";
}
//线条
$pointArr3=getNewXY($pointArr3);
$pointNum3= sizeof($pointArr3) / 2;
$tmpColor3=$legendArr[3][2];
if($DEBUG){
	for($i=0;$i<14;){
		echo "pointArr3[$i]=$pointArr3[$i], ";
		$i++;
		echo "pointArr3[$i]=$pointArr3[$i]<br>";
		$i++;
	}
}

//SMSC -> MS
for($i=1;$i<=25;$i++){
	$pointArr4[$i*2] = $i-1;
	$pointArr4[$i*2+1]	= $smscStat[4][$i];
	if($DEBUG) echo "smscStat[4][$i]={$smscStat[4][$i]}<br>";
}
//线条
$pointArr4=getNewXY($pointArr4);
$pointNum4= sizeof($pointArr4) / 2;
$tmpColor4=$legendArr[4][2];
if($DEBUG){
	for($i=0;$i<14;){
		echo "pointArr4[$i]=$pointArr4[$i], ";
		$i++;
		echo "pointArr4[$i]=$pointArr4[$i]<br>";
		$i++;
	}
}

//SMSC -> SMPP
for($i=1;$i<=25;$i++){
	$pointArr5[$i*2] = $i-1;
	$pointArr5[$i*2+1]	= $smscStat[5][$i];
}
//线条
$pointArr5=getNewXY($pointArr5);
$pointNum5= sizeof($pointArr5) / 2;
$tmpColor5=$legendArr[5][2];
if($DEBUG){
	for($i=0;$i<14;){
		echo "pointArr5[$i]=$pointArr5[$i], ";
		$i++;
		echo "pointArr5[$i]=$pointArr5[$i]<br>";
		$i++;
	}
}

//画线条
for($i=1;$i<25;$i++){
	for($j=0;$j<6;$j++){
		if ($j == $selTopVal) continue;
		$pointDef = "pointArr".$j;
		$colorDef = "tmpColor".$j;
		imageline($im,${$pointDef}[$i*2],${$pointDef}[$i*2+1],${$pointDef}[$i*2+2],${$pointDef}[$i*2+3],${$colorDef});
	}
}
//selTopVal最后画，让它在最前面
for($i=1;$i<25;$i++){
	$pointDef = "pointArr".$selTopVal;
	$colorDef = "tmpColor".$selTopVal;
	imageline($im,${$pointDef}[$i*2],${$pointDef}[$i*2+1],${$pointDef}[$i*2+2],${$pointDef}[$i*2+3],${$colorDef});
}

//-----输出图形文件，请勿修改
//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>";

?>
