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

class User_model extends MY_Model {

    public $table = 'user';
    public $key = 'uid';


    public function __construct()
    {
        parent::__construct();
    }

    public function getUserFromDiscuz($uid){
        $discuz_db = $this->load->database('discuz', true);
        $this->load->helper('discuz_helper');

        $user = $discuz_db->get_where('common_member', array('uid' => $uid))->row_array();       
         
        if($discuz_db->char_set == 'gbk'){
            $user = odz_encode($user, $discuz_db->char_set);
        }
        return $user;
    }

    //获取用户性别
    public function get_user_gender_from_discuz($uid) {
        $discuz_db = $this->load->database('discuz', true);
        $user = $discuz_db->get_where('common_member_profile', array('uid' => $uid))->row_array();
        return $user;
    }

    //获取用户签名
    public function get_user_signature_from_discuz($uids) {
        $discuz_db = $this->load->database('discuz', true);
        $this->load->helper('discuz_helper');
        $discuz_db->select('uid, sightml');
        $discuz_db->where_in('uid',$uids);
        $user = $discuz_db->get('common_member_field_forum')->result_array();

        if($discuz_db->char_set == 'gbk'){
            $user = odz_encode($user, $discuz_db->char_set);
        }
        return $user;
    }

    public function get_usergroup_from_discuz() {
        $discuz_db = $this->load->database('discuz', true);
        $usergroup=$discuz_db->get('common_usergroup')->result_array();
        if($discuz_db->char_set == 'gbk'){
            $usergroup = odz_encode($usergroup, $discuz_db->char_set);
        }
        return $usergroup;
    }

    public function get_devicetoken($to_uid) {
        $discuz_db = $this->load->database('discuz', true);
        $discuz_db->select('devicetoken, platform');
        $discuz_db->where('uid', $to_uid);
        $discuz_db->where('status', 1);
        return $discuz_db->get('mocuz_member_device')->result_array();
    }

    public function getScoreFromDiscuz($uid){
        $discuz_db = $this->load->database('discuz', true);

        $score = $discuz_db->get_where('common_member_count', array('uid' => $uid))->row_array();
        return $score;
    }

    public function getByUid($uid){
        $this->db->where('uid', $uid);

        return $this->db->get($this->table)->row_array();
    }

    /**
     * ��ȡ��Ƭ
     * @return [type] [description]
     */
    public function getUserCardInfo($where){
        $query = $this->db->get_where($this->table,$where);
        return $query->result_array();
    }

    public function saveCardInfo($getdata){
        extract($getdata);
        $data = array(
            'uid' => $uid,
            'username' => $username,
            'realname' => $realname,
            'sex' =>$sex,
            'age' =>$age,
            'tel' =>$tel,
            'buys' =>0,
            'status' =>1,//��Ƭ����
            'level' =>1,//��ʼ����ɫ ��ͨȨ��
            'type' => 0,//����ʽ
            'createtime' =>time(),//���ۿ���ͨʱ��
            'updatetime' =>time()
        );
        return $this->db->insert($this->table, $data);
    }

    public function checkCardInfo($tbname,$username)
    {
        $query = $this->db->get_where($tbname,array('username'=>$username));
        if($query->num_rows() > 0 )
        {
            return FALSE;
        }
    }


    public function updateByUid($data, $uid){
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }

    public function getUsersFromDiscuz($uids=array()){
        $discuz_db = $this->load->database('discuz', true);
        $this->load->helper('discuz_helper');
        $discuz_db->where_in('uid',$uids);
        $user = $discuz_db->get('common_member')->result_array();       
         
        if($discuz_db->char_set == 'gbk'){
            $user = odz_encode($user, $discuz_db->char_set);
        }
        return $user;
    }

    public function get_users_gender_from_discuz($uids=array()) {
        $discuz_db = $this->load->database('discuz', true);
        $discuz_db->where_in('uid',$uids);
        $user = $discuz_db->get('common_member_profile')->result_array();
        return $user;
    } 

}