事务异常处理

master
shenzhuan 2 years ago
parent 0ee220cec4
commit 1c8cf7bc0a

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

@ -63,32 +63,23 @@ func main() {
GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client()) GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client())
UpdateSkuClient := proto.NewUpdateSkuService("shop-product", rpcServer.Client()) UpdateSkuClient := proto.NewUpdateSkuService("shop-product", rpcServer.Client())
//分页查询商品列表 //添加购物车
router.GET("/increase", func(c *gin.Context) { router.GET("/increase", func(c *gin.Context) {
// "number": 0,
// "productId": 0,
// "productSkuId": 0
number, _ := strconv.Atoi(c.Request.FormValue("number")) number, _ := strconv.Atoi(c.Request.FormValue("number"))
productId, _ := strconv.Atoi(c.Request.FormValue("productId")) productId, _ := strconv.Atoi(c.Request.FormValue("productId"))
productSkuId, _ := strconv.Atoi(c.Request.FormValue("productSkuId")) productSkuId, _ := strconv.Atoi(c.Request.FormValue("productSkuId"))
uuid := c.Request.Header["Uuid"][0] uuid := c.Request.Header["Uuid"][0]
//intUuid, _ := strconv.Atoi(uuid)
// intput := "101010101110110"
cc := common.GetInput(uuid) cc := common.GetInput(uuid)
out := common.SQ(cc) out := common.SQ(cc)
sum := 0 sum := 0
for o := range out { for o := range out {
sum += o sum += o
} }
//fmt.Println(sum)
//Token校验 //Token校验
//拼接请求信息 //拼接请求信息
tokenReq := &proto.TokenReq{ tokenReq := &proto.TokenReq{
Uuid: uuid, Uuid: uuid,
} }
//响应 //响应
tokenResp, err := GetUserTokenClient.GetUserToken(context.TODO(), tokenReq) tokenResp, err := GetUserTokenClient.GetUserToken(context.TODO(), tokenReq)
//拼接请求信息 //拼接请求信息
@ -107,26 +98,29 @@ func main() {
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),
} }
respDetail, err := ShowProductDetailClient.ShowProductDetail(context.TODO(), reqDetail) respDetail, err := ShowProductDetailClient.ShowProductDetail(context.TODO(), reqDetail)
if err != nil {
log.Println("ShowProductDetail err : ", err)
common.RespFail(c.Writer, respErr, "查询商品详情失败!")
return
}
if respDetail != nil { if respDetail != nil {
req.ProductName = respDetail.ProductDetail[0].Name req.ProductName = respDetail.ProductDetail[0].Name
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)
//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 +133,25 @@ 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(" /UpdateSkuClient resp :", respUpdate.IsSuccess) log.Println(" /UpdateSkuClient resp :", respUpdate.IsSuccess)
//开始增加购物车
resp.ProductSkuSimple = respSkuDetail.ProductSku[0] resp, err = client.AddCart(context.TODO(), req)
resp.ProductSimple = respDetail.ProductDetail[0]
log.Println(" /AddCart resp :", resp)
//根据响应做输出 //根据响应做输出
if err != nil { if err != nil {
log.Println(err.Error()) log.Println("addCart err ", err)
common.RespFail(c.Writer, resp, "请求失败") updateSkuReq.ProductSku.Stock += req.Number
_, err = UpdateSkuClient.UpdateSku(context.TODO(), updateSkuReq)
log.Println("rollback sku is Err :", err)
common.RespFail(c.Writer, resp, "添加购物车失败!")
return return
} }
resp.ProductSkuSimple = respSkuDetail.ProductSku[0]
resp.ProductSimple = respDetail.ProductDetail[0]
log.Println(" /AddCart resp :", resp)
////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,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() //
u.mysqlDB.Create(&cart) tb := u.mysqlDB.Create(&cart)
//err = errors.New("400", "测试异常", 400)
fmt.Println("repository AddCart >>>> ", cart) fmt.Println("repository AddCart >>>> ", cart)
return &cart, nil return &cart, tb.Error //err
} }

@ -20,14 +20,15 @@ func (u *CartHandler) AddCart(ctx context.Context, req *proto.AddCartReq, resp *
obj, err := u.CartService.AddCart(req) obj, err := u.CartService.AddCart(req)
if err != nil { if err != nil {
println(" AddCart err :", err) 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() return err
//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
} }

@ -54,6 +54,9 @@ 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.Uuid)
u.UserDataService.SetUserToken(strconv.Itoa(uuid), []byte(res), time.Duration(1)*time.Hour)
fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res) fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res)
} else { } else {
resp.IsLogin = false resp.IsLogin = false

Loading…
Cancel
Save