<?php
namespace app\common\model;
use think\Model;
class Admin extends Model
{
    /**
     * 管理员列表
     */
    public function getAdminList($condition = array(), $field = '*', $page = 0, $order = 'admin_id desc')
    {
        if ($page) {
            $member_list = db('admin')->alias('a')->join('__ADMINGROUP__ g', 'g.group_id = a.admin_group_id', 'LEFT')->where($condition)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $member_list;
            return $member_list->items();
        } else {
            return db('admin')->alias('a')->join('__ADMINGROUP__ g', 'g.group_id = a.admin_group_id', 'LEFT')->where($condition)->order($order)->select();
        }
    }

    /**
     * 新增管理员
     */
    public function addAdmin($data)
    {
        return db('admin')->insertGetId($data);
    }

    /**
     * 编辑管理员
     */
    public function editAdmin($condition, $data)
    {
        return db('admin')->where($condition)->update($data);
    }

    /**
     * 删除管理员
     */
    public function delAdmin($condition)
    {
        return db('admin')->where($condition)->delete();
    }

    /**
     * 取单个管理员
     */
    public function getOneAdmin($condition, $field = '*')
    {
        return db('admin')->field($field)->where($condition)->find();
    }

    //单个组管理员列表
    public function ogroupAdmin($admin_id){
        $map['admin_group_id'] = 2;
        $map['admin_id'] = array('neq',$admin_id);
        return db('admin')->field('admin_name,admin_id')->where($map)->select();
    }

    //所有公司
    public function allgsAdmin(){
        return db('admin')->field('admin_name,admin_id')->where("admin_group_id=2")->select();
    }

    //用户登录查询所属公司(admin_pid:登录人的id)
    public function suosgsAdmin($admin_pid){
        $map['admin_id'] = $admin_pid;
        $gsadmin = db('admin')->field('admin_name,admin_group_id,admin_id,admin_pid')->where($map)->find();
        if($gsadmin['admin_group_id']==2 || $gsadmin['admin_group_id']==1){
            $ogsadmin = $gsadmin;
        }else{
            $where['admin_id']=$gsadmin['admin_pid'];
            $ogsadmin = db('admin')->field('admin_name,admin_group_id,admin_id')->where($where)->find();
        }
        return $ogsadmin;
    }
    
    //公司下的所有客服(admin_pid:公司id)
    public function suosservice($admin_pid){
        $map['admin_group_id'] = 3;
        if($admin_pid==1){
            $allkef = db('admin')->field('admin_name,admin_id')->where($map)->select();
        }else{
            $map['admin_pid'] = $admin_pid;
            $allkef = db('admin')->field('admin_name,admin_id')->where($map)->select();
        }
        return $allkef;
    }

    
}