go_dreamfactory/sys/db/user.go
2022-06-10 14:20:25 +08:00

64 lines
1.4 KiB
Go

package db
import (
"go_dreamfactory/pb"
"go_dreamfactory/lego/core"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
const ( //Redis
DB_UserTable core.SqlTable = "user" //用户表
)
type IUser interface {
User_FindByAccount(user *pb.DB_UserData) (*pb.DB_UserData, error)
User_FindById(id string) (*pb.DB_UserData, error)
User_Create(user *pb.DB_UserData) error
User_Update(data *pb.DB_UserData) (err error)
}
//
func (this *DB) User_FindByAccount(user *pb.DB_UserData) (*pb.DB_UserData, error) {
filter := bson.M{
"serverid": user.ServerId,
"account": user.Account,
}
sr := this.mgo.FindOne(DB_UserTable, filter)
var nd *pb.DB_UserData
err := sr.Decode(&nd)
return nd, err
}
func (this *DB) User_FindById(id string) (*pb.DB_UserData, error) {
filter := bson.M{
"userid": id,
}
sr := this.mgo.FindOne(DB_UserTable, filter)
user := &pb.DB_UserData{}
err := sr.Decode(user)
return user, err
}
func (this *DB) User_Create(user *pb.DB_UserData) (err error) {
user.UserId = primitive.NewObjectID().Hex()
_, err = this.mgo.InsertOne(DB_UserTable, user)
return err
}
//更新用户数据到DB
func (this *DB) User_Update(data *pb.DB_UserData) (err error) {
err = this.mgo.FindOneAndUpdate(
DB_UserTable,
bson.M{"userid": data.UserId},
bson.M{"$set": bson.M{
"nicename": data.NiceName,
}},
options.FindOneAndUpdate().SetUpsert(true),
).Err()
return err
}