go_dreamfactory/modules/battlerecord/model.go
2023-08-25 11:01:48 +08:00

107 lines
2.9 KiB
Go

package battlerecord
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"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)}},
})
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) getUserBurieds(uid string) (results *pb.DBBuried, err error) {
results = &pb.DBBuried{}
if err = this.model.Get(uid, results); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
err = nil
results = &pb.DBBuried{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Items: make(map[int32]*pb.DBBuriedItem),
}
err = this.model.Add(uid, results)
}
return
}
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
}