go_dreamfactory/modules/user/model_expand.go
2023-06-29 09:33:02 +08:00

115 lines
3.3 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_dreamfactory/utils"
"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{}
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,
"modifynameCount": 1, //修改名称1次
"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)
}
// 累计登录天数 和 全局buff清理
func (this *ModelExpand) updateLoginDay(uid string, timestamp int64) (err error) {
var de *pb.DBUserExpand
if de, err = this.GetUserExpand(uid); err == nil {
count := de.LoginAddCount + 1
update := map[string]interface{}{
"loginAddCount": count,
"globalbuff": 0,
}
// 更新连续等登录天数
if utils.IsYestoday(timestamp) {
update["loginContinueCount"] = de.LoginContinueCount + 1
}
err = this.ChangeUserExpand(uid, update)
}
return
}