go_dreamfactory/modules/entertainment/model.go
2023-11-17 16:11:32 +08:00

92 lines
2.2 KiB
Go

package entertainment
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"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 *Entertainment
}
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableEntertainm
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Entertainment)
// 通过uid创建索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelComp) getEntertainmList(uid string) (result *pb.DBXXLData, err error) {
var (
dbModel *db.DBModel
)
result = &pb.DBXXLData{
Reward: map[int32]int32{},
Card: map[string]int32{},
}
if db.IsCross() {
if tag, _, b := utils.UIdSplit(uid); b {
if conn, err := db.ServerDBConn(tag); err == nil {
dbModel = db.NewDBModel(comm.TableEntertainm, conn)
}
}
}
if dbModel == nil {
err = this.Get(uid, result)
} else {
err = dbModel.Get(uid, result)
}
if err != nil {
if redis.RedisNil != err { // 没有数据直接创建新的数据
result.Id = primitive.NewObjectID().Hex()
result.Uid = uid
result.Reward = make(map[int32]int32)
result.Card = make(map[string]int32)
}
// 初始化默认获得类型为1的卡片
for _, v := range this.module.configure.GetInitGameConsumeHero() {
result.Card[v] = 1
}
if dbModel == nil {
err = this.Add(uid, result)
} else {
err = dbModel.Add(uid, result)
}
}
err = nil
return result, err
}
func (this *modelComp) modifyEntertainmList(uid string, data map[string]interface{}) error {
var (
err error
)
if db.IsCross() {
if tag, _, b := utils.UIdSplit(uid); b {
if conn, err := db.ServerDBConn(tag); err == nil {
dbModel := db.NewDBModel(comm.TableEntertainm, conn)
err = dbModel.Change(uid, data)
}
}
} else {
err = this.Change(uid, data)
}
return err
}