159 lines
4.0 KiB
Go
159 lines
4.0 KiB
Go
package pack
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego"
|
|
"go_dreamfactory/lego/base/rpcx"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/services"
|
|
"go_dreamfactory/sys/cache"
|
|
"go_dreamfactory/sys/configure"
|
|
"go_dreamfactory/sys/db"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func newService(ops ...rpcx.Option) core.IService {
|
|
s := new(TestService)
|
|
s.Configure(ops...)
|
|
return s
|
|
}
|
|
|
|
//梦工厂基础服务对象
|
|
type TestService struct {
|
|
rpcx.RPCXService
|
|
}
|
|
|
|
//初始化相关系统
|
|
func (this *TestService) InitSys() {
|
|
this.RPCXService.InitSys()
|
|
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.cache success!")
|
|
}
|
|
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.db success!")
|
|
}
|
|
if err := configure.OnInit(this.GetSettings().Sys["configure"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.configure err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.configure success!")
|
|
}
|
|
}
|
|
|
|
var service core.IService
|
|
var s_gateComp comm.ISC_GateRouteComp = services.NewGateRouteComp()
|
|
var module = new(Pack)
|
|
|
|
//测试环境下初始化db和cache 系统
|
|
func TestMain(m *testing.M) {
|
|
service = newService(
|
|
rpcx.SetConfPath("../../bin/conf/worker_2.yaml"),
|
|
rpcx.SetVersion("1.0.0.0"),
|
|
)
|
|
service.OnInstallComp( //装备组件
|
|
s_gateComp, //此服务需要接受用户的消息 需要装备网关组件
|
|
)
|
|
go func() {
|
|
lego.Run(service, //运行模块
|
|
module,
|
|
)
|
|
}()
|
|
time.Sleep(time.Second * 3)
|
|
defer os.Exit(m.Run())
|
|
}
|
|
|
|
func Test_Log(t *testing.T) {
|
|
// data, _ := ptypes.MarshalAny(&pb.Pack_Getlist_Req{})
|
|
// s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{Method: "pack.getlist", Message: data}, &pb.RPCMessageReply{})
|
|
itmes := make([]*pb.DB_UserItemData, 0)
|
|
err := module.model_pack_comp.GetList("0_62a9afd994fe03b7aaee6773", &itmes)
|
|
log.Debugf("item:%v err:%v", itmes, err)
|
|
}
|
|
|
|
func Test_Pack_UpdateGridToUserPack(t *testing.T) {
|
|
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))
|
|
data := make([]interface{}, len(grids))
|
|
for i, v := range grids {
|
|
data[i] = v
|
|
}
|
|
module.model_pack_comp.DB.InsertMany(DB_PackTable, data)
|
|
return
|
|
}
|