<?php
/**
 * 用户模型
 */
class User_model extends CI_Model {

    private $collection_name = 'users';

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

    public function set_collection_name($collection_name){
	    $this->collection_name = $collection_name;
	    return $this;
    }

    /**
     * 用户登录
     */
    public function get_user_with_username_password($username, $password){
		return $this->mongo_db->where_or(array("username"=> $username, "mobile"=> $username , "email"=> $username))->where(array("password"=> $password))->get($this->collection_name);
    }

    /**
     * 判断是否存在
     * @param $field
     * @param $val
     * @return bool
     */
    public function is_exists($field,$val,$old_val = NULL){
        if($old_val){
            $users = $this->mongo_db->set_wheres(array('$and'=>array(array($field=>array('$ne'=>$old_val)),array($field=>$val))))->get($this->collection_name);
        }else {
            $users = $this->mongo_db->where(array($field => $val))->get($this->collection_name);
        }
        if($users && count($users)>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 列表用户
     */
    public function list_user($limit,$offset,$like = NULL,$where = NULL,$order = NULL){
        if(!$order){
            $order = array("create_time"=>"ASC");
        }
        if($like && $where){
            $where_or = array('username'=>$this->mongo_db->create_like($like),'name'=>$this->mongo_db->create_like($like),'mobile'=>$this->mongo_db->create_like($like),'email'=>$this->mongo_db->create_like($like));
            return  $this->mongo_db->where_or($where_or)->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }else if($like && !$where){
            $where_or = array('username'=>$this->mongo_db->create_like($like),'name'=>$this->mongo_db->create_like($like),'mobile'=>$this->mongo_db->create_like($like),'email'=>$this->mongo_db->create_like($like));
            return  $this->mongo_db->where_or($where_or)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }else if(!$like && $where){
            return  $this->mongo_db->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }else{
            return  $this->mongo_db->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }
    }

    /**
     * 批量更新多个字段或者单个字段
     * @param $field
     * @param $wheres
     * @param null $value
     * @return mixed
     */
    public function set_val($field,$wheres,$value = NULL){
        if(is_array($field)){
            return $this->mongo_db->set_wheres($wheres)->set($field)->update_all($this->collection_name);
        }else {
            return $this->mongo_db->set_wheres($wheres)->set($field, $value)->update_all($this->collection_name);
        }
    }

    /**
     * 用户选择列表
     */
    public function select_users($user_type,$banch_id=null){
        $order = array("create_time"=>"ASC");
        if($banch_id != null){
            return  $this->mongo_db->order_by($order)->where(array("user_type"=>$user_type,"status"=>"10","branch.branch_id"=>$banch_id))->get($this->collection_name);
        }else{
            return  $this->mongo_db->order_by($order)->where(array("user_type"=>$user_type,"status"=>"10"))->get($this->collection_name);
        }
    }

    /**
     * 获取用户信息
     */
    public function get_user_with_user_id($user_id){
        return $this->mongo_db->where(array("user_id"=> $user_id))->find_one($this->collection_name);
    }

    public function get_user_with_code($code){
        return $this->mongo_db->where(array("code"=> $code))->find_one($this->collection_name);
    }

    /**
     * 获取数据条数
     */
    public function count_user($like = NULL,$where = NULL){
        if($like && $where){
            $where_or = array('username'=>$this->mongo_db->create_like($like),'name'=>$this->mongo_db->create_like($like),'mobile'=>$this->mongo_db->create_like($like),'email'=>$this->mongo_db->create_like($like));
            return $this->mongo_db->where_or($where_or)->where($where)->count($this->collection_name);
        }else if($like && !$where){
            $where_or = array('username'=>$this->mongo_db->create_like($like),'name'=>$this->mongo_db->create_like($like),'mobile'=>$this->mongo_db->create_like($like),'email'=>$this->mongo_db->create_like($like));
            return $this->mongo_db->where_or($where_or)->count($this->collection_name);
        }else if(!$like && $where){
            return $this->mongo_db->where($where)->count($this->collection_name);
        }else{
            return $this->mongo_db->count($this->collection_name);
        }
    }
    
    /**
     *新增用户 
     */
    public function insert_user($user_data){
		return $this->mongo_db->insert($this->collection_name, $user_data);
    }
    
    /**
     *更新用户 
     */
    public function update_user($user_data){
        $data['filter'] = array("user_id"=>$user_data['user_id']);
        $data['update'] = $user_data;
        return $this->mongo_db->update($this->collection_name,$data);
    }

    /**
     * 删除用户
     */
    public function delete_user($user_id){
        return $this->mongo_db->where(array("user_id"=>$user_id))->delete($this->collection_name);
    }

    /**
     * 删除多个用户
     */
    public function delete_all_user($user_ids){
        return $this->mongo_db->where_in("user_id",$user_ids)->delete($this->collection_name,true);
    }
}