|
|
|
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 _ UserIntegralModel = (*customUserIntegralModel)(nil)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// UserIntegralModel is an interface to be customized, add more methods here,
|
|
|
|
// and implement the added methods in customUserIntegralModel.
|
|
|
|
UserIntegralModel interface {
|
|
|
|
userIntegralModel
|
|
|
|
UpdateIntegral(ctx context.Context, tx *gorm.DB, integral *UserIntegral) error
|
|
|
|
// ChangeIntegral 用户积分变动
|
|
|
|
ChangeIntegral(ctx context.Context, tx *gorm.DB, userId int64, change int64, ignoreNotEnough bool) (int64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
customUserIntegralModel struct {
|
|
|
|
*defaultUserIntegralModel
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewUserIntegralModel returns a model for the database table.
|
|
|
|
func NewUserIntegralModel(conn *gorm.DB) UserIntegralModel {
|
|
|
|
return &customUserIntegralModel{
|
|
|
|
defaultUserIntegralModel: newUserIntegralModel(conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customUserIntegralModel) UpdateIntegral(ctx context.Context, tx *gorm.DB, integral *UserIntegral) error {
|
|
|
|
if integral.Integral < 0 {
|
|
|
|
integral.Integral = 0
|
|
|
|
}
|
|
|
|
db := gormx.WithTx(ctx, m.DB, tx)
|
|
|
|
|
|
|
|
result := db.Model(&integral).Update("integral", integral.Integral)
|
|
|
|
return gormx.WrapUpdateErr(result.Error, result.RowsAffected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customUserIntegralModel) ChangeIntegral(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.UserIntegralNotEnoughError, "用户积分不足")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 用户积分记录不存在,进行插入
|
|
|
|
if err = m.Insert(ctx, tx, &UserIntegral{
|
|
|
|
UserId: userId,
|
|
|
|
Integral: change,
|
|
|
|
}); err != nil {
|
|
|
|
return nerr.NewError(nerr.NewUserIntegralError, "新建用户积分失败")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return nerr.NewError(nerr.GetUserIntegralError, "获取当前用户积分失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if data.Integral+change < 0 {
|
|
|
|
if ignoreNotEnough {
|
|
|
|
data.Integral = 0
|
|
|
|
} else {
|
|
|
|
return nerr.NewError(nerr.UserIntegralNotEnoughError, "用户积分不足")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.Integral += change
|
|
|
|
if err = m.UpdateIntegral(ctx, tx, data); err != nil {
|
|
|
|
if errors.Is(err, gormx.ErrRowsAffectedZero) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nerr.NewError(nerr.UpdateUserIntegralError, "更新用户积分失败")
|
|
|
|
}
|
|
|
|
resp = data.Integral
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}, func(err error) bool {
|
|
|
|
return errors.Is(err, gormx.ErrRowsAffectedZero)
|
|
|
|
})
|
|
|
|
return resp, err
|
|
|
|
}
|