🎨 移除多余模块

This commit is contained in:
2026-04-08 12:19:24 +08:00
parent 22bb5fdc94
commit 7599146f24
192 changed files with 623 additions and 13983 deletions

View File

@@ -0,0 +1,6 @@
package common
type ServiceGroup struct {
AttachmentCategoryService
FileUploadAndDownloadService
}

View File

@@ -0,0 +1,66 @@
package common
import (
"errors"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common"
"gorm.io/gorm"
)
type AttachmentCategoryService struct{}
// AddCategory 创建/更新的分类
func (a *AttachmentCategoryService) AddCategory(req *common.ExaAttachmentCategory) (err error) {
// 检查是否已存在相同名称的分类
if (!errors.Is(global.GVA_DB.Take(&common.ExaAttachmentCategory{}, "name = ? and pid = ?", req.Name, req.Pid).Error, gorm.ErrRecordNotFound)) {
return errors.New("分类名称已存在")
}
if req.ID > 0 {
if err = global.GVA_DB.Model(&common.ExaAttachmentCategory{}).Where("id = ?", req.ID).Updates(&common.ExaAttachmentCategory{
Name: req.Name,
Pid: req.Pid,
}).Error; err != nil {
return err
}
} else {
if err = global.GVA_DB.Create(&common.ExaAttachmentCategory{
Name: req.Name,
Pid: req.Pid,
}).Error; err != nil {
return err
}
}
return nil
}
// DeleteCategory 删除分类
func (a *AttachmentCategoryService) DeleteCategory(id *int) error {
var childCount int64
global.GVA_DB.Model(&common.ExaAttachmentCategory{}).Where("pid = ?", id).Count(&childCount)
if childCount > 0 {
return errors.New("请先删除子级")
}
return global.GVA_DB.Where("id = ?", id).Unscoped().Delete(&common.ExaAttachmentCategory{}).Error
}
// GetCategoryList 分类列表
func (a *AttachmentCategoryService) GetCategoryList() (res []*common.ExaAttachmentCategory, err error) {
var fileLists []common.ExaAttachmentCategory
err = global.GVA_DB.Model(&common.ExaAttachmentCategory{}).Find(&fileLists).Error
if err != nil {
return res, err
}
return a.getChildrenList(fileLists, 0), nil
}
// getChildrenList 子类
func (a *AttachmentCategoryService) getChildrenList(categories []common.ExaAttachmentCategory, parentID uint) []*common.ExaAttachmentCategory {
var tree []*common.ExaAttachmentCategory
for _, category := range categories {
if category.Pid == parentID {
category.Children = a.getChildrenList(categories, category.ID)
tree = append(tree, &category)
}
}
return tree
}

View File

