From 5bb2133811d90a7866a14a12864f077491ec63ed Mon Sep 17 00:00:00 2001 From: chenlin Date: Wed, 18 Sep 2024 16:39:29 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=90=8C=E6=AD=A5dev=5F1.2.0=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=9B=E5=BB=BA=E7=A7=9F=E8=B5=81=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E6=97=B6=E5=BC=82=E5=B8=B8=E9=80=80=E5=87=BA=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=95=B0=E6=8D=AE=E5=BA=93=E6=AD=BB=E9=94=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/order.go | 138 ++++++++------------------------------------ 1 file changed, 25 insertions(+), 113 deletions(-) diff --git a/controller/order.go b/controller/order.go index f27a700..b7d5a99 100644 --- a/controller/order.go +++ b/controller/order.go @@ -159,7 +159,7 @@ func WXPaySuccess(c *gin.Context) { var orderCreateLocker sync.Mutex -// 创建订单 +// RentCardOrderCreate 创建订单 func RentCardOrderCreate(c *gin.Context) { req := struct { GameCardList []model.CardInfo `json:"game_card_list"` @@ -178,11 +178,7 @@ func RentCardOrderCreate(c *gin.Context) { return } - //bodyString := strings.Replace(string(body), "\"[", "[", -1) - //bodyString = strings.Replace(string(bodyString), "]\"", "]", -1) - //bodyString = strings.Replace(string(bodyString), "\\", "", -1) logger.Info("bodyString:", string(bodyString)) - err = json.Unmarshal([]byte(bodyString), &req) if err != nil { logger.Error(errors.New("OrderCreate Unmarshal req err")) @@ -250,9 +246,6 @@ func RentCardOrderCreate(c *gin.Context) { rentCard = model.GetUserRentCard(tx, uc.Uid) fmt.Println("*********** rentCard is:", rentCard) if rentCard == nil { - //logger.Error(errors.New("GetUserByUid err")) - //RespJson(c, status.InternalServerError, nil) - //return rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax} } @@ -261,16 +254,10 @@ func RentCardOrderCreate(c *gin.Context) { rentCard = model.GetUserRentCard(nil, uc.Uid) fmt.Println("*********** rentCard is:", rentCard) if rentCard == nil { - //logger.Error(errors.New("GetUserByUid err")) - //RespJson(c, status.InternalServerError, nil) - //return rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax} } } - //if uc.Uid == 45935373 { - // rentCard.CanRentCount -= 1 - //} if cardCount > rentCard.CanRentCount { if req.Price == 0 { tx.Rollback() @@ -280,20 +267,22 @@ func RentCardOrderCreate(c *gin.Context) { return } - // 订单成功后 扣减库存 - //model.UnPayOrderSetCancel(user.Uid) - orderCreateLocker.Lock() defer orderCreateLocker.Unlock() isRentCount, err := rentCard.IsHaveUnreturnedOrders(cardCount) - //unreturnedOrders, err := model.IsHaveUnreturnedOrders(uc.Uid) // if err != nil { + if req.Price == 0 { + tx.Rollback() + } logger.Error("err:", err) RespJson(c, status.InternalServerError, nil) return } if isRentCount { + if req.Price == 0 { + tx.Rollback() + } logger.Error("unreturnedOrders") RespJson(c, status.HaveUnreturnedOrders, nil) return @@ -301,11 +290,17 @@ func RentCardOrderCreate(c *gin.Context) { online, err := model.IsGameCardListOnline(req.GameCardList) if err != nil { + if req.Price == 0 { + tx.Rollback() + } logger.Error("err:", err) RespJson(c, status.InternalServerError, nil) return } if !online { + if req.Price == 0 { + tx.Rollback() + } logger.Error("unreturnedOrders") RespJson(c, status.GoodsSoldOut, nil) return @@ -314,6 +309,9 @@ func RentCardOrderCreate(c *gin.Context) { store.ID = req.StoreId err = store.Info() if err != nil { + if req.Price == 0 { + tx.Rollback() + } logger.Error("store err:", err) RespJson(c, status.InternalServerError, nil) return @@ -341,10 +339,11 @@ func RentCardOrderCreate(c *gin.Context) { PostageType: 1, } 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) if err != nil && err != model.RecordNotFound { + if req.Price == 0 { + tx.Rollback() + } logger.Error("user coupon err:", err) RespJson(c, status.InternalServerError, nil) return @@ -355,48 +354,24 @@ func RentCardOrderCreate(c *gin.Context) { 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) if err != nil { + if req.Price == 0 { + tx.Rollback() + } logger.Error("err:", err) RespJson(c, status.OrderStockOut, nil) return } if stockEnough { + if req.Price == 0 { + tx.Rollback() + } logger.Error("order stock out ") RespJson(c, status.OrderStockOut, nil) 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 tx == nil { 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) if err != nil { tx.Rollback() @@ -470,12 +437,6 @@ func RentCardOrderCreate(c *gin.Context) { return } - //err = order.OrderCreate() - //if err != nil { - // logger.Error("err:", err) - // RespJson(c, status.InternalServerError, nil) - // return - //} begin := model.DB.Begin() err = order.Create(begin) if err != nil { @@ -485,24 +446,6 @@ func RentCardOrderCreate(c *gin.Context) { 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) if err != nil { begin.Rollback() @@ -518,12 +461,6 @@ func RentCardOrderCreate(c *gin.Context) { 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() if err != nil { logger.Error(err) @@ -538,37 +475,12 @@ func RentCardOrderCreate(c *gin.Context) { 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) 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, "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{}{ "web_pay": webPay,