<?php
require_once("db.php");

//查询赞
/*
function getPraise($userkey, $imgkey)
{
	$res = 0;
	$sql = sprintf("select count(*) as num from Praise where Userkey=%d and Imgkey=%d", $userkey, $imgkey);
	$result = querySql($sql);
	$row = mysql_fetch_array($result);
	if($row)
	{
		$res = $row['num'];
	}
	else
	{
		$res = 0;
	}
	freeResult($result);
	return $res;
}
*/

//新建相册
function addAlbum($userkey, $name, $desc)
{
	$sql = sprintf("insert into ServerPhotoAlbum(Userkey,Name,Introduction,CreateDate,UpdateDate,State,Type) values(%d,'%s','%s','%s','%s',%d,%d)", 
		$userkey, $name, $desc, date('y-m-d G:i:s'), date('y-m-d G:i:s'), 0, 0); 
	
	executeSql($sql);
	return getLastInsertId();
}

//新建相册
function addAlbumEx($userkey, $name, $desc, $foldername)
{
	$sql = sprintf("insert into ServerPhotoAlbum(Userkey,Name,Introduction,CreateDate,UpdateDate,State,Type,FolderName) values(%d,'%s','%s','%s','%s',%d,%d,'%s')", 
		$userkey, $name, $desc, date('y-m-d G:i:s'), date('y-m-d G:i:s'), 0, 0, $foldername); 
	
	executeSql($sql);
	return getLastInsertId();
}

function getAlbum($userkey, $foldername)
{
	$sql = sprintf("select * from ServerPhotoAlbum where Userkey=%d and FolderName='%s'", $userkey, $foldername);
	$result = querySql($sql);
	return $result;
}

//更新相册
function updateAlbum($userkey, $name, $desc, $foldername)
{
	$sql = sprintf("update ServerPhotoAlbum set Name='%s',Introduction='%s',UpdateDate='%s' where Userkey='%s' and FolderName='%s' and Type=0", 
		$name, $desc, date('y-m-d G:i:s'), $userkey, $foldername); 
	
	executeSql($sql);
}

//新建旅途
function addTrip($userkey, $name, $desc, $StartCity, $EndCity, $TransferCity, $foldername)
{
	$sql = sprintf("insert into ServerTrip(Userkey,Name,Introduction,StartCity,EndCity,TransferCity,CreateDate,UpdateDate,State,FolderName) values(%d,'%s','%s','%s','%s','%s','%s','%s',%d,'%s')", 
		$userkey, $name, $desc, $StartCity, $EndCity, $TransferCity, date('y-m-d G:i:s'), date('y-m-d G:i:s'), 0, $foldername); 
	executeSql($sql);
	return getLastInsertId();
}


function getTripByFolderName($userkey, $foldername)
{
	$sql = sprintf("select * from ServerTrip where Userkey=%d and FolderName='%s'", $userkey, $foldername);
	$result = querySql($sql);
	return $result;
}

//新建旅途
function updateTrip($userkey, $name, $desc, $StartCity, $EndCity, $TransferCity, $foldername)
{
	$sql = sprintf("update ServerTrip set Name='%s',Introduction='%s',StartCity='%s',EndCity='%s',TransferCity='%s',UpdateDate='%s' where Userkey=%d and FolderName='%s'",
		$name, $desc, $StartCity, $EndCity, $TransferCity, date('y-m-d G:i:s'), $userkey, $foldername); 
	executeSql($sql);
}

//新建专辑
function addSpecial($userkey, $name, $desc, $foldername)
{
	$sql = sprintf("insert into ServerPhotoAlbum(Userkey,Name,Introduction,CreateDate,UpdateDate,State,Type,FolderName) values(%d,'%s','%s','%s','%s',%d,%d,'%s')", 
		$userkey, $name, $desc, date('y-m-d G:i:s'), date('y-m-d G:i:s'), 0, 1, $foldername); 
	
	executeSql($sql);
	return getLastInsertId();
}

//更新专辑
function updateSpecial($userkey, $name, $desc, $foldername)
{
	$sql = sprintf("update ServerPhotoAlbum set Name='%s',Introduction='%s',UpdateDate='%s' where Userkey='%s' and FolderName='%s' and Type=1", 
		$name, $desc, date('y-m-d G:i:s'), $userkey, $foldername); 
	executeSql($sql);
}


//新建专辑子文件夹
function addSpecialSubFolder($specialkey, $name, $desc, $foldername)
{
	$sql = sprintf("insert into ServerSpecialFolder(PhotoAlbumKey,Name,Introduction,State,FolderName) values(%d,'%s','%s',%d,'%s')", 
		$specialkey, $name, $desc, 0, $foldername); 
	
	executeSql($sql);
	return getLastInsertId();
}

