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

class Set_invite_model extends MY_Model {

    public $invite_tab = 'mocuz_invite';
    public $invite_user_tab = 'mocuz_invite_log';
    public function __construct()
    {

        parent::__construct();
        $this->db = $this->load->database('discuz', true);
        $this->load->helper('discuz_helper');
    }
    public function get_invite($limit,$offset){

        foreach($this->invite_where() as $key => $value){
            if($key == 'username'){
                $this->db->like($key, $value);
            }else{
                $this->db->where($key, $value);
            }
            
        }

        $this->db->limit($limit,$offset);
        $this->db->order_by('create_time', 'desc');
        $listinfo = $this->db->get($this->invite_tab)->result_array();
        if($this->db->char_set == 'gbk'){
            return odz_encode($listinfo, $this->db->char_set);
        }
        return $listinfo;
    }

    public function count(){
        
        foreach($this->invite_where() as $key => $value){
            if($key == 'username'){
                $this->db->like($key, $value);
            }else{
                $this->db->where($key, $value);
            }
        }

        return $this->db->get($this->invite_tab)->num_rows();
    }

    public function invite_where($key, $value){
        static $where = array();
        if(!empty($value)){
            $where[$key] = $value;
        }
        return $where;
    }

    public function count_user($uid){

         $this->db->where('invite_uid',$uid);
        return $this->db->get($this->invite_user_tab)->num_rows();
    }

    public function get_invite_user($uid,$limit,$offset){
        $this->db->where('invite_uid',$uid);
        $this->db->limit($limit,$offset);
        $this->db->join($this->invite_tab, 'mocuz_invite_log.uid = mocuz_invite.uid');
        $listinfo = $this->db->get($this->invite_user_tab)->result_array();
        if($this->db->char_set == 'gbk'){
            return odz_encode($listinfo, $this->db->char_set);
        }
        return $listinfo;
        
    }

    public function get_invite_One($id)
    {
        $this->db->where('id',$id);
        $listinfo = $this->db->get($this->table)->row_array();
        if($this->db->char_set == 'gbk'){
            return odz_encode($listinfo, $this->db->char_set);
        }
        return $listinfo;
    }
}