调整背包数据层接口
This commit is contained in:
parent
9a8ac28320
commit
e464879799
@ -28,13 +28,13 @@ func (this *Api_Comp) Getlist(session comm.IUserSession, agrs map[string]interfa
|
|||||||
session.SendMsg(string(this.module.GetType()), GetlistResp, &pb.Pack_Getlist_Resp{Grids: grids})
|
session.SendMsg(string(this.module.GetType()), GetlistResp, &pb.Pack_Getlist_Resp{Grids: grids})
|
||||||
if code == pb.ErrorCode_Success {
|
if code == pb.ErrorCode_Success {
|
||||||
go func() { //异步处理修改数据
|
go func() { //异步处理修改数据
|
||||||
this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
this.module.model_pack_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
||||||
this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...)
|
this.module.model_pack_comp.Pack_DeleteUserPack(session.GetUserId(), dels...)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil {
|
if items, err = this.module.model_pack_comp.Pack_QueryUserPack(session.GetUserId()); err != nil {
|
||||||
log.Errorf("QueryUserPackReq err:%v", err)
|
log.Errorf("QueryUserPackReq err:%v", err)
|
||||||
code = pb.ErrorCode_CacheReadError
|
code = pb.ErrorCode_CacheReadError
|
||||||
return
|
return
|
||||||
|
@ -3,29 +3,45 @@ package pack
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go_dreamfactory/lego/core"
|
"go_dreamfactory/lego/core"
|
||||||
"go_dreamfactory/lego/sys/mgo"
|
|
||||||
"go_dreamfactory/lego/sys/redis"
|
|
||||||
"go_dreamfactory/modules"
|
"go_dreamfactory/modules"
|
||||||
"go_dreamfactory/pb"
|
"go_dreamfactory/pb"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||||
)
|
)
|
||||||
|
|
||||||
///背包缓存数据管理组件
|
///背包缓存数据管理组件
|
||||||
type Cache_Comp struct {
|
type Model_Pack_Comp struct {
|
||||||
modules.Model_Comp
|
modules.Model_Comp
|
||||||
module *Pack
|
module *Pack
|
||||||
}
|
}
|
||||||
|
|
||||||
//组件初始化接口
|
//组件初始化接口
|
||||||
func (this *Cache_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
func (this *Model_Pack_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
||||||
this.ModuleCompBase.Init(service, module, comp, options)
|
this.Model_Comp.Init(service, module, comp, opt)
|
||||||
this.module = module.(*Pack)
|
this.module = module.(*Pack)
|
||||||
|
this.TableName = "pack"
|
||||||
|
//创建uid索引
|
||||||
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
||||||
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///查询用户背包数据
|
///查询用户背包数据
|
||||||
func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
|
func (this *Model_Pack_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
|
||||||
|
var (
|
||||||
|
data map[string]*pb.DB_UserItemData = make(map[string]*pb.DB_UserItemData)
|
||||||
|
)
|
||||||
|
if err = this.GetHM(uId, data); err == nil {
|
||||||
|
itmes = make([]*pb.DB_UserItemData, len(data))
|
||||||
|
n := 0
|
||||||
|
for _, v := range data {
|
||||||
|
itmes[n] = v
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
// var (
|
// var (
|
||||||
// lists []interface{}
|
// lists []interface{}
|
||||||
// temp map[string]interface{}
|
// temp map[string]interface{}
|
||||||
@ -51,50 +67,58 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD
|
|||||||
}
|
}
|
||||||
|
|
||||||
///查询用户指定格子的物品数据
|
///查询用户指定格子的物品数据
|
||||||
func (this *Cache_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) {
|
func (this *Model_Pack_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) {
|
||||||
var (
|
|
||||||
itmes []*pb.DB_UserItemData
|
|
||||||
temp map[string]interface{}
|
|
||||||
)
|
|
||||||
itme = &pb.DB_UserItemData{}
|
itme = &pb.DB_UserItemData{}
|
||||||
if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil {
|
err = this.GetH(uId, grid, itme)
|
||||||
return
|
|
||||||
} else if err == redis.RedisNil {
|
// var (
|
||||||
if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil {
|
// itmes []*pb.DB_UserItemData
|
||||||
temp = make(map[string]interface{})
|
// temp map[string]interface{}
|
||||||
for _, v := range itmes {
|
// )
|
||||||
temp[v.GridId] = v
|
// itme = &pb.DB_UserItemData{}
|
||||||
}
|
// if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil {
|
||||||
this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp)
|
// return
|
||||||
for _, v := range itmes {
|
// } else if err == redis.RedisNil {
|
||||||
if v.GridId == grid {
|
// if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil {
|
||||||
itme = v
|
// temp = make(map[string]interface{})
|
||||||
return
|
// for _, v := range itmes {
|
||||||
}
|
// temp[v.GridId] = v
|
||||||
}
|
// }
|
||||||
err = fmt.Errorf("no found uid:%s grid:%s", uId, grid)
|
// this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp)
|
||||||
} else if err == mgo.MongodbNil {
|
// for _, v := range itmes {
|
||||||
err = nil
|
// if v.GridId == grid {
|
||||||
}
|
// itme = v
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// err = fmt.Errorf("no found uid:%s grid:%s", uId, grid)
|
||||||
|
// } else if err == mgo.MongodbNil {
|
||||||
|
// err = nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//更新用户的背包信息
|
//更新用户的背包信息
|
||||||
func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItemData) (err error) {
|
func (this *Model_Pack_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItemData) (err error) {
|
||||||
temp := make(map[string]interface{})
|
var data map[string]*pb.DB_UserItemData = make(map[string]*pb.DB_UserItemData)
|
||||||
for _, v := range itmes {
|
for _, v := range itmes {
|
||||||
temp[v.GridId] = v
|
data[v.GridId] = v
|
||||||
}
|
|
||||||
if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil {
|
|
||||||
this.module.db_comp.Pack_UpdateGridToUserPack(uId, itmes...)
|
|
||||||
}
|
}
|
||||||
|
err = this.SetHM(uId, data)
|
||||||
|
// temp := make(map[string]interface{})
|
||||||
|
// for _, v := range itmes {
|
||||||
|
// temp[v.GridId] = v
|
||||||
|
// }
|
||||||
|
// if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil {
|
||||||
|
// this.module.db_comp.Pack_UpdateGridToUserPack(uId, itmes...)
|
||||||
|
// }
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//更新用户的背包信息
|
//更新用户的背包信息
|
||||||
func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) {
|
func (this *Model_Pack_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) {
|
||||||
if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil {
|
if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil {
|
||||||
err = this.module.db_comp.Pack_DeleteGridToUserPack(uId, gridIds...)
|
err = this.module.db_comp.Pack_DeleteGridToUserPack(uId, gridIds...)
|
||||||
}
|
}
|
||||||
@ -102,7 +126,7 @@ func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err
|
|||||||
}
|
}
|
||||||
|
|
||||||
//查询用户背包物品数量
|
//查询用户背包物品数量
|
||||||
func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
func (this *Model_Pack_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||||
var (
|
var (
|
||||||
itmes []*pb.DB_UserItemData
|
itmes []*pb.DB_UserItemData
|
||||||
err error
|
err error
|
||||||
@ -119,7 +143,7 @@ func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
///添加或则减少物品到用户背包
|
///添加或则减少物品到用户背包
|
||||||
func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
|
func (this *Model_Pack_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
|
||||||
var (
|
var (
|
||||||
itmes []*pb.DB_UserItemData
|
itmes []*pb.DB_UserItemData
|
||||||
modifys []*pb.DB_UserItemData
|
modifys []*pb.DB_UserItemData
|
||||||
@ -144,7 +168,7 @@ func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum
|
|||||||
}
|
}
|
||||||
|
|
||||||
///添加或则减少多个物品到用户背包
|
///添加或则减少多个物品到用户背包
|
||||||
func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
func (this *Model_Pack_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||||
var (
|
var (
|
||||||
itmes []*pb.DB_UserItemData
|
itmes []*pb.DB_UserItemData
|
||||||
modifys []*pb.DB_UserItemData
|
modifys []*pb.DB_UserItemData
|
||||||
@ -182,7 +206,7 @@ func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int3
|
|||||||
}
|
}
|
||||||
|
|
||||||
///修改指定格子的物品数量
|
///修改指定格子的物品数量
|
||||||
func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) {
|
func (this *Model_Pack_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) {
|
||||||
var (
|
var (
|
||||||
itme *pb.DB_UserItemData
|
itme *pb.DB_UserItemData
|
||||||
grid *pb.DB_UserItemData
|
grid *pb.DB_UserItemData
|
||||||
@ -217,7 +241,7 @@ func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
///添加移除物品到用户背包
|
///添加移除物品到用户背包
|
||||||
func (this *Cache_Comp) pack_addItemToUserPack(items []*pb.DB_UserItemData, itemId int32, addnum int32) (modifys []*pb.DB_UserItemData, leftnum int64) {
|
func (this *Model_Pack_Comp) pack_addItemToUserPack(items []*pb.DB_UserItemData, itemId int32, addnum int32) (modifys []*pb.DB_UserItemData, leftnum int64) {
|
||||||
var (
|
var (
|
||||||
num int64
|
num int64
|
||||||
isNew bool
|
isNew bool
|
@ -21,7 +21,7 @@ func NewModule() core.IModule {
|
|||||||
type Pack struct {
|
type Pack struct {
|
||||||
modules.ModuleBase
|
modules.ModuleBase
|
||||||
api_comp *Api_Comp
|
api_comp *Api_Comp
|
||||||
cache_comp *Cache_Comp
|
model_pack_comp *Model_Pack_Comp
|
||||||
db_comp *DB_Comp
|
db_comp *DB_Comp
|
||||||
configure_comp *Configure_Comp
|
configure_comp *Configure_Comp
|
||||||
}
|
}
|
||||||
@ -41,8 +41,7 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core.
|
|||||||
func (this *Pack) OnInstallComp() {
|
func (this *Pack) OnInstallComp() {
|
||||||
this.ModuleBase.OnInstallComp()
|
this.ModuleBase.OnInstallComp()
|
||||||
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||||
this.cache_comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp)
|
this.model_pack_comp = this.RegisterComp(new(Model_Pack_Comp)).(*Model_Pack_Comp)
|
||||||
//this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp)
|
|
||||||
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,14 +49,14 @@ func (this *Pack) OnInstallComp() {
|
|||||||
///查询用户背包物品数量
|
///查询用户背包物品数量
|
||||||
func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||||
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
|
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
|
||||||
amount = this.cache_comp.Pack_QueryUserPackItemAmount(uId, itemid)
|
amount = this.model_pack_comp.Pack_QueryUserPackItemAmount(uId, itemid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///添加单个物品到背包 (可以加物品和减物品)
|
///添加单个物品到背包 (可以加物品和减物品)
|
||||||
func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) {
|
func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) {
|
||||||
defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
|
defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil)
|
||||||
if err = this.cache_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
|
if err = this.model_pack_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
|
||||||
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
|
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -66,7 +65,7 @@ func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error
|
|||||||
///添加多个物品到背包 (可以加物品和减物品)
|
///添加多个物品到背包 (可以加物品和减物品)
|
||||||
func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||||
defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
|
defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
|
||||||
if err = this.cache_comp.Pack_AddItemsToUserPack(uId, items); err != nil {
|
if err = this.model_pack_comp.Pack_AddItemsToUserPack(uId, items); err != nil {
|
||||||
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
|
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package pack
|
package pack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"go_dreamfactory/comm"
|
"go_dreamfactory/comm"
|
||||||
"go_dreamfactory/lego"
|
"go_dreamfactory/lego"
|
||||||
@ -17,7 +16,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/protobuf/ptypes"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newService(ops ...rpcx.Option) core.IService {
|
func newService(ops ...rpcx.Option) core.IService {
|
||||||
@ -74,8 +76,78 @@ func TestMain(m *testing.M) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Log(t *testing.T) {
|
func Test_Log(t *testing.T) {
|
||||||
data, _ := ptypes.MarshalAny(&pb.Pack_Getlist_Req{})
|
// data, _ := ptypes.MarshalAny(&pb.Pack_Getlist_Req{})
|
||||||
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{Method: "pack.getlist", Message: data}, &pb.RPCMessageReply{})
|
// s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{Method: "pack.getlist", Message: data}, &pb.RPCMessageReply{})
|
||||||
|
|
||||||
// items, err := module.db_comp.Pack_QueryUserPack("liwei1dao")
|
// items, err := module.db_comp.Pack_QueryUserPack("liwei1dao")
|
||||||
// log.Debugf("item:%v err:%v", items, err)
|
// log.Debugf("item:%v err:%v", items, err)
|
||||||
|
uid := "0_62a9afd994fe03b7aaee6773"
|
||||||
|
Pack_UpdateGridToUserPack(
|
||||||
|
uid,
|
||||||
|
&pb.DB_UserItemData{
|
||||||
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
|
UId: uid,
|
||||||
|
IsEmpty: false,
|
||||||
|
ItemId: 1001,
|
||||||
|
Amount: 99,
|
||||||
|
CTime: time.Now().Unix(),
|
||||||
|
IsNewItem: true,
|
||||||
|
},
|
||||||
|
&pb.DB_UserItemData{
|
||||||
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
|
UId: uid,
|
||||||
|
IsEmpty: false,
|
||||||
|
ItemId: 1001,
|
||||||
|
Amount: 12,
|
||||||
|
CTime: time.Now().Unix(),
|
||||||
|
IsNewItem: true,
|
||||||
|
},
|
||||||
|
&pb.DB_UserItemData{
|
||||||
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
|
UId: uid,
|
||||||
|
IsEmpty: false,
|
||||||
|
ItemId: 1002,
|
||||||
|
Amount: 99,
|
||||||
|
CTime: time.Now().Unix(),
|
||||||
|
IsNewItem: true,
|
||||||
|
},
|
||||||
|
&pb.DB_UserItemData{
|
||||||
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
|
UId: uid,
|
||||||
|
IsEmpty: false,
|
||||||
|
ItemId: 1003,
|
||||||
|
Amount: 78,
|
||||||
|
CTime: time.Now().Unix(),
|
||||||
|
IsNewItem: true,
|
||||||
|
},
|
||||||
|
&pb.DB_UserItemData{
|
||||||
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
|
UId: uid,
|
||||||
|
IsEmpty: false,
|
||||||
|
ItemId: 1004,
|
||||||
|
Amount: 99,
|
||||||
|
CTime: time.Now().Unix(),
|
||||||
|
IsNewItem: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//更新背包格子数据
|
||||||
|
func Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_UserItemData) (err error) {
|
||||||
|
models := make([]mongo.WriteModel, len(grids))
|
||||||
|
for i, v := range grids {
|
||||||
|
models[i] = mongo.NewUpdateOneModel().SetFilter(bson.M{"_id": v.GridId}).SetUpdate(
|
||||||
|
bson.M{"$set": bson.M{
|
||||||
|
"uid": v.UId,
|
||||||
|
"isempty": v.IsEmpty,
|
||||||
|
"itemid": v.ItemId,
|
||||||
|
"amount": v.Amount,
|
||||||
|
"ctime": v.CTime,
|
||||||
|
"etime": v.ETime,
|
||||||
|
"isnewitem": v.IsNewItem,
|
||||||
|
}}).SetUpsert(true)
|
||||||
|
}
|
||||||
|
module.model_pack_comp.DB.BulkWrite(DB_PackTable, models, options.BulkWrite().SetOrdered(false))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user