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.

66 lines
1.6 KiB
Go

package model
import (
"context"
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
"gorm.io/gorm"
)
var _ UserGradeModel = (*customUserGradeModel)(nil)
const (
DefaultUserGrade = 1
DefaultUserGradeLevel = 1
DefaultUserGradeStar = 0
DefaultUserGradeBravePoint = 0
MaxUserGrade = 9
MaxUserGradeLevel = 4
MaxUserGradeStar = 5
MaxUserGradeBravePoint = 3000
)
type (
// UserGradeModel is an interface to be customized, add more methods here,
// and implement the added methods in customUserGradeModel.
UserGradeModel interface {
userGradeModel
UpdateOptimistic(ctx context.Context, tx *gorm.DB, data *UserGrade) error
}
customUserGradeModel struct {
*defaultUserGradeModel
}
)
// NewUserGradeModel returns a model for the database table.
func NewUserGradeModel(conn *gorm.DB) UserGradeModel {
return &customUserGradeModel{
defaultUserGradeModel: newUserGradeModel(conn),
}
}
func (m *customUserGradeModel) UpdateOptimistic(ctx context.Context, tx *gorm.DB, data *UserGrade) error {
if data.Grade < DefaultUserGrade {
data.Grade = DefaultUserGrade
}
if data.Level < DefaultUserGradeLevel {
data.Level = DefaultUserGradeLevel
}
if data.Star < DefaultUserGradeStar {
data.Star = DefaultUserGradeStar
}
if data.BravePoint < DefaultUserGradeBravePoint {
data.BravePoint = DefaultUserGradeBravePoint
}
db := gormx.WithTx(ctx, m.DB, tx)
result := db.Model(&data).
Updates(map[string]interface{}{
"grade": data.Grade,
"level": data.Level,
"star": data.Star,
"brave_point": data.BravePoint,
})
return gormx.WrapUpdateErr(result.Error, result.RowsAffected)
}