235 lines
6.8 KiB
Go
235 lines
6.8 KiB
Go
package models
|
||
|
||
import (
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/beego/beego/v2/client/orm"
|
||
)
|
||
|
||
// FileInfo 文件信息模型
|
||
// 对应 yz_files 表结构
|
||
type FileInfo struct {
|
||
ID int64 `orm:"column(id);auto" json:"id"`
|
||
TenantID string `orm:"column(tenant_id);size(64)" json:"tenant_id"`
|
||
|
||
// 用户关联信息(通过JWT认证获取)
|
||
UserID int `orm:"column(user_id);default(0)" json:"user_id"`
|
||
|
||
// 文件基础信息
|
||
FileName string `orm:"column(file_name);size(255)" json:"file_name"`
|
||
OriginalName string `orm:"column(original_name);size(255)" json:"original_name"`
|
||
FilePath string `orm:"column(file_path);size(500)" json:"file_path"`
|
||
FileURL string `orm:"column(file_url);size(500);null" json:"file_url"`
|
||
FileSize int64 `orm:"column(file_size);default(0)" json:"file_size"`
|
||
FileType string `orm:"column(file_type);size(50)" json:"file_type"`
|
||
FileExt string `orm:"column(file_ext);size(20)" json:"file_ext"`
|
||
MD5 string `orm:"column(md5);size(32);null" json:"md5"`
|
||
|
||
// 分类信息
|
||
Category string `orm:"column(category);size(100)" json:"category"`
|
||
SubCategory string `orm:"column(sub_category);size(100);null" json:"sub_category"`
|
||
|
||
// 状态信息
|
||
Status int8 `orm:"column(status);default(1)" json:"status"`
|
||
IsPublic int8 `orm:"column(is_public);default(0)" json:"is_public"`
|
||
|
||
// 上传信息
|
||
UploadBy string `orm:"column(upload_by);size(100)" json:"upload_by"`
|
||
UploadTime time.Time `orm:"column(upload_time);type(datetime);auto_now_add" json:"upload_time"`
|
||
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time,omitempty"`
|
||
|
||
// 关联的用户信息(非数据库字段)
|
||
User *User `orm:"-" json:"user,omitempty"`
|
||
}
|
||
|
||
// TableName 设置表名
|
||
func (f *FileInfo) TableName() string {
|
||
return "yz_files"
|
||
}
|
||
|
||
// CanPreview 判断文件是否可以在线预览
|
||
func (f *FileInfo) CanPreview() bool {
|
||
previewableExts := map[string]bool{
|
||
// 图片格式
|
||
".jpg": true,
|
||
".jpeg": true,
|
||
".png": true,
|
||
".gif": true,
|
||
".bmp": true,
|
||
".webp": true,
|
||
".svg": true,
|
||
// 文档格式(仅支持 .docx,不支持旧的 .doc、Excel、PPT)
|
||
".pdf": true,
|
||
".txt": true,
|
||
".docx": true,
|
||
// 视频格式
|
||
".mp4": true,
|
||
".webm": true,
|
||
// 音频格式
|
||
".mp3": true,
|
||
".wav": true,
|
||
}
|
||
return previewableExts[strings.ToLower(f.FileExt)]
|
||
}
|
||
|
||
// GetAllFiles 获取所有文件信息
|
||
func GetAllFiles() ([]*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var files []*FileInfo
|
||
_, err := o.QueryTable("yz_files").Filter("status", 1).OrderBy("-upload_time").All(&files)
|
||
return files, err
|
||
}
|
||
|
||
// GetFilesByUserID 根据用户ID获取文件列表
|
||
func GetFilesByUserID(userID int) ([]*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var files []*FileInfo
|
||
_, err := o.QueryTable("yz_files").Filter("user_id", userID).Filter("status", 1).OrderBy("-upload_time").All(&files)
|
||
return files, err
|
||
}
|
||
|
||
// GetFileById 根据ID获取文件信息
|
||
func GetFileById(id int64) (*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
file := &FileInfo{ID: id}
|
||
err := o.Read(file, "ID")
|
||
if err == orm.ErrNoRows {
|
||
return nil, err
|
||
}
|
||
return file, nil
|
||
}
|
||
|
||
// GetFilesByTenant 根据租户ID获取文件信息
|
||
func GetFilesByTenant(tenantID string) ([]*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var files []*FileInfo
|
||
_, err := o.QueryTable("yz_files").Filter("tenant_id", tenantID).OrderBy("-upload_time").All(&files)
|
||
return files, err
|
||
}
|
||
|
||
// GetFilesByCategory 根据分类获取文件信息
|
||
func GetFilesByCategory(category string) ([]*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var files []*FileInfo
|
||
_, err := o.QueryTable("yz_files").Filter("category", category).OrderBy("-upload_time").All(&files)
|
||
return files, err
|
||
}
|
||
|
||
// GetFilesByStatus 根据状态获取文件信息
|
||
func GetFilesByStatus(status int8) ([]*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var files []*FileInfo
|
||
_, err := o.QueryTable("yz_files").Filter("status", status).OrderBy("-upload_time").All(&files)
|
||
return files, err
|
||
}
|
||
|
||
// AddFile 添加新文件信息
|
||
func AddFile(file *FileInfo) (int64, error) {
|
||
o := orm.NewOrm()
|
||
id, err := o.Insert(file)
|
||
return id, err
|
||
}
|
||
|
||
// UpdateFile 更新文件信息
|
||
func UpdateFile(file *FileInfo) error {
|
||
o := orm.NewOrm()
|
||
_, err := o.Update(file)
|
||
return err
|
||
}
|
||
|
||
// DeleteFile 删除文件信息(软删除,设置状态为0并记录删除时间)
|
||
func DeleteFile(id int64) error {
|
||
o := orm.NewOrm()
|
||
file := &FileInfo{ID: id}
|
||
if err := o.Read(file, "ID"); err != nil {
|
||
return err
|
||
}
|
||
file.Status = 0
|
||
now := time.Now()
|
||
file.DeleteTime = &now
|
||
_, err := o.Update(file, "Status", "DeleteTime")
|
||
return err
|
||
}
|
||
|
||
// HardDeleteFile 硬删除文件信息
|
||
func HardDeleteFile(id int64) error {
|
||
o := orm.NewOrm()
|
||
_, err := o.Delete(&FileInfo{ID: id})
|
||
return err
|
||
}
|
||
|
||
// GetFileStatistics 获取文件统计信息
|
||
func GetFileStatistics(tenantID string) (map[string]interface{}, error) {
|
||
o := orm.NewOrm()
|
||
|
||
// 总文件数
|
||
totalCount, err := o.QueryTable("yz_files").Filter("tenant_id", tenantID).Filter("status", 1).Count()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 总文件大小
|
||
var totalSize int64
|
||
err = o.Raw("SELECT COALESCE(SUM(file_size), 0) FROM yz_files WHERE tenant_id = ? AND status = 1", tenantID).QueryRow(&totalSize)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 按分类统计
|
||
var categoryStats []orm.Params
|
||
_, err = o.Raw("SELECT category, COUNT(*) as count, COALESCE(SUM(file_size), 0) as size FROM yz_files WHERE tenant_id = ? AND status = 1 GROUP BY category", tenantID).Values(&categoryStats)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return map[string]interface{}{
|
||
"total_count": totalCount,
|
||
"total_size": totalSize,
|
||
"category_stats": categoryStats,
|
||
}, nil
|
||
}
|
||
|
||
// SearchFiles 搜索文件(通过原始文件名搜索)
|
||
func SearchFiles(keyword string, tenantID string) ([]*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var files []*FileInfo
|
||
|
||
// 构建查询条件 - 只通过原始文件名搜索
|
||
qs := o.QueryTable("yz_files").Filter("tenant_id", tenantID).Filter("status", 1)
|
||
|
||
// 搜索原始文件名
|
||
qs = qs.Filter("original_name__icontains", keyword)
|
||
|
||
_, err := qs.OrderBy("-upload_time").All(&files)
|
||
return files, err
|
||
}
|
||
|
||
// GetFileByMD5 根据MD5获取文件信息(获取唯一的文件记录)
|
||
func GetFileByMD5(md5 string) (*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var file FileInfo
|
||
err := o.QueryTable("yz_files").Filter("md5", md5).Filter("status", 1).OrderBy("upload_time").One(&file)
|
||
if err == orm.ErrNoRows {
|
||
return nil, nil
|
||
}
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &file, nil
|
||
}
|
||
|
||
// GetFileByMD5AndTenant 根据MD5和租户ID获取文件信息
|
||
func GetFileByMD5AndTenant(md5 string, tenantID string) (*FileInfo, error) {
|
||
o := orm.NewOrm()
|
||
var file FileInfo
|
||
err := o.QueryTable("yz_files").Filter("md5", md5).Filter("tenant_id", tenantID).Filter("status", 1).OrderBy("upload_time").One(&file)
|
||
if err == orm.ErrNoRows {
|
||
return nil, nil
|
||
}
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &file, nil
|
||
}
|