180 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			180 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package models
 | ||
| 
 | ||
| import (
 | ||
| 	"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"`
 | ||
| 	
 | ||
| 	// 分类信息
 | ||
| 	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"`
 | ||
| 	
 | ||
| 	// 关联的用户信息(非数据库字段)
 | ||
| 	User         *User     `orm:"-" json:"user,omitempty"`
 | ||
| }
 | ||
| 
 | ||
| // TableName 设置表名
 | ||
| func (f *FileInfo) TableName() string {
 | ||
| 	return "yz_files"
 | ||
| }
 | ||
| 
 | ||
| // 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
 | ||
| 	_, err := o.Update(file, "Status")
 | ||
| 	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)
 | ||
| 	
 | ||
| 	// 搜索文件名、原始文件名、分类(使用or条件)
 | ||
| 	cond := orm.NewCondition()
 | ||
| 	cond = cond.Or("file_name__icontains", keyword).
 | ||
| 		Or("original_name__icontains", keyword).
 | ||
| 		Or("category__icontains", keyword)
 | ||
| 	
 | ||
| 	qs = qs.SetCond(cond)
 | ||
| 	
 | ||
| 	_, err := qs.OrderBy("-upload_time").All(&files)
 | ||
| 	return files, err
 | ||
| } |