@@ -0,0 +1,71 @@
package common
import (
"errors"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common"
"gorm.io/gorm"
)
type FileUploadAndDownloadService struct{}
var FileUploadAndDownloadServiceApp = new(FileUploadAndDownloadService)
//@author: [piexlmax](https://github.com/piexlmax)
//@function: FindOrCreateFile
//@description: 上传文件时检测当前文件属性,如果没有文件则创建,有则返回文件的当前切片
//@param: fileMd5 string, fileName string, chunkTotal int
//@return: file model.ExaFile, err error
func (e *FileUploadAndDownloadService) FindOrCreateFile(fileMd5 string, fileName string, chunkTotal int) (file common.ExaFile, err error) {
var cfile common.ExaFile
cfile.FileMd5 = fileMd5
cfile.FileName = fileName
cfile.ChunkTotal = chunkTotal
if errors.Is(global.GVA_DB.Where("file_md5 = ? AND file_name = ? AND is_finish = ?", fileMd5, fileName, true).First(&file).Error, gorm.ErrRecordNotFound) {
err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
return file, err
}
cfile.IsFinish = true
cfile.FilePath = file.FilePath
err = global.GVA_DB.Create(&cfile).Error
return cfile, err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateFileChunk
//@description: 创建文件切片记录
//@param: id uint, fileChunkPath string, fileChunkNumber int
//@return: error
func (e *FileUploadAndDownloadService) CreateFileChunk(id uint, fileChunkPath string, fileChunkNumber int) error {
var chunk common.ExaFileChunk
chunk.FileChunkPath = fileChunkPath
chunk.ExaFileID = id
chunk.FileChunkNumber = fileChunkNumber
err := global.GVA_DB.Create(&chunk).Error
return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteFileChunk
//@description: 删除文件切片记录
//@param: fileMd5 string, fileName string, filePath string
//@return: error
func (e *FileUploadAndDownloadService) DeleteFileChunk(fileMd5 string, filePath string) error {
var chunks []common.ExaFileChunk
var file common.ExaFile
err := global.GVA_DB.Where("file_md5 = ?", fileMd5).First(&file).
Updates(map[string]interface{}{
"IsFinish": true,
"file_path": filePath,
}).Error
if err != nil {
return err
}
err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
return err
}

View File

@@ -0,0 +1,130 @@
package common
import (
"errors"
"mime/multipart"
"strings"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common"
commonReq "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
"github.com/flipped-aurora/gin-vue-admin/server/utils/upload"
"gorm.io/gorm"
)
//@author: [piexlmax](https://github.com/piexlmax)
//@function: Upload
//@description: 创建文件上传记录
//@param: file model.ExaFileUploadAndDownload
//@return: error
func (e *FileUploadAndDownloadService) Upload(file common.ExaFileUploadAndDownload) error {
return global.GVA_DB.Create(&file).Error
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: FindFile
//@description: 查询文件记录
//@param: id uint
//@return: model.ExaFileUploadAndDownload, error
func (e *FileUploadAndDownloadService) FindFile(id uint) (common.ExaFileUploadAndDownload, error) {
var file common.ExaFileUploadAndDownload
err := global.GVA_DB.Where("id = ?", id).First(&file).Error
return file, err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteFile
//@description: 删除文件记录
//@param: file model.ExaFileUploadAndDownload
//@return: err error
func (e *FileUploadAndDownloadService) DeleteFile(file common.ExaFileUploadAndDownload) (err error) {
var fileFromDb common.ExaFileUploadAndDownload
fileFromDb, err = e.FindFile(file.ID)
if err != nil {
return
}
oss := upload.NewOss()
if err = oss.DeleteFile(fileFromDb.Key); err != nil {
return errors.New("文件删除失败")
}
err = global.GVA_DB.Where("id = ?", file.ID).Unscoped().Delete(&file).Error
return err
}
// EditFileName 编辑文件名或者备注
func (e *FileUploadAndDownloadService) EditFileName(file common.ExaFileUploadAndDownload) (err error) {
var fileFromDb common.ExaFileUploadAndDownload
return global.GVA_DB.Where("id = ?", file.ID).First(&fileFromDb).Update("name", file.Name).Error
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetFileRecordInfoList
//@description: 分页获取数据
//@param: info request.ExaAttachmentCategorySearch
//@return: list interface{}, total int64, err error
func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info commonReq.ExaAttachmentCategorySearch) (list []common.ExaFileUploadAndDownload, total int64, err error) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB.Model(&common.ExaFileUploadAndDownload{})
if len(info.Keyword) > 0 {
db = db.Where("name LIKE ?", "%"+info.Keyword+"%")
}
if info.ClassId > 0 {
db = db.Where("class_id = ?", info.ClassId)
}
err = db.Count(&total).Error
if err != nil {
return
}
err = db.Limit(limit).Offset(offset).Order("id desc").Find(&list).Error
return list, total, err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UploadFile
//@description: 根据配置文件判断是文件上传到本地或者七牛云
//@param: header *multipart.FileHeader, noSave string
//@return: file model.ExaFileUploadAndDownload, err error
func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string, classId int) (file common.ExaFileUploadAndDownload, err error) {
oss := upload.NewOss()
filePath, key, uploadErr := oss.UploadFile(header)
if uploadErr != nil {
return file, uploadErr
}
s := strings.Split(header.Filename, ".")
f := common.ExaFileUploadAndDownload{
Url: filePath,
Name: header.Filename,
ClassId: classId,
Tag: s[len(s)-1],
Key: key,
}
if noSave == "0" {
// 检查是否已存在相同key的记录
var existingFile common.ExaFileUploadAndDownload
err = global.GVA_DB.Where(&common.ExaFileUploadAndDownload{Key: key}).First(&existingFile).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return f, e.Upload(f)
}
return f, err
}
return f, nil
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: ImportURL
//@description: 导入URL
//@param: file model.ExaFileUploadAndDownload
//@return: error
func (e *FileUploadAndDownloadService) ImportURL(file *[]common.ExaFileUploadAndDownload) error {
return global.GVA_DB.Create(&file).Error
}