<?php
try
{
	$output = array();
	$addIndex = 0;
	
	$WORK_STATUS = 0;
	$enableSec = 600;
	$enableDateTime = date("Y-m-d H:i:s", time() - $enableSec);
	
	//データベース接続
	$HOST = "192.168.100.153";
	$DB = "ansys";
	$USER = "postgres";
	$PASS = "am#71ZJ*";
	
	$conn = pg_connect("
		host	=	". $HOST. "
		dbname	=	". $DB. "
		user	=	". $USER. "
		password=	". $PASS. "
	");
	
	// 搬入累計の計算
	$selShippingTotal = "SELECT * FROM qgis.concrete_shipping_record";
	$selShippingTotal .= " WHERE record_date >= '". date("Y-m-d 00:00:00"). "'";
	$selShippingTotal .= " AND record_date <= '". date("Y-m-d 23:59:59"). "'";
	$selShippingTotal .= " AND const_id='7'";
	$selShippingTotal .= " AND status='2'";
	$selShippingTotal .= ";";
	$resShippingTotal = pg_query($conn, $selShippingTotal) or die (pg_last_error(). "<br>". $selShippingTotal. "<br />");
	$shippingTotalCount = pg_num_rows($resShippingTotal);
	$output[$addIndex++] = $shippingTotalCount;
	
	$sql = "SELECT * FROM qgis.ts_terminal";
	$sql .= " WHERE app_id='13'";
	$sql .= " AND const_id='7'";
	$sql .= " AND mesdatetime >= '". $enableDateTime. "'";
	if (strcmp($WORK_STATUS, 0) != 0)
		$sql .= " AND work_status='". $WORK_STATUS. "'";
	$sql .= " ORDER BY regdatetime;";
	$res = pg_query($conn, $sql);
	if (!($resList = pg_query($conn, $sql)))
		throw new Exception("");
	
	$numTemrinal = pg_num_rows($res);
	for($iCount = 0; $iCount < $numTemrinal; $iCount++)
	{
		$row = pg_fetch_array($res, $iCount);
		
		$selShipping = "SELECT * FROM qgis.concrete_shipping_record";
		$selShipping .= " WHERE android_id='". $row["android_id"]. "'";
		$selShipping .= " AND record_date >= '". date("Y-m-d 00:00:00"). "'";
		$selShipping .= " AND record_date <= '". date("Y-m-d 23:59:59"). "'";
		$selShipping .= " AND const_id='7'";
		$selShipping .= " AND status='1'";
		$selShipping .= ";";
		if (!($resShipping = pg_query($conn, $selShipping)))
				throw new Exception("");
		$shippingCount = pg_num_rows($resShipping);
		if ($shippingCount == 0)
			$shippingCount = 1;
		$output[$addIndex++] = $row["terminal_name"]. "[". $shippingCount. "]". ",". $row["lon"]. ",". $row["lat"]. ",". $row["work_status"];
	}
	
	$numoutput = count($output);
	for($iCount = 0; $iCount < $numoutput; $iCount++)
	{
		if ($iCount > 0)
			print "\\n";
		print $output[$iCount];
	}
}
catch(Exception $exc)
{
	print "";
}
?>
