|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"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
|
|
|
|
// ChangeCoin 用户弹币变动
|
|
|
|
ChangeCoin(ctx context.Context, tx *gorm.DB, userId int64, change int64, ignoreNotEnough bool) (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 {
|
|
|
|
coin.Coin = 0
|
|
|
|
}
|
|
|
|
db := gormx.WithTx(ctx, m.DB, tx)
|
|
|
|
|
|
|
|
result := db.Model(&coin).Update("coin", coin.Coin)
|
|
|
|
return gormx.WrapUpdateErr(result.Error, result.RowsAffected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customUserCoinModel) ChangeCoin(ctx context.Context, tx *gorm.DB, userId int64, change int64, ignoreNotEnough bool) (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 {
|
|
|
|
if ignoreNotEnough {
|
|
|
|
change = 0
|
|
|
|
} else {
|
|
|
|
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 {
|
|
|
|
if ignoreNotEnough {
|
|
|
|
data.Coin = 0
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|