<?php

namespace user;

class account
{
    /**
     * 用户登录鉴权
     *
     * @param string $username
     * @param string $password
     * @return array|string[]
     */
    public function login($username,$password){
        global $DB;
        if(is_eid($username)){
            $userinfo=$DB->getRow("SELECT * FROM pre_user_base WHERE eid=:eid LIMIT 1", [':eid'=>$username]);
        }elseif(is_email($username)){
            $userinfo=$DB->getRow("SELECT * FROM pre_user_base WHERE email=:email LIMIT 1", [':email'=>$username]);
        }elseif(is_mobile($username)){
            $userinfo=$DB->getRow("SELECT * FROM pre_user_base WHERE callnumber=:callnumber LIMIT 1", [':callnumber'=>$username]);
        }else{
            $userinfo=$DB->getRow("SELECT * FROM pre_user_base WHERE username=:username LIMIT 1", [':username'=>$username]);
        }
        if(empty($userinfo))return array('uid'=>'-1','msg'=>'用户不存在');
        if($password!=$userinfo['password'])return array('uid'=>'-2','msg'=>'用户名密码错误');
        $adminer=0;
        if($userinfo['status']==0){
            return array('uid'=>'-3','msg'=>'用户状态不正常');
        }elseif($userinfo['status']==2){
            $adminer=1;
        }
        return array('uid'=>$userinfo['uid'],'adminer'=>$adminer);
    }

    /**
     * 用户信息调取
     *
     * @param $uid
     * @return array|string[]
     */
    public function info($uid){
        global $DB;
        $userinfo=$DB->getRow("SELECT * FROM pre_user_base WHERE uid=:uid LIMIT 1", [':uid'=>$uid]);
        $userinfo2=$DB->getRow("SELECT * FROM pre_user_info WHERE uid=:uid LIMIT 1", [':uid'=>$uid]);
        if(empty($userinfo)||empty($userinfo2))return array('err'=>'-1','msg'=>'用户不存在');
        return array(
            'uid'       =>  $userinfo['uid'],
            'username'  =>  $userinfo['userinfo'],
            'email'     =>  $userinfo['email'],
            'phone'     =>  $userinfo['callnumber'],
            'eid'       =>  $userinfo['eid'],
            'name'      =>  $userinfo2['name'],
            'gender'    =>  $this->gander($userinfo2['gender']),
            'alias'     =>  $userinfo2['alias'],
            'address'   =>  $userinfo2['address'],
            'post'      =>  $userinfo2['post'],
            'dep'       =>  $this->depcha($userinfo2['dep']),
        );
    }

    /**
     * 根据EID调取用户信息
     * @param $eid
     * @return array|false
     */
    public function info_to_eid($eid){
        global $medoo;
        $userbase=$medoo->select("user_base","*",[
            "eid"=>$eid
        ]);
        return $userbase[0];
    }

    /**
     * 基础信息注册
     * @param $username
     * @param $password
     * @param $email
     * @param $phone
     * @param $eid
     * @return array|false|mixed|string
     */
    public function basereg($username,$password,$email,$phone,$eid){
        global $medoo;
        return $medoo->insert("user_base", [
            "username"  => $username,
            "password"  => $password,
            "email"     => $email,
            "callnumber"=> $phone,
            "eid"       => $eid,
            "status"    => '1'
        ]);
    }

    /**
     * 扩展信息注册
     * @param $uid
     * @param $name
     * @param $gender
     * @param $alias
     * @param $address
     * @param $post
     * @param $dep
     * @return array|false|mixed|string
     */
    public function inforeg($uid,$name,$gender,$alias,$address,$post,$dep=1){
        global $medoo;
        return $medoo->insert("user_info", [
            'uid'=>$uid,
            'name'=>$name,
            'gender'=>$gender,
            'alias'=>$alias,
            'address'=>$address,
            'post'=>$post,
            'dep'=>$dep
        ]);
    }

    /**
     * 删除用户
     * @param $uid
     * @return int|void
     */
    public function deluser($uid){
        global $medoo;
        if($medoo->delete("user_base", ['uid'=>$uid])!=0 && $medoo->delete("user_info", ['uid'=>$uid])!=0){
          return 1;
        }
    }

    /**
     * 检查用户注册表的内容
     * @param $check
     * @param $string
     * @return bool
     */
    public function check($check,$string){
        global $medoo;
        $datas = $medoo->select("user_base",'*', [$check => $string]);
        if(empty($datas)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 私有函数：性别转换
     * @param $gander
     * @return string
     */
    private function gander($gander){
        if($gander==1){
            return '男';
        }elseif($gander==2){
            return '女';
        }else{
            return "其他：".$gander;
        }
    }

    /**
     * 私有函数：部门名称转换
     * @param $did
     * @return mixed
     */
    private function depcha($did){
        global $DB;
        $depinfo=$DB->getRow("SELECT * FROM oid_dep_info WHERE did=:did LIMIT 1", [':did'=>$did]);
        return $depinfo['name'];
    }

}