package user import ( "fmt" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/utils" "time" uuid "github.com/satori/go.uuid" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) const ( //Redis TableUser core.SqlTable = "user" //用户表 ) type ModelUser struct { modules.MCompModel } func (this *ModelUser) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { err = this.MCompModel.Init(service, module, comp, options) this.TableName = string(TableUser) return } func (this *ModelUser) FindByAccount(sid int32, account string) (*pb.DBUser, error) { filter := bson.M{ "sid": sid, "binduid": account, } sr := this.DB.FindOne(TableUser, filter) var nd *pb.DBUser err := sr.Decode(&nd) return nd, err } //查询昵称 func (this *ModelUser) NickNameIsExist(name string) bool { if err := this.DB.FindOne(TableUser, bson.M{"name": name}).Err(); err != nil { if err == mongo.ErrNoDocuments { //无记录 return true } } return false } //创建初始用户 func (this *ModelUser) User_Create(user *pb.DBUser) (err error) { now := time.Now().Unix() _id := primitive.NewObjectID().Hex() user.Id = _id user.Uid = fmt.Sprintf("%d_%s", user.Sid, _id) user.Uuid = uuid.NewV4().String() user.Lv = 1 //初始等级 user.Ctime = now user.Logintime = now return this.Add(user.Uid, user) } //获取用户 func (this *ModelUser) GetUser(uid string) (user *pb.DBUser) { user = &pb.DBUser{} if err := this.Get(uid, user); err != nil { log.Errorf("getUser err:%v", err) return } return } //设置属性 func (this *ModelUser) updateUserAttr(uid string, data map[string]interface{}) error { return this.Change(uid, data) } //是否今天首次登录 func (this *ModelUser) isLoginFirst(timestamp int64) bool { now := time.Now() if timestamp == 0 || timestamp > now.Unix() { log.Debugf("lastlogin time great now") return false } tt := time.Unix(timestamp, 0) if !utils.IsToday(timestamp) { if tt.Before(now) { return true } } return false } //删除用户数据 func (this *ModelUser) delete(uid string) { if err := this.Del(uid); err != nil { log.Errorf("%v", err) } } // 修改玩家名字 func (this *ModelUser) modifyName(uid string, newName string) (code pb.ErrorCode) { user := this.GetUser(uid) if user == nil { code = pb.ErrorCode_UserSessionNobeing return } // 修改名称 update := map[string]interface{}{ "name": newName, } if err := this.Change(uid, update); err != nil { code = pb.ErrorCode_DBError return } return } // func(this *ModelUser) UpLevel(){ }