//更新专辑子文件夹
function updateSpecialSubFolder($specialkey, $name, $desc, $foldername)
{
	$sql = sprintf("update ServerSpecialFolder set Name='%s',Introduction='%s' where PhotoAlbumKey=%d and FolderName='%s'", 
		$name, $desc, $specialkey, $foldername); 
	executeSql($sql);
}

function getAlbumByName($userkey, $albumname)
{
	$sql = sprintf("select * from ServerPhotoAlbum where Type=0 and Userkey=%d and FolderName='%s'", $userkey, $albumname);
	$result = querySql($sql);
	return $result;
}

function getSpecialByName($userkey, $albumname)
{
	$sql = sprintf("select * from ServerPhotoAlbum where Type=1 and Userkey=%d and FolderName='%s'", $userkey, $albumname);
	$result = querySql($sql);
	return $result;
}

function getTripByName($userkey, $tripname)
{
	$sql = sprintf("select * from ServerTrip where Userkey=%d and FolderName='%s'", $userkey, $tripname);
	$result = querySql($sql);
	return $result;
}

function getSpecialSubFolderByName($specialkey, $name)
{
	$sql = sprintf("select * from ServerSpecialFolder where PhotoAlbumKey=%d and FolderName='%s'", $specialkey, $name);
	$result = querySql($sql);
	return $result;
}

function getSpecialSubFolderByNameEx($name)
{
	$sql = sprintf("select * from ServerSpecialFolder where FolderName='%s'",$name);
	$result = querySql($sql);
	return $result;
}

function checkAlbum($Userkey, $foldername, $foldercode, $folderdesc)
{
	$mainkey = 0;
	$folderRes = getAlbumByName($Userkey, $foldercode);
	$folderRow = mysql_fetch_array($folderRes);
	if($folderRow)
	{
		$mainkey = $folderRow['Mainkey'];
	}
	else
	{	
		$mainkey = addAlbumEx($Userkey, $foldername, $folderdesc, $foldercode);
	}	
	freeResult($folderRes);
	return $mainkey;
}

function checkSpecial($Userkey, $foldername, $foldercode, $folderdesc)
{
	$mainkey = 0;
	$folderRes = getSpecialByName($Userkey, $foldercode);
	$folderRow = mysql_fetch_array($folderRes);
	if($folderRow)
	{
		$mainkey = $folderRow['Mainkey'];
	}
	else
	{	
		$mainkey = addSpecial($Userkey, $foldername, $folderdesc, $foldercode);
	}	
	freeResult($folderRes);
	return $mainkey;
}

function checkSpecialSubFolder($folderkey, $subfoldername, $subfoldercode, $subfolderdesc)
{
	$mainkey = 0;
	$folderRes = getSpecialSubFolderByName($folderkey, $subfoldercode);
	$folderRow = mysql_fetch_array($folderRes);
	if($folderRow)
	{
		$mainkey = $folderRow['Mainkey'];
	}
	else
	{	
		$mainkey = addSpecialSubFolder($folderkey, $subfoldername, $subfolderdesc, $subfoldercode);
	}	
	freeResult($folderRes);
	return $mainkey;
}

function checkTrip($Userkey, $foldername, $foldercode, $folderdesc, $StartCity, $EndCity, $TransferCity)
{
	$mainkey = 0;
	$folderRes = getTripByName($Userkey, $foldercode);
	$folderRow = mysql_fetch_array($folderRes);
	if($folderRow)
	{
		$mainkey = $folderRow['Mainkey'];
	}
	else
	{	
		$mainkey = addTrip($Userkey, $foldername, $folderdesc, $StartCity, $EndCity, $TransferCity, $foldercode);
	}	
	freeResult($folderRes);
	return $mainkey;
}

//
//是否专辑成员
//
function isSpecialMember($specialkey, $userkey)
{
	$sql = sprintf("select count(*) as count from SpecialMember where Specialkey=%d and Userkey=%d and Type=1", $specialkey, $userkey);
	$count = getRecordCount($sql);	
	if($count>0)
		return 1;
	else 
		return 0;
}	

//
//是否专辑管理者
//
function isSpecialManager($specialkey, $userkey)
{
	$sql = sprintf("select count(*) as count from SpecialMember where Specialkey=%d and Userkey=%d and IsManager=1", $specialkey, $userkey);
	$count = getRecordCount($sql);	
	if($count>0)
		return 1;
	else 
		return 0;
}	

function getSpecialManager($specialkey)
{
	$sql = sprintf("select * from SpecialMember where Specialkey=%d and IsManager=1", $specialkey);
	$result = querySql($sql);
	return $result;
}

