package order import ( "context" "hfb_sys/backend/internal/logging" "hfb_sys/backend/internal/model" "go.uber.org/zap" "gorm.io/gorm" ) func (r *Repository) prepareRefund(order *model.RentalOrder, amountCent int64, bizType string, remark string) (*refundAction, error) { if amountCent <= 0 { return nil, nil } if r.refundStarter == nil { return nil, ErrDependencyUnavailable } order.RefundStatus = refundStatusPending order.RefundAmountCent = amountCent order.RefundedAt = nil return &refundAction{ OrderID: order.ID, RefundAmountCent: amountCent, BizType: bizType, Remark: remark, }, nil } // outstandingPaidOrderPaymentCentTx 算出订单实际收到、尚未退回的下单款项。 // 口径是已支付的下单流水合计减去已退/在退的退款流水合计,重复关单不会重复退。 func outstandingPaidOrderPaymentCentTx(tx *gorm.DB, orderID uint64) (int64, error) { var paidCent int64 if err := tx.Model(&model.PaymentOrder{}). Where("order_id = ? AND biz_type = ? AND status = ?", orderID, "order_pay", "paid"). Select("COALESCE(SUM(amount_cent), 0)").Scan(&paidCent).Error; err != nil { return 0, err } if paidCent <= 0 { return 0, nil } var refundedCent int64 if err := tx.Model(&model.PaymentOrder{}). Where("order_id = ? AND biz_type <> ? AND status IN ?", orderID, "order_pay", []string{"refunding", "refunded"}). Select("COALESCE(SUM(amount_cent), 0)").Scan(&refundedCent).Error; err != nil { return 0, err } if refundedCent >= paidCent { return 0, nil } return paidCent - refundedCent, nil } // pendingPaymentOrderPaidAtChannelForAdmin 在客服关单前核对待支付订单的渠道收款情况。 func (r *Repository) pendingPaymentOrderPaidAtChannelForAdmin(ctx context.Context, orderID uint64) bool { if r.paymentProbe == nil { return false } var status string err := r.db.WithContext(ctx).Model(&model.RentalOrder{}). Where("id = ?", orderID). Pluck("status", &status).Error if err != nil || status != orderStatusPendingPayment { return false } return r.channelPaidBeforeClose(ctx, orderID) } func (r *Repository) startRefundBestEffort(ctx context.Context, action *refundAction) { if action == nil || r.refundStarter == nil { return } if _, err := r.refundStarter.StartRefund(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark); err != nil { logging.FromContext(ctx).Error("订单退款提交失败", zap.String("module", "order"), zap.Uint64("order_id", action.OrderID), zap.String("biz_type", action.BizType), zap.Int64("amount_cent", action.RefundAmountCent), zap.Error(err), ) } }