|
|
|
@ -5,8 +5,10 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
|
|
|
|
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
|
|
|
|
|
"github.com/dtm-labs/dtm/client/dtmcli"
|
|
|
|
|
"github.com/go-micro/plugins/v4/wrapper/select/roundrobin"
|
|
|
|
|
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
|
|
|
|
|
"github.com/lithammer/shortuuid/v3"
|
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
|
"go-micro.dev/v4/client"
|
|
|
|
|
"go-micro.dev/v4/web"
|
|
|
|
@ -70,43 +72,74 @@ func main() {
|
|
|
|
|
micro.WrapClient(roundrobin.NewClientWrapper()),
|
|
|
|
|
)
|
|
|
|
|
//AddCartClient := proto.NewAddCartService("shop-cart", rpcServer.Client())
|
|
|
|
|
//UpdateCartClient := proto.NewUpdateCartService("shop-cart", rpcServer.Client())
|
|
|
|
|
UpdateCartClient := proto.NewUpdateCartService("shop-cart", rpcServer.Client())
|
|
|
|
|
//ShowProductDetailClient := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
|
|
|
|
|
//ShowDetailSkuClient := proto.NewShowDetailSkuService("shop-product", rpcServer.Client())
|
|
|
|
|
GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client())
|
|
|
|
|
//UpdateSkuClient := proto.NewUpdateSkuService("shop-product", rpcServer.Client())
|
|
|
|
|
AddTraderClient := proto.NewAddTradeOrderService("trade-order", rpcServer.Client())
|
|
|
|
|
//UpdateTraderClient := proto.NewUpdateTradeOrderService("trade-order", rpcServer.Client())
|
|
|
|
|
UpdateTraderClient := proto.NewUpdateTradeOrderService("trade-order", rpcServer.Client())
|
|
|
|
|
|
|
|
|
|
//开始拆分 DTM服务
|
|
|
|
|
//
|
|
|
|
|
//router.POST("/updateSku", func(c *gin.Context) {
|
|
|
|
|
// req := &proto.UpdateSkuReq{}
|
|
|
|
|
// if err := c.BindJSON(req); err != nil {
|
|
|
|
|
// log.Fatalln(err)
|
|
|
|
|
// }
|
|
|
|
|
// _, err := UpdateSkuClient.UpdateSku(context.TODO(), req)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// log.Println("/updateSku err ", err)
|
|
|
|
|
// c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "修改库存失败!"})
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
// c.JSON(http.StatusOK, gin.H{"updateSku": "SUCCESS", "Message": "修改库存成功!"})
|
|
|
|
|
//})
|
|
|
|
|
//router.POST("/updateSku-compensate", func(c *gin.Context) {
|
|
|
|
|
// req := &proto.UpdateSkuReq{}
|
|
|
|
|
// if err := c.BindJSON(req); err != nil {
|
|
|
|
|
// log.Fatalln(err)
|
|
|
|
|
// }
|
|
|
|
|
// req.ProductSku.Stock += Number
|
|
|
|
|
// _, err := UpdateSkuClient.UpdateSku(context.TODO(), req)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// log.Println("/updateSku err ", err)
|
|
|
|
|
// c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "回滚库存失败!"})
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
// c.JSON(http.StatusOK, gin.H{"updateSku-compensate": "SUCCESS", "Message": "回滚库存成功!"})
|
|
|
|
|
//})
|
|
|
|
|
|
|
|
|
|
router.POST("/updateCart", func(c *gin.Context) {
|
|
|
|
|
req := &proto.UpdateCartReq{}
|
|
|
|
|
if err := c.BindJSON(req); err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
req.IsDeleted = true
|
|
|
|
|
_, err := UpdateCartClient.UpdateCart(context.TODO(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("/updateCart err ", err)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "删除购物车失败!"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"updateCart": "SUCCESS", "Message": "删除购物车成功!"})
|
|
|
|
|
})
|
|
|
|
|
router.POST("/updateCart-compensate", func(c *gin.Context) {
|
|
|
|
|
req := &proto.UpdateCartReq{}
|
|
|
|
|
if err := c.BindJSON(req); err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
req.IsDeleted = false
|
|
|
|
|
_, err := UpdateCartClient.UpdateCart(context.TODO(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("/updateCart err ", err)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "回滚购物车失败!"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"updateCart-compensate": "SUCCESS", "Message": "回滚购物车成功!"})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.POST("/addTrade", func(c *gin.Context) {
|
|
|
|
|
req := &proto.AddTradeOrderReq{}
|
|
|
|
|
if err := c.BindJSON(req); err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
_, err := AddTraderClient.AddTradeOrder(context.TODO(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("/addTrade err ", err)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "新增订单失败!"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"addTrade": "SUCCESS", "Message": "新增订单成功!"})
|
|
|
|
|
})
|
|
|
|
|
router.POST("/addTrade-compensate", func(c *gin.Context) {
|
|
|
|
|
req := &proto.AddTradeOrderReq{}
|
|
|
|
|
if err := c.BindJSON(req); err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
req.TradeOrder.IsDeleted = true
|
|
|
|
|
_, err := UpdateTraderClient.UpdateTradeOrder(context.TODO(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("/addTrade err ", err)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "回滚订单失败!"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"addTrade-compensate": "SUCCESS", "Message": "回滚订单成功!"})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//新增订单API
|
|
|
|
|
router.GET("/cartAdvanceOrder", func(c *gin.Context) {
|
|
|
|
|
//开始检验登录
|
|
|
|
|
uuid := c.Request.Header["Uuid"][0]
|
|
|
|
@ -149,18 +182,24 @@ func main() {
|
|
|
|
|
RecipientAddressId: int32(recipientAddressId),
|
|
|
|
|
TradeOrder: tradeOrder,
|
|
|
|
|
}
|
|
|
|
|
resp, err = AddTraderClient.AddTradeOrder(context.TODO(), req)
|
|
|
|
|
fmt.Println(resp)
|
|
|
|
|
//CartId = resp.ID
|
|
|
|
|
//测试异常
|
|
|
|
|
//err = errors.New("400", "测试异常", 400)
|
|
|
|
|
updateCartReq := &proto.UpdateCartReq{
|
|
|
|
|
Id: cartIds[0],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//全局事务
|
|
|
|
|
gid := shortuuid.New()
|
|
|
|
|
saga := dtmcli.NewSaga(DtmServer, gid).
|
|
|
|
|
Add(QSBusi+"/updateCart", QSBusi+"/updateCart-compensate", updateCartReq).
|
|
|
|
|
Add(QSBusi+"/addTrade", QSBusi+"/addTrade-compensate", req)
|
|
|
|
|
err = saga.Submit()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("/addCart err ", err)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"dtm_reslut": "FAILURE", "Message": "新增购物车失败!"})
|
|
|
|
|
return
|
|
|
|
|
log.Println("saga submit err :", err)
|
|
|
|
|
common.RespFail(c.Writer, resp, "添加失败")
|
|
|
|
|
}
|
|
|
|
|
log.Println(" /saga submit submit :", gid)
|
|
|
|
|
////writer data message row total field
|
|
|
|
|
common.RespOK(c.Writer, resp, "请求成功")
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"addCart": "SUCCESS", "Message": "新增购物车成功!"})
|
|
|
|
|
})
|
|
|
|
|
//router.POST("/addCart-compensate", func(c *gin.Context) {
|
|
|
|
|
// req := &proto.AddCartReq{}
|
|
|
|
|