This commit is contained in:
2025-10-21 18:41:08 +08:00
parent 36a614d62b
commit 84046e51e7
5 changed files with 523 additions and 61 deletions

View File

@@ -5,6 +5,7 @@ import (
"database/sql"
"hm-server/app/main/api/internal/svc"
"hm-server/app/main/model"
"hm-server/common/globalkey"
"net/http"
"strings"
"time"
@@ -73,11 +74,11 @@ func (l *WechatPayRefundCallbackLogic) handleQueryOrderRefund(orderNo string, st
}
}
// 更新退款记录状态
refund, err := l.svcCtx.OrderRefundModel.FindOneByOrderId(ctx, order.Id)
// 查找最新的pending状态的退款记录
refund, err := l.findLatestPendingRefund(ctx, order.Id)
if err != nil {
if err == model.ErrNotFound {
logx.Errorf("未找到订单对应的退款记录: orderNo=%s, orderId=%d", orderNo, order.Id)
logx.Errorf("未找到订单对应的待处理退款记录: orderNo=%s, orderId=%d", orderNo, order.Id)
return nil // 没有退款记录时不报错,只记录警告
}
return errors.Wrapf(err, "查找退款记录失败: orderNo=%s", orderNo)
@@ -213,3 +214,23 @@ func (l *WechatPayRefundCallbackLogic) WechatPayRefundCallback(w http.ResponseWr
l.sendSuccessResponse(w)
return nil
}
// findLatestPendingRefund 查找订单最新的pending状态退款记录
func (l *WechatPayRefundCallbackLogic) findLatestPendingRefund(ctx context.Context, orderId int64) (*model.OrderRefund, error) {
// 使用SelectBuilder查询最新的pending状态退款记录
builder := l.svcCtx.OrderRefundModel.SelectBuilder().
Where("order_id = ? AND status = ? AND del_state = ?", orderId, model.OrderRefundStatusPending, globalkey.DelStateNo).
OrderBy("id DESC").
Limit(1)
refunds, err := l.svcCtx.OrderRefundModel.FindAll(ctx, builder, "")
if err != nil {
return nil, err
}
if len(refunds) == 0 {
return nil, model.ErrNotFound
}
return refunds[0], nil
}