package dao

import (
	"gorm.io/gorm"
)

type File struct {
	BaseModel
	UUID        string `gorm:"unique;type varchar(32);comment:'文件UUID'"`
	UserId      int32  `gorm:"comment:'上传者ID'"`
	FileName    string `gorm:"type:varchar(255);comment:'文件名称'"`
	FileSize    uint32 `gorm:"comment:'文件大小'"`
	FileType    string `gorm:"comment:'文件类型，对应html中支持的Content-Type类型'"`
	FileContent []byte `gorm:"type:mediumblob;comment:'文件内容，目前最大为16M'"`
}

func File_Insert(data *File) (uint64, error) {
	if err := s.db.Create(data).Error; err != nil {
		return 0, err
	}
	return uint64(data.ID), nil
}

func File_Query(uuid string) (*File, error) {
	var data File
	if err := s.db.Where("uuid = ?", uuid).First(&data).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		return nil, err
	}
	return &data, nil
}

func File_Delete(uuid string) error {
	return s.db.Where("uuid = ?", uuid).Delete(&File{}).Error
}
