go_dreamfactory/modules/integral/model_rank.go

74 lines
1.7 KiB
Go

package integral
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/modules"
"go_dreamfactory/sys/db"
"strconv"
"github.com/go-redis/redis/v8"
)
type modelRank struct {
modules.MCompModel
module *Integral
}
// 组件初始化接口
func (this *modelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableIntegralRank
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Integral)
return
}
// 更新排名
func (this *modelRank) updateRank(Score int32, uid string, nandu int) (err error) {
var (
pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO())
menbers *redis.Z
cmd *redis.IntCmd
tableName string
)
menbers = &redis.Z{Score: float64(Score), Member: uid}
tableName = fmt.Sprintf("%s-%s", db.CrossTag(), this.TableName+strconv.Itoa(int(nandu)))
if cmd = pipe.ZAdd(tableName, menbers); err != nil {
this.module.Errorln(err)
}
if _, err = pipe.Exec(); err != nil {
this.module.Errorln(err)
return
}
if _, err = cmd.Result(); err != nil {
this.module.Errorln(err)
return
}
return
}
// 获取排行榜前50的用户名单
func (this *modelRank) queryIntegralRankUser(nandu int) (ranks []string, err error) {
var (
result []string
tableName string
)
tableName = fmt.Sprintf("%s-%s", db.CrossTag(), this.TableName+strconv.Itoa(int(nandu)))
if result, err = this.DBModel.Redis.ZRevRange(tableName, 0, comm.MaxRankList).Result(); err != nil {
this.module.Errorln(err)
return
}
ranks = make([]string, 0)
for i := 0; i < len(result); i += 1 {
ranks = append(ranks, result[i])
}
return
}