<?php

namespace app\api\model;

use think\Model;

/**
 * TOKEN
 */
class UserToken extends Model
{
    /**
     * 表名
     */
    protected $table = 'user_token';
    protected $dateFormat = false;

    /**
     * @return UserToken
     */
    public static function build()
    {
        return new self();
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description
     * @param $token
     * @return array|bool|false|\PDOStatement|string|Model
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function validateToken($token)
    {
        $time = date("Y-m-d H:i:s", time());
        $where = ['access_token'=>$token,'expiry_time'=>['gt',$time]];

        $user = self::build()->alias('t')
            ->join('user u', 't.user_uuid=u.uuid')
            ->where($where)
            ->field('u.*')
            ->find();

        if ($user) {
            $user = $user->toArray();
            unset($user['pay_password']);
            return $user;
        } else {
            return false;
        }
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description
     * @param $uuid
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function storeToken($uuid){

        $where = ["user_uuid" => $uuid];
        $bool = self::build()->where($where)->field('access_token,refresh_token,expiry_time')->find();

        $token['update_time'] = timeToDate(time());
        $token['expiry_time'] = timeToDate(time() + 604800);

        if($bool){
            $token['access_token'] = $bool['access_token'];
            $token['refresh_token'] = $bool['refresh_token'];
            $bool = self::build()->where($where)->update($token);
        }else{
            $token['uuid'] = uuid();
            $token['access_token'] = token($uuid);
            $token['refresh_token'] = token($uuid);
            $token['user_uuid'] = $uuid;
            $token['create_time'] = timeToDate(time());
            $bool = self::build()->insert($token);
        }
        return $bool?$token:false;
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description
     * @param $requestData
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function refreshToken($requestData)
    {
        $where = ["refresh_token" => $requestData['refresh_token']];
        $info = self::build()->where($where)
            ->field(['user_uuid','access_token','refresh_token','expiry_time'])->find();

        if ($info) {
            $token['update_time'] = timeToDate(time());
            $token['expiry_time'] = timeToDate(time() + 604800);
            $token['access_token'] = token($info['user_uuid']);
            $token['refresh_token'] = token($info['user_uuid']);
            $bool = self::build()->where($where)->update($token);
            $token['user_uuid'] = $info['user_uuid'];
            return $bool?$token:false;
        }

        return false;
    }
}