<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Follow_model extends MY_Model {

    public $table = 'follow';
    protected $insert_filed = array(
        'uid', 'fuid', 'fusername', 'createtime'
    );
    public function __construct()
    {
        parent::__construct();
    }

    public function insert($data)
    {
        return parent::insert($data);
    }

    public function replace($data)
    {
        return parent::replace($data);
    }

    public function follow($uid, $fuid, $fusername) {
        $is_followed = null;
        $is_followed = $this->is_followed($uid, $fuid);
        if ($is_followed) {
            $this->cancel_follow($uid, $fuid);
        } else {
            $this->add_follow($uid, $fuid, $fusername);
        }
        return !$is_followed;
    }

    //加关注
    public function add_follow($uid, $fuid, $fusername) {
        $data = array(
            'uid' => $uid,
            'fuid' => $fuid,
            'fusername' => $fusername,
            'createtime' => time()
        );
        $this->insert($data);
    }

    //取消关注
    public function cancel_follow($uid, $fuid) {
        $this->db->where('uid', $uid);
        $this->db->where('fuid', $fuid);
        $this->db->delete($this->table);
        return true;
    }

    public function is_followed($uid, $fuid)
    {
        $ret = array();
        $this->db->where('uid', $uid);
        $this->db->where('fuid', $fuid);
        $ret = $this->db->get($this->table)->row_array();
        if (empty($ret)) {
            return false;
        } else {
            return true;
        }
    }



}