feat(withdrawal): 增加提现审核对账与历史记录
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"hfb_sys/backend/pkg/crypto"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type withdrawalFixture struct {
|
||||
@@ -31,6 +33,12 @@ func newWithdrawalFixture(t *testing.T) withdrawalFixture {
|
||||
&model.WithdrawalRequest{},
|
||||
&model.WalletAccount{},
|
||||
&model.WalletLedger{},
|
||||
&model.RentalListing{},
|
||||
&model.RentalOrder{},
|
||||
&model.GameAccount{},
|
||||
&model.AdminPickup{},
|
||||
&model.AdminPickupFinancialAdjustment{},
|
||||
&model.DistributionSite{},
|
||||
); err != nil {
|
||||
t.Fatalf("migrate fixture: %v", err)
|
||||
}
|
||||
@@ -62,6 +70,205 @@ func newWithdrawalFixture(t *testing.T) withdrawalFixture {
|
||||
}
|
||||
}
|
||||
|
||||
type withdrawalQueryCounter struct {
|
||||
logger.Interface
|
||||
count int
|
||||
}
|
||||
|
||||
func (c *withdrawalQueryCounter) Trace(_ context.Context, _ time.Time, fc func() (string, int64), _ error) {
|
||||
c.count++
|
||||
fc()
|
||||
}
|
||||
|
||||
func TestAdminListUsesBatchQueries(t *testing.T) {
|
||||
f := newWithdrawalFixture(t)
|
||||
for i := 0; i < 5; i++ {
|
||||
item := model.WithdrawalRequest{
|
||||
WithdrawNo: "WD-BATCH-" + string(rune('A'+i)),
|
||||
UserID: f.userID,
|
||||
AmountCent: int64(1000 + i),
|
||||
ActualAmountCent: int64(1000 + i),
|
||||
AccountType: "bank",
|
||||
AccountName: "Test User",
|
||||
AccountNo: "****0000",
|
||||
Status: "pending",
|
||||
}
|
||||
if err := f.db.Create(&item).Error; err != nil {
|
||||
t.Fatalf("create withdrawal: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
counter := &withdrawalQueryCounter{Interface: logger.Default.LogMode(logger.Silent)}
|
||||
repo := NewRepository(f.db.Session(&gorm.Session{Logger: counter}), f.repo.walletRepo, f.repo.encryptor)
|
||||
result, err := repo.AdminList(context.Background(), AdminListQuery{Page: 1, Size: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("AdminList failed: %v", err)
|
||||
}
|
||||
if len(result.Items) != 5 {
|
||||
t.Fatalf("AdminList items = %d, want 5", len(result.Items))
|
||||
}
|
||||
if counter.count > 4 {
|
||||
t.Fatalf("AdminList queries = %d, want batch queries (<=4)", counter.count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminReviewContextSummarizesIncomeAndHistory(t *testing.T) {
|
||||
f := newWithdrawalFixture(t)
|
||||
renter := model.User{Phone: "13800000002", Nickname: "renter", Status: "active"}
|
||||
if err := f.db.Create(&renter).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account := model.GameAccount{OwnerID: f.userID, ServerRegion: "cn", LoginPlatform: "pc", Title: "审核订单"}
|
||||
if err := f.db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listing := model.RentalListing{ListingNo: "202610280362", AccountID: account.ID, OwnerID: f.userID, Status: "active"}
|
||||
if err := f.db.Create(&listing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
orders := []model.RentalOrder{
|
||||
{OrderNo: "RO-WALLET", ListingID: listing.ID, AccountID: account.ID, OwnerID: f.userID, RenterID: renter.ID, Status: "completed", SettlementStatus: "settled", SettlementMode: "owner_wallet", RentAmountCent: 1000, OwnerRentAmountCent: 700},
|
||||
{OrderNo: "RO-PLATFORM", ListingID: listing.ID, AccountID: account.ID, OwnerID: f.userID, RenterID: renter.ID, Status: "completed", SettlementStatus: "arbitrated", SettlementMode: "platform_managed", RentAmountCent: 200, OwnerRentAmountCent: 100, OfflineSettlementStatus: "settled", OfflineSettlementAmountCent: 100},
|
||||
{OrderNo: "RO-OPEN", ListingID: listing.ID, AccountID: account.ID, OwnerID: f.userID, RenterID: renter.ID, Status: "renting", SettlementStatus: "unsettled", SettlementMode: "owner_wallet"},
|
||||
}
|
||||
for i := range orders {
|
||||
if err := f.db.Create(&orders[i]).Error; err != nil {
|
||||
t.Fatalf("create order: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
orderID := orders[0].ID
|
||||
ledgers := []model.WalletLedger{
|
||||
{LedgerNo: "WL-OWNER-INCOME", UserID: f.userID, OrderID: &orderID, Direction: "in", AmountCent: 700, BalanceType: "available", BizType: "owner_income", BizNo: "RO-WALLET"},
|
||||
{LedgerNo: "WL-DEPOSIT-COMP", UserID: f.userID, OrderID: &orderID, Direction: "in", AmountCent: 50, BalanceType: "available", BizType: "deposit_compensation", BizNo: "RO-WALLET"},
|
||||
}
|
||||
if err := f.db.Create(&ledgers).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
pickups := []model.AdminPickup{
|
||||
{PickupNo: "PK-WALLET", ListingID: listing.ID, AccountID: account.ID, OwnerID: f.userID, AdminID: f.adminID, Status: "completed", SettlementMode: "owner_wallet", SettleAmountCent: 200, CompletedAt: &now},
|
||||
{PickupNo: "PK-PLATFORM", ListingID: listing.ID, AccountID: account.ID, OwnerID: f.userID, AdminID: f.adminID, Status: "completed", SettlementMode: "platform_managed", SettleAmountCent: 100, OfflineSettlementStatus: "settled", CompletedAt: &now},
|
||||
}
|
||||
if err := f.db.Create(&pickups).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adjustments := []model.AdminPickupFinancialAdjustment{
|
||||
{PickupID: pickups[0].ID, SettleDeltaCent: 50, SettlementMode: "owner_wallet", Status: "settled", Reason: "补款", CreatedBy: f.adminID},
|
||||
{PickupID: pickups[1].ID, SettleDeltaCent: 20, SettlementMode: "platform_managed", Status: "settled", Reason: "补款", CreatedBy: f.adminID},
|
||||
}
|
||||
if err := f.db.Create(&adjustments).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
withdrawals := []model.WithdrawalRequest{
|
||||
{WithdrawNo: "WD-CURRENT", UserID: f.userID, AmountCent: 300, ActualAmountCent: 300, AccountType: "bank", AccountName: "Test User", AccountNo: "****0000", Status: "pending", CreatedAt: now},
|
||||
{WithdrawNo: "WD-PAID", UserID: f.userID, AmountCent: 300, ActualAmountCent: 300, AccountType: "bank", AccountName: "Test User", AccountNo: "****0000", Status: "completed", CreatedAt: now.Add(-time.Hour)},
|
||||
{WithdrawNo: "WD-REJECTED", UserID: f.userID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "Test User", AccountNo: "****0000", Status: "rejected", CreatedAt: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
if err := f.db.Create(&withdrawals).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
contextDTO, err := f.repo.AdminReviewContext(context.Background(), withdrawals[0].ID, WithdrawalReviewContextQuery{PageSize: 10})
|
||||
if err != nil {
|
||||
t.Fatalf("AdminReviewContext failed: %v", err)
|
||||
}
|
||||
summary := contextDTO.Summary
|
||||
if summary.OwnerOrderCount != 3 || summary.FinalizedOrderCount != 2 {
|
||||
t.Fatalf("order summary = %+v", summary)
|
||||
}
|
||||
if summary.OwnerPickupCount != 2 || summary.PickupIncomeCent != 370 {
|
||||
t.Fatalf("pickup summary = %+v", summary)
|
||||
}
|
||||
if summary.WalletPickupIncomeCent != 250 || summary.PlatformManagedPickupIncomeCent != 120 {
|
||||
t.Fatalf("pickup income summary = %+v", summary)
|
||||
}
|
||||
if summary.WalletActualIncomeCent != 1000 || summary.PlatformManagedIncomeCent != 220 {
|
||||
t.Fatalf("income summary = %+v", summary)
|
||||
}
|
||||
if summary.WithdrawalCount != 3 || summary.WithdrawnAmountCent != 300 || summary.PendingWithdrawalAmountCent != 300 || summary.RejectedWithdrawalCount != 1 {
|
||||
t.Fatalf("withdrawal summary = %+v", summary)
|
||||
}
|
||||
if summary.ReconcileDifferenceAmountCent != 400 {
|
||||
t.Fatalf("reconcile difference = %d, want 400", summary.ReconcileDifferenceAmountCent)
|
||||
}
|
||||
if len(contextDTO.Orders) != 3 || contextDTO.Orders[0].ListingNo != listing.ListingNo {
|
||||
t.Fatalf("review orders = %+v", contextDTO.Orders)
|
||||
}
|
||||
if len(contextDTO.Pickups) != 2 || contextDTO.Pickups[0].ListingNo != listing.ListingNo {
|
||||
t.Fatalf("review pickups = %+v", contextDTO.Pickups)
|
||||
}
|
||||
if len(contextDTO.Withdrawals) != 3 || contextDTO.Withdrawals[0].WithdrawNo != "WD-CURRENT" {
|
||||
t.Fatalf("review withdrawals = %+v", contextDTO.Withdrawals)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminReviewContextScopesIncomeByWithdrawalSite(t *testing.T) {
|
||||
f := newWithdrawalFixture(t)
|
||||
siteA := model.DistributionSite{SiteCode: "audit-a", SiteName: "审核站A", Domain: "a.example.com", Status: "active"}
|
||||
siteB := model.DistributionSite{SiteCode: "audit-b", SiteName: "审核站B", Domain: "b.example.com", Status: "active"}
|
||||
if err := f.db.Create(&siteA).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.Create(&siteB).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
renter := model.User{Phone: "13800000003", Nickname: "renter-site", Status: "active"}
|
||||
if err := f.db.Create(&renter).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account := model.GameAccount{OwnerID: f.userID, ServerRegion: "cn", LoginPlatform: "pc", Title: "分站审核"}
|
||||
if err := f.db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listingA := model.RentalListing{ListingNo: "SITE-A-LISTING", AccountID: account.ID, OwnerID: f.userID, Status: "active", DistributionSiteID: &siteA.ID}
|
||||
listingB := model.RentalListing{ListingNo: "SITE-B-LISTING", AccountID: account.ID, OwnerID: f.userID, Status: "active", DistributionSiteID: &siteB.ID}
|
||||
if err := f.db.Create(&listingA).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.Create(&listingB).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
orderA := model.RentalOrder{OrderNo: "RO-SITE-A", ListingID: listingA.ID, AccountID: account.ID, OwnerID: f.userID, RenterID: renter.ID, Status: "completed", SettlementStatus: "settled", SettlementMode: "owner_wallet", DistributionSiteID: &siteA.ID}
|
||||
orderB := model.RentalOrder{OrderNo: "RO-SITE-B", ListingID: listingB.ID, AccountID: account.ID, OwnerID: f.userID, RenterID: renter.ID, Status: "completed", SettlementStatus: "settled", SettlementMode: "owner_wallet", DistributionSiteID: &siteB.ID}
|
||||
if err := f.db.Create(&orderA).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.Create(&orderB).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.Create(&[]model.WalletLedger{
|
||||
{LedgerNo: "WL-SITE-A", UserID: f.userID, DistributionSiteID: siteA.ID, OrderID: &orderA.ID, Direction: "in", AmountCent: 100, BalanceType: "available", BizType: "owner_income", BizNo: orderA.OrderNo},
|
||||
{LedgerNo: "WL-SITE-B", UserID: f.userID, DistributionSiteID: siteB.ID, OrderID: &orderB.ID, Direction: "in", AmountCent: 999, BalanceType: "available", BizType: "owner_income", BizNo: orderB.OrderNo},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.Create(&[]model.AdminPickup{
|
||||
{PickupNo: "PK-SITE-A", ListingID: listingA.ID, AccountID: account.ID, OwnerID: f.userID, AdminID: f.adminID, Status: "completed", SettlementMode: "owner_wallet", SettleAmountCent: 100},
|
||||
{PickupNo: "PK-SITE-B", ListingID: listingB.ID, AccountID: account.ID, OwnerID: f.userID, AdminID: f.adminID, Status: "completed", SettlementMode: "owner_wallet", SettleAmountCent: 999},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current := model.WithdrawalRequest{WithdrawNo: "WD-SITE-A", UserID: f.userID, DistributionSiteID: siteA.ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "Test User", AccountNo: "****0000", Status: "pending"}
|
||||
if err := f.db.Create(¤t).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
contextDTO, err := f.repo.AdminReviewContext(context.Background(), current.ID, WithdrawalReviewContextQuery{PageSize: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if contextDTO.Summary.OwnerOrderCount != 1 || contextDTO.Summary.OwnerPickupCount != 1 {
|
||||
t.Fatalf("site-scoped business count = %+v", contextDTO.Summary)
|
||||
}
|
||||
if contextDTO.Summary.WalletActualIncomeCent != 200 || contextDTO.Summary.WithdrawalCount != 1 {
|
||||
t.Fatalf("site-scoped income/withdrawal = %+v", contextDTO.Summary)
|
||||
}
|
||||
}
|
||||
|
||||
func (f withdrawalFixture) create(t *testing.T, amountCent int64) *WithdrawalDTO {
|
||||
t.Helper()
|
||||
dto, err := f.repo.Create(context.Background(), f.userID, CreateWithdrawalRequest{
|
||||
|
||||
Reference in New Issue
Block a user