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

/**
 * 获取用户关注状态
 */
function getUserAttentionState($userkey, $targetuserkey)
{
	$res = 0;
	$sql = sprintf("select count(*) as num from Attention where Userkey=%d and TargetUserkey=%d", $userkey, $targetuserkey);
	$result = querySql($sql);
	$row = mysql_fetch_array($result);
	if($row)
	{
		$res = $row['num'];
	}
	else
	{
		$res = 0;
	}
	freeResult($result);
	return $res;
}

/**
 * 获取好友状态
 */
function getFriendState($userkey, $targetuserkey)
{
	$res = 0;
	$sql = sprintf("select count(*) as num from Friend where Userkey=%d and Friendkey=%d", $userkey, $targetuserkey);
	$result = querySql($sql);
	$row = mysql_fetch_array($result);
	if($row)
	{
		$res = $row['num'];
	}
	else
	{
		$res = 0;
	}
	freeResult($result);
	return $res;
}

/**
 * 添加好友
 */
function addFriend($userkey, $targetuserkey)
{
	$sql = sprintf("insert into Friend(Userkey,Friendkey,CreateDate) values(%d,%d,'%s')", $userkey, $targetuserkey, date('y-m-d G:i:s'));
	executeSql($sql);
}

/**
 * 双方添加好友
 */
function addBathFriend($userkey, $targetuserkey)
{
	if(getFriendState($userkey, $targetuserkey)<=0)
	{
		addFriend($userkey, $targetuserkey);
	}
	
	if(getFriendState($targetuserkey, $userkey)<=0)
	{
		addFriend($targetuserkey, $userkey);
	}
}

function delFriend($userkey, $targetuserkey)
{
	$sql = sprintf("delete from Friend where Userkey=%d and Friendkey=%d", $userkey, $targetuserkey);
	executeSql($sql);
}

?>