<?php

if (!function_exists("GetSQLValueString")) {
  	
	function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
	{
	  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

	  switch ($theType) {
	    case "text":
	      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	      break;    
	    case "long":
	    case "int":
	      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
	      break;
	    case "double":
	      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
	      break;
	    case "date":
	      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	      break;
	    case "defined":
	      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
	      break;
	  	}
	  return $theValue;
	}

}
//模拟提交的变量对其进行转码 2018-07
function save2mssql($theValue) 
{ 
	return iconv('UTF-8','GBK',$theValue);
}	
//对查询后的结果进行转码 2018-07
function loadFromMssql($theValue) 
{ 
	return iconv('GBK','UTF-8',$theValue);
}
 
     
//-------通过 GUID 获取 username---------
$username='';

if (isset($GUID)) {
	$is_exist=0;
	$query_RecordsetCFG = "SELECT top 1 username  FROM users where GUID='". $GUID ."' order by ID ";
	//echo $query_RecordsetCFG;
	$row_RecordsetCFGs=$dsorder->query($query_RecordsetCFG);
	if (is_array($row_RecordsetCFGs) || is_object($row_RecordsetCFGs)){
		foreach ($row_RecordsetCFGs as $row_RecordsetCFG)
		{ 
			$is_exist=1;
			$username= $row_RecordsetCFG['username'];
		}
	}
	if (!$is_exist) {
		die();
	}
	echo '{"username":"'.$username.'",';
				
}


//select action

if (isset($_GET['action']) && $_GET['action']=='readall') {
	//全部已读	
	$query_readall = "update msg set ctl='readed' ,LastStatus=".GetSQLValueString(date("Y-m-d H:i:s"), "text")." where username='". $username ."' and (ctl='' or  ctl isnull)";
	$orders->query($query_readall);
	//获取未读数量和队列SQLStr
	$unread=0;
	$query_Recordsetunread = "SELECT count(DISTINCT msg_txt)   FROM msg where username='".$username."' and (ctl='' or  ctl isnull)";
	foreach ($orders->query($query_Recordsetunread) as $row_Recordsetunread)
	{	
		$unread=$row_Recordsetunread[0]; 
	}
	die ('"unread":'.$unread.',"sucess":"1"}');
		
}else if (isset($_GET['action']) && $_GET['action']=='readone') {
	//读了一条
	$query_read = "update msg set ctl='readed',LastStatus=".GetSQLValueString(date("Y-m-d H:i:s"), "text")." where username='". $username ."' and ID=".$_GET['ReadID'];
	$orders->query($query_read);
	//获取未读数量和队列SQLStr
	$unread=0;
	$query_Recordsetunread = "SELECT count(DISTINCT msg_txt)   FROM msg where username='".$username."' and (ctl='' or  ctl isnull)";
	foreach ($orders->query($query_Recordsetunread) as $row_Recordsetunread)
	{	
		$unread=$row_Recordsetunread[0]; 
	}
	if ($_GET['SyncQueue']==1) die ('"unread":'.$unread.',"sucess":"1"}');
	echo '"unread":'.$unread.',';
				
	$query_RecordsetMsgSQLstr = "SELECT ID,AddTime, msg_txt,MsgType  FROM msg where username='". $username ."' and (ctl='' or  ctl isnull)   group by msg_txt order by ID ";

}else if (isset($_GET['action']) && $_GET['action']=='readed') {
	//获取已读数量和队列SQLStr
	$readed=0;
	$query_Recordsetunread = "SELECT count(DISTINCT msg_txt)   FROM msg where username='".$username."' and   ctl  is not null and  ctl <>''";
	foreach ($orders->query($query_Recordsetunread) as $row_Recordsetunread)
	{	
		$readed=$row_Recordsetunread[0]; 
	}
	echo '"readed":'.$readed.',';
				
	$query_RecordsetMsgSQLstr = "SELECT ID,AddTime, msg_txt,MsgType,LastStatus  FROM msg where username='". $username ."' and  ctl  is not null and  ctl <>''   group by msg_txt order by ID ";

}else{
	//获取未读数量和队列SQLStr
	$unread=0;
	$query_Recordsetunread = "SELECT count(DISTINCT msg_txt)   FROM msg where username='".$username."' and (ctl='' or  ctl isnull)";
	foreach ($orders->query($query_Recordsetunread) as $row_Recordsetunread)
	{	
		$unread=$row_Recordsetunread[0]; 
	}
	echo '"unread":'.$unread.',';
				
	$query_RecordsetMsgSQLstr = "SELECT ID,AddTime, msg_txt,MsgType,LastStatus  FROM msg where username='". $username ."' and (ctl='' or  ctl isnull)   group by msg_txt order by ID ";
}

	
	
	
	
	
	
				
echo '"msg":[';	
foreach ($orders->query($query_RecordsetMsgSQLstr) as $row_Recordset1)
{
	$ID=$row_Recordset1['ID'];
	$AddTime=$row_Recordset1['AddTime'];
	$MsgType=$row_Recordset1['MsgType'];
	$msg_txt=str_replace('"','\"',$row_Recordset1['msg_txt']);
	$LastStatus=$row_Recordset1['LastStatus'];
	echo '
	{
	"ID":'.$ID.',
	"AddTime":"'.$AddTime.'",
	"MsgType":"'.$MsgType.'",
	"LastStatus":"'.$LastStatus.'",
	"msg_txt":"'.$msg_txt.'"
	},';
}    
echo "
	]}";
	

?>