<?php

namespace addons\exam\model;

use addons\exam\enum\UserStatus;
use app\common\library\Auth;
use fast\Random;

class UserModel extends BaseModel
{
    // 表名
    protected $name = 'user';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // /**
    //  * 登录并返回token
    //  * @param $username
    //  * @param $password
    //  * @param $user_type
    //  * @return string
    //  */
    // public static function login($username, $password, $user_type)
    // {
    //     $username = "{$user_type}-$username";
    //     if (!$user = self::where('username', $username)->find()) {
    //         api_fail('登录账号或密码错误');
    //     }
    //     if ($user['password'] != Auth::instance()->getEncryptPassword($password, $user['salt'])) {
    //         api_fail('登录账号或密码错误');
    //     }
    //
    //     Auth::instance()->direct($user['id']);
    //     return Auth::instance()->getToken();
    // }

    /**
     * 当前登录用户信息
     * @return Auth|null
     */
    public static function info()
    {
        if (Auth::instance()->isLogin()) {
            return Auth::instance();
        }
        return null;
    }

    /**
     * 快速注册用户
     * @param string $username
     * @param string $nickname
     * @param string $avatar
     * @param int $gender
     * @param string $password
     * @param string $mobile
     * @return UserModel
     */
    public static function fastRegister(string $username, string $nickname = '', string $avatar = '', int $gender = 0, string $password = '', string $mobile = '')
    {
        if (self::where('username', $username)->count()) {
            fail('该账号已被注册');
        }
        // 不严格要求可以去除
        // if ($mobile && self::where('mobile', $mobile)->count()) {
        //     fail('该手机号码已被绑定');
        // }

        $salt = Random::alnum();
        return self::create([
            'username'  => $username,
            'mobile'    => $mobile,
            'email'     => $mobile . '@qq.com',
            'salt'      => $salt,
            'password'  => Auth::instance()->getEncryptPassword($password ?: $username, $salt),
            'nickname'  => $nickname,
            'avatar'    => $avatar,
            'gender'    => $gender,
            'status'    => UserStatus::NORMAL,
            'logintime' => time(),
        ]);
    }

    /**
     * 是否必须绑定手机号
     * @param $user
     * @return void
     */
    public static function isMustBindMobile($user)
    {
        $system_config = getConfig('system_config');
        $bind_mobile   = $system_config['bind_mobile'] ?? 0;
        if ($bind_mobile == 2 && (!isset($user['mobile']) || !$user['mobile'])) {
            fail('请先绑定手机号');
        }
    }

    /************************** 关联关系 **************************/

    public function agent()
    {
        return $this->hasOne(\app\admin\model\juhepay\AgentModel::class, 'user_id', 'id');
    }

    public function shop()
    {
        return $this->hasOne(\app\admin\model\juhepay\ShopModel::class, 'user_id', 'id');
    }
}
