<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class Friends extends Model
{
	public static function single($className = __CLASS__)
	{
		if (is_null(self::$_self[$className]))
		{
			self::$_self[$className] = new $className;
		}
		
		return self::$_self[$className];
	}
    
    public static function table()
    {
        return 'ig_friend';
    }

    public function create($uid, $fuid)
    {
        $this->Db()->insert(self::table())->value(array('uid'=>$uid,'fuid'=>$fuid,'createdate'=>CTIME))->q();
        $this->Db()->insert(self::table())->value(array('uid'=>$fuid,'fuid'=>$uid,'createdate'=>CTIME))->q();
        $this->Db()->update(Memberfields::table())->incrField(array('friends'=>1))->where(array('uid'=>$uid))->q();
        $this->Db()->update(Memberfields::table())->incrField(array('friends'=>1))->where(array('uid'=>$fuid))->q();
    }

    public function delete($uid, $fuid)
    {
        $this->Db()->delete(self::table())->where(array('uid'=>$uid,'fuid'=>$fuid))->q();
        $this->Db()->delete(self::table())->where(array('fuid'=>$uid,'uid'=>$fuid))->q();
        $this->Db()->update(Memberfields::table())->decrField(array('friends'=>1))->where(array('uid'=>$uid))->q();
        $this->Db()->update(Memberfields::table())->decrField(array('friends'=>1))->where(array('uid'=>$fuid))->q();
    }

    public function isFriend($uid, $fuid)
    {
        $check = $this->Db()->getValue('SELECT COUNT(*) FROM '.self::table().' WHERE uid='.$uid.' AND fuid='.$fuid);
        return $check ? true : false;
    }
}