File "shipping_20130808.php"

Full path: /www/ansys/qgis/004_iwakuni/record/Backup/shipping_20130808.php
File size: 10.85 KiB (11114 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	自動でクラスを参照する
//	関数名	:	__autoload()
//	備考	:	参照先は./class/XXX.phpとする
//	更新	:	2013/03/12 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function __autoload($className)
{
	$fPath = "./class/" . str_replace("_", "/", $className) . ".php";
	require_once $fPath;
}

// 点が線分のどちらがわに存在するかをチェック
function getSide($p_, $a_, $b_)
{
	$x = "lon";
	$y = "lat";
	$exter = $p_[$x] * ($a_[$y] - $b_[$y]) + $a_[$x] * ($b_[$y] - $p_[$y]) + $b_[$x] * ($p_[$y] - $a_[$y]);
	if (0 < $exter)
		return 1;
	else if (0 > $exter)
		return -1;
	else
		return 0;
}

// 点がエリアに含まれるかをチェック
function insideArea($point_, $area_)
{
	$totalSide = 0;
	$linkNum = count($area_);
	$bIndex = 0;
	
	for($iCount = 0; $iCount < $linkNum; $iCount++)
	{
		if ($linkNum > $iCount + 1)
			$bIndex = $iCount + 1;
		else
			$bIndex = 0;
		
		$side = getSide($point_, $area_[$iCount], $area_[$bIndex]);
		if ($side == 0)
			return 1;
		else
			$totalSide += $side;
	}
	if (abs($totalSide) == $linkNum)
		return 1;
	
	return 0;
}

//===============================================
//	CommonUtilities
//	備考		:	
//	更新		:	2013/03/12 KMK-Teraoka
//===============================================
$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$LOG_FILE = "./log/output.log";

$CONST_ID = 7;
$APPLICATION_ID = 13;
$TERMINAL_TABLE = "qgis.ts_terminal";
$HIS_TABLE = "qgis.ts_his_measure";
$SHIPPING_TABLE = "qgis.concrete_shipping_record";

$CONST_NAME = "岩国飛行場";

$COUNT_START_HI = "00:00";
$COUNT_END_HI = "23:59";

// (PTS周辺)
$OUT_COUNT_AREA = array(
	array("lat"	=>	36.907378,	"lon"	=>	139.935339),
	array("lat"	=>	36.907781,	"lon"	=>	139.936197),
	array("lat"	=>	36.906588,	"lon"	=>	139.936755),
	array("lat"	=>	36.906280,	"lon"	=>	139.935972)
);

// (本館周辺)
$IN_COUNT_AREA = array(
	array("lat"	=>	36.908244,	"lon"	=>	139.937539),
	array("lat"	=>	36.908527,	"lon"	=>	139.938343),
	array("lat"	=>	36.907532,	"lon"	=>	139.939116),
	array("lat"	=>	36.907180,	"lon"	=>	139.938215)
);

//===============================================
//	DEBUG_MODE
//	備考		:	
//	更新		:	2013/03/12 KMK-Teraoka
//===============================================
define("DEBUG_MODE", "1");
Debug::setMode(DEBUG_MODE);

try
{
	//================================================
	//	データベース接続
	//	備考		:	
	//	更新		:	2013/06/17 KMK-Teraoka
	//================================================
	$conn = pg_connect("
		host	=	". $HOST. "
		dbname	=	". $DB. "
		user	=	". $USER. "
		password=	". $PASS. "
	");
	if (!$conn)
		throw new Exception("データベースへの接続に失敗しました");
	
	//================================================
	//	データ格納配列
	//	備考		:	
	//	更新		:	2013/08/07 KMK-Teraoka
	//================================================
	$shipping = array();
	
	//================================================
	//	日付の取得
	//	備考		:	
	//	更新		:	2013/08/07 KMK-Teraoka
	//================================================
	$shipping["date"] = date("Y-m-d 00:00:00");
	
	//================================================
	//	端末一覧を取得
	//	備考		:	
	//	更新		:	2013/08/07 KMK-Teraoka
	//================================================
	$selTerminalList = "SELECT android_id FROM ". $TERMINAL_TABLE. "";
	$selTerminalList .= " WHERE const_id='". $CONST_ID. "'";
	$selTerminalList .= " AND app_id='". $APPLICATION_ID. "'";
	$selTerminalList .= " AND upddatetime >= '". $shipping["date"]. "'";
	$selTerminalList .= ";";
	if (!($resTerminalList = pg_query($conn, $selTerminalList)))
		throw new Exception("データ取得に失敗<br />". $resTerminalList. "<br />". pg_last_error(). "<br />");
	
	$length = pg_num_rows($resTerminalList);
	for($iCount = 0; $iCount < $length; $iCount++)
	{
		$rowTerminalList = pg_fetch_assoc($resTerminalList, $iCount);
		$shipping["terminal"][$iCount]["android_id"] = $rowTerminalList["android_id"];
	}
	Debug::writes($selTerminalList);
	
	//================================================
	//	出荷、搬入データを集計
	//	備考		:	
	//	更新		:	2013/08/07 KMK-Teraoka
	//================================================
	for($iCount = 0; $iCount < $length; $iCount++)
	{
		$androidId = $shipping["terminal"][$iCount]["android_id"];
		
		$selHistory = "SELECT lat, lon, regdatetime FROM ". $HIS_TABLE. "";
		$selHistory .= " WHERE android_id='". $androidId. "'";
		$selHistory .= " AND const_id='". $CONST_ID."'";
		$selHistory .= " AND app_id='". $APPLICATION_ID. "'";
		$selHistory .= " AND regdatetime >= '". $shipping["date"]. "'";
		$selHistory .= ";";
		
		Debug::writes($selHistory);
		
		if (!($resHistory = pg_query($conn, $selHistory)))
			throw new Exception("データ取得に失敗<br />". $resHistory. "<br />". pg_last_error(). "<br />");
		
		$passOut = 0;
		$shipping["terminal"][$iCount]["status"] = -1;	// -1:不明、0:往路、1:復路
		$shipping["terminal"][$iCount]["in_count"] = 0;
		$shipping["terminal"][$iCount]["out_count"] = 0;
		$shipping["terminal"][$iCount]["record_count"] = 0;
		
		$hisLength = pg_num_rows($resHistory);
		for($jCount = 0; $jCount < $hisLength; $jCount++)
		{
			$rowHistory = pg_fetch_assoc($resHistory, $jCount);
			if ($passOut == 0)
			{
				if (insideArea(array("lat"	=>	$rowHistory["lat"], "lon"	=>	$rowHistory["lon"]), $OUT_COUNT_AREA) == 1)
				{
					$passOut = 1;
					$shipping["terminal"][$iCount]["record"][$shipping["terminal"][$iCount]["record_count"]]["date"] = $rowHistory["regdatetime"];
					$shipping["terminal"][$iCount]["record"][$shipping["terminal"][$iCount]["record_count"]]["out_or_in"] = "1";
					$shipping["terminal"][$iCount]["record_count"] = $shipping["terminal"][$iCount]["record_count"] + 1;
					$shipping["terminal"][$iCount]["out_count"] = $shipping["terminal"][$iCount]["out_count"] + 1;
					$$shipping["terminal"][$iCount]["status"] = 0;
				}
			}
			else
			{
				if (insideArea(array("lat"	=>	$rowHistory["lat"], "lon"	=>	$rowHistory["lon"]), $IN_COUNT_AREA) == 1)
				{
					$passOut = 0;
					$shipping["terminal"][$iCount]["record"][$shipping["terminal"][$iCount]["record_count"]]["date"] = $rowHistory["regdatetime"];
					$shipping["terminal"][$iCount]["record"][$shipping["terminal"][$iCount]["record_count"]]["out_or_in"] = "2";
					$shipping["terminal"][$iCount]["record_count"] = $shipping["terminal"][$iCount]["record_count"] + 1;
					$shipping["terminal"][$iCount]["in_count"] = $shipping["terminal"][$iCount]["in_count"] + 1;
					$shipping["terminal"][$iCount]["status"] = 1;
				}
			}
		}
	}
	Debug::writes($shipping);
	
	//================================================
	//	出荷、搬入データを更新
	//	備考		:	
	//	更新		:	2013/08/07 KMK-Teraoka
	//================================================
	for($iCount = 0; $iCount < $length; $iCount++)
	{
		$androidId = $shipping["terminal"][$iCount]["android_id"];
		for($jCount = 0; $jCount < $shipping["terminal"][$iCount]["record_count"]; $jCount++)
		{
			$selRecord = "SELECT count(*) FROM ". $SHIPPING_TABLE. "";
			$selRecord .= " WHERE record_date='". $shipping["terminal"][$iCount]["record"][$jCount]["date"]. "'";
			$selRecord .= " AND android_id='". $androidId. "'";
			$selRecord .= " AND status='". $shipping["terminal"][$iCount]["record"][$jCount]["out_or_in"]. "'";
			$selRecord .= ";";
			Debug::writes($selRecord);
			if (!($resRecord = pg_query($conn, $selRecord)))
				throw new Exception("データ取得に失敗<br />". $resRecord. "<br />". pg_last_error(). "<br />");
			$rowRecord = pg_fetch_assoc($resRecord, 0);
			
			if ($rowRecord["count"] == 0)
			{
				// INSERT
				$insRecord = "INSERT INTO ". $SHIPPING_TABLE. "";
				$insRecord .= " VALUES ('". $shipping["terminal"][$iCount]["record"][$jCount]["date"]. "', '". $androidId. "', '". $shipping["terminal"][$iCount]["record"][$jCount]["out_or_in"]. "', current_timestamp)";
				$insRecord .= ";";
				Debug::writes($insRecord);
				if (!($resRecord = pg_query($conn, $insRecord)))
					throw new Exception("データ挿入に失敗<br />". $resRecord. "<br />". pg_last_error(). "<br />");
			}
		}
	}
//	for($iCount = 0; $iCount < $length; $iCount++)
//	{
//		$androidId = $shipping["terminal"][$iCount]["android_id"];
//		
//		$selShipping = "SELECT android_id FROM ". $SHIPPING_TABLE. "";
//		$selShipping .= " WHERE android_id='". $androidId. "'";
//		$selShipping .= " AND record_date='". $shipping["date"]. "'";
//		$selShipping .= ";";
//		
//		if (!($resShipping = pg_query($conn, $selShipping)))
//			throw new Exception("データ取得に失敗<br />". $resShipping. "<br />". pg_last_error(). "<br />");
//		$shippingLength = pg_num_rows($resShipping);
//		if ($shippingLength == 0)
//		{
//			// INSERT
//			$insShipping = "INSERT INTO ". $SHIPPING_TABLE. "";
//			$insShipping .= " (record_date, android_id, status, out_count, in_count, upddatetime, regdatetime)";
//			$insShipping .= " VALUES ('". $shipping["date"]. "', '". $androidId. "', '". $shipping["terminal"][$iCount]["status"]. "', '". $shipping["terminal"][$iCount]["out_count"]. "', '". $shipping["terminal"][$iCount]["in_count"]. "', current_timestamp, current_timestamp)";
//			$insShipping .= ";";
//			Debug::writes($insShipping);
//			if (!($resShipping = pg_query($conn, $insShipping)))
//				throw new Exception("データ挿入に失敗<br />". $resShipping. "<br />". pg_last_error(). "<br />");
//		}
//		else
//		{
//			// UPDATE
//			$updShipping = "UPDATE ". $SHIPPING_TABLE. " SET";
//			$updShipping .= " status='". $shipping["terminal"][$iCount]["status"]. "'";
//			$updShipping .= ", out_count='". $shipping["terminal"][$iCount]["out_count"]. "'";
//			$updShipping .= ", in_count='". $shipping["terminal"][$iCount]["in_count"]. "'";
//			$updShipping .= ", upddatetime=current_timestamp";
//			$updShipping .= " WHERE android_id='". $androidId. "'";
//			$updShipping .= " AND record_date='". $shipping["date"]. "'";
//			$updShipping .= ";";
//			
//			Debug::writes($updShipping);
//			if (!($resShipping = pg_query($conn, $updShipping)))
//				throw new Exception("データ更新に失敗<br />". $resShipping. "<br />". pg_last_error(). "<br />");
//		}
//	}
}
catch(Exception $exc)
{
	print "<html>";
	print "<head>";
	print "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">";
	print "<title>". "コンクリート品質管理システム -出荷、搬入記録-". "</title>";
	print "</head>";
	print "<body>";
	
	print "<font color=\"#FF0000\">";
	print "【スクリプトエラー】<br />";
	print $exc->getMessage(). "<br />";
	print "</font>";
	
	print "<font size=\"3\">";
	print "<form name=\"topForm_2\" method=\"post\" action=\"". "../index.php". "\" style=\"display:inline\">";
	print "<a href=\"javascript:topForm_2.submit()\">トップ画面へ戻る</a>";
	print "</form>";
	print "<br />";
	print "</font>";
	
	print "</body>";
	print "</html>";
}
?>

PHP File Manager