<?php

namespace app\model;

use app\BaseModel;
use think\model\concern\SoftDelete;
use think\model\relation\HasOne;

/***
 * 系统用户文件
 * @property int $id
 * @property int $user_id
 * @property string $name
 */
class SysUserFile extends BaseModel
{
    use SoftDelete;

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

    public function getThumbnailAttr($thumbnail, $row): string
    {
        return $row['is_directory'] ? '' : (string)$row['url'];
    }

    public function getDownloadUrlAttr($downloadUrl, $row): string
    {
        return $row['is_directory'] ? '' : (string)$row['url'];
    }

    public function getUrlAttr($url, $row): string
    {
        return $row['is_directory'] ? '' : (string)$row['url'];
    }

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

    public function searchNameAttr($query, $value): void
    {
        $query->whereLike('name', '%' . $value . '%');
    }

    public function searchContentTypeAttr($query, $value): void
    {
        $query->whereLike('content_type', $value . '%');
    }

    public function searchParentIdAttr($query, $value): void
    {
        $query->where('parent_id', $value);
    }

    public function searchUserIdAttr($query, $value): void
    {
        $query->where('user_id', $value);
    }

}