Files
hfb_sys/backend/internal/modules/platformops/repository_test.go
T

201 lines
8.2 KiB
Go

package platformops
import (
"testing"
"time"
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
)
func TestOverviewAggregatesAllTenantsAndSites(t *testing.T) {
db := database.NewTestDB()
tenants := []model.Tenant{
{Code: "tenant_a", Name: "A站", Status: "active"},
{Code: "tenant_b", Name: "B站", Status: "active"},
}
if err := db.Create(&tenants).Error; err != nil {
t.Fatal(err)
}
users := []model.User{
{TenantID: tenants[0].ID, Phone: "13100000101", Status: "active", RealnameStatus: "verified"},
{TenantID: tenants[0].ID, Phone: "13100000102", Status: "active"},
{TenantID: tenants[1].ID, Phone: "13100000201", Status: "active", RealnameStatus: "verified"},
{TenantID: tenants[1].ID, Phone: "13100000202", Status: "active"},
}
if err := db.Create(&users).Error; err != nil {
t.Fatal(err)
}
for index, tenant := range tenants {
account := model.GameAccount{
SourceTenantID: tenant.ID,
OwnerID: users[index*2].ID,
GameName: "delta_force",
ServerRegion: "国服",
LoginPlatform: "QQ",
Title: tenant.Name + "账号",
Status: "published",
}
if err := db.Create(&account).Error; err != nil {
t.Fatal(err)
}
listing := model.RentalListing{
SourceTenantID: tenant.ID,
ListingNo: "OPS-" + tenant.Code,
AccountID: account.ID,
OwnerID: users[index*2].ID,
Status: "published",
ReviewStatus: "approved",
PriceCent: 1000,
}
if err := db.Create(&listing).Error; err != nil {
t.Fatal(err)
}
}
orders := []model.RentalOrder{
{
OrderNo: "OPS-ORDER-1",
BuyerTenantID: tenants[0].ID,
SourceTenantID: tenants[1].ID,
PaymentTenantID: tenants[0].ID,
RenterID: users[0].ID,
OwnerID: users[2].ID,
Status: "completed",
RentAmountCent: 1000,
SourceAmountCent: 800,
},
{
OrderNo: "OPS-ORDER-2",
BuyerTenantID: tenants[1].ID,
SourceTenantID: tenants[0].ID,
PaymentTenantID: tenants[1].ID,
RenterID: users[2].ID,
OwnerID: users[0].ID,
Status: "pending_payment",
RentAmountCent: 2000,
SourceAmountCent: 1600,
},
}
if err := db.Create(&orders).Error; err != nil {
t.Fatal(err)
}
now := time.Now()
ledgers := []model.WalletLedger{
{LedgerNo: "OPS-WL-A-IN", TenantID: tenants[0].ID, UserID: users[0].ID, Direction: "in", AmountCent: 1000, BalanceType: "available", BizType: "owner_income", BizNo: "OPS-ORDER-1", CreatedAt: now},
{LedgerNo: "OPS-WL-A-OUT", TenantID: tenants[0].ID, UserID: users[0].ID, Direction: "out", AmountCent: 200, BalanceType: "available", BizType: "withdrawal", BizNo: "OPS-WD-A", CreatedAt: now.AddDate(0, 0, -1)},
{LedgerNo: "OPS-WL-B-IN", TenantID: tenants[1].ID, UserID: users[2].ID, Direction: "in", AmountCent: 300, BalanceType: "available", BizType: "owner_income", BizNo: "OPS-ORDER-2", CreatedAt: now},
{LedgerNo: "OPS-WL-B-OUT", TenantID: tenants[1].ID, UserID: users[2].ID, Direction: "out", AmountCent: 100, BalanceType: "available", BizType: "withdrawal", BizNo: "OPS-WD-B", CreatedAt: now},
}
if err := db.Create(&ledgers).Error; err != nil {
t.Fatal(err)
}
withdrawals := []model.WithdrawalRequest{
{WithdrawNo: "OPS-WD-A", TenantID: tenants[0].ID, UserID: users[0].ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "A站用户", AccountNo: "****0001", Status: "pending"},
{WithdrawNo: "OPS-WD-B", TenantID: tenants[1].ID, UserID: users[2].ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "B站用户", AccountNo: "****0002", Status: "processing"},
{WithdrawNo: "OPS-WD-DONE", TenantID: tenants[1].ID, UserID: users[3].ID, AmountCent: 100, ActualAmountCent: 100, AccountType: "bank", AccountName: "B站用户", AccountNo: "****0003", Status: "completed"},
}
if err := db.Create(&withdrawals).Error; err != nil {
t.Fatal(err)
}
legacySite := model.DistributionSite{
SiteCode: "legacy_platform_ops", SiteName: "历史分销测试站",
Domain: "legacy-platform-ops.example.com", Status: "active",
}
if err := db.Create(&legacySite).Error; err != nil {
t.Fatal(err)
}
legacyUser := model.User{
TenantID: tenants[0].ID, Phone: "13100000999", Status: "active",
DistributionSiteID: legacySite.ID,
}
if err := db.Create(&legacyUser).Error; err != nil {
t.Fatal(err)
}
legacyAccount := model.GameAccount{
SourceTenantID: tenants[0].ID, OwnerID: legacyUser.ID,
GameName: "delta_force", ServerRegion: "国服", LoginPlatform: "QQ",
Title: "历史分销商品", Status: "published",
}
if err := db.Create(&legacyAccount).Error; err != nil {
t.Fatal(err)
}
legacyListing := model.RentalListing{
SourceTenantID: tenants[0].ID, ListingNo: "OPS-LEGACY-DIST",
AccountID: legacyAccount.ID, OwnerID: legacyUser.ID,
Status: "published", ReviewStatus: "approved", PriceCent: 5000,
DistributionSiteID: &legacySite.ID,
}
if err := db.Create(&legacyListing).Error; err != nil {
t.Fatal(err)
}
legacyOrder := model.RentalOrder{
OrderNo: "OPS-LEGACY-ORDER", BuyerTenantID: tenants[0].ID,
SourceTenantID: tenants[0].ID, PaymentTenantID: tenants[0].ID,
ListingID: legacyListing.ID, AccountID: legacyAccount.ID,
OwnerID: legacyUser.ID, RenterID: legacyUser.ID,
Status: "completed", RentAmountCent: 5000, SourceAmountCent: 4000,
DistributionSiteID: &legacySite.ID,
}
if err := db.Create(&legacyOrder).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&model.WalletLedger{
LedgerNo: "OPS-LEGACY-WL", TenantID: tenants[0].ID, UserID: legacyUser.ID,
DistributionSiteID: legacySite.ID, Direction: "in", AmountCent: 5000,
BalanceType: "available", BizType: "owner_income", BizNo: legacyOrder.OrderNo,
}).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&model.WithdrawalRequest{
WithdrawNo: "OPS-LEGACY-WD", TenantID: tenants[0].ID, UserID: legacyUser.ID,
DistributionSiteID: legacySite.ID, AmountCent: 500, ActualAmountCent: 500,
AccountType: "bank", AccountName: "历史站用户", AccountNo: "****9999", Status: "pending",
}).Error; err != nil {
t.Fatal(err)
}
var userCount int64
if err := db.Model(&model.User{}).Count(&userCount).Error; err != nil || userCount != 5 {
t.Fatalf("seeded users = %d, err=%v", userCount, err)
}
overview, err := NewRepository(db).Overview(t.Context())
if err != nil {
t.Fatal(err)
}
if overview.Totals.TenantCount != 2 || overview.Totals.UserCount != 4 ||
overview.Totals.VerifiedUsers != 2 || overview.Totals.TotalListings != 2 {
t.Fatalf("overview totals = %+v", overview.Totals)
}
if overview.Totals.TotalOrders != 2 || overview.Totals.PaidOrders != 1 ||
overview.Totals.PaidGMVCent != 1000 {
t.Fatalf("overview order totals = %+v", overview.Totals)
}
if overview.Totals.WalletInflowCent != 1300 || overview.Totals.WalletOutflowCent != 300 ||
overview.Totals.TodayWalletInflowCent != 1300 || overview.Totals.TodayWalletOutflowCent != 100 ||
overview.Totals.PendingWithdrawals != 2 {
t.Fatalf("overview wallet totals = %+v", overview.Totals)
}
if len(overview.Sites) != 2 || len(overview.Trend) != 7 {
t.Fatalf("sites=%d trend=%d", len(overview.Sites), len(overview.Trend))
}
if overview.Sites[0].UserCount != 2 || overview.Sites[0].BuyerOrders != 1 ||
overview.Sites[0].SourceOrders != 0 || overview.Sites[0].TodayNewListings != 1 ||
overview.Sites[0].WalletInflowCent != 1000 || overview.Sites[0].WalletOutflowCent != 200 ||
overview.Sites[0].PendingWithdrawals != 1 {
t.Fatalf("site A stats = %+v", overview.Sites[0])
}
if overview.Sites[1].UserCount != 2 || overview.Sites[1].BuyerOrders != 0 ||
overview.Sites[1].SourceOrders != 1 || overview.Sites[1].TodayNewListings != 1 ||
overview.Sites[1].WalletInflowCent != 300 || overview.Sites[1].WalletOutflowCent != 100 ||
overview.Sites[1].TodayWalletInflowCent != 300 || overview.Sites[1].TodayWalletOutflowCent != 100 ||
overview.Sites[1].PendingWithdrawals != 1 {
t.Fatalf("site B stats = %+v", overview.Sites[1])
}
todayTrend := overview.Trend[6]
if todayTrend.NewUsers != 4 || todayTrend.NewListings != 2 ||
todayTrend.Orders != 1 || todayTrend.PaidGMVCent != 1000 ||
todayTrend.WalletInflowCent != 1300 || todayTrend.WalletOutflowCent != 100 {
t.Fatalf("today trend = %+v", todayTrend)
}
}