go_dreamfactory/modules/catchbugs/model.go
2024-03-07 18:40:29 +08:00

79 lines
2.0 KiB
Go

package catchbugs
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/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelComp struct {
modules.MCompModel
module *CatchBugs
}
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*CatchBugs)
this.TableName = comm.TablekCanineRabbit
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取用户全部的埋点数据
func (this *modelComp) getModel(uid string) (info *pb.DBCatchBugs, err error) {
info = &pb.DBCatchBugs{}
if err = this.Get(uid, info); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
info = &pb.DBCatchBugs{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Books: make(map[int32]int32),
Weekaward: make(map[int32]bool),
Allaward: make(map[int32]bool),
}
err = this.Add(uid, info)
}
return
}
// 获取用户全部的埋点数据
func (this *modelComp) getCorssModel(uid string) (info *pb.DBCatchBugs, err error) {
var (
model *db.DBModel
)
info = &pb.DBCatchBugs{}
if !this.module.IsCross() {
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
this.module.Errorln(err)
} else {
if err = model.Get(uid, info); err != nil && err != mgo.MongodbNil {
this.module.Errorf("err:%v", err)
}
if err == mgo.MongodbNil {
info = &pb.DBCatchBugs{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Books: make(map[int32]int32),
Weekaward: make(map[int32]bool),
Allaward: make(map[int32]bool),
}
err = model.Add(uid, info)
}
}
}
return
}