<?php defined('SYSPATH') or die('No direct script access.');

class Auth_User_Model extends Model {

	function __construct(){

		$this->db = new Database();

	}

	public function userlist($member=false,$ture=0){
		$this->db->join('user_group', 'user_group.group_id=','user.user_role');
		if($member){
			$this->db->where('user_group.group_name', $member);
		}
		if($ture){
			$this->db->where('user.salesmanid', 0);
		}
		return $this->db->get('user');

	}
	
	public function getlist($member=false,$ture=0){
		$this->db->join('user_group', 'user_group.group_id=','user.user_role');
		if($member){
			$this->db->where('user_group.group_name', $member);
		}
		if($ture){
			$this->db->where('user.salesmanid', 0);
		}
		return $this->db->get('user');

	}
	
	public function usercount($search=false,$member=false,$ture=0,$page_no=false,$showlines=false){
		$this->db->join('user_group', 'user_group.group_id=','user.user_role',"LEFT");
		//$this->db->join('salesman', 'salesman.id','user.salesmanid',"LEFT");
		if($member){
			$this->db->where('user_group.group_name', $member);
		}
		if($ture){
			if(is_array($ture)){
			$this->db->in('user.salesmanid',$ture);
			}else{
			$this->db->where('user.salesmanid',$ture);
			}
		}
		if($search['user_name']){
			$this->db->like('user.user_name', $search['user_name']);
		}
		if($search['user_company']){
			$this->db->like('user.company_name', $search['user_company']);
		}
		
		if($page_no){
				$this->db->limit($showlines,($page_no-1)*$showlines);
		}
		return $this->db->get('user');

	}
	
	function useroperation($id){
		$this->db->join('user_group', 'user_group.group_id=','user.user_role',"LEFT");
		$this->db->join('salesman', 'salesman.id','user.salesmanid',"LEFT");
		$this->db->where('user.user_id', $id);
		return $this->db->get('user');
	}

	public function getuser(){

		$this->db->join('user_group', 'user_group.group_id=','user.user_role');
		$this->db->where('user_group.group_name', 'employee');
		return $this->db->get('user');

	}

	public function getone($id=0){
		$this->db->where('user_id', $id);
		return $this->db->get('user');

	}

	function getcheck($name=0){
		$this->db->where('user_name', $name);
		return $this->db->get('user');
	}

	function getroles($id=0){
		$this->db->where('menu_id', $id);
		$arr=$this->db->get('menu_group')->result_array(false);
		foreach($arr as $arr ){
			$array[]=$arr["group_id"];
		}
		return @$array;
	}

	function add($data=0){
		return   $inster=$this->db->insert('user', $data);
		/*  $id=$inster->insert_id();

		foreach($roles as $role){

		$this->db->insert('menu_group', array("menu_id"=>$id,"group_id"=>$role));
		}*/

	}

	function delete($id_list=0){
		//业务系统
		//$this->db->from('salesman')->set(array('userid' => 'null'))->where(array('userid' =>$id_list['user_id']))->update();
//		if($this->db->getwhere('salesman',array('userid'=>$id_list['user_id']))->result_array()){
//			$this->db->where('userid', $id_list['user_id']);
//			$this->db->update('salesman', array('userid'=>""));
//		}
		return $this->db->delete('user',$id_list);
	}

	function review($id=0){
		$this->db->where('user_id', $id);
		return  $this->db->update('user', array('user_status'=>1));
	}

	function edit($data=0){
		$this->db->where('user_id', $data['user_id']);
		return  $this->db->update('user', $data);

	}
	function distribute($select_id=0,$sid=0){
		$this->db->where('user_id', $select_id['user_id']);
		return  $this->db->update('user', array('salesmanid'=>$sid));

	}
	function updateUserPwd($pwd,$userid){
		return $this->db->where("user_id",$userid)->update("user",array("user_pwd"=>$pwd));
	}
	function validOldPwd($pwd,$userid){
		return $this->db->where(array("user_id"=>$userid,"user_pwd"=>$pwd))->get("user")->count();
	}
} // End Auth Role Model