<?php namespace App\Models;

use CodeIgniter\Model;

class SignModel extends Model
{
    protected $DBGroup = 'default';                               //默认连接的数据库
    protected $table      = 'users';                              //指定模型要使用的数据库表
    protected $returnType     = 'array';

    protected $allowedFields = [                                 //允许更改，写入数据库的字段
        "username",
        "password",
        "nick",
        "status",
        "role",
        "create_time",
        "upate_time",
        "login_time",
        "register_time",
        "token"
    ];



    /****************************************************
     *       注册一个新用户
     /****************************************************/
     public function register($data){
        $sql = 'insert into users (username,nick,password,status,role,register_time) values ("'.$data["username"].'","'
        .$data["nick"].'","'
        .$data["password"].'","'
        .$data["status"].'","'
        .$data["role"].'","'
        .$data["register_time"].'")';
        $insertObj = $this->db->query($sql);
     }

     /****************************************************
     *       根据用户名，获取数据库记录
     /****************************************************/
     public function getItemByUsername($username){
        $sql = 'select * from users where username="'.$username.'"';
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
     }

     /****************************************************
     *       写入用户token
     /****************************************************/
     public function writeIntoToken($data){
         $sql = 'update users set token="'.$data["token"].'",login_time="'.$data["login_time"].'" where username="'.$data["username"].'"'; 
         $obj = $this->db->query($sql);
     }

     /****************************************************
     *       清除token
     /****************************************************/
     public function clearToken($data){
        $sql = 'update users set token="" where username="'.$data["username"].'"'; 
        $obj = $this->db->query($sql);
    }


}