From 3f60376061fc02ce873bb7fb02dcd68ea635e8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sun, 18 Sep 2022 08:18:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(user):=E7=94=A8=E6=88=B7=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=99=A8=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/end_user.go | 10 ++++++---- controller/user.go | 9 ++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/controller/end_user.go b/controller/end_user.go index 42ff28c..9445102 100644 --- a/controller/end_user.go +++ b/controller/end_user.go @@ -1,6 +1,7 @@ package controller import ( + "database/sql" "electricity_bill_calc/excel" "electricity_bill_calc/global" "electricity_bill_calc/response" @@ -202,13 +203,14 @@ func modifyEndUserRegisterRecord(c *gin.Context) { result.NotAccept("抄表数据合法性验证失败。") return } - tx := global.DBConn.NewSession() - if err = tx.Begin(); err != nil { + ctx, cancel := global.TimeoutContext() + defer cancel() + tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{}) + if err != nil { result.Error(http.StatusInternalServerError, err.Error()) return } - defer tx.Close() - err = service.EndUserService.UpdateEndUserRegisterRecord(tx, *meter) + err = service.EndUserService.UpdateEndUserRegisterRecord(&tx, &ctx, *meter) if err != nil { tx.Rollback() result.Error(http.StatusInternalServerError, err.Error()) diff --git a/controller/user.go b/controller/user.go index def1121..9532ceb 100644 --- a/controller/user.go +++ b/controller/user.go @@ -324,6 +324,7 @@ func modifyAccountDetail(c *gin.Context) { return } newUserInfo := new(model.UserDetail) + newUserInfo.Id = targetUserId newUserInfo.Name = &modForm.Name newUserInfo.Region = modForm.Region newUserInfo.Address = modForm.Address @@ -334,13 +335,15 @@ func modifyAccountDetail(c *gin.Context) { result.BadRequest("用户月服务费无法解析。") return } - _, err = global.DBConn.ID(targetUserId).Update(newUserInfo) + _, err = global.DB.NewUpdate().Model(newUserInfo). + WherePK(). + Column("name", "region", "address", "contact", "phone", "unit_service_fee"). + Exec(c) if err != nil { result.Error(http.StatusInternalServerError, err.Error()) return } - cache.AbolishRelation("user") - cache.AbolishRelation(fmt.Sprintf("user_%s", targetUserId)) + cache.AbolishRelation(fmt.Sprintf("user:%s", targetUserId)) result.Updated("指定用户的信息已经更新。") }