feat: 优化用户弹币数据模型.

main
NorthLan 2 years ago
parent b1da15e8db
commit 7eddb6853b

@ -16,7 +16,6 @@ type (
// 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)
}
@ -33,7 +32,7 @@ func NewUserCoinModel(conn *gorm.DB) UserCoinModel {
}
}
func (m *customUserCoinModel) UpdateCoin(ctx context.Context, tx *gorm.DB, coin *UserCoin) error {
func (m *customUserCoinModel) updateCoin(ctx context.Context, tx *gorm.DB, coin *UserCoin) error {
if coin.Coin < 0 {
return errors.New("无法将弹币更新至负数")
}
@ -55,8 +54,8 @@ func (m *customUserCoinModel) ChangeCoin(ctx context.Context, tx *gorm.DB, userI
}
// 用户积分记录不存在,进行插入
if err = m.Insert(ctx, tx, &UserCoin{
UserId: userId,
Coin: change,
UserId: userId,
Coin: change,
}); err != nil {
return nerr.NewError(nerr.NewUserCoinErr, "新建用户弹币记录失败")
}
@ -69,7 +68,7 @@ func (m *customUserCoinModel) ChangeCoin(ctx context.Context, tx *gorm.DB, userI
return nerr.NewError(nerr.UserCoinNotEnoughErr, "用户弹币不足")
}
data.Coin += change
if err = m.UpdateCoin(ctx, tx, data); err != nil {
if err = m.updateCoin(ctx, tx, data); err != nil {
if errors.Is(err, gormx.ErrRowsAffectedZero) {
return err
}

Loading…
Cancel
Save