|
|
|
@ -2,12 +2,10 @@ package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"gorm.io/plugin/optimisticlock"
|
|
|
|
|
"live-service/common/nerr"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -20,8 +18,8 @@ type (
|
|
|
|
|
userIntegralModel
|
|
|
|
|
Transact(ctx context.Context, tx *gorm.DB, fn func(tx *gorm.DB) error) error
|
|
|
|
|
InsertTx(ctx context.Context, tx *gorm.DB, data *UserIntegral) error
|
|
|
|
|
FindIntegral(ctx context.Context, tx *gorm.DB, userId int64) (int64, error)
|
|
|
|
|
UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, addon int64) error
|
|
|
|
|
FindOneTx(ctx context.Context, tx *gorm.DB, userId int64) (*UserIntegral, error)
|
|
|
|
|
UpdateTx(ctx context.Context, tx *gorm.DB, integral *UserIntegral) error
|
|
|
|
|
// ChangeIntegral 用户积分变动
|
|
|
|
|
ChangeIntegral(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error)
|
|
|
|
|
}
|
|
|
|
@ -47,45 +45,42 @@ func (m *customUserIntegralModel) InsertTx(ctx context.Context, tx *gorm.DB, dat
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *customUserIntegralModel) UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, integral int64) error {
|
|
|
|
|
if integral < 0 {
|
|
|
|
|
func (m *customUserIntegralModel) UpdateTx(ctx context.Context, tx *gorm.DB, integral *UserIntegral) error {
|
|
|
|
|
if integral.Integral < 0 {
|
|
|
|
|
return errors.New("无法将积分更新至负数")
|
|
|
|
|
}
|
|
|
|
|
db := withTx(ctx, m.conn, tx)
|
|
|
|
|
|
|
|
|
|
result := db.Table(m.table).
|
|
|
|
|
Where("`user_id` = ?", userId).
|
|
|
|
|
Update("`integral`", integral)
|
|
|
|
|
result := db.Model(&integral).Updates(&UserIntegral{Integral: integral.Integral, Version: optimisticlock.Version{Int64: 1}})
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
// TODO 这里得处理一下
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
|
logx.Statf("更新积分影响行数为0, user_id: %d, integral: %d", userId, integral)
|
|
|
|
|
return nil
|
|
|
|
|
return ErrRowsAffectedZero
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *customUserIntegralModel) FindIntegral(ctx context.Context, tx *gorm.DB, userId int64) (int64, error) {
|
|
|
|
|
var resp int64
|
|
|
|
|
err := withTx(ctx, m.conn, tx).Table(m.table).
|
|
|
|
|
Select(fmt.Sprintf("%s.integral", m.table)).
|
|
|
|
|
func (m *customUserIntegralModel) FindOneTx(ctx context.Context, tx *gorm.DB, userId int64) (*UserIntegral, error) {
|
|
|
|
|
var resp UserIntegral
|
|
|
|
|
err := withTx(ctx, m.conn, tx).Model(&UserIntegral{}).
|
|
|
|
|
Where("`user_id` = ?", userId).Take(&resp).Error
|
|
|
|
|
switch err {
|
|
|
|
|
case nil:
|
|
|
|
|
return resp, nil
|
|
|
|
|
return &resp, nil
|
|
|
|
|
case gormc.ErrNotFound:
|
|
|
|
|
return 0, ErrNotFound
|
|
|
|
|
return nil, ErrNotFound
|
|
|
|
|
default:
|
|
|
|
|
return 0, err
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *customUserIntegralModel) ChangeIntegral(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error) {
|
|
|
|
|
resp := change
|
|
|
|
|
err := withTx(ctx, m.conn, tx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
integral, err := m.FindIntegral(ctx, tx, userId)
|
|
|
|
|
var err error
|
|
|
|
|
for i := VersionRetryCount; i > 0; i-- {
|
|
|
|
|
err = withTx(ctx, m.conn, tx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
data, err := m.FindOneTx(ctx, tx, userId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, ErrNotFound) {
|
|
|
|
|
if change < 0 {
|
|
|
|
@ -103,17 +98,27 @@ func (m *customUserIntegralModel) ChangeIntegral(ctx context.Context, tx *gorm.D
|
|
|
|
|
return errors.Wrap(err, "获取当前用户积分失败")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if integral+change < 0 {
|
|
|
|
|
if data.Integral+change < 0 {
|
|
|
|
|
return errors.New("用户积分不足")
|
|
|
|
|
}
|
|
|
|
|
if err = m.UpdateIntegralTx(ctx, tx, userId, integral+change); err != nil {
|
|
|
|
|
data.Integral += change
|
|
|
|
|
if err = m.UpdateTx(ctx, tx, data); err != nil {
|
|
|
|
|
if errors.Is(err, ErrRowsAffectedZero) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return errors.Wrap(err, "更新用户积分失败")
|
|
|
|
|
}
|
|
|
|
|
resp = integral + change
|
|
|
|
|
resp = data.Integral
|
|
|
|
|
return nil
|
|
|
|
|
}, &sql.TxOptions{
|
|
|
|
|
Isolation: sql.LevelReadCommitted,
|
|
|
|
|
ReadOnly: false,
|
|
|
|
|
})
|
|
|
|
|
if err != nil && errors.Is(err, ErrRowsAffectedZero) {
|
|
|
|
|
// 未能正确更新,直接重试
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
// 其它错误退出循环
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
|
|
|
|