go_dreamfactory/modules/user/model_expand.go

97 lines
2.8 KiB
Go

package user
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
// 记录一些扩展数据
type ModelExpand struct {
modules.MCompModel
module *User
}
func (this *ModelExpand) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableUserExpand
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*User)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取用户通过扩展表
func (this *ModelExpand) GetUserExpand(uid string) (result *pb.DBUserExpand, err error) {
result = &pb.DBUserExpand{
Expitem: map[string]int32{},
Race: map[int32]int32{},
Herofrag: map[string]int32{},
}
if db.IsCross() {
if model, err := this.module.GetDBModelByUid(uid, this.TableName); err != nil {
this.module.Error("Cross GetDBModuleByUid", log.Field{Key: "uid", Value: uid})
return result, err
} else {
if err = model.Get(uid, result); err != nil && mongo.ErrNoDocuments != err {
this.module.Error("Cross Get", log.Field{Key: "uid", Value: uid})
return result, err
}
}
} else {
if err = this.Get(uid, result); err != nil && mongo.ErrNoDocuments == err {
globalConf := this.module.ModuleTools.GetGlobalConf()
initUpdate := map[string]interface{}{
"uid": uid,
"sociatyTicket": globalConf.GuildBossInitialNum, //公会BOSS挑战券
"expitem": make(map[string]int32, 0), // 初始化
"mline": make(map[string]int32, 0),
"race": make(map[string]int32, 0),
"givetime": configure.Now().Unix(),
"herofrag": make(map[string]int32, 0), // 初始化
}
result.SociatyTicket = globalConf.GuildBossInitialNum
if err = this.module.modelExpand.ChangeUserExpand(uid, initUpdate); err != nil {
this.module.Error("创建初始修改名称次数",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "params", Value: initUpdate},
log.Field{Key: "err", Value: err.Error()},
)
return
}
// this.module.Error("Get", log.Field{Key: "uid", Value: uid})
return result, err
}
}
return
}
// 修改用户扩展数据
func (this *ModelExpand) ChangeUserExpand(uid string, value map[string]interface{}) (err error) {
if len(value) == 0 {
return nil
}
var (
model *db.DBModel
)
if db.IsCross() {
if model, err = this.module.GetDBModelByUid(uid, this.TableName); err == nil {
return model.Change(uid, value)
} else {
this.module.Errorln(err)
return err
}
}
return this.Change(uid, value)
}