修复超时订单重置流程

This commit is contained in:
yml2213
2026-06-19 18:36:46 +08:00
parent 2df935f74c
commit 72285ed638
11 changed files with 449 additions and 46 deletions
+37 -8
View File
@@ -347,6 +347,8 @@ func (j *Job) handleRenterConfirmTimeout(ctx context.Context, now time.Time, cfg
if cfg.RenterConfirmTimeoutMinutes <= 0 {
return 0, nil
}
deadline := now.Add(-time.Duration(cfg.RenterConfirmTimeoutMinutes) * time.Minute)
var rows []model.RentalOrder
err := j.db.WithContext(ctx).
Where("status = ? AND handoff_status = ?", "pending_handoff", "pending_renter_confirm").
@@ -356,15 +358,42 @@ func (j *Job) handleRenterConfirmTimeout(ctx context.Context, now time.Time, cfg
if err != nil {
return 0, err
}
if len(rows) == 0 {
return 0, nil
}
// 批量查询所有订单的最新交接记录,减少 N+1 查询
orderIDs := make([]uint64, len(rows))
for i, row := range rows {
orderIDs[i] = row.ID
}
var handoffs []model.HandoffRecord
if err := j.db.WithContext(ctx).
Where("order_id IN ? AND type = ?", orderIDs, "owner_handoff").
Order("order_id ASC, id DESC").
Find(&handoffs).Error; err != nil {
return 0, err
}
// 构建 order_id -> 最新交接记录的映射
latestHandoff := make(map[uint64]model.HandoffRecord, len(handoffs))
for _, h := range handoffs {
if _, exists := latestHandoff[h.OrderID]; !exists {
latestHandoff[h.OrderID] = h
}
}
count := 0
deadline := now.Add(-time.Duration(cfg.RenterConfirmTimeoutMinutes) * time.Minute)
for _, row := range rows {
var handoff model.HandoffRecord
err := j.db.WithContext(ctx).
Where("order_id = ? AND type = ?", row.ID, "owner_handoff").
Order("id DESC").
First(&handoff).Error
if err != nil || handoff.CreatedAt.After(deadline) {
handoff, exists := latestHandoff[row.ID]
if !exists {
continue
}
// 判断阶段开始时间:优先使用 HandoffStartedAt(管理员重置后刷新的时间)
stageStart := handoff.CreatedAt
if row.HandoffStartedAt != nil && row.HandoffStartedAt.After(handoff.CreatedAt) {
stageStart = *row.HandoffStartedAt
}
if stageStart.After(deadline) {
continue
}
if err := j.updateOrder(ctx, row.ID, "order.timeout.renter_confirm", func(tx *gorm.DB, order *model.RentalOrder) (string, error) {
@@ -460,7 +489,7 @@ func (j *Job) handleOwnerReturnConfirmTimeout(ctx context.Context, now time.Time
}
var rows []model.RentalOrder
err := j.db.WithContext(ctx).
Where("status = ? AND handoff_status = ? AND updated_at <= ?", "pending_checkout_confirm", "pending_owner_checkout", now.Add(-time.Duration(cfg.OwnerReturnConfirmTimeoutMinutes)*time.Minute)).
Where("status = ? AND handoff_status = ? AND COALESCE(handoff_started_at, updated_at) <= ?", "pending_checkout_confirm", "pending_owner_checkout", now.Add(-time.Duration(cfg.OwnerReturnConfirmTimeoutMinutes)*time.Minute)).
Order("id ASC").
Limit(100).
Find(&rows).Error