1.同步dev_1.2.0,修改创建租赁订单时异常退出导致数据库死锁问题
This commit is contained in:
parent
27068279ce
commit
5bb2133811
|
@ -159,7 +159,7 @@ func WXPaySuccess(c *gin.Context) {
|
||||||
|
|
||||||
var orderCreateLocker sync.Mutex
|
var orderCreateLocker sync.Mutex
|
||||||
|
|
||||||
// 创建订单
|
// RentCardOrderCreate 创建订单
|
||||||
func RentCardOrderCreate(c *gin.Context) {
|
func RentCardOrderCreate(c *gin.Context) {
|
||||||
req := struct {
|
req := struct {
|
||||||
GameCardList []model.CardInfo `json:"game_card_list"`
|
GameCardList []model.CardInfo `json:"game_card_list"`
|
||||||
|
@ -178,11 +178,7 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//bodyString := strings.Replace(string(body), "\"[", "[", -1)
|
|
||||||
//bodyString = strings.Replace(string(bodyString), "]\"", "]", -1)
|
|
||||||
//bodyString = strings.Replace(string(bodyString), "\\", "", -1)
|
|
||||||
logger.Info("bodyString:", string(bodyString))
|
logger.Info("bodyString:", string(bodyString))
|
||||||
|
|
||||||
err = json.Unmarshal([]byte(bodyString), &req)
|
err = json.Unmarshal([]byte(bodyString), &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(errors.New("OrderCreate Unmarshal req err"))
|
logger.Error(errors.New("OrderCreate Unmarshal req err"))
|
||||||
|
@ -250,9 +246,6 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
rentCard = model.GetUserRentCard(tx, uc.Uid)
|
rentCard = model.GetUserRentCard(tx, uc.Uid)
|
||||||
fmt.Println("*********** rentCard is:", rentCard)
|
fmt.Println("*********** rentCard is:", rentCard)
|
||||||
if rentCard == nil {
|
if rentCard == nil {
|
||||||
//logger.Error(errors.New("GetUserByUid err"))
|
|
||||||
//RespJson(c, status.InternalServerError, nil)
|
|
||||||
//return
|
|
||||||
rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax}
|
rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,16 +254,10 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
rentCard = model.GetUserRentCard(nil, uc.Uid)
|
rentCard = model.GetUserRentCard(nil, uc.Uid)
|
||||||
fmt.Println("*********** rentCard is:", rentCard)
|
fmt.Println("*********** rentCard is:", rentCard)
|
||||||
if rentCard == nil {
|
if rentCard == nil {
|
||||||
//logger.Error(errors.New("GetUserByUid err"))
|
|
||||||
//RespJson(c, status.InternalServerError, nil)
|
|
||||||
//return
|
|
||||||
rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax}
|
rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if uc.Uid == 45935373 {
|
|
||||||
// rentCard.CanRentCount -= 1
|
|
||||||
//}
|
|
||||||
if cardCount > rentCard.CanRentCount {
|
if cardCount > rentCard.CanRentCount {
|
||||||
if req.Price == 0 {
|
if req.Price == 0 {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
@ -280,20 +267,22 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 订单成功后 扣减库存
|
|
||||||
//model.UnPayOrderSetCancel(user.Uid)
|
|
||||||
|
|
||||||
orderCreateLocker.Lock()
|
orderCreateLocker.Lock()
|
||||||
defer orderCreateLocker.Unlock()
|
defer orderCreateLocker.Unlock()
|
||||||
|
|
||||||
isRentCount, err := rentCard.IsHaveUnreturnedOrders(cardCount)
|
isRentCount, err := rentCard.IsHaveUnreturnedOrders(cardCount)
|
||||||
//unreturnedOrders, err := model.IsHaveUnreturnedOrders(uc.Uid) //
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("err:", err)
|
logger.Error("err:", err)
|
||||||
RespJson(c, status.InternalServerError, nil)
|
RespJson(c, status.InternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if isRentCount {
|
if isRentCount {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("unreturnedOrders")
|
logger.Error("unreturnedOrders")
|
||||||
RespJson(c, status.HaveUnreturnedOrders, nil)
|
RespJson(c, status.HaveUnreturnedOrders, nil)
|
||||||
return
|
return
|
||||||
|
@ -301,11 +290,17 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
|
|
||||||
online, err := model.IsGameCardListOnline(req.GameCardList)
|
online, err := model.IsGameCardListOnline(req.GameCardList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("err:", err)
|
logger.Error("err:", err)
|
||||||
RespJson(c, status.InternalServerError, nil)
|
RespJson(c, status.InternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !online {
|
if !online {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("unreturnedOrders")
|
logger.Error("unreturnedOrders")
|
||||||
RespJson(c, status.GoodsSoldOut, nil)
|
RespJson(c, status.GoodsSoldOut, nil)
|
||||||
return
|
return
|
||||||
|
@ -314,6 +309,9 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
store.ID = req.StoreId
|
store.ID = req.StoreId
|
||||||
err = store.Info()
|
err = store.Info()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("store err:", err)
|
logger.Error("store err:", err)
|
||||||
RespJson(c, status.InternalServerError, nil)
|
RespJson(c, status.InternalServerError, nil)
|
||||||
return
|
return
|
||||||
|
@ -341,10 +339,11 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
PostageType: 1,
|
PostageType: 1,
|
||||||
}
|
}
|
||||||
var userCoupon model.UserCoupon
|
var userCoupon model.UserCoupon
|
||||||
//err = model.NewUserCouponQuerySet(model.DB).ActivityTypeEq(3).UidEq(uc.Uid).StateEq(1).
|
|
||||||
// OrderAscByID().Limit(1).One(&userCoupon)
|
|
||||||
err = model.NewUserCouponQuerySet(model.DB).IDEq(req.UserCouponId).One(&userCoupon)
|
err = model.NewUserCouponQuerySet(model.DB).IDEq(req.UserCouponId).One(&userCoupon)
|
||||||
if err != nil && err != model.RecordNotFound {
|
if err != nil && err != model.RecordNotFound {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("user coupon err:", err)
|
logger.Error("user coupon err:", err)
|
||||||
RespJson(c, status.InternalServerError, nil)
|
RespJson(c, status.InternalServerError, nil)
|
||||||
return
|
return
|
||||||
|
@ -355,48 +354,24 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
order.PostageType = 2
|
order.PostageType = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以支付成功作为订单下单成功的依据,不是订单创建 已修改
|
|
||||||
//var gameCardGoodsStock model.GameCardGoodsStock
|
|
||||||
//err = model.NewGameCardGoodsStockQuerySet(model.DB).StoreIdEq(req.StoreId).GameCardIdEq(req.GameCardId).One(&gameCardGoodsStock)
|
|
||||||
//if err != nil {
|
|
||||||
// logger.Error("err:", err)
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//if gameCardGoodsStock.RentStock <= 0 {
|
|
||||||
// logger.Error("order stock out ")
|
|
||||||
// RespJson(c, status.OrderStockOut, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
stockEnough, err := model.IsCardGoodsStockEnough(req.GameCardList, req.StoreId)
|
stockEnough, err := model.IsCardGoodsStockEnough(req.GameCardList, req.StoreId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("err:", err)
|
logger.Error("err:", err)
|
||||||
RespJson(c, status.OrderStockOut, nil)
|
RespJson(c, status.OrderStockOut, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if stockEnough {
|
if stockEnough {
|
||||||
|
if req.Price == 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
logger.Error("order stock out ")
|
logger.Error("order stock out ")
|
||||||
RespJson(c, status.OrderStockOut, nil)
|
RespJson(c, status.OrderStockOut, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if user.OpenMemberChannel == model.OpenMemberChannelStorePromotion {
|
|
||||||
// go func() {
|
|
||||||
// exist, err := model.QueryRecordExist(fmt.Sprintf("SELECT * FROM `order` WHERE uid = %d", uc.Uid))
|
|
||||||
// if err != nil {
|
|
||||||
// logger.Error("order exist err:", err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if !exist && user.OpenMemberTime.Format(model.DateTimeFormat) == time.Now().Format(model.DateTimeFormat) {
|
|
||||||
// _, err = model.NewUserQuerySet(model.DB).UidEq(uc.Uid).GetUpdater().SetStoreId(uint64(req.StoreId)).UpdateNum()
|
|
||||||
// if err != nil {
|
|
||||||
// logger.Error("update user err:", err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }()
|
|
||||||
//}
|
|
||||||
|
|
||||||
//fmt.Println("PayPrice:", order.PayPrice)
|
|
||||||
if req.Price == 0 {
|
if req.Price == 0 {
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
tx = model.TransactionBegin()
|
tx = model.TransactionBegin()
|
||||||
|
@ -413,14 +388,6 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 减库存
|
// 减库存
|
||||||
//sql := fmt.Sprintf("UPDATE game_card_goods_stock SET rent_stock= rent_stock-1 WHERE store_id=%d AND game_card_id=%d;", req.StoreId, req.GameCardId)
|
|
||||||
//fmt.Println("sql:", sql)
|
|
||||||
//err = model.DB.Exec(sql).Error
|
|
||||||
//if err != nil {
|
|
||||||
// logger.Errorf("err:", err)
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
err := model.GameCardGoodsInventoryReduction(req.GameCardList, req.StoreId, tx)
|
err := model.GameCardGoodsInventoryReduction(req.GameCardList, req.StoreId, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
@ -470,12 +437,6 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//err = order.OrderCreate()
|
|
||||||
//if err != nil {
|
|
||||||
// logger.Error("err:", err)
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
begin := model.DB.Begin()
|
begin := model.DB.Begin()
|
||||||
err = order.Create(begin)
|
err = order.Create(begin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -485,24 +446,6 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 减库存
|
|
||||||
//sql := fmt.Sprintf("UPDATE game_card_goods_stock SET rent_stock= rent_stock-1 WHERE store_id=%d AND game_card_id=%d;", req.StoreId, req.GameCardId)
|
|
||||||
//fmt.Println("sql:", sql)
|
|
||||||
//err = begin.Exec(sql).Error
|
|
||||||
//if err != nil {
|
|
||||||
// begin.Rollback()
|
|
||||||
// logger.Errorf("err:", err)
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
// 订单回调成功减库存
|
|
||||||
//err = model.GameCardGoodsInventoryReduction(req.GameCardList, req.StoreId, begin)
|
|
||||||
//if err != nil {
|
|
||||||
// begin.Rollback()
|
|
||||||
// logger.Error("inventory reduction err:%s", err.Error())
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
err = order.CreatOrderCard(req.GameCardList, req.StoreId, begin)
|
err = order.CreatOrderCard(req.GameCardList, req.StoreId, begin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
begin.Rollback()
|
begin.Rollback()
|
||||||
|
@ -518,12 +461,6 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//orderId := fmt.Sprintf("%d", order.ID)
|
|
||||||
//orderId = "100000"
|
|
||||||
//fmt.Println("orderId", orderId)
|
|
||||||
//fmt.Println("Price", req.Price)
|
|
||||||
//fmt.Println("WxOpenID", user.WxOpenID)
|
|
||||||
//fmt.Println("WxPayRentCard", wxpay.WxPayRentCard)
|
|
||||||
configInfo, err := model.PayConfigInfo()
|
configInfo, err := model.PayConfigInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
|
@ -538,37 +475,12 @@ func RentCardOrderCreate(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//webPay, err := wxpay.WebPay(order.OrderSn, req.Price, user.WxOpenID, "N", wxpay.WxPayRentCard, configInfo.NotifyUrl)
|
|
||||||
//if err != nil {
|
|
||||||
// logger.Error(errors.New("WebPay err"))
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
webPay, err := wxpay.HmJsPayUnifiedOrder(order.OrderSn, req.Price, user.WxOpenID, configInfo.NotifyUrl)
|
webPay, err := wxpay.HmJsPayUnifiedOrder(order.OrderSn, req.Price, user.WxOpenID, configInfo.NotifyUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(errors.New("WebPay err"))
|
logger.Error(errors.New("WebPay err"))
|
||||||
RespJson(c, status.InternalServerError, nil)
|
RespJson(c, status.InternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//webPay, err := wxpay.HmJsPayUnifiedOrder(order.OrderSn, req.Price, user.WxOpenID, "N", wxpay.WxPayRentCard, configInfo.NotifyUrl)
|
|
||||||
//if err != nil {
|
|
||||||
// logger.Error(errors.New("WebPay err"))
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
|
|
||||||
//num, err := model.NewOrderQuerySet(model.DB).IDEq(order.ID).GetUpdater().
|
|
||||||
// SetOutTradeNo(webPay.NonceStr).SetMchid(config.AppConfig.WxMchID).UpdateNum()
|
|
||||||
//if err != nil {
|
|
||||||
// logger.Error("err:", err)
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//if num == 0 {
|
|
||||||
// logger.Error("UpdateNum is 0")
|
|
||||||
// RespJson(c, status.InternalServerError, nil)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
|
|
||||||
ret := map[string]interface{}{
|
ret := map[string]interface{}{
|
||||||
"web_pay": webPay,
|
"web_pay": webPay,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user