feat: 订单自动过期-查看时检查并取消超时订单,收银台倒计时结束自动取消

This commit is contained in:
2026-05-28 02:17:47 +08:00
parent 23badb4526
commit 38b03878e8
4 changed files with 74 additions and 8 deletions
+22 -2
View File
@@ -39,7 +39,7 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
orderID, _ := strconv.Atoi(c.Param("id"))
var order models.Order
if err := utils.DB.Where("id = ? AND user_id = ?", orderID, userID).First(&order).Error; err != nil {
if err := utils.DB.Preload("OrderItems").Where("id = ? AND user_id = ?", orderID, userID).First(&order).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Order not found"})
return
}
@@ -49,6 +49,20 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
return
}
if order.PaymentExpiresAt != nil && time.Now().After(*order.PaymentExpiresAt) {
tx := utils.DB.Begin()
tx.Model(&order).Update("status", models.OrderStatusCancelled)
for _, item := range order.OrderItems {
var inventory models.Inventory
if err := tx.Where("product_id = ?", item.ProductID).First(&inventory).Error; err == nil {
tx.Model(&inventory).UpdateColumn("quantity", inventory.Quantity+item.Quantity)
}
}
tx.Commit()
c.JSON(http.StatusBadRequest, gin.H{"error": "支付已超时,订单已取消"})
return
}
var channels []models.PaymentChannel
utils.DB.Where("is_enabled = ? AND type = ?", true, "bepusdt").Order("sort_order ASC").Find(&channels)
if len(channels) == 0 {
@@ -169,15 +183,21 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
}
var remainingSeconds int64
var expiresAt *time.Time
if expiration > 0 {
remainingSeconds = expiration - time.Now().Unix()
if remainingSeconds < 0 {
remainingSeconds = 0
}
if remainingSeconds > 0 {
t := time.Now().Add(time.Duration(remainingSeconds) * time.Second)
expiresAt = &t
}
}
utils.DB.Model(&order).Updates(map[string]interface{}{
"payment_method": "bepusdt",
"payment_method": "bepusdt",
"payment_expires_at": expiresAt,
})
paymentInfo := map[string]interface{}{