diff --git a/product-service/domain/repository/product_repository.go b/product-service/domain/repository/product_repository.go index 0d57a23..b82c2c9 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 - tb := u.mysqlDB.Raw(sql, id).Scan(&productSku) + u.mysqlDB.Raw(sql, id).Scan(&productSku) fmt.Println("repository ShowProductSku >>>> ", productSku) - return &productSku, tb.Error + return &productSku, nil } func (u *ProductRepository) ShowDetailSku(id int32) (obj *model.ProductSku, err error) { @@ -98,12 +98,7 @@ 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) - // 测试失败 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 + u.mysqlDB.Debug().Model(&model.ProductSku{}).Where("id=?", sku.SkuId).Update("stock", sku.Stock) + return true, nil } diff --git a/product-service/handler/product_handler.go b/product-service/handler/product_handler.go index 6c9e7ae..88683b9 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 err + return nil } diff --git a/shoppingCart-service/client/client.go b/shoppingCart-service/client/client.go index 079907b..e14b9a8 100644 --- a/shoppingCart-service/client/client.go +++ b/shoppingCart-service/client/client.go @@ -100,15 +100,14 @@ func main() { } log.Println("GetUserToken success : ", tokenResp) - //拼接 添加购物车请求 + //拼接请求信息 req := &proto.AddCartReq{ Number: int32(number), ProductId: int32(productId), ProductSkuId: int32(productSkuId), UserId: int32(sum), } - //定义添加购物车的响应 - resp := &proto.AddCartResp{} + resp, err := client.AddCart(context.TODO(), req) //商品详情 reqDetail := &proto.ProductDetailReq{ Id: int32(productId), @@ -119,19 +118,15 @@ 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(" /AddCart req :", req) + log.Println(" /ShowDetailSku resp :", respSkuDetail) + + //添加购物车 远程调用服务 + log.Println(" /AddCart req :", req) if respSkuDetail.ProductSku[0].Stock < req.Number { common.RespFail(c.Writer, &proto.AddCartResp{}, "库存不足,添加失败") @@ -144,27 +139,21 @@ func main() { } respUpdate, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq) if err != nil { - 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 :", err) } + 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 30e6ff4..2162f1b 100644 --- a/shoppingCart-service/domain/repository/cart_repository.go +++ b/shoppingCart-service/domain/repository/cart_repository.go @@ -40,9 +40,8 @@ func (u *CartRepository) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart UserId: req.UserId, } cart.CreateTime = time.Now() // - //err = errors.New("400", "测试异常", 400) - tb := u.mysqlDB.Create(&cart) + u.mysqlDB.Create(&cart) fmt.Println("repository AddCart >>>> ", cart) - return &cart, tb.Error //err + return &cart, nil } diff --git a/shoppingCart-service/handler/cart_handler.go b/shoppingCart-service/handler/cart_handler.go index 704933a..23e1104 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 err + return nil } diff --git a/user-service/handler/user.go b/user-service/handler/user.go index 7a5b47d..eb76739 100644 --- a/user-service/handler/user.go +++ b/user-service/handler/user.go @@ -54,10 +54,7 @@ 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 = ""