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