事务异常处理

master
shenzhuan 2 years ago
parent ef538a66a0
commit a9fd4c1ab2

@ -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
u.mysqlDB.Raw(sql, id).Scan(&productSku) tb := u.mysqlDB.Raw(sql, id).Scan(&productSku)
fmt.Println("repository ShowProductSku >>>> ", productSku) fmt.Println("repository ShowProductSku >>>> ", productSku)
return &productSku, nil return &productSku, tb.Error
} }
func (u *ProductRepository) ShowDetailSku(id int32) (obj *model.ProductSku, err 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) { 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)
u.mysqlDB.Debug().Model(&model.ProductSku{}).Where("id=?", sku.SkuId).Update("stock", sku.Stock) // 测试失败 stockssss
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
} }

@ -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 nil return err
} }

@ -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, "请求成功")
}) })

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

@ -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 nil return err
} }

@ -54,7 +54,10 @@ 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