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 }