go_dreamfactory/modules/mainline/model_mainline.go
2023-07-25 16:14:29 +08:00

68 lines
1.9 KiB
Go

package mainline
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type ModelMline struct {
modules.MCompModel
module *Mainline
}
func (this *ModelMline) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableMainline
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Mainline)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取用户全部的埋点数据
func (this *ModelMline) getMainlineData(uid string) (results *pb.DBMainline, err error) {
results = &pb.DBMainline{}
if err = this.Get(uid, results); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
err = nil
results = &pb.DBMainline{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Level: make(map[int32]int32),
Chapteraward: make(map[int32]*pb.DBMainlineAward),
Exploreaward: make(map[int32]*pb.DBMainlineAward),
Groupaward: make(map[int32]*pb.DBMainlineAward),
Ps: make(map[int32]int32),
}
err = this.Add(uid, results)
}
return
}
func (this *ModelMline) updateMainlineData(uid string, data *pb.DBMainline) (err error) {
if err = this.Change(uid, map[string]interface{}{
"Level": data.Level,
"chapteraward": data.Chapteraward,
"exploreaward": data.Exploreaward,
"groupaward": data.Groupaward,
"ps": data.Ps,
}); err != nil {
this.module.Error("更新用户任务数据 错误!", log.Field{Key: "err", Value: err.Error()})
return
}
return
}