//
//专辑管理者
//
function getSpecialManagerNum($specialkey)
{
	$sql = sprintf("select count(*) as count from SpecialMember where Specialkey=%d and Type=1", $specialkey);
	$count = getRecordCount($sql);	
	return $count;
}

//
//专辑成员数量
//
function getSpecialMemberNum($specialkey)
{
	$sql = sprintf("select count(*) as count from SpecialMember where Specialkey=%d", $specialkey);
	$count = getRecordCount($sql);	
	return $count;
}	

//
//专辑照片数量
//
function getSpecialImgNum($specialkey, $verify)
{
	$sql = sprintf("select count(*) as count from ImgInfo where Type=2 and Folderkey=%d and Verify=%d", $specialkey, $verify);
	$count = getRecordCount($sql);	
	return $count;
}	

//
//专辑照片数量
//
function getSpecialSubFolderImgNum($specialsubfolderkey, $verify)
{
	$sql = sprintf("select count(*) as count from ImgInfo where Type=2 and SubFolderFolderkey=%d and Verify=%d", $specialsubfolderkey, $verify);
	$count = getRecordCount($sql);	
	return $count;
}	

//
//获取专辑
//
function getSpecialByMainkey($specialkey)
{
	$sql = sprintf("select * from ServerPhotoAlbum where Type=1 and Mainkey=%d", $specialkey);
	$result = querySql($sql);
	return $result;
}	

//添加专辑成员
function addSpecialMember($specialkey, $userkey, $ismanager, $type)
{
	$isMember = isSpecialMember($specialkey, $userkey);
	if($isMember == 0)
	{
		$sql = sprintf("insert into SpecialMember(Specialkey,Userkey,IsManager,Type,CreateDate) 
			values(%d,%d,%d,%d,'%s')", 
			$specialkey, $userkey, $ismanager, $type, date('y-m-d G:i:s')); 
		executeSql($sql);
	}
}

//删除专辑成员
function removeSpecialMemberByMainkey($specialkey, $userkey)
{
	$sql = sprintf("delete from SpecialMember where Specialkey=%d and Userkey=%d",
		$specialkey, $userkey); 
	executeSql($sql);
}

//
//修改专辑管理者
//
function setSpecialManager($specialkey, $targetkey, $ismanager)
{
	$sql = sprintf("update SpecialMember set IsManager=%d where Specialkey=%d and Userkey=%d", $ismanager, $specialkey, $targetkey);
	executeSql($sql);
}	

//
//是否已经关注专辑
//
function isAttentionSpecial($specialkey, $userkey)
{
	$sql = sprintf("select count(*) as count from SpecialAttention where Specialkey=%d and Userkey=%d", $specialkey, $userkey);
	$count = getRecordCount($sql);	
	if($count>0)
		return 1;
	else 
		return 0;
}

//关注专辑
function addAttentionSpecial($specialkey, $userkey)
{
	$sql = sprintf("insert into SpecialAttention(Userkey,Specialkey,CreateDate) values(%d,%d,'%s')", 
		$userkey, $specialkey, date('y-m-d G:i:s')); 
	executeSql($sql);
}

//取消关注专辑
function delAttentionSpecial($specialkey, $userkey)
{
	$sql = sprintf("delete from SpecialAttention where Userkey=%d and Specialkey=%d",  $userkey, $specialkey); 
	executeSql($sql);
}

//获取关注专辑数量
function getAttentionSpecialNum($specialkey)
{
	$sql = sprintf("select count(*) as count from SpecialAttention where Specialkey=%d", $specialkey);
	$count = getRecordCount($sql);	
	return $count;
}

//获取好友位置共享权限
function getPositionSharePower($userkey, $targetkey)
{
	$sql = sprintf("select count(*) as count from PositionSharePower where Userkey=%d and Targetkey=%d", $userkey, $targetkey);
	$count = getRecordCount($sql);	
	if($count>0)
		return 1;
	else 
		return 0;
}

//删除照片
function delPhoto($imgkey)
{
	$sql = sprintf("delete from ImgInfo where Mainkey=%d",  $imgkey); 
	executeSql($sql);
}

//删除专辑文件夹
function delSpecialSubFolderkey($specialsubfolderkey)
{
	$sql = sprintf("delete from ServerSpecialFolder where Mainkey=%d",  $specialsubfolderkey); 
	executeSql($sql);
	$sql = sprintf("delete from ImgInfo where SubFolderFolderkey=%d",  $specialsubfolderkey); 
	executeSql($sql);
}

