|
|
@ -100,14 +100,15 @@ func main() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("GetUserToken success : ", tokenResp)
|
|
|
|
log.Println("GetUserToken success : ", tokenResp)
|
|
|
|
|
|
|
|
|
|
|
|
//拼接请求信息
|
|
|
|
//拼接 添加购物车请求
|
|
|
|
req := &proto.AddCartReq{
|
|
|
|
req := &proto.AddCartReq{
|
|
|
|
Number: int32(number),
|
|
|
|
Number: int32(number),
|
|
|
|
ProductId: int32(productId),
|
|
|
|
ProductId: int32(productId),
|
|
|
|
ProductSkuId: int32(productSkuId),
|
|
|
|
ProductSkuId: int32(productSkuId),
|
|
|
|
UserId: int32(sum),
|
|
|
|
UserId: int32(sum),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp, err := client.AddCart(context.TODO(), req)
|
|
|
|
//定义添加购物车的响应
|
|
|
|
|
|
|
|
resp := &proto.AddCartResp{}
|
|
|
|
//商品详情
|
|
|
|
//商品详情
|
|
|
|
reqDetail := &proto.ProductDetailReq{
|
|
|
|
reqDetail := &proto.ProductDetailReq{
|
|
|
|
Id: int32(productId),
|
|
|
|
Id: int32(productId),
|
|
|
@ -118,15 +119,19 @@ func main() {
|
|
|
|
req.ProductMainPicture = respDetail.ProductDetail[0].MainPicture
|
|
|
|
req.ProductMainPicture = respDetail.ProductDetail[0].MainPicture
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Println(" /ShowProductDetail resp :", respDetail)
|
|
|
|
//log.Println(" /ShowProductDetail resp :", respDetail)
|
|
|
|
//SKU详情
|
|
|
|
//SKU详情
|
|
|
|
reqDetail.Id = req.ProductSkuId
|
|
|
|
reqDetail.Id = req.ProductSkuId
|
|
|
|
respSkuDetail, err := ShowDetailSkuClient.ShowDetailSku(context.TODO(), reqDetail)
|
|
|
|
respSkuDetail, err := ShowDetailSkuClient.ShowDetailSku(context.TODO(), reqDetail)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("ShowDetailSku err : ", err)
|
|
|
|
|
|
|
|
common.RespFail(c.Writer, respErr, "查询商品库存失败!")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//log.Println(" /ShowDetailSku resp :", respSkuDetail)
|
|
|
|
|
|
|
|
|
|
|
|
log.Println(" /ShowDetailSku resp :", respSkuDetail)
|
|
|
|
//修改库存 远程调用服务
|
|
|
|
|
|
|
|
//log.Println(" /AddCart req :", req)
|
|
|
|
//添加购物车 远程调用服务
|
|
|
|
|
|
|
|
log.Println(" /AddCart req :", req)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if respSkuDetail.ProductSku[0].Stock < req.Number {
|
|
|
|
if respSkuDetail.ProductSku[0].Stock < req.Number {
|
|
|
|
common.RespFail(c.Writer, &proto.AddCartResp{}, "库存不足,添加失败")
|
|
|
|
common.RespFail(c.Writer, &proto.AddCartResp{}, "库存不足,添加失败")
|
|
|
@ -139,21 +144,27 @@ func main() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
respUpdate, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq)
|
|
|
|
respUpdate, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Println(" /UpdateSkuClient resp :", err)
|
|
|
|
log.Println(" /UpdateSku err :", err)
|
|
|
|
|
|
|
|
common.RespFail(c.Writer, resp, "扣减库存失败!")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(" /UpdateSku resp :", respUpdate.IsSuccess)
|
|
|
|
|
|
|
|
resp, err = client.AddCart(context.TODO(), req)
|
|
|
|
|
|
|
|
//根据响应做输出 回滚库存
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println("AddCart err :", err)
|
|
|
|
|
|
|
|
updateSkuReq.ProductSku.Stock += req.Number
|
|
|
|
|
|
|
|
_, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq)
|
|
|
|
|
|
|
|
log.Println("rollback update sku :", err)
|
|
|
|
|
|
|
|
common.RespFail(c.Writer, resp, "AddCart失败")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println(" /UpdateSkuClient resp :", respUpdate.IsSuccess)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resp.ProductSkuSimple = respSkuDetail.ProductSku[0]
|
|
|
|
resp.ProductSkuSimple = respSkuDetail.ProductSku[0]
|
|
|
|
resp.ProductSimple = respDetail.ProductDetail[0]
|
|
|
|
resp.ProductSimple = respDetail.ProductDetail[0]
|
|
|
|
|
|
|
|
|
|
|
|
log.Println(" /AddCart resp :", resp)
|
|
|
|
log.Println(" /AddCart resp :", resp)
|
|
|
|
|
|
|
|
|
|
|
|
//根据响应做输出
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
common.RespFail(c.Writer, resp, "请求失败")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
////writer data message row total field
|
|
|
|
////writer data message row total field
|
|
|
|
common.RespOK(c.Writer, resp, "请求成功")
|
|
|
|
common.RespOK(c.Writer, resp, "请求成功")
|
|
|
|
})
|
|
|
|
})
|
|
|
|