<?php

namespace app\model;

/**
 * @desc 文件表
 * @property int id
 * @property int type
 * @property string path
 * @property string create_time
 */
class FileModel extends BaseModel
{
    protected $name = 'file';
    protected $pk = 'id';


    /**
     * @desc 根据主键获取
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getById($id)
    {
        $self = self::where('id', $id)->find();
        if(empty($self)){
            return [];
        }
        return $self->toArray();
    }

    /**
     * @desc 获取文件路径
     * @param $id
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getFilePath($id)
    {
        $self = self::where('id', $id)->find();
        if(!empty($self)){
            return $self->path;
        }

        return '';
    }
    /**
     * @desc 将文件入库
     * @param $path
     * @param int $type
     * @return int
     */
    public static function insertFile($path, int $type = 1)
    {
        $self = new self;
        $self->type = $type;
        $self->path = $path;
        $self->create_time = date('Y-m-d H:i:s');
        $self->save();
        return $self->id;
    }
}