fix(wallet): 保留历史分销用户零租户归属

This commit is contained in:
yml
2026-11-26 22:39:39 +08:00
parent a6e7631a7d
commit fab8301569
2 changed files with 30 additions and 3 deletions
@@ -277,9 +277,10 @@ func resolveTenantID(db *gorm.DB, explicitTenantID uint64, userID uint64) uint64
return explicitTenantID return explicitTenantID
} }
if userID > 0 { if userID > 0 {
var tenantID uint64 var user model.User
if err := db.Model(&model.User{}).Select("tenant_id").Where("id = ?", userID).Scan(&tenantID).Error; err == nil && tenantID > 0 { if err := db.Select("tenant_id").Where("id = ?", userID).Take(&user).Error; err == nil {
return tenantID // 历史分销用户明确属于 tenant_id=0,不能再用当前后台租户覆盖。
return user.TenantID
} }
} }
if tenantID := tenantIDFromDB(db); tenantID > 0 { if tenantID := tenantIDFromDB(db); tenantID > 0 {
@@ -538,6 +538,15 @@ func TestWithdrawalAdminAllTenantScopeIncludesLegacyDistribution(t *testing.T) {
if err := db.Create(&users).Error; err != nil { if err := db.Create(&users).Error; err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := db.Create(&model.WalletAccount{
TenantID: 0,
UserID: users[0].ID,
DistributionSiteID: site.ID,
FrozenBalanceCent: 1000,
Status: "active",
}).Error; err != nil {
t.Fatal(err)
}
withdrawals := []model.WithdrawalRequest{ withdrawals := []model.WithdrawalRequest{
{WithdrawNo: "WD-LEGACY-SITE", TenantID: 0, UserID: users[0].ID, DistributionSiteID: site.ID, AmountCent: 1000, ActualAmountCent: 1000, AccountType: "bank", AccountName: "张三", AccountNo: "****0001", Status: "pending"}, {WithdrawNo: "WD-LEGACY-SITE", TenantID: 0, UserID: users[0].ID, DistributionSiteID: site.ID, AmountCent: 1000, ActualAmountCent: 1000, AccountType: "bank", AccountName: "张三", AccountNo: "****0001", Status: "pending"},
{WithdrawNo: "WD-DACHUI", TenantID: 101, UserID: users[1].ID, AmountCent: 2000, ActualAmountCent: 2000, AccountType: "bank", AccountName: "张三", AccountNo: "****0002", Status: "pending"}, {WithdrawNo: "WD-DACHUI", TenantID: 101, UserID: users[1].ID, AmountCent: 2000, ActualAmountCent: 2000, AccountType: "bank", AccountName: "张三", AccountNo: "****0002", Status: "pending"},
@@ -599,4 +608,21 @@ func TestWithdrawalAdminAllTenantScopeIncludesLegacyDistribution(t *testing.T) {
if reviewed.Status != "processing" { if reviewed.Status != "processing" {
t.Fatalf("legacy withdrawal review status = %q, want processing", reviewed.Status) t.Fatalf("legacy withdrawal review status = %q, want processing", reviewed.Status)
} }
paid, err := repo.ConfirmPayment(dachuiFinanceCtx, 5, withdrawals[0].ID, ConfirmPaymentRequest{
Remark: "已通过微信转账",
})
if err != nil {
t.Fatalf("dachui legacy confirm payment: %v", err)
}
if paid.Status != "completed" {
t.Fatalf("legacy withdrawal paid status = %q, want completed", paid.Status)
}
var walletAccount model.WalletAccount
if err := db.Where("user_id = ? AND distribution_site_id = ?", users[0].ID, site.ID).
First(&walletAccount).Error; err != nil {
t.Fatal(err)
}
if walletAccount.FrozenBalanceCent != 0 {
t.Fatalf("legacy wallet frozen balance = %d, want 0", walletAccount.FrozenBalanceCent)
}
} }