feat(v2): 对账主体——周期查单收敛 + 已付订单抽查 + 退款修复扫描/卡滞告警 + main 装配 reconcile Runner
Task 5(债务 #6):两条腿收敛对账——① SyncPendingTask 调度 P2 gateway.SyncPendingAttempts 逐 pending attempt 查单收敛(防掉单);② PaidSpotCheckTask 对近期已付 attempt 反查渠道 核对金额/币种,漂移(渠道侧已退款/拒付而本地仍 paid)只告警不改状态。 追加两条 P4 T3 opus review 义务(该 review 产出时本 worktree 已分叉,P4 退款主体在主 checkout;这里对本 worktree 已有的 store.RefundStore/OrderStore 接口——P4 T2,在 base 里——建自愈扫描,设计为可在合并 P4 后继续工作): - RefundApplyTask:退款修复扫描,重算 succeeded 退款之和,自愈「退款成功但订单卡 paid」 的崩溃窗口(MarkRefundStatus 翻 succeeded 后、ApplyRefundToOrder 调用前崩溃)。候选订单 =有 succeeded 退款的订单 ∪ 当前处于 refunding/partially_refunded 态的订单;走既有 ApplyRefundToOrder 条件 UPDATE,目标态与当前态一致时跳过,天然幂等。 - RefundStuckAlertTask:卡滞 processing/manual_pending 退款超阈值(默认 30min)打 WARN, 只观测不改状态;渠道退款查询 API 面留待后续。 main 装配前 4 job(order-expire/usage-refresh/sync-pending/paid-spotcheck)+ 上述两个退款 job 挂上 reconcile.Runner;acctPicker 的 limit_aware 用量源改用 reconcile.NewUsageSource 替 NopUsage。crypto 冷启动 Warm/孤儿扫描(AddCryptoJobs)留给 Task 6 追加。 config.go 新增 ReconcileConfig(含 Task 6 预留的 orphan_* 字段)+ 默认值;crypto.go 补 SetReservationLoader 构造后注入 setter(Task 6 依赖)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
This commit is contained in:
@@ -147,3 +147,30 @@ func (s *OrderStore) SumPaidAttemptMinorByAccountSince(since time.Time) (map[str
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListRecentlyPaidAttempts 列近期(paid_at>=since)已付 attempt,供对账抽查反查渠道核对。
|
||||
func (s *OrderStore) ListRecentlyPaidAttempts(since time.Time, limit int) ([]model.Attempt, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 100
|
||||
}
|
||||
var out []model.Attempt
|
||||
if err := s.db.Where("status = ? AND paid_at >= ?", model.AttemptPaid, since).
|
||||
Order("id DESC").Limit(limit).Find(&out).Error; err != nil {
|
||||
return nil, fmt.Errorf("store.ListRecentlyPaidAttempts: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListOrdersByStatus 按状态集合列订单,供退款修复扫描(Task 5 P4 义务)定位「当前处于
|
||||
// 退款相关态」的候选订单,与 RefundStore.ListDistinctOutTradeNosByStatus(succeeded)取并集。
|
||||
func (s *OrderStore) ListOrdersByStatus(statuses []model.OrderStatusV2, limit int) ([]model.OrderV2, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
}
|
||||
var out []model.OrderV2
|
||||
if err := s.db.Where("status IN ?", statuses).
|
||||
Order("id ASC").Limit(limit).Find(&out).Error; err != nil {
|
||||
return nil, fmt.Errorf("store.ListOrdersByStatus: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -123,3 +123,56 @@ func TestSumPaidAttemptMinorByAccountSince(t *testing.T) {
|
||||
t.Fatalf("聚合 = %+v, want acct-1=15000 acct-2=7000", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListRecentlyPaidAttempts(t *testing.T) {
|
||||
db := model.OpenTestDB(t)
|
||||
s := store.NewOrderStore(db)
|
||||
now := time.Date(2026, 7, 10, 12, 0, 0, 0, time.UTC)
|
||||
mk := func(no string, st model.AttemptStatus, paidAgo time.Duration) {
|
||||
paid := now.Add(-paidAgo)
|
||||
_ = s.CreateAttempt(&model.Attempt{OutTradeNo: no, Channel: "fake", ProviderRef: "R-" + no,
|
||||
AmountMinor: 100, Currency: "USDT", Status: st, PaidAt: &paid})
|
||||
}
|
||||
mk("RECENT", model.AttemptPaid, 10*time.Minute) // 近期已付 → 命中
|
||||
mk("OLD", model.AttemptPaid, 5*time.Hour) // 太旧 → 不命中
|
||||
mk("PEND", model.AttemptPending, 1*time.Minute) // 未付 → 不命中
|
||||
|
||||
got, err := s.ListRecentlyPaidAttempts(now.Add(-time.Hour), 50)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].OutTradeNo != "RECENT" {
|
||||
t.Fatalf("只应含 RECENT, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListOrdersByStatus 供 P6+P4 义务的「退款修复扫描」定位候选订单:按状态集合
|
||||
// 列订单(如 refunding/partially_refunded),与 refund 表的 succeeded 记录取并集
|
||||
// 作为重算候选。
|
||||
func TestListOrdersByStatus(t *testing.T) {
|
||||
db := model.OpenTestDB(t)
|
||||
s := store.NewOrderStore(db)
|
||||
mk := func(no string, st model.OrderStatusV2) {
|
||||
_ = s.CreateOrder(&model.OrderV2{OutTradeNo: no, AmountMinor: 100, Currency: "CNY", Status: st})
|
||||
}
|
||||
mk("O-PAID", model.OrderPaidV2)
|
||||
mk("O-REFUNDING", model.OrderRefundingV2)
|
||||
mk("O-PART", model.OrderPartRefundedV2)
|
||||
mk("O-DONE", model.OrderRefundedV2)
|
||||
mk("O-PENDING", model.OrderPendingV2)
|
||||
|
||||
got, err := s.ListOrdersByStatus([]model.OrderStatusV2{model.OrderRefundingV2, model.OrderPartRefundedV2}, 50)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("应命中 2 张(REFUNDING/PART), got %d: %+v", len(got), got)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, o := range got {
|
||||
seen[o.OutTradeNo] = true
|
||||
}
|
||||
if !seen["O-REFUNDING"] || !seen["O-PART"] {
|
||||
t.Fatalf("命中集合不对: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +128,38 @@ func (s *RefundStore) MarkRefundStatus(refundID string, from, to model.RefundSta
|
||||
return res.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
// ListDistinctOutTradeNosByStatus 列有某状态退款的 distinct out_trade_no,供退款修复
|
||||
// 扫描(RefundApplyTask)定位「有 succeeded 退款」的候选订单——self-heal「退款成功但
|
||||
// 订单卡 paid」的崩溃窗口(P4 T3 review 义务,见 task-5-brief 外的两条追加义务)。
|
||||
func (s *RefundStore) ListDistinctOutTradeNosByStatus(status model.RefundStatus, limit int) ([]string, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
}
|
||||
var out []string
|
||||
if err := s.db.Model(&model.Refund{}).Where("status = ?", status).
|
||||
Group("out_trade_no").Order("out_trade_no ASC").Limit(limit).
|
||||
Pluck("out_trade_no", &out).Error; err != nil {
|
||||
return nil, fmt.Errorf("store.ListDistinctOutTradeNosByStatus: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListStuckRefunds 列 status 落在给定集合、且 updated_at 早于 before 的退款行,供
|
||||
// 「卡滞 processing/manual_pending 退款告警」只读观测扫描用(不改状态)。updated_at
|
||||
// 用作「进入当前状态」的近似时刻——本表除 MarkRefundStatus/CreateRefundGuarded 外
|
||||
// 不写,近似成立。
|
||||
func (s *RefundStore) ListStuckRefunds(statuses []model.RefundStatus, before time.Time, limit int) ([]model.Refund, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
}
|
||||
var out []model.Refund
|
||||
if err := s.db.Where("status IN ? AND updated_at < ?", statuses, before).
|
||||
Order("updated_at ASC").Limit(limit).Find(&out).Error; err != nil {
|
||||
return nil, fmt.Errorf("store.ListStuckRefunds: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListManualPending lists refunds awaiting manual (crypto) settlement.
|
||||
func (s *RefundStore) ListManualPending(limit int) ([]model.Refund, error) {
|
||||
if limit <= 0 || limit > 200 {
|
||||
|
||||
@@ -100,6 +100,68 @@ func TestApplyRefundToOrderFully(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestListDistinctOutTradeNosByStatus 供退款修复扫描定位「有 succeeded 退款」的候选
|
||||
// 订单(自愈依据):同订单多笔 succeeded 退款只应出现一次(distinct)。
|
||||
func TestListDistinctOutTradeNosByStatus(t *testing.T) {
|
||||
db := model.OpenTestDB(t)
|
||||
rs := NewRefundStore(db)
|
||||
_ = rs.CreateRefund(&model.Refund{RefundID: "s1", OutTradeNo: "PAY-S1", AmountMinor: 100, Currency: "CNY", Status: model.RefundSucceeded})
|
||||
_ = rs.CreateRefund(&model.Refund{RefundID: "s2", OutTradeNo: "PAY-S1", AmountMinor: 200, Currency: "CNY", Status: model.RefundSucceeded}) // 同单第二笔
|
||||
_ = rs.CreateRefund(&model.Refund{RefundID: "s3", OutTradeNo: "PAY-S2", AmountMinor: 100, Currency: "CNY", Status: model.RefundSucceeded})
|
||||
_ = rs.CreateRefund(&model.Refund{RefundID: "p1", OutTradeNo: "PAY-S3", AmountMinor: 100, Currency: "CNY", Status: model.RefundProcessing}) // 非 succeeded,不应命中
|
||||
|
||||
got, err := rs.ListDistinctOutTradeNosByStatus(model.RefundSucceeded, 50)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("应 distinct 出 2 个 out_trade_no, got %d: %+v", len(got), got)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, no := range got {
|
||||
seen[no] = true
|
||||
}
|
||||
if !seen["PAY-S1"] || !seen["PAY-S2"] {
|
||||
t.Fatalf("命中集合不对: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListStuckRefunds 供「卡滞 processing/manual_pending 退款告警」:只挑 updated_at
|
||||
// 早于阈值的 processing/manual_pending 行,requested/succeeded/failed 不命中。
|
||||
func TestListStuckRefunds(t *testing.T) {
|
||||
db := model.OpenTestDB(t)
|
||||
rs := NewRefundStore(db)
|
||||
now := time.Date(2026, 7, 10, 12, 0, 0, 0, time.UTC)
|
||||
mk := func(id, no string, st model.RefundStatus, updatedAgo time.Duration) {
|
||||
if err := rs.CreateRefund(&model.Refund{RefundID: id, OutTradeNo: no, AmountMinor: 100, Currency: "CNY", Status: st}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Model(&model.Refund{}).Where("refund_id = ?", id).
|
||||
Update("updated_at", now.Add(-updatedAgo)).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
mk("stuck-proc", "PAY-T1", model.RefundProcessing, 45*time.Minute) // 超阈值 → 命中
|
||||
mk("fresh-proc", "PAY-T2", model.RefundProcessing, 5*time.Minute) // 未超 → 不命中
|
||||
mk("stuck-manual", "PAY-T3", model.RefundManualPending, 2*time.Hour) // 超阈值 → 命中
|
||||
mk("done", "PAY-T4", model.RefundSucceeded, 2*time.Hour) // 已终态 → 不命中
|
||||
|
||||
got, err := rs.ListStuckRefunds([]model.RefundStatus{model.RefundProcessing, model.RefundManualPending}, now.Add(-30*time.Minute), 50)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("应命中 2 笔卡滞, got %d: %+v", len(got), got)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, r := range got {
|
||||
seen[r.RefundID] = true
|
||||
}
|
||||
if !seen["stuck-proc"] || !seen["stuck-manual"] {
|
||||
t.Fatalf("命中集合不对: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListManualPending(t *testing.T) {
|
||||
db := model.OpenTestDB(t)
|
||||
rs := NewRefundStore(db)
|
||||
|
||||
Reference in New Issue
Block a user