<?php
namespace App\Models;

class System_root extends BaseModel
{
    protected $table = "system_root";
    public $timestamps = false;

    //获取管理员权限列表
    public function getRootlist($value = '', $type = 1, $limit = 10, $sort = 1, $ads = 'asc')
    {
        $key = $this->getFeild($type);
        $sort = is_integer($sort) ? $this->getFeild($sort) : $sort;
        $data = $this->orderby($sort, $ads);
        if (!(empty($value)) && is_array($value)) {
            $data = $data->where($value);
        } else if (!(empty($value))) {
            $data = $this->where($key, $value);
        }
        $data = $data->paginate($limit);

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

    //获取权限信息
    public function getRootDetails($value, $type = 1, $jointable = '', $columnn = '', $columnw = '')
    {
        $key = $this->getFeild($type);
        if (empty($jointable)) {
            $data = $this->where($key, $value)->first();
        } else {
            $data = $this->where($key, $value)->join($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw)->first();
        }
        ;
        if (!$data) {
            return -7010100202; //没有权限信息
        }
        return $data->toArray();
    }

    //字段对应值
    private function getFeild($num)
    {
        $data = array(
            '1' => 'id',
            '2' => 'level',
            '3' => 'name',
            '4' => 'settingmoney',
        );
        return $data[$num];
    }
    //获取角色信息
    public function getInfo($value, $type)
    {
        $key = $this->getFeild($type);
        $data = $this->where($key, $value)->first();
        if (!$data) {
            return -7010100202; //没有权限信息
        }
        return $data->toArray();
    }
    public function getlist($list = 10)
    {
        $data = $this->orderBy('settingmoney', 'desc')->paginate($list);
        if (!$data) {
            return -7050000204;
        }
        return $data->toArray();
    }

    //修改权限信息
    public function editRoot($id, $data)
    {
        $res = $this->where('id', $id)->update($data);
        if (!$res) {
            return -7010100302;
        }
        return 1;
    }
    //所有角色
    public function getAllRole()
    {
        $data = $this->get();
        return $data->toArray();
    }
    //检测是否有等级为0的角色
    public function checkSupper($ids)
    {
        $data = $this->whereIn('id', $ids)->where('level', 0)->get();
        if (!$data) {
            return -1234455;
        }
        $data = $data->toArray();
        if (empty($data)) {
            return -1234455;
        }
        return 1;
    }
    /**
     * 得到超管的角色id
     */
    protected function getSupperRole()
    {
        $role = S('ADMIN_ROLE_ID');
        if ($role != -1) {
			return $role;
        }
        $re = $this->where('level', 0)->first();
        if (!$re) {
            return -1234466; //no supper role
        }
        S('ADMIN_ROLE_ID', $re->id);
        return $re->id;
    }

}
