From a9fd4c1ab2352fbcfd2a797b616cc87d6814c5fd Mon Sep 17 00:00:00 2001 From: shenzhuan Date: Fri, 28 Oct 2022 11:33:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/repository/product_repository.go | 13 ++++-- product-service/handler/product_handler.go | 2 +- shoppingCart-service/client/client.go | 41 ++++++++++++------- .../domain/repository/cart_repository.go | 5 ++- shoppingCart-service/handler/cart_handler.go | 2 +- user-service/handler/user.go | 3 ++ 6 files changed, 43 insertions(+), 23 deletions(-) diff --git a/product-service/domain/repository/product_repository.go b/product-service/domain/repository/product_repository.go index b82c2c9..0d57a23 100644 --- a/product-service/domain/repository/product_repository.go +++ b/product-service/domain/repository/product_repository.go @@ -84,9 +84,9 @@ func (u *ProductRepository) ShowProductSku(id int32) (product *[]model.ProductSk sql := "select id ,name ,attribute_symbol_list,sell_price,stock from product_sku where product_id= ?" var productSku []model.ProductSku - u.mysqlDB.Raw(sql, id).Scan(&productSku) + tb := u.mysqlDB.Raw(sql, id).Scan(&productSku) fmt.Println("repository ShowProductSku >>>> ", productSku) - return &productSku, nil + return &productSku, tb.Error } func (u *ProductRepository) ShowDetailSku(id int32) (obj *model.ProductSku, err error) { @@ -98,7 +98,12 @@ 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 + // 测试失败 stockssss + 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..079907b 100644 --- a/shoppingCart-service/client/client.go +++ b/shoppingCart-service/client/client.go @@ -100,14 +100,15 @@ func main() { } log.Println("GetUserToken success : ", tokenResp) - //拼接请求信息 + //拼接 添加购物车请求 req := &proto.AddCartReq{ Number: int32(number), ProductId: int32(productId), ProductSkuId: int32(productSkuId), UserId: int32(sum), } - resp, err := client.AddCart(context.TODO(), req) + //定义添加购物车的响应 + resp := &proto.AddCartResp{} //商品详情 reqDetail := &proto.ProductDetailReq{ Id: int32(productId), @@ -118,15 +119,19 @@ func main() { 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) + 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 { common.RespFail(c.Writer, &proto.AddCartResp{}, "库存不足,添加失败") @@ -139,21 +144,27 @@ 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(" /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.ProductSimple = respDetail.ProductDetail[0] log.Println(" /AddCart resp :", resp) - //根据响应做输出 - if err != nil { - log.Println(err.Error()) - common.RespFail(c.Writer, resp, "请求失败") - return - } ////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..30e6ff4 100644 --- a/shoppingCart-service/domain/repository/cart_repository.go +++ b/shoppingCart-service/domain/repository/cart_repository.go @@ -40,8 +40,9 @@ func (u *CartRepository) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart UserId: req.UserId, } cart.CreateTime = time.Now() // - u.mysqlDB.Create(&cart) + //err = errors.New("400", "测试异常", 400) + tb := u.mysqlDB.Create(&cart) 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..704933a 100644 --- a/shoppingCart-service/handler/cart_handler.go +++ b/shoppingCart-service/handler/cart_handler.go @@ -29,5 +29,5 @@ func (u *CartHandler) AddCart(ctx context.Context, req *proto.AddCartReq, resp * 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..7a5b47d 100644 --- a/user-service/handler/user.go +++ b/user-service/handler/user.go @@ -54,7 +54,10 @@ func (u *User) GetUserToken(ctx context.Context, req *proto.TokenReq, resp *prot if res != "" { resp.IsLogin = true resp.Token = res + uuid := common.ToInt(req.GetUuid()) fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res) + //续命机制 + u.UserDataService.SetUserToken(strconv.Itoa(uuid), []byte(res), time.Duration(1)*time.Hour) } else { resp.IsLogin = false resp.Token = ""