70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package integral
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/redis/pipe"
|
|
"go_dreamfactory/modules"
|
|
"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
|
|
)
|
|
|
|
menbers = &redis.Z{Score: float64(Score), Member: uid}
|
|
|
|
if cmd = pipe.ZAdd(this.TableName+strconv.Itoa(nandu), 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
|
|
)
|
|
if result, err = this.DBModel.Redis.ZRevRange(fmt.Sprintf("%s-%s", this.DBModel.ServiceId, this.TableName+strconv.Itoa(nandu)), 0, 50).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
|
|
}
|