diff --git a/pb/proto/user/user_db.proto b/pb/proto/user/user_db.proto index c79ee461b..e43ceefac 100644 --- a/pb/proto/user/user_db.proto +++ b/pb/proto/user/user_db.proto @@ -8,15 +8,16 @@ message Cache_UserData { } message DB_UserData { - string uid = 1; - string uuid = 2; //玩家唯一uuid - string binduid = 3; //玩家账号 - string name = 4; //玩家名 - int32 sid = 5; //区服id - string createip = 6; //创建账号时的ip - string lastloginip = 7; //最后一次登录时的ip - int64 ctime = 8; //玩家创号时间戳 - int64 logintime = 9; //最后一次登录时间 - int32 FriendPoint = 10; //友情点 - int32 avatar = 11; //头像 + string id = 1; // @go_tags(`bson:"_id"`) ID + string uid = 2; + string uuid = 3; //玩家唯一uuid + string binduid = 4; //玩家账号 + string name = 5; //玩家名 + int32 sid = 6; //区服id + string createip = 7; //创建账号时的ip + string lastloginip = 8; //最后一次登录时的ip + int64 ctime = 9; //玩家创号时间戳 + int64 logintime = 10; //最后一次登录时间 + int32 FriendPoint = 11; //友情点 + int32 avatar = 12; //头像 } \ No newline at end of file diff --git a/pb/user_db.pb.go b/pb/user_db.pb.go index ab1da5a4b..6cfe560eb 100644 --- a/pb/user_db.pb.go +++ b/pb/user_db.pb.go @@ -88,17 +88,18 @@ type DB_UserData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid,omitempty"` - Uuid string `protobuf:"bytes,2,opt,name=uuid,proto3" json:"uuid,omitempty"` //玩家唯一uuid - Binduid string `protobuf:"bytes,3,opt,name=binduid,proto3" json:"binduid,omitempty"` //玩家账号 - Name string `protobuf:"bytes,4,opt,name=name,proto3" json:"name,omitempty"` //玩家名 - Sid int32 `protobuf:"varint,5,opt,name=sid,proto3" json:"sid,omitempty"` //区服id - Createip string `protobuf:"bytes,6,opt,name=createip,proto3" json:"createip,omitempty"` //创建账号时的ip - Lastloginip string `protobuf:"bytes,7,opt,name=lastloginip,proto3" json:"lastloginip,omitempty"` //最后一次登录时的ip - Ctime int64 `protobuf:"varint,8,opt,name=ctime,proto3" json:"ctime,omitempty"` //玩家创号时间戳 - Logintime int64 `protobuf:"varint,9,opt,name=logintime,proto3" json:"logintime,omitempty"` //最后一次登录时间 - FriendPoint int32 `protobuf:"varint,10,opt,name=FriendPoint,proto3" json:"FriendPoint,omitempty"` //友情点 - Avatar int32 `protobuf:"varint,11,opt,name=avatar,proto3" json:"avatar,omitempty"` //头像 + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" bson:"_id"` // ID + Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid,omitempty"` + Uuid string `protobuf:"bytes,3,opt,name=uuid,proto3" json:"uuid,omitempty"` //玩家唯一uuid + Binduid string `protobuf:"bytes,4,opt,name=binduid,proto3" json:"binduid,omitempty"` //玩家账号 + Name string `protobuf:"bytes,5,opt,name=name,proto3" json:"name,omitempty"` //玩家名 + Sid int32 `protobuf:"varint,6,opt,name=sid,proto3" json:"sid,omitempty"` //区服id + Createip string `protobuf:"bytes,7,opt,name=createip,proto3" json:"createip,omitempty"` //创建账号时的ip + Lastloginip string `protobuf:"bytes,8,opt,name=lastloginip,proto3" json:"lastloginip,omitempty"` //最后一次登录时的ip + Ctime int64 `protobuf:"varint,9,opt,name=ctime,proto3" json:"ctime,omitempty"` //玩家创号时间戳 + Logintime int64 `protobuf:"varint,10,opt,name=logintime,proto3" json:"logintime,omitempty"` //最后一次登录时间 + FriendPoint int32 `protobuf:"varint,11,opt,name=FriendPoint,proto3" json:"FriendPoint,omitempty"` //友情点 + Avatar int32 `protobuf:"varint,12,opt,name=avatar,proto3" json:"avatar,omitempty"` //头像 } func (x *DB_UserData) Reset() { @@ -133,6 +134,13 @@ func (*DB_UserData) Descriptor() ([]byte, []int) { return file_user_user_db_proto_rawDescGZIP(), []int{1} } +func (x *DB_UserData) GetId() string { + if x != nil { + return x.Id + } + return "" +} + func (x *DB_UserData) GetUid() string { if x != nil { return x.Uid @@ -222,25 +230,26 @@ var file_user_user_db_proto_rawDesc = []byte{ 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x12, 0x28, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, - 0x61, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0x9f, 0x02, 0x0a, 0x0b, - 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x75, - 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, - 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, - 0x64, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, + 0x61, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0xaf, 0x02, 0x0a, 0x0b, + 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x69, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, + 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, + 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, + 0x64, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, - 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x73, 0x69, - 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x69, 0x70, 0x18, 0x06, 0x20, + 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, + 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x73, 0x69, + 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x69, 0x70, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x69, 0x70, 0x12, 0x20, 0x0a, - 0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x69, 0x70, 0x18, 0x07, 0x20, 0x01, + 0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x69, 0x70, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x69, 0x70, 0x12, - 0x14, 0x0a, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, + 0x14, 0x0a, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74, 0x69, - 0x6d, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74, + 0x6d, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69, - 0x6e, 0x74, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, + 0x6e, 0x74, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, - 0x0b, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x42, 0x06, 0x5a, + 0x0c, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } diff --git a/sys/cache/core.go b/sys/cache/core.go index 79073b21d..1f4236367 100644 --- a/sys/cache/core.go +++ b/sys/cache/core.go @@ -8,7 +8,6 @@ type ( ISys interface { IUser //户模块的相关缓存接口 IPack //背包模块的线管缓存接口 - IMail //邮件相关的缓存接口 IFriend //好友相关的缓存接口 } ) diff --git a/sys/cache/mail.go b/sys/cache/mail.go deleted file mode 100644 index 53b2ec73e..000000000 --- a/sys/cache/mail.go +++ /dev/null @@ -1,30 +0,0 @@ -package cache - -import ( - "go_dreamfactory/pb" - "go_dreamfactory/sys/db" -) - -// 邮件暂时不写redis -const ( //Redis - Redis_MailCache string = "mail:%s" -) - -type IMail interface { - QueryUserMail(uId string) (mail []*pb.DB_MailData, err error) -} - -// 查询玩家邮件数据 -func (this *Cache) QueryUserMail(uId string) (mail []*pb.DB_MailData, err error) { - // var result []interface{} - // if result, err = this.redis.HGetAll(fmt.Sprintf(Redis_MailCache, uId), reflect.TypeOf(&pb.DB_MailData{})); err != nil { - // return - // } else { - // mail = make([]*pb.DB_MailData, len(result)) - // for i, v := range result { - // mail[i] = v.(*pb.DB_MailData) - // } - // } - mail, _ = db.Defsys.Mail_QueryUserMail(uId) - return -} diff --git a/sys/db/model.go b/sys/db/model.go new file mode 100644 index 000000000..a08b3ed08 --- /dev/null +++ b/sys/db/model.go @@ -0,0 +1,84 @@ +package db + +import ( + "errors" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" + + "go.mongodb.org/mongo-driver/bson" +) + +type Autogenerated struct { + ID string `json:"_id"` + UID string `json:"uid"` + Act string `json:"act"` // insert update delete + D []interface{} +} + +// type data struct { +// Table string +// Wheremap [map[string]]interface{} // 如果是insert 条件就是nil del 只有条件 +// Modifymap map[string]map[string]interface{} +// insertData []interface{} +// } + +type QueryStruct struct { + Selector bson.M + Query bson.M +} + +const ( + DB_ModelTable core.SqlTable = "model" +) + +type IModel interface { + Model_UpdateDBBylog() (err error) // 读取日志并更新对应的表 + Model_InsertDBBylog(data *Autogenerated) (err error) // 插入日志 + Model_UpdateUserDataByUid(uid string) (err error) // 读取指定玩家信息到db中 +} + +func (this *DB) Mail_UpdateDBBylog() (err error) { + data := &Autogenerated{} + this.mgo.FindOneAndDelete(DB_ModelTable, bson.M{}).Decode(data) // 查找最新的一条并且删除 + + log.Debugf("======= insert log : %+v =======", data) + if data.Act == "insert" { + query := data.D[1].([]interface{}) + + _, err := this.mgo.InsertMany(data.D[0].(core.SqlTable), query) + if err != nil { + log.Errorf("insert %s db err:%v", data.D[0].(core.SqlTable), err) + } + } else if data.Act == "delete" { + + _, err := this.mgo.DeleteMany(data.D[0].(core.SqlTable), data.D[1].(map[string]interface{})) + if err != nil { + log.Errorf("delete %s db err:%v", data.D[0].(core.SqlTable), err) + } + } else { // update + if len(data.D) < 2 { + log.Errorf("parameter len _id : %s,uid : %s d.len:%v", data.ID, data.UID, len(data.D)) + return errors.New("parameter len err") + } + where := data.D[1].(map[string]interface{}) + _obj := &QueryStruct{} + for k, v := range where { + _obj.Selector[k] = v + } + query := data.D[2].(map[string]interface{}) + for k, v := range query { + _obj.Query[k] = v + } + this.mgo.FindOneAndUpdate(data.D[0].(core.SqlTable), _obj.Selector, _obj.Query) + } + return +} + +func (this *DB) Model_InsertDBBylog(data *Autogenerated) (err error) { + + _, err = this.mgo.InsertOne(DB_ModelTable, data) + if err != nil { + log.Errorf("insert model db err %v", err) + } + return err +} diff --git a/sys/db/user.go b/sys/db/user.go index 3b6e2fbf4..9250b704b 100644 --- a/sys/db/user.go +++ b/sys/db/user.go @@ -1,10 +1,13 @@ package db import ( + "fmt" "go_dreamfactory/pb" + "time" "go_dreamfactory/lego/core" + 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/options" @@ -44,7 +47,11 @@ func (this *DB) User_FindById(id string) (*pb.DB_UserData, error) { } func (this *DB) User_Create(user *pb.DB_UserData) (err error) { - user.Uid = primitive.NewObjectID().Hex() + _id := primitive.NewObjectID().Hex() + user.Id = _id + user.Uid = fmt.Sprintf("%d_%s", user.Sid, _id) + user.Uuid = uuid.NewV4().String() + user.Ctime = time.Now().Unix() _, err = this.mgo.InsertOne(DB_UserTable, user) return err } diff --git a/sys/db/user_test.go b/sys/db/user_test.go index 8cd774c0a..2b07fea2e 100644 --- a/sys/db/user_test.go +++ b/sys/db/user_test.go @@ -11,8 +11,8 @@ import ( func TestCreate(t *testing.T) { user := &pb.DB_UserData{ - Binduid: "legu3", - Name: "乐谷3", + Binduid: "legu131", + Name: "legu131", Sid: 1, }