go_dreamfactory/modules/user/model_expand.go
2022-09-14 19:06:31 +08:00

72 lines
1.7 KiB
Go

package user
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"go.mongodb.org/mongo-driver/mongo"
)
// 记录一些扩展数据
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)
return
}
//获取用户通过扩展表
func (this *ModelExpand) GetUserExpand(uid string) (result *pb.DBUserExpand, err error) {
result = &pb.DBUserExpand{}
if err = this.module.modelExpand.Get(uid, result); err != nil && mongo.ErrNoDocuments != err {
return
}
err = nil
return result, err
}
//修改用户扩展数据
func (this *ModelExpand) ChangeUserExpand(uid string, value map[string]interface{}) (err error) {
if len(value) == 0 {
return nil
}
return this.module.modelExpand.Change(uid, value)
}
// 累计登录天数
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,
}
// 更新连续等登录天数
if utils.IsYestoday(timestamp) {
update["loginContinueCount"] = de.LoginContinueCount + 1
}
err = this.ChangeUserExpand(uid, update)
}
return
}
// 通关了普通塔
func (this *ModelExpand) CompleteNormalPagoda(uid string) (err error) {
update := map[string]interface{}{
"completePagoda": true,
}
err = this.ChangeUserExpand(uid, update)
return
}