//删除专辑
function delSpecial($specialkey)
{
	$sql = sprintf("delete from ServerPhotoAlbum where Mainkey=%d",  $specialkey); 
	executeSql($sql);
	$sql = sprintf("delete from ServerSpecialFolder where PhotoAlbumKey=%d",  $specialkey); 
	executeSql($sql);
	$sql = sprintf("delete from ImgInfo where Folderkey=%d",  $specialkey); 
	executeSql($sql);
}

//
//修改专辑开放权限
//
function setSpecialOpenByMainkey($specialkey, $isopen)
{
	$sql = sprintf("update ServerPhotoAlbum set IsOpen=%d where Mainkey=%d", $isopen, $specialkey);
	executeSql($sql);
}	

//
//专辑查看次数+1
//
function addSpecialLookNum($specialkey)
{
	$sql = sprintf("update ServerPhotoAlbum set LookNum=LookNum+1 where Mainkey=%d", $specialkey);
	executeSql($sql);
}	

//
//查询标签列表
//
function getTabCategory()
{
	$sql = sprintf("select * from TabCategory");
	$result = querySql($sql);
	return $result;
}	

//
//查询标签列表
//
function getTabByCategorykey($categorykey)
{
	$sql = sprintf("select * from Tab where Categorykey=%d", $categorykey);
	$result = querySql($sql);
	return $result;
}	

//
//举报
//
function addReport($userkey, $imgkey, $context)
{
	$sql = sprintf("insert into Report(Userkey,Imgkey,Content,CreateDate) values(%d,%d,'%s','%s')", 
		$userkey, $imgkey, $context, date('y-m-d G:i:s')); 
	executeSql($sql);
}	

//
//查询赞/答谢
//
function getPraise($userkey, $imgkey, $type)
{
	$sql = sprintf("select count(*) as count from Praise where Type=%d and Userkey=%d and Imgkey=%d", 
		$type, $userkey, $imgkey); 
	$count = getRecordCount($sql);	
	if($count>0)
		return 1;
	else 
		return 0;
}	

//
//查询专辑照片
//
function getSpecialImgByPosition($special, $topleft, $topright, $bottomleft, $bottomright)
{
	$sql = sprintf("select AsText(ImgInfo.Location) as ImgInfo_Location 
		from ImgInfo where Folderkey=%d and Intersects(ImgInfo.Location, GeomFromText('POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f))')) ", 
		$special, $topleft->x, $topleft->y, $topright->x, $topright->y, $bottomright->x, $bottomright->y, $bottomleft->x, $bottomleft->y, $topleft->x, $topleft->y); 
	$result = querySql($sql);
	return $result;
}	


//
//编辑用户信息
//
/*
function updateUserInfo($userkey, )
{
	$sql = sprintf("update ServerPhotoAlbum set LookNum=LookNum+1 where Mainkey=%d", $specialkey);
	executeSql($sql);

}	*/

//
//位置共享授权/取消授权
//
function positionAuthorization($userkey, $targetkey)
{
	$sql = sprintf("update ServerPhotoAlbum set LookNum=LookNum+1 where Mainkey=%d", $specialkey);
	executeSql($sql);
}	

//
//位置共享授权
//
function addPositionPower($userkey, $targetkey)
{
	$sql = sprintf("insert into PositionSharePower(Userkey,Targetkey) values(%d,%d)", $userkey, $targetkey);
	executeSql($sql);
}	

//
//取消授权
//
function delPositionPower($userkey, $targetkey)
{
	$sql = sprintf("delete from PositionSharePower where Userkey=%d and Targetkey=%d", $userkey, $targetkey);
	executeSql($sql);
}

//
//查询用户信息
//
function getUserInfo($targetkey)
{
	$sql = sprintf("select * from UserInfo where Mainkey=%d", $targetkey);
	$result = querySql($sql);
	return $result;
}

//
//查询用户信息
//
function getUserLocationInfo($targetkey)
{
	$sql = sprintf("select Mainkey,Name,UserIconAddr,AsText(Location) as ImgInfo_Location from UserInfo where Mainkey=%d", $targetkey);
	$result = querySql($sql);
	return $result;
}

//
//查询用户信息
//
function getUserFriendLocationInfo($userkey)
{
	$sql = sprintf("select 
		UserInfo.Mainkey as Mainkey,
		UserInfo.Name as Name,
		UserInfo.UserIconAddr as UserIconAddr,
		AsText(UserInfo.Location) as ImgInfo_Location 
		from UserInfo 
		inner join PositionSharePower on UserInfo.Mainkey=PositionSharePower.Userkey where PositionSharePower.Targetkey=%d", 
		$userkey);
	$result = querySql($sql);
	return $result;
}
?>