From f5194c7ef83c6b683d254e3b1c78d4980c7efd1b Mon Sep 17 00:00:00 2001 From: NorthLan <6995syu@163.com> Date: Wed, 18 May 2022 01:27:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9A=82=E6=97=B6=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=A7=AF=E5=88=86=E5=BD=B1=E5=93=8D=E8=A1=8C?= =?UTF-8?q?=E6=95=B0=E4=B8=BA0=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/user_center/model/user_integral_model.go | 15 +++++++++------ .../internal/logic/gift/user_send_gift_logic.go | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/user_center/model/user_integral_model.go b/app/user_center/model/user_integral_model.go index 912c34d..89c2f00 100644 --- a/app/user_center/model/user_integral_model.go +++ b/app/user_center/model/user_integral_model.go @@ -5,6 +5,7 @@ import ( "fmt" "git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc" "github.com/pkg/errors" + "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" "live-service/common/nerr" ) @@ -45,20 +46,22 @@ func (m *customUserIntegralModel) InsertTx(ctx context.Context, tx *gorm.DB, dat return err } -func (m *customUserIntegralModel) UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, change int64) error { - if change < 0 { +func (m *customUserIntegralModel) UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, integral int64) error { + if integral < 0 { return errors.New("无法将积分更新至负数") } db := withTx(ctx, m.conn, tx) result := db.Table(m.table). - Where("user_id = ?", userId). - Update("integral", change) + Where("`user_id` = ?", userId). + Update("`integral`", integral) if result.Error != nil { return result.Error } + // TODO 这里得处理一下 if result.RowsAffected == 0 { - return ErrRowsAffectedZero + logx.Statf("更新积分影响行数为0, user_id: %d, integral: %d", userId, integral) + return nil } return nil } @@ -67,7 +70,7 @@ func (m *customUserIntegralModel) FindIntegral(ctx context.Context, tx *gorm.DB, var resp int64 err := withTx(ctx, m.conn, tx).Table(m.table). Select(fmt.Sprintf("%s.integral", m.table)). - Where("user_id = ?", userId).Take(&resp).Error + Where("`user_id` = ?", userId).Take(&resp).Error switch err { case nil: return resp, nil diff --git a/app/user_center/rpc/internal/logic/gift/user_send_gift_logic.go b/app/user_center/rpc/internal/logic/gift/user_send_gift_logic.go index 541b11d..fd6b4cb 100644 --- a/app/user_center/rpc/internal/logic/gift/user_send_gift_logic.go +++ b/app/user_center/rpc/internal/logic/gift/user_send_gift_logic.go @@ -5,7 +5,6 @@ import ( "git.noahlan.cn/northlan/ntools-go/uuid" "live-service/app/user_center/model" "live-service/app/user_center/rpc/internal/logic/gift_collect" - "live-service/app/user_center/rpc/internal/logic/integral" "live-service/app/user_center/rpc/internal/svc" "live-service/app/user_center/rpc/pb" "strconv" @@ -62,14 +61,18 @@ func (l *UserSendGiftLogic) UserSendGift(in *pb.UserSendGiftReq) (*pb.UserSendGi addonIntegral = calcIntegral(l.svcCtx.Config, in.Platform, tmpData.IsPaid, tmpData.Price, in.Num) } } - newIntegral, err := integral.NewChangeIntegralLogic(l.ctx, l.svcCtx).ChangeIntegral(&pb.ChangeIntegralReq{ - UserId: in.UserId, - Change: addonIntegral, - }) + newIntegral, err := l.svcCtx.UserIntegralModel.ChangeIntegral(l.ctx, nil, in.UserId, addonIntegral) if err != nil { return nil, err } - resp.Integral = newIntegral + if err != nil { + return nil, err + } + resp.Integral = &pb.ChangeIntegralResp{ + UserId: in.UserId, + Change: addonIntegral, + Integral: newIntegral, + } } return resp, nil }