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

main
NorthLan 2 years ago
parent b1da15e8db
commit 7eddb6853b

@ -16,7 +16,6 @@ type (
// and implement the added methods in customUserCoinModel. // and implement the added methods in customUserCoinModel.
UserCoinModel interface { UserCoinModel interface {
userCoinModel userCoinModel
UpdateCoin(ctx context.Context, tx *gorm.DB, integral *UserCoin) error
// ChangeCoin 用户弹币变动 // ChangeCoin 用户弹币变动
ChangeCoin(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error) 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 { if coin.Coin < 0 {
return errors.New("无法将弹币更新至负数") 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{ if err = m.Insert(ctx, tx, &UserCoin{
UserId: userId, UserId: userId,
Coin: change, Coin: change,
}); err != nil { }); err != nil {
return nerr.NewError(nerr.NewUserCoinErr, "新建用户弹币记录失败") 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, "用户弹币不足") return nerr.NewError(nerr.UserCoinNotEnoughErr, "用户弹币不足")
} }
data.Coin += change 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) { if errors.Is(err, gormx.ErrRowsAffectedZero) {
return err return err
} }

Loading…
Cancel
Save