<?php
namespace App\Models;
use DB;
class System_user extends BaseModel {
	protected $table = "system_user";
	public $timestamps = false;

	//获取管理员列表
	function getAdminlist($value = '', $type = 1, $page = 10, $sort = 5, $ads = 'desc', $jointable = '') {
		$key = $this->getFeild($type);
		$sort = is_integer($sort) ? $this->getFeild($sort) : $sort;
		//DB::connection()->enableQueryLog();
//		if ($jointable == 'system_root') {
//			if (is_array($value) && count($value) > 0) {
//				$data = $this->select('system_user.id', 'loginname', 'system_user.root_id', 'system_root.name', 'system_user.status', 'remarks', 'add_time')->join($jointable, 'system_user.root_id', $jointable . '.id')->where($value)->orderby($sort, $ads)->paginate($page);
//			} else {
//				$data = $this->select('system_user.id', 'loginname', 'system_user.root_id', 'system_root.name', 'system_user.status', 'remarks', 'add_time')->join($jointable, 'system_user.root_id', $jointable . '.id')->orderby($sort, $ads)->paginate($page);
//			}
//		}else{
			if (empty($value)) {
				$data = $this->orderby($sort, $ads)->paginate($page);
			} else if (is_array($value)) {
				$data = $this->where($value)->orderby($sort, $ads)->paginate($page);
			} else {
				$data = $this->where($key, $value)->orderby($sort, $ads)->paginate($page);
			}
//		}

		if (!$data) {
			return -7010100102; //没有列表数据
		}
		return $data->toArray();
	}

	//获取管理员信息
	function getAdminInfo($value, $type = 1, $jointable = '', $columnn = '', $columnw = '') {
		$key = $this->getFeild($type);
		if (empty($jointable)) {
			$data = $this->where($key, $value)->first();
		} elseif ($jointable == 'system_root') {
			$data = $this->select('system_user.id', 'loginname', 'system_user.root_id', 'system_root.name', 'system_user.status', 'remarks', 'add_time')->where('system_user.' . $key, $value)->join($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw)->first();
		} else {
			$data = $this->where($key, $value)->join($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw)->first();
		}

		if (!$data) {
			return -7010100202; //没有管理员信息
		}
		return $data->toArray();
	}

	protected function checkSuperAdmin($admin_id){

	}

	//字段对应值
	private function getFeild($num) {
		$data = array(
			'1' => 'id',
			'2' => 'loginname',
			'3' => 'root_id',
			'4' => 'status',
			'5' => 'add_time',
		);
		return $data[$num];
	}
	//更新
	function updateInfo($data, $id) {
		$res = $this->where('id', $id)->update($data);
		if (!$res) {
			return -7010101202; //更新失败
		}
		return 1;
	}

	/**
	 *登录验证
	 * [adminUser description]
	 * @param  [type] $account [description]
	 * @param  [type] $pwd     [description]
	 * @return [type]          [description]
	 */
	function adminLogin($account, $password, $checktwo = false) {
		$account = strtolower($account);
		if (empty($account) || empty($password)) {
			return -400844; //密码或用户名不能为空
		}
		if (!$user = $this->where(['loginname' => $account, 'status' => 1])->first()) {
			return -400845; //用户不存在
		}

		$yecp = ($checktwo == true) ? $user->encryption_2 : $user->encryption;
		$ypwd = ($checktwo == true) ? $user->password_2 : $user->password;

		$newpasswd = md5(md5($yecp . $password));
		if ($newpasswd == $ypwd) {
			return array('admin_name' => $user->loginname, 'admin_id' => $user->id,'passwd'=>$newpasswd);
		}
		return -400846; //登录成功
	}

	/**
	 * 检查admin信息是否存在
	 */
	protected function checkAdmin($admin){
		if(!is_array($admin)){
			return 1;
		}
		$re=$this->where('loginname',$admin['admin_name'])->where('password',$admin['passwd'])->first();
		if(!$re){
			return -400878;
		}
		return 1;
	}

	/**
	 * 检查同一管理员信息是否存在
	 */
	protected function checkAdminname($name){
		$name = trim($name);
		$re=$this->where('loginname',$name)->first();
		if(!$re){
			return 1;
		}
		return -7090100102;
	}

	function addAdmin($data) {
		$res = $this->insert($data);
		if (!$res) {
			return -7050001022; //添加失败
		}
		return 1;
	}

	//开关
	function closeGame($data, $loginname) {
		$res = $this->where('loginname', $loginname)->update($data);
		if (!$res) {
			return -5040000122;
		}
		return $res;
	}

	// /**
	//  * 检查登录密码
	//  *
	//  * @param $user
	//  * @param $password
	//  * @return bool
	//  */
	// public function checkPassword($user, $password) {
	// 	$accountPassword = $this->where(['id' => $user->id, 'status' => 1])->first();
	// 	$dataPassword = md5(md5($accountPassword->encryption . $password));
	// 	return $dataPassword == $accountPassword->account_password ? true : false;
	// }
    //权限检测
    protected function hasRoot($code){
        $admin_id=session('adminInfo.admin_id');
        $check=$this->where('id',$admin_id)->where('root_id',0)->first();
        if($check){
            return 1;
        }
        $data=$this->join('dc_priv_role','dc_priv_role.role_id',$this->table.'.root_id')
            ->join('dc_priv_code','dc_priv_role.priv_id','dc_priv_code.id')
            ->where('dc_priv_code.priv_code',$code)
            ->where('system_user.id',$admin_id)
            ->get();
        $data=$data->toArray();
        if(count($data)>0){
            return 1;
        }
        return -7030050022;//没有权限
    }
	
	function checkActMoney($money){
		$admin_id=session('adminInfo.admin_id');
		$sql = "SELECT max(settingmoney) as maxmoney FROM system_root WHERE system_root.id in (SELECT role_id FROM dc_role WHERE admin_id={$admin_id})";
		
		$moneydata = DB::select($sql);
		if(empty($moneydata)){
			return -7030060102;	//未获取相应权限，权限错误
		}
		$limitmoney = current($moneydata)->maxmoney;
		
		if($money>$limitmoney){
			return -7030060002;	//可操作金额超出限额
		}
		return 1;
	}
	//获取权限名称
    function getAdminInfoByName($admin_user){
	    $data=$this->join('system_root',$this->table.'.root_id','system_root.id')
            ->where('loginname',$admin_user)
            ->first();
	    if(!$data){
	        return -3020034722;
        }
        return $data->toArray();
    }
    //更新token
    function refreshToken($admin_id,$token)
    {
        $res=$this->where('id',$admin_id)->update(['token'=>$token]);
        if(!$res){
            return -2050050022;//更新token失败
        }
        return 1;
    }
    //检测token
    function checkToken($admin_id,$token){
//        if(\App\Model\Role::hasRoot('passToken')){
//            return 1;
//        }
        $res=$this->where('id',$admin_id)->where('token',$token)->first();
        if(!$res){
            return -2050050422;//token
        }
        return 1;
    }

    protected function checkOpen($id){
        $res=$this->where('id',$id)->where('root_id',1)->first();
        if(!$res){
            return -20001;
        }
        return 1;
    }
}