go_dreamfactory/modules/moonlv/model_moonlv.go
2024-01-19 10:26:22 +08:00

51 lines
1.3 KiB
Go

package moonlv
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"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 modelMoonlv struct {
modules.MCompModel
module *Moonlv
}
func (this *modelMoonlv) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = string(comm.TableMoonlv)
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Moonlv)
// uid 创建索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelMoonlv) getMoonlvList(session comm.IUserSession) (result *pb.DBMoonLv, err error) {
uid := session.GetUserId()
result = &pb.DBMoonLv{}
if err = this.Get(uid, result); err != nil {
if mongo.ErrNoDocuments == err {
result.Id = primitive.NewObjectID().Hex()
result.Uid = uid
result.Cid = 1 // 默认1级
result.Reward = make(map[int32]bool)
result.Tasks = make(map[int32]int32)
this.Add(uid, result)
err = nil
}
return
}
return
}
func (this *modelMoonlv) modifyMoonlvList(uid string, data map[string]interface{}) error {
return this.Change(uid, data)
}