go_dreamfactory/modules/battlerecord/model.go
2023-08-31 16:18:43 +08:00

92 lines
2.7 KiB
Go

package battlerecord
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
mgooptions "go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelComp struct {
modules.MCompModel
module *BattleRecord
}
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = string(comm.TableBattlerecord)
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*BattleRecord)
// uid 创建索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
indexModel := mongo.IndexModel{
Keys: bson.M{"expireAt": 1}, // 设置TTL索引列"expire_date"
Options: mgooptions.Index().SetExpireAfterSeconds(0), // 设置过期时间(单位:秒)
}
_, err = this.DB.CreateIndex(core.SqlTable(this.TableName), indexModel) //设置 验证码过期时间索引
return
}
func (this *modelComp) inquire(bid string) (result *pb.DBBattlePlayRecord, err error) {
result = &pb.DBBattlePlayRecord{}
if err = this.DBModel.DB.FindOne(core.SqlTable(this.TableName), bson.M{"_id": bid}).Decode(result); err != nil {
this.module.Errorln(err)
return
}
return
}
//插入记录
func (this *modelComp) addRecord(result *pb.DBBattlePlayRecord) (err error) {
if _, err = this.DBModel.DB.InsertOne(core.SqlTable(this.TableName), result); err != nil {
this.module.Errorln(err)
return
}
return
}
//获取记录模型对象
func (this *modelComp) getrecordModel(uid string) (model *recordModel, err error) {
var m *db.DBModel
if db.IsCross() {
if m, err = this.module.GetDBModelByUid(uid, this.TableName); err != nil {
return
}
model = &recordModel{module: this.module, model: m}
} else {
model = &recordModel{module: this.module, model: this.DBModel}
}
return
}
// 埋点专属模型 会封装特殊的数据转换接口
type recordModel struct {
module *BattleRecord
model *db.DBModel
}
func (this *recordModel) inquire(bid string) (result *pb.DBBattlePlayRecord, err error) {
result = &pb.DBBattlePlayRecord{}
if err = this.model.DB.FindOne(core.SqlTable(this.model.TableName), bson.M{"_id": bid}).Decode(result); err != nil {
this.module.Errorln(err)
return
}
return
}
//插入记录
func (this *recordModel) addRecord(result *pb.DBBattlePlayRecord) (err error) {
if _, err = this.model.DB.InsertOne(core.SqlTable(this.model.TableName), result); err != nil {
this.module.Errorln(err)
return
}
return
}