<?php

namespace app\model;

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

/**
 * 系统文件模型
 */
class SysFileRecord extends BaseModel
{
    protected $name = "sys_file_record";
    protected $pk = "id";

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

    public function searchNameAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('name', 'like', '%' . $value . '%');
    }

    public function searchPathAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('path', 'like', '%' . $value . '%');
    }

    public function searchCreateNicknameAttr(Query $query, string $value): void
    {
        $value != "" && $query->whereIn('create_user_id', SysUser::where('username', $value)->column('user_id'));
    }

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


}