后端重构
This commit is contained in:
@@ -40,10 +40,12 @@ var ProviderSet = wire.NewSet(
|
||||
datasystem.NewExportTemplateRepo,
|
||||
datasystem.NewAutoCodeRepo,
|
||||
datasystem.NewAutoCodeHistoryRepo,
|
||||
datasystem.NewAutoCodePackageRepo,
|
||||
// Example
|
||||
dataexample.NewFileUploadRepo,
|
||||
dataexample.NewCustomerRepo,
|
||||
dataexample.NewAttachmentCategoryRepo,
|
||||
dataexample.NewBreakpointContinueRepo,
|
||||
)
|
||||
|
||||
// Data 数据层包装器
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"kra/internal/biz/example"
|
||||
"kra/internal/data/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type breakpointContinueRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewBreakpointContinueRepo 创建断点续传仓储
|
||||
func NewBreakpointContinueRepo(db *gorm.DB) example.BreakpointContinueRepo {
|
||||
return &breakpointContinueRepo{db: db}
|
||||
}
|
||||
|
||||
// FindOrCreateFile 查找或创建文件记录
|
||||
func (r *breakpointContinueRepo) FindOrCreateFile(ctx context.Context, fileMd5, fileName string, chunkTotal int) (*example.ExaFile, error) {
|
||||
var file model.ExaFile
|
||||
cfile := model.ExaFile{
|
||||
FileMd5: fileMd5,
|
||||
FileName: fileName,
|
||||
ChunkTotal: int64(chunkTotal),
|
||||
}
|
||||
|
||||
// 检查是否已有完成的文件
|
||||
if errors.Is(r.db.WithContext(ctx).Where("file_md5 = ? AND is_finish = ?", fileMd5, true).First(&file).Error, gorm.ErrRecordNotFound) {
|
||||
// 没有完成的文件,查找或创建
|
||||
err := r.db.WithContext(ctx).Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).
|
||||
FirstOrCreate(&file, cfile).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 加载切片
|
||||
var chunks []model.ExaFileChunk
|
||||
r.db.WithContext(ctx).Where("exa_file_id = ?", file.ID).Find(&chunks)
|
||||
return toBizExaFileWithChunks(&file, chunks), nil
|
||||
}
|
||||
|
||||
// 已有完成的文件,创建新记录
|
||||
cfile.IsFinish = true
|
||||
cfile.FilePath = file.FilePath
|
||||
if err := r.db.WithContext(ctx).Create(&cfile).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBizExaFileWithChunks(&cfile, nil), nil
|
||||
}
|
||||
|
||||
// CreateFileChunk 创建文件切片记录
|
||||
func (r *breakpointContinueRepo) CreateFileChunk(ctx context.Context, fileID uint, fileChunkPath string, fileChunkNumber int) error {
|
||||
chunk := model.ExaFileChunk{
|
||||
ExaFileID: int64(fileID),
|
||||
FileChunkPath: fileChunkPath,
|
||||
FileChunkNumber: int64(fileChunkNumber),
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(&chunk).Error
|
||||
}
|
||||
|
||||
// DeleteFileChunk 删除文件切片记录
|
||||
func (r *breakpointContinueRepo) DeleteFileChunk(ctx context.Context, fileMd5, filePath string) error {
|
||||
var file model.ExaFile
|
||||
err := r.db.WithContext(ctx).Where("file_md5 = ?", fileMd5).First(&file).
|
||||
Updates(map[string]interface{}{
|
||||
"is_finish": true,
|
||||
"file_path": filePath,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).Where("exa_file_id = ?", file.ID).Delete(&model.ExaFileChunk{}).Unscoped().Error
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toBizExaFileWithChunks(m *model.ExaFile, chunks []model.ExaFileChunk) *example.ExaFile {
|
||||
file := &example.ExaFile{
|
||||
ID: uint(m.ID),
|
||||
FileName: m.FileName,
|
||||
FileMd5: m.FileMd5,
|
||||
FilePath: m.FilePath,
|
||||
ChunkTotal: int(m.ChunkTotal),
|
||||
IsFinish: m.IsFinish,
|
||||
}
|
||||
if len(chunks) > 0 {
|
||||
file.ExaFileChunk = make([]example.ExaFileChunk, len(chunks))
|
||||
for i, chunk := range chunks {
|
||||
file.ExaFileChunk[i] = example.ExaFileChunk{
|
||||
ID: uint(chunk.ID),
|
||||
ExaFileID: uint(chunk.ExaFileID),
|
||||
FileChunkNumber: int(chunk.FileChunkNumber),
|
||||
FileChunkPath: chunk.FileChunkPath,
|
||||
}
|
||||
}
|
||||
}
|
||||
return file
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SysAutoCodePackage 代码包数据模型
|
||||
type SysAutoCodePackage struct {
|
||||
gorm.Model
|
||||
Desc string `json:"desc" gorm:"comment:描述"`
|
||||
Label string `json:"label" gorm:"comment:展示名"`
|
||||
Template string `json:"template" gorm:"comment:模版"`
|
||||
PackageName string `json:"packageName" gorm:"comment:包名"`
|
||||
Module string `json:"-" gorm:"comment:模块"`
|
||||
}
|
||||
|
||||
func (SysAutoCodePackage) TableName() string {
|
||||
return "sys_auto_code_packages"
|
||||
}
|
||||
|
||||
type autoCodePackageRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewAutoCodePackageRepo 创建代码包仓储
|
||||
func NewAutoCodePackageRepo(db *gorm.DB) system.AutoCodePackageRepo {
|
||||
return &autoCodePackageRepo{db: db}
|
||||
}
|
||||
|
||||
// Create 创建包
|
||||
func (r *autoCodePackageRepo) Create(ctx context.Context, pkg *system.SysAutoCodePackage) error {
|
||||
entity := toDataAutoCodePackage(pkg)
|
||||
if err := r.db.WithContext(ctx).Create(entity).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
pkg.ID = entity.ID
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除包
|
||||
func (r *autoCodePackageRepo) Delete(ctx context.Context, id uint) error {
|
||||
return r.db.WithContext(ctx).Delete(&SysAutoCodePackage{}, id).Error
|
||||
}
|
||||
|
||||
// DeleteByNames 根据名称删除包
|
||||
func (r *autoCodePackageRepo) DeleteByNames(ctx context.Context, names []string) error {
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.WithContext(ctx).Where("package_name IN ?", names).Delete(&SysAutoCodePackage{}).Error
|
||||
}
|
||||
|
||||
// All 获取所有包
|
||||
func (r *autoCodePackageRepo) All(ctx context.Context) ([]system.SysAutoCodePackage, error) {
|
||||
var entities []SysAutoCodePackage
|
||||
err := r.db.WithContext(ctx).Find(&entities).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBizAutoCodePackages(entities), nil
|
||||
}
|
||||
|
||||
// First 查询包
|
||||
func (r *autoCodePackageRepo) First(ctx context.Context, packageName, template string) (*system.SysAutoCodePackage, error) {
|
||||
var entity SysAutoCodePackage
|
||||
err := r.db.WithContext(ctx).Where("package_name = ? AND template = ?", packageName, template).First(&entity).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return toBizAutoCodePackage(&entity), nil
|
||||
}
|
||||
|
||||
// FindByPackageName 根据包名查询
|
||||
func (r *autoCodePackageRepo) FindByPackageName(ctx context.Context, packageName string) (*system.SysAutoCodePackage, error) {
|
||||
var entity SysAutoCodePackage
|
||||
err := r.db.WithContext(ctx).Where("package_name = ?", packageName).First(&entity).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return toBizAutoCodePackage(&entity), nil
|
||||
}
|
||||
|
||||
// BatchCreate 批量创建
|
||||
func (r *autoCodePackageRepo) BatchCreate(ctx context.Context, pkgs []system.SysAutoCodePackage) error {
|
||||
if len(pkgs) == 0 {
|
||||
return nil
|
||||
}
|
||||
entities := make([]SysAutoCodePackage, len(pkgs))
|
||||
for i, pkg := range pkgs {
|
||||
entities[i] = *toDataAutoCodePackage(&pkg)
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(&entities).Error
|
||||
}
|
||||
|
||||
// DeleteByIDs 根据ID批量删除
|
||||
func (r *autoCodePackageRepo) DeleteByIDs(ctx context.Context, ids []uint) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.WithContext(ctx).Delete(&SysAutoCodePackage{}, ids).Error
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toBizAutoCodePackage(m *SysAutoCodePackage) *system.SysAutoCodePackage {
|
||||
return &system.SysAutoCodePackage{
|
||||
ID: m.ID,
|
||||
Desc: m.Desc,
|
||||
Label: m.Label,
|
||||
Template: m.Template,
|
||||
PackageName: m.PackageName,
|
||||
Module: m.Module,
|
||||
}
|
||||
}
|
||||
|
||||
func toBizAutoCodePackages(models []SysAutoCodePackage) []system.SysAutoCodePackage {
|
||||
result := make([]system.SysAutoCodePackage, len(models))
|
||||
for i, m := range models {
|
||||
result[i] = *toBizAutoCodePackage(&m)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toDataAutoCodePackage(pkg *system.SysAutoCodePackage) *SysAutoCodePackage {
|
||||
return &SysAutoCodePackage{
|
||||
Model: gorm.Model{ID: pkg.ID},
|
||||
Desc: pkg.Desc,
|
||||
Label: pkg.Label,
|
||||
Template: pkg.Template,
|
||||
PackageName: pkg.PackageName,
|
||||
Module: pkg.Module,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user