feat(v2): gateway 经 Picker 路由选账户 + retry 换号(ExcludeAccounts);替换首个 enabled

This commit is contained in:
wangjia
2026-07-10 14:02:33 +08:00
parent 381288e707
commit c06f6d3a3c
7 changed files with 109 additions and 17 deletions
+19
View File
@@ -0,0 +1,19 @@
package store
import (
"fmt"
"github.com/wangjia/pay/internal/model"
)
// AttemptAccountIDs returns the distinct account_ids already tried for an order on a
// channel (used by retry to switch to a not-yet-tried account via PickHint.ExcludeAccounts).
func (s *OrderStore) AttemptAccountIDs(outTradeNo, channel string) ([]string, error) {
var ids []string
if err := s.db.Model(&model.Attempt{}).
Where("out_trade_no = ? AND channel = ? AND account_id <> ''", outTradeNo, channel).
Distinct().Pluck("account_id", &ids).Error; err != nil {
return nil, fmt.Errorf("store.AttemptAccountIDs: %w", err)
}
return ids, nil
}
+39
View File
@@ -0,0 +1,39 @@
package store_test
import (
"testing"
"github.com/wangjia/pay/internal/model"
"github.com/wangjia/pay/internal/store"
)
func TestAttemptAccountIDs(t *testing.T) {
s := store.NewOrderStore(model.OpenTestDB(t))
if err := s.CreateOrder(&model.OrderV2{OutTradeNo: "PAY-AC1", Subject: "x", AmountMinor: 100, Currency: "USDT", Status: model.OrderPendingV2}); err != nil {
t.Fatalf("order: %v", err)
}
for _, tc := range []struct{ ref, acct string }{{"R1", "a1"}, {"R2", "a2"}, {"R3", "a1"}} {
if err := s.CreateAttempt(&model.Attempt{
OutTradeNo: "PAY-AC1", Channel: "fake", AccountID: tc.acct, Provider: "fake",
ProviderRef: tc.ref, RenderType: "crypto_address", AmountMinor: 100, Currency: "USDT",
Status: model.AttemptPending,
}); err != nil {
t.Fatalf("attempt %s: %v", tc.ref, err)
}
}
ids, err := s.AttemptAccountIDs("PAY-AC1", "fake")
if err != nil {
t.Fatalf("AttemptAccountIDs: %v", err)
}
set := map[string]bool{}
for _, id := range ids {
set[id] = true
}
if len(set) != 2 || !set["a1"] || !set["a2"] {
t.Fatalf("应含 distinct a1,a2, got %v", ids)
}
// 其它渠道 / 其它单不串。
if got, _ := s.AttemptAccountIDs("PAY-AC1", "alipay"); len(got) != 0 {
t.Fatalf("别的渠道应空, got %v", got)
}
}