You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.5 KiB
Go
86 lines
2.5 KiB
Go
2 years ago
|
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
|
||
|
"github.com/pkg/errors"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/plugin/optimisticlock"
|
||
|
"live-service/common/nerr"
|
||
|
)
|
||
|
|
||
|
var _ UserCoinModel = (*customUserCoinModel)(nil)
|
||
|
|
||
|
type (
|
||
|
// UserCoinModel is an interface to be customized, add more methods here,
|
||
|
// and implement the added methods in customUserCoinModel.
|
||
|
UserCoinModel interface {
|
||
|
userCoinModel
|
||
|
UpdateCoin(ctx context.Context, tx *gorm.DB, integral *UserCoin) error
|
||
|
// ChangeCoin 用户弹币变动
|
||
|
ChangeCoin(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error)
|
||
|
}
|
||
|
|
||
|
customUserCoinModel struct {
|
||
|
*defaultUserCoinModel
|
||
|
}
|
||
|
)
|
||
|
|
||
|
// NewUserCoinModel returns a model for the database table.
|
||
|
func NewUserCoinModel(conn *gorm.DB) UserCoinModel {
|
||
|
return &customUserCoinModel{
|
||
|
defaultUserCoinModel: newUserCoinModel(conn),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *customUserCoinModel) UpdateCoin(ctx context.Context, tx *gorm.DB, coin *UserCoin) error {
|
||
|
if coin.Coin < 0 {
|
||
|
return errors.New("无法将弹币更新至负数")
|
||
|
}
|
||
|
db := gormx.WithTx(ctx, m.DB, tx)
|
||
|
|
||
|
result := db.Model(&coin).Updates(&UserCoin{Coin: coin.Coin, Version: optimisticlock.Version{Int64: 1}})
|
||
|
return gormx.WrapUpdateErr(result.Error, result.RowsAffected)
|
||
|
}
|
||
|
|
||
|
func (m *customUserCoinModel) ChangeCoin(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error) {
|
||
|
resp := change
|
||
|
err := gormx.WithRetry(VersionRetryCount, func() error {
|
||
|
return m.Transact(tx, func(tx *gorm.DB) error {
|
||
|
data, err := m.FindOne(ctx, tx, userId)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, ErrNotFound) {
|
||
|
if change < 0 {
|
||
|
return nerr.NewError(nerr.UserCoinNotEnoughErr, "用户弹币不足")
|
||
|
}
|
||
|
// 用户积分记录不存在,进行插入
|
||
|
if err = m.Insert(ctx, tx, &UserCoin{
|
||
|
UserId: userId,
|
||
|
Coin: change,
|
||
|
}); err != nil {
|
||
|
return nerr.NewError(nerr.NewUserCoinErr, "新建用户弹币记录失败")
|
||
|
}
|
||
|
return nil
|
||
|
} else {
|
||
|
return nerr.NewError(nerr.GetUserCoinErr, "获取当前用户弹币失败")
|
||
|
}
|
||
|
}
|
||
|
if data.Coin+change < 0 {
|
||
|
return nerr.NewError(nerr.UserCoinNotEnoughErr, "用户弹币不足")
|
||
|
}
|
||
|
data.Coin += change
|
||
|
if err = m.UpdateCoin(ctx, tx, data); err != nil {
|
||
|
if errors.Is(err, gormx.ErrRowsAffectedZero) {
|
||
|
return err
|
||
|
}
|
||
|
return nerr.NewError(nerr.UpdateUserCoinErr, "更新用户弹币失败")
|
||
|
}
|
||
|
resp = data.Coin
|
||
|
return nil
|
||
|
})
|
||
|
}, func(err error) bool {
|
||
|
return errors.Is(err, gormx.ErrRowsAffectedZero)
|
||
|
})
|
||
|
return resp, err
|
||
|
}
|