<?php

namespace app\model;


use app\BaseModel;
use think\db\Query;
use think\model\relation\HasOne;

class SysLoginRecord extends BaseModel
{

    protected $name = "sys_login_record";
    protected $pk = "id";


    public function user(): HasOne
    {
        return $this->hasOne(SysUser::class, 'username', 'username')->field('user_id,username,nickname');
    }

    public function searchUsernameAttr(Query $query, string $value, array $data): void
    {
        $value != '' && $query->where('username', $value);
    }

    public function searchLoginTypeAttr(Query $query, int $value, array $data): void
    {
        $value > 0 && $query->where('login_type', $value);
    }

    public function searchNicknameAttr(Query $query, string $value, array $data): void
    {
        $value != '' && $query->whereIn('username', SysUser::where('username', 'like', "%{$value}%")->column('username'));
    }

    public function searchCreateTimeAttr(Query $query, array $value, array $data): void
    {
        $value = array_filter($value);
        count($value) >= 2 && $query->whereBetweenTime('create_time', $value[0], $value[1]);
    }

}