2026-07-03 15:58:29 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"seeyon-filesystem/config"
|
|
|
|
|
"seeyon-filesystem/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// FolderRepository 文件夹数据访问
|
|
|
|
|
type FolderRepository struct{}
|
|
|
|
|
|
|
|
|
|
// NewFolderRepository 创建文件夹Repository实例
|
|
|
|
|
func NewFolderRepository() *FolderRepository {
|
|
|
|
|
return &FolderRepository{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create 创建文件夹
|
|
|
|
|
func (r *FolderRepository) Create(folder *model.Folder) error {
|
|
|
|
|
return config.DB.Create(folder).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindByID 根据ID查找文件夹
|
|
|
|
|
func (r *FolderRepository) FindByID(id uint64) (*model.Folder, error) {
|
|
|
|
|
var folder model.Folder
|
|
|
|
|
err := config.DB.First(&folder, id).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &folder, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 17:33:33 +08:00
|
|
|
// FindByBizID 根据业务ID查找文件夹
|
|
|
|
|
func (r *FolderRepository) FindByBizID(bizID string, ownerID uint64) (*model.Folder, error) {
|
|
|
|
|
var folder model.Folder
|
|
|
|
|
err := config.DB.Where("biz_id = ? AND owner_id = ? AND status = 1", bizID, ownerID).
|
|
|
|
|
First(&folder).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &folder, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 15:58:29 +08:00
|
|
|
// ListByParent 查询子文件夹列表
|
|
|
|
|
func (r *FolderRepository) ListByParent(parentID uint64, ownerID uint64) ([]model.Folder, error) {
|
|
|
|
|
var folders []model.Folder
|
|
|
|
|
err := config.DB.Where("parent_id = ? AND owner_id = ? AND status = 1", parentID, ownerID).
|
|
|
|
|
Order("name ASC").Find(&folders).Error
|
|
|
|
|
return folders, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 17:33:33 +08:00
|
|
|
// CountByParent 统计子文件夹数量
|
|
|
|
|
func (r *FolderRepository) CountByParent(parentID uint64, ownerID uint64) (int64, error) {
|
|
|
|
|
var count int64
|
|
|
|
|
err := config.DB.Model(&model.Folder{}).
|
|
|
|
|
Where("parent_id = ? AND owner_id = ? AND status = 1", parentID, ownerID).
|
|
|
|
|
Count(&count).Error
|
|
|
|
|
return count, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListByParentAll 查询子文件夹列表(管理员模式, 不限owner)
|
|
|
|
|
func (r *FolderRepository) ListByParentAll(parentID uint64) ([]model.Folder, error) {
|
|
|
|
|
var folders []model.Folder
|
|
|
|
|
err := config.DB.Where("parent_id = ? AND status = 1", parentID).
|
|
|
|
|
Order("name ASC").Find(&folders).Error
|
|
|
|
|
return folders, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CountByParentAll 统计子文件夹数量(管理员模式, 不限owner)
|
|
|
|
|
func (r *FolderRepository) CountByParentAll(parentID uint64) (int64, error) {
|
|
|
|
|
var count int64
|
|
|
|
|
err := config.DB.Model(&model.Folder{}).
|
|
|
|
|
Where("parent_id = ? AND status = 1", parentID).
|
|
|
|
|
Count(&count).Error
|
|
|
|
|
return count, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 15:58:29 +08:00
|
|
|
// FindByPath 根据路径查找文件夹
|
|
|
|
|
func (r *FolderRepository) FindByPath(path string, ownerID uint64) (*model.Folder, error) {
|
|
|
|
|
var folder model.Folder
|
|
|
|
|
err := config.DB.Where("path = ? AND owner_id = ? AND status = 1", path, ownerID).First(&folder).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &folder, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindByNameAndParent 根据名称和父文件夹查找(用于判断重名)
|
|
|
|
|
func (r *FolderRepository) FindByNameAndParent(name string, parentID uint64, ownerID uint64) (*model.Folder, error) {
|
|
|
|
|
var folder model.Folder
|
|
|
|
|
err := config.DB.Where("name = ? AND parent_id = ? AND owner_id = ? AND status = 1",
|
|
|
|
|
name, parentID, ownerID).First(&folder).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &folder, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListTrashed 查询回收站文件夹
|
|
|
|
|
func (r *FolderRepository) ListTrashed(ownerID uint64) ([]model.Folder, error) {
|
|
|
|
|
var folders []model.Folder
|
|
|
|
|
err := config.DB.Where("owner_id = ? AND status = 0", ownerID).
|
|
|
|
|
Order("updated_at DESC").Find(&folders).Error
|
|
|
|
|
return folders, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update 更新文件夹
|
|
|
|
|
func (r *FolderRepository) Update(folder *model.Folder) error {
|
|
|
|
|
return config.DB.Save(folder).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SoftDelete 软删除
|
|
|
|
|
func (r *FolderRepository) SoftDelete(id uint64) error {
|
|
|
|
|
return config.DB.Model(&model.Folder{}).Where("id = ?", id).Update("status", 0).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore 从回收站恢复
|
|
|
|
|
func (r *FolderRepository) Restore(id uint64) error {
|
|
|
|
|
return config.DB.Model(&model.Folder{}).Where("id = ?", id).Update("status", 1).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HardDelete 永久删除
|
|
|
|
|
func (r *FolderRepository) HardDelete(id uint64) error {
|
|
|
|
|
return config.DB.Unscoped().Delete(&model.Folder{}, id).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetChildrenPath 获取所有子孙文件夹的路径前缀(用于级联操作)
|
|
|
|
|
func (r *FolderRepository) GetChildrenByPath(pathPrefix string, ownerID uint64) ([]model.Folder, error) {
|
|
|
|
|
var folders []model.Folder
|
|
|
|
|
err := config.DB.Where("path LIKE ? AND owner_id = ?",
|
|
|
|
|
pathPrefix+"%", ownerID).Find(&folders).Error
|
|
|
|
|
return folders, err
|
|
|
|
|
}
|