diff --git a/product-service/domain/repository/product_repository.go b/product-service/domain/repository/product_repository.go index b82c2c9..cf5ce17 100644 --- a/product-service/domain/repository/product_repository.go +++ b/product-service/domain/repository/product_repository.go @@ -98,7 +98,11 @@ func (u *ProductRepository) ShowDetailSku(id int32) (obj *model.ProductSku, err func (u *ProductRepository) UpdateSku(req *proto.UpdateSkuReq) (isSuccess bool, err error) { sku := req.GetProductSku() + isSuccess = true //u.mysqlDB.Updates(sku) - u.mysqlDB.Debug().Model(&model.ProductSku{}).Where("id=?", sku.SkuId).Update("stock", sku.Stock) - return true, nil + tb := u.mysqlDB.Debug().Model(&model.ProductSku{}).Where("id=?", sku.SkuId).Update("stock", sku.Stock) + if tb.Error != nil { + isSuccess = false + } + return isSuccess, tb.Error } diff --git a/product-service/handler/product_handler.go b/product-service/handler/product_handler.go index 88683b9..6c9e7ae 100644 --- a/product-service/handler/product_handler.go +++ b/product-service/handler/product_handler.go @@ -122,5 +122,5 @@ func (u *ProductHandler) UpdateSku(ctx context.Context, req *proto.UpdateSkuReq, println("UpdateSku err :", err) } resp.IsSuccess = isSuccess - return nil + return err } diff --git a/shoppingCart-service/client/client.go b/shoppingCart-service/client/client.go index e14b9a8..0302ae9 100644 --- a/shoppingCart-service/client/client.go +++ b/shoppingCart-service/client/client.go @@ -63,32 +63,23 @@ func main() { GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client()) UpdateSkuClient := proto.NewUpdateSkuService("shop-product", rpcServer.Client()) - //分页查询商品列表 + //添加购物车 router.GET("/increase", func(c *gin.Context) { - // "number": 0, - // "productId": 0, - // "productSkuId": 0 number, _ := strconv.Atoi(c.Request.FormValue("number")) productId, _ := strconv.Atoi(c.Request.FormValue("productId")) productSkuId, _ := strconv.Atoi(c.Request.FormValue("productSkuId")) uuid := c.Request.Header["Uuid"][0] - - //intUuid, _ := strconv.Atoi(uuid) - // intput := "101010101110110" cc := common.GetInput(uuid) out := common.SQ(cc) sum := 0 for o := range out { sum += o } - //fmt.Println(sum) - //Token校验 //拼接请求信息 tokenReq := &proto.TokenReq{ Uuid: uuid, } - //响应 tokenResp, err := GetUserTokenClient.GetUserToken(context.TODO(), tokenReq) //拼接请求信息 @@ -107,26 +98,29 @@ func main() { ProductSkuId: int32(productSkuId), UserId: int32(sum), } - resp, err := client.AddCart(context.TODO(), req) + resp := &proto.AddCartResp{} //商品详情 reqDetail := &proto.ProductDetailReq{ Id: int32(productId), } respDetail, err := ShowProductDetailClient.ShowProductDetail(context.TODO(), reqDetail) + if err != nil { + log.Println("ShowProductDetail err : ", err) + common.RespFail(c.Writer, respErr, "查询商品详情失败!") + return + } if respDetail != nil { req.ProductName = respDetail.ProductDetail[0].Name req.ProductMainPicture = respDetail.ProductDetail[0].MainPicture } - log.Println(" /ShowProductDetail resp :", respDetail) + //log.Println(" /ShowProductDetail resp :", respDetail) //SKU详情 reqDetail.Id = req.ProductSkuId respSkuDetail, err := ShowDetailSkuClient.ShowDetailSku(context.TODO(), reqDetail) - - 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 { common.RespFail(c.Writer, &proto.AddCartResp{}, "库存不足,添加失败") @@ -139,21 +133,25 @@ func main() { } respUpdate, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq) if err != nil { - log.Println(" /UpdateSkuClient resp :", err) + log.Println(" /UpdateSku err :", err) + common.RespFail(c.Writer, resp, "修改库存失败!") + return } log.Println(" /UpdateSkuClient resp :", respUpdate.IsSuccess) - - resp.ProductSkuSimple = respSkuDetail.ProductSku[0] - resp.ProductSimple = respDetail.ProductDetail[0] - - log.Println(" /AddCart resp :", resp) - + //开始增加购物车 + resp, err = client.AddCart(context.TODO(), req) //根据响应做输出 if err != nil { - log.Println(err.Error()) - common.RespFail(c.Writer, resp, "请求失败") + log.Println("addCart err ", err) + updateSkuReq.ProductSku.Stock += req.Number + _, err = UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq) + log.Println("rollback sku is Err :", err) + common.RespFail(c.Writer, resp, "添加购物车失败!") return } + resp.ProductSkuSimple = respSkuDetail.ProductSku[0] + resp.ProductSimple = respDetail.ProductDetail[0] + log.Println(" /AddCart resp :", resp) ////writer data message row total field common.RespOK(c.Writer, resp, "请求成功") }) diff --git a/shoppingCart-service/domain/repository/cart_repository.go b/shoppingCart-service/domain/repository/cart_repository.go index 2162f1b..7e9d693 100644 --- a/shoppingCart-service/domain/repository/cart_repository.go +++ b/shoppingCart-service/domain/repository/cart_repository.go @@ -40,8 +40,8 @@ func (u *CartRepository) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart UserId: req.UserId, } cart.CreateTime = time.Now() // - u.mysqlDB.Create(&cart) - + tb := u.mysqlDB.Create(&cart) + //err = errors.New("400", "测试异常", 400) fmt.Println("repository AddCart >>>> ", cart) - return &cart, nil + return &cart, tb.Error //err } diff --git a/shoppingCart-service/handler/cart_handler.go b/shoppingCart-service/handler/cart_handler.go index 23e1104..b52e6dc 100644 --- a/shoppingCart-service/handler/cart_handler.go +++ b/shoppingCart-service/handler/cart_handler.go @@ -20,14 +20,15 @@ func (u *CartHandler) AddCart(ctx context.Context, req *proto.AddCartReq, resp * obj, err := u.CartService.AddCart(req) if err != nil { println(" AddCart err :", err) + } else { + //count = u.ProductDataService.CountNum() + //fmt.Println(">>>>>>>>>>>>> page product success :", obj) + //cart := &proto.ShoppingCart{} + //err1 := common.SwapToStruct(obj, resp) + resp.CanSetShoppingCartNumber = int64(obj.Number) + resp.ShoppingCartNumber = int64(obj.Number) + resp.IsBeyondMaxLimit = false // 查询sku + fmt.Println(" increase handler >>>>>> ", resp) } - //count = u.ProductDataService.CountNum() - //fmt.Println(">>>>>>>>>>>>> page product success :", obj) - //cart := &proto.ShoppingCart{} - //err1 := common.SwapToStruct(obj, resp) - resp.CanSetShoppingCartNumber = int64(obj.Number) - resp.ShoppingCartNumber = int64(obj.Number) - resp.IsBeyondMaxLimit = false // 查询sku - fmt.Println(" increase handler >>>>>> ", resp) - return nil + return err } diff --git a/user-service/handler/user.go b/user-service/handler/user.go index eb76739..2deb0e6 100644 --- a/user-service/handler/user.go +++ b/user-service/handler/user.go @@ -54,6 +54,9 @@ func (u *User) GetUserToken(ctx context.Context, req *proto.TokenReq, resp *prot if res != "" { resp.IsLogin = true resp.Token = res + // 续命 + uuid := common.ToInt(req.Uuid) + u.UserDataService.SetUserToken(strconv.Itoa(uuid), []byte(res), time.Duration(1)*time.Hour) fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res) } else { resp.IsLogin = false