From a083be6e68a32656721150866d8916a0a26539dc Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Sun, 1 Nov 2020 19:50:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=AD=E7=82=B9=E7=BB=AD?= =?UTF-8?q?=E4=BC=A0=E7=9A=84=E6=8F=90=E4=BA=A4=E6=96=B9=E5=BC=8F=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/exa_simple_uploader.go | 45 +++++++++++++-------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/server/service/exa_simple_uploader.go b/server/service/exa_simple_uploader.go index 25777c3d..ae2b783d 100644 --- a/server/service/exa_simple_uploader.go +++ b/server/service/exa_simple_uploader.go @@ -51,27 +51,26 @@ func MergeFileMd5(md5 string, fileName string) (err error) { if err != nil { return err } - //创建事务 - tx := global.GVA_DB.Begin() - //删除切片信息 - err = tx.Delete(&model.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, false).Error - // 添加文件信息 - if err != nil { - fmt.Println(err) - tx.Rollback() - } - err = tx.Create(&model.ExaSimpleUploader{ - Identifier: md5, - IsDone: true, - FilePath: finishDir + fileName, - Filename: fileName, - }).Error - if err != nil { - fmt.Println(err) - tx.Rollback() - } - tx.Commit() - //清除切片 - err = os.RemoveAll(dir) - return + err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { + //删除切片信息 + if err = tx.Delete(&model.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, false).Error; err != nil { + fmt.Println(err) + return err + } + data := model.ExaSimpleUploader{ + Identifier: md5, + IsDone: true, + FilePath: finishDir + fileName, + Filename: fileName, + } + // 添加文件信息 + if err = tx.Create(&data).Error; err != nil { + fmt.Println(err) + return err + } + return nil + }) + + err = os.RemoveAll(dir) //清除切片 + return err }