From db5b5767998d8898bfb55272f0f349101cb8f2af Mon Sep 17 00:00:00 2001 From: Hiven Date: Thu, 10 Aug 2023 14:49:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_resource_logic.go | 45 +++++++++++++++++-------------- utils/file/upload.go | 13 ++++----- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/model/gmodel/fs_resource_logic.go b/model/gmodel/fs_resource_logic.go index e7749e4a..19e095db 100644 --- a/model/gmodel/fs_resource_logic.go +++ b/model/gmodel/fs_resource_logic.go @@ -53,30 +53,35 @@ func (m *FsResourceModel) RowSelectBuilder(selectData []string) *gorm.DB { // 事务 func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context, connGorm *gorm.DB) error) error { - tx := m.db.Table(m.name).WithContext(ctx).Begin() - defer func() { - if r := recover(); r != nil { - tx.Rollback() - } - }() - - if err := tx.Error; err != nil { - tx.Rollback() + return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + err := fn(ctx, tx) return err - } + }) - if err := fn(ctx, tx); err != nil { - tx.Rollback() - return err - } + // tx := m.db.Table(m.name).WithContext(ctx) + // defer func() { + // if r := recover(); r != nil { + // tx.Rollback() + // } + // }() - err := tx.Commit().Error - if err != nil { - tx.Rollback() - return err - } + // if err := tx.Error; err != nil { + // tx.Rollback() + // return err + // } - return err + // if err := fn(ctx, tx); err != nil { + // tx.Rollback() + // return err + // } + + // err := tx.Commit().Error + // if err != nil { + // tx.Rollback() + // return err + // } + + // return err } func (m *FsResourceModel) TableName() string { diff --git a/utils/file/upload.go b/utils/file/upload.go index d3114273..f6f6a1d6 100644 --- a/utils/file/upload.go +++ b/utils/file/upload.go @@ -66,11 +66,9 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er var resourceId string = req.FileHash var uploadBaseRes = UploadBaseRes{} - resourceModel := gmodel.NewFsResourceModel(upload.MysqlConn) - - err := resourceModel.Trans(upload.Ctx, func(ctx context.Context, connGorm *gorm.DB) error { - resourceModelTS := gmodel.NewFsResourceModel(connGorm) - resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId) + err := upload.MysqlConn.Transaction(func(tx *gorm.DB) error { + resourceModelTS := gmodel.NewFsResourceModel(tx) + resourceInfo, err := resourceModelTS.FindOneById(upload.Ctx, resourceId) if err == nil && resourceInfo.ResourceId != "" { uploadBaseRes.Status = 1 uploadBaseRes.ResourceId = resourceId @@ -110,7 +108,7 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er uploadBaseRes.ResourceUrl = url var version string = "0.0.1" var nowTime = time.Now() - _, err = resourceModelTS.Create(ctx, &gmodel.FsResource{ + _, err = resourceModelTS.Create(upload.Ctx, &gmodel.FsResource{ ResourceId: resourceId, UserId: &req.UserId, GuestId: &req.GuestId, @@ -128,8 +126,11 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er } } } + + // 返回 nil 提交事务 return nil }) + if err != nil { return nil, err }