58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package timer
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/cron"
|
|
"go_dreamfactory/lego/sys/log"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type PagodaRank struct {
|
|
modules.MCompModel
|
|
service core.IService
|
|
}
|
|
|
|
//组件初始化接口
|
|
func (this *PagodaRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
|
|
this.TableName = comm.TablePagodaRecord
|
|
this.MCompModel.Init(service, module, comp, options)
|
|
this.service = service
|
|
return
|
|
}
|
|
|
|
func (this *PagodaRank) Start() (err error) {
|
|
err = this.MCompModel.Start()
|
|
cron.AddFunc("*/60 * * * * ?", this.Timer) //每60s执行一次
|
|
return
|
|
}
|
|
|
|
// 处理排行榜排序
|
|
func (this *PagodaRank) Timer() {
|
|
data := make([]interface{}, 0) // options.Find().SetLimit(comm.MaxRankList)
|
|
if _data, err := this.DB.Find(comm.TableSeasonPagoda, bson.M{}, options.Find().SetSort(bson.M{"pagodaId": -1}).SetLimit(comm.MaxRankList)); err == nil {
|
|
for _data.Next(context.TODO()) {
|
|
temp := &pb.DBPagodaRecord{}
|
|
if err = _data.Decode(temp); err == nil {
|
|
data = append(data, temp)
|
|
}
|
|
}
|
|
}
|
|
if len(data) > 0 {
|
|
err := this.Redis.RPush(comm.TablePagodaRankList, data...)
|
|
if err == nil {
|
|
err = this.Redis.Ltrim(comm.TablePagodaRankList, -1*len(data), -1) //对一个列表进行修剪
|
|
if err != nil {
|
|
log.Errorf("delete failed")
|
|
}
|
|
}
|
|
}
|
|
}
|