90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
![]() |
package model
|
|||
|
|
|||
|
import (
|
|||
|
"fmt"
|
|||
|
"github.com/go-redis/redis/v8"
|
|||
|
"gorm.io/gorm"
|
|||
|
"samba/pkg/log"
|
|||
|
"samba/util/rdbkey"
|
|||
|
"samba/util/util"
|
|||
|
)
|
|||
|
|
|||
|
type ClubInfo struct {
|
|||
|
ID int `gorm:"column:id" json:"id"`
|
|||
|
Name string `gorm:"column:name" json:"name"` // 俱乐部id
|
|||
|
PrettyNo int `gorm:"column:pretty_no" json:"pretty_no"`
|
|||
|
Status int `gorm:"column:status" json:"status"` // 状态,1-正常,0-禁用
|
|||
|
HeadUrl string `gorm:"column:head_url" json:"head_url"`
|
|||
|
IconId int `gorm:"column:iconid" json:"iconid"`
|
|||
|
Exp int64 `gorm:"column:exp" json:"exp"` // 经验值
|
|||
|
Level int `gorm:"column:level" json:"level"`
|
|||
|
UserNums int `gorm:"column:usernums" json:"usernums"` // 俱乐部人数
|
|||
|
SolidFee int `gorm:"column:solid_fee" json:"solid_fee"` // 固定分佣比例
|
|||
|
Score int64 `gorm:"column:score" json:"score"` // 俱乐部积分
|
|||
|
Creator int64 `gorm:"column:creator" json:"creator"` // 俱乐部创建者
|
|||
|
Open int `gorm:"column:open" json:"open"` // 1打开,0关闭
|
|||
|
UserScore int64 `gorm:"column:user_score" json:"user_score"`
|
|||
|
|
|||
|
CreatorName string `gorm:"-" json:"-"` // 俱乐部创建者名称
|
|||
|
}
|
|||
|
|
|||
|
func (t *ClubInfo) TableName() string {
|
|||
|
return "t_club_info"
|
|||
|
}
|
|||
|
|
|||
|
func (t *ClubInfo) HasRobot() bool {
|
|||
|
// TODO 白名单配置
|
|||
|
return true
|
|||
|
}
|
|||
|
|
|||
|
type ClubInfoOp struct {
|
|||
|
db *gorm.DB
|
|||
|
rdb *redis.Client
|
|||
|
}
|
|||
|
|
|||
|
func NewClubInfoOp() *ClubInfoOp {
|
|||
|
return &ClubInfoOp{rdb: rdbBaseInfo, db: userDB}
|
|||
|
}
|
|||
|
|
|||
|
func (op *ClubInfoOp) LoadDb(clubId int) (*ClubInfo, error) {
|
|||
|
var user ClubInfo
|
|||
|
result := op.db.Where("id = ? And status = 1", clubId).First(&user)
|
|||
|
if result.Error != nil {
|
|||
|
return nil, result.Error
|
|||
|
}
|
|||
|
return &user, result.Error
|
|||
|
}
|
|||
|
|
|||
|
func (op *ClubInfoOp) LoadAll() ([]ClubInfo, error) {
|
|||
|
var clubs []ClubInfo
|
|||
|
result := op.db.Where("status = 1").Find(&clubs)
|
|||
|
if result.Error != nil {
|
|||
|
return nil, result.Error
|
|||
|
}
|
|||
|
return clubs, result.Error
|
|||
|
}
|
|||
|
|
|||
|
func (op *ClubInfoOp) Load(clubId int) (*ClubInfo, error) {
|
|||
|
exist := true
|
|||
|
club, err := util.Redis2Struct[ClubInfo](op.rdb, rdbkey.ClubKey(clubId))
|
|||
|
if err != nil {
|
|||
|
log.Error(fmt.Sprintf("Load club info error: %v.key:%v", err, rdbkey.ClubKey(clubId)))
|
|||
|
exist = false
|
|||
|
}
|
|||
|
|
|||
|
creator, err := NewUserInfoOp().Load(club.Creator)
|
|||
|
if err != nil {
|
|||
|
return nil, err
|
|||
|
}
|
|||
|
club.CreatorName = creator.MNick
|
|||
|
|
|||
|
// 回写redis
|
|||
|
if exist {
|
|||
|
err = util.Struct2Redis(op.rdb, rdbkey.ClubKey(clubId), club)
|
|||
|
if err != nil {
|
|||
|
log.Error(fmt.Sprintf("back write club info error: %v.key:%v", err, rdbkey.ClubKey(clubId)))
|
|||
|
}
|
|||
|
}
|
|||
|
return club, nil
|
|||
|
}
|