package buried import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/lego/sys/redis" "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 modelBuried struct { modules.MCompModel module *Buried } func (this *modelBuried) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = string(comm.TableBuried) err = this.MCompModel.Init(service, module, comp, options) this.module = module.(*Buried) // uid 创建索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } // 获取用户全部的埋点数据 func (this *modelBuried) getUserBurieds(uid string) (results *pb.DBBuried, err error) { results = &pb.DBBuried{} if err = this.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.Add(uid, results) } return } // 更新用户数据 func (this *modelBuried) updateUserBurieds(uid string, data *pb.DBBuried) (err error) { err = this.Change(uid, map[string]interface{}{ "items": data.Items, }) return } func (this *modelBuried) userlock(uid string) (result *redis.RedisMutex, err error) { return this.DBModel.Redis.NewRedisMutex(fmt.Sprintf("ulockburied:%s", uid)) } // 更新埋点数据到db中 func (this *modelBuried) getburiedModel(uid string) (model *buriedModel, err error) { var m *db.DBModel if db.IsCross() { if m, err = this.module.GetDBModelByUid(uid, this.TableName); err != nil { return } model = &buriedModel{module: this.module, model: m} } else { model = &buriedModel{module: this.module, model: this.DBModel} } return } // 埋点专属模型 会封装特殊的数据转换接口 type buriedModel struct { module *Buried model *db.DBModel } // 获取用户全部的埋点数据 func (this *buriedModel) 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 *buriedModel) updateUserBurieds(uid string, data *pb.DBBuried) (err error) { err = this.model.Change(uid, map[string]interface{}{ "items": data.Items, }) return }