84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
![]() |
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"gorm.io/gorm"
|
||
|
"samba/pkg/log"
|
||
|
"samba/util/rdbkey"
|
||
|
"sort"
|
||
|
)
|
||
|
|
||
|
type ClubLevel struct {
|
||
|
ID int `gorm:"column:id" json:"id"`
|
||
|
Level int `gorm:"column:level" json:"level"`
|
||
|
Exp int64 `gorm:"column:exp" json:"exp"` // 经验值
|
||
|
Usernums int `gorm:"column:usernums" json:"usernums"`
|
||
|
Fee int `gorm:"column:fee" json:"fee"` // 俱乐部分佣比例,百分位
|
||
|
Number int `gorm:"column:number" json:"number"` // 数量个数
|
||
|
}
|
||
|
|
||
|
func (t *ClubLevel) TableName() string {
|
||
|
return "t_club_level_config"
|
||
|
}
|
||
|
|
||
|
type ClubLevelOp struct {
|
||
|
rdb *redis.Client
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
|
||
|
func NewClubLevelOp() *ClubLevelOp {
|
||
|
return &ClubLevelOp{rdb: rdbConfig, db: configDB}
|
||
|
}
|
||
|
|
||
|
func (op *ClubLevelOp) loadLevelTable() ([]*ClubLevel, error) {
|
||
|
var clubLevels []*ClubLevel
|
||
|
maps, err := op.rdb.HGetAll(context.Background(), rdbkey.ClubLevelKey()).Result()
|
||
|
if err != nil {
|
||
|
result := op.db.Order("level asc").First(&clubLevels)
|
||
|
if result.Error != nil {
|
||
|
log.Error(fmt.Sprintf("select table:%v fail", (&ClubLevel{}).TableName()))
|
||
|
return []*ClubLevel{}, result.Error
|
||
|
}
|
||
|
return clubLevels, nil
|
||
|
}
|
||
|
for k, m := range maps {
|
||
|
var clubLevel ClubLevel
|
||
|
if err = json.Unmarshal([]byte(m), &clubLevel); err != nil {
|
||
|
log.Error(fmt.Sprintf("unmarshal fail. err:%v. rdbkey:%v field key:%v value:%v", err, rdbkey.ClubLevelKey(), k, m))
|
||
|
return []*ClubLevel{}, err
|
||
|
}
|
||
|
clubLevels = append(clubLevels, &clubLevel)
|
||
|
}
|
||
|
sort.Slice(clubLevels, func(i, j int) bool {
|
||
|
return clubLevels[i].Level < clubLevels[j].Level
|
||
|
})
|
||
|
return clubLevels, nil
|
||
|
}
|
||
|
|
||
|
func (op *ClubLevelOp) GetLevel(clubInfo *ClubInfo) *ClubLevel {
|
||
|
levels, _ := op.loadLevelTable()
|
||
|
if len(levels) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
var cl *ClubLevel
|
||
|
for _, clubLevel := range levels {
|
||
|
if clubInfo.Exp >= clubLevel.Exp {
|
||
|
cl = clubLevel
|
||
|
} else {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
return cl
|
||
|
}
|
||
|
|
||
|
func (op *ClubLevelOp) GetLevelByClubId(clubId int) *ClubLevel {
|
||
|
clubInfo, _ := NewClubInfoOp().Load(clubId)
|
||
|
if clubInfo == nil {
|
||
|
return nil
|
||
|
}
|
||
|
return op.GetLevel(clubInfo)
|
||
|
}
|