Revert "事务异常处理"

This reverts commit a9fd4c1ab2.
master
shenzhuan 2 years ago
parent a9fd4c1ab2
commit 0ee220cec4

@ -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= ?" sql := "select id ,name ,attribute_symbol_list,sell_price,stock from product_sku where product_id= ?"
var productSku []model.ProductSku var productSku []model.ProductSku
tb := u.mysqlDB.Raw(sql, id).Scan(&productSku) u.mysqlDB.Raw(sql, id).Scan(&productSku)
fmt.Println("repository ShowProductSku >>>> ", productSku) fmt.Println("repository ShowProductSku >>>> ", productSku)
return &productSku, tb.Error return &productSku, nil
} }
func (u *ProductRepository) ShowDetailSku(id int32) (obj *model.ProductSku, err error) { 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) { func (u *ProductRepository) UpdateSku(req *proto.UpdateSkuReq) (isSuccess bool, err error) {
sku := req.GetProductSku() sku := req.GetProductSku()
isSuccess = true
//u.mysqlDB.Updates(sku) //u.mysqlDB.Updates(sku)
// 测试失败 stockssss u.mysqlDB.Debug().Model(&model.ProductSku{}).Where("id=?", sku.SkuId).Update("stock", sku.Stock)
tb := u.mysqlDB.Debug().Model(&model.ProductSku{}).Where("id=?", sku.SkuId).Update("stock", sku.Stock) return true, nil
if tb.Error != nil {
isSuccess = false
}
return isSuccess, tb.Error
} }

@ -122,5 +122,5 @@ func (u *ProductHandler) UpdateSku(ctx context.Context, req *proto.UpdateSkuReq,
println("UpdateSku err :", err) println("UpdateSku err :", err)
} }
resp.IsSuccess = isSuccess resp.IsSuccess = isSuccess
return err return nil
} }

@ -100,15 +100,14 @@ 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),
@ -119,19 +118,15 @@ 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{}, "库存不足,添加失败")
@ -144,27 +139,21 @@ func main() {
} }
respUpdate, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq) respUpdate, err := UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq)
if err != nil { if err != nil {
log.Println(" /UpdateSku err :", err) log.Println(" /UpdateSkuClient resp :", 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, "请求成功")
}) })

@ -40,9 +40,8 @@ func (u *CartRepository) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart
UserId: req.UserId, UserId: req.UserId,
} }
cart.CreateTime = time.Now() // cart.CreateTime = time.Now() //
//err = errors.New("400", "测试异常", 400) u.mysqlDB.Create(&cart)
tb := u.mysqlDB.Create(&cart)
fmt.Println("repository AddCart >>>> ", cart) fmt.Println("repository AddCart >>>> ", cart)
return &cart, tb.Error //err return &cart, nil
} }

@ -29,5 +29,5 @@ func (u *CartHandler) AddCart(ctx context.Context, req *proto.AddCartReq, resp *
resp.ShoppingCartNumber = int64(obj.Number) resp.ShoppingCartNumber = int64(obj.Number)
resp.IsBeyondMaxLimit = false // 查询sku resp.IsBeyondMaxLimit = false // 查询sku
fmt.Println(" increase handler >>>>>> ", resp) fmt.Println(" increase handler >>>>>> ", resp)
return err return nil
} }

@ -54,10 +54,7 @@ func (u *User) GetUserToken(ctx context.Context, req *proto.TokenReq, resp *prot
if res != "" { if res != "" {
resp.IsLogin = true resp.IsLogin = true
resp.Token = res resp.Token = res
uuid := common.ToInt(req.GetUuid())
fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res) fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res)
//续命机制
u.UserDataService.SetUserToken(strconv.Itoa(uuid), []byte(res), time.Duration(1)*time.Hour)
} else { } else {
resp.IsLogin = false resp.IsLogin = false
resp.Token = "" resp.Token = ""

Loading…
Cancel
Save