go_dreamfactory/modules/entertainment/modelrecord.go
2024-01-02 18:08:32 +08:00

71 lines
1.8 KiB
Go

package entertainment
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelRecode struct {
modules.MCompModel
module *Entertainment
}
// 组件初始化接口
func (this *modelRecode) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableEntertainRecode
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Entertainment)
// 通过uid创建索引
if _, err = this.DB.CreateMany(core.SqlTable(this.TableName), []mongo.IndexModel{
{
Keys: bsonx.Doc{{Key: "p1", Value: bsonx.Int32(1)},
{Key: "p2", Value: bsonx.Int32(1)}},
},
}); err != nil {
return
}
return
}
// 更新排名
func (this *modelRecode) updateXxlRecord(record *pb.DBXxlRecord) (err error) {
if _, err = this.DBModel.DB.InsertOne(core.SqlTable(this.TableName), record); err != nil {
this.module.Errorln(err)
return
}
return
}
func (this *modelRecode) getXxlRecord(uid string) (results []*pb.DBXxlRecord, err error) {
var (
cursor *mongo.Cursor
)
results = make([]*pb.DBXxlRecord, 0)
if cursor, err = this.DBModel.DB.Find(comm.TableEntertainRecode, bson.M{"$or": []bson.M{{"p1": uid},
{"p2": uid}}}, options.Find().SetSort(bson.M{"ctime": -1}),
options.Find().SetLimit(int64(comm.MaxRankList))); err != nil {
this.module.Errorln(err)
return
} else {
for cursor.Next(context.Background()) {
temp := &pb.DBXxlRecord{}
if err = cursor.Decode(temp); err != nil {
this.module.Errorln(err)
return
}
results = append(results, temp)
}
}
return
}