fix(search): 修复跨租户重复手机号查询报错
This commit is contained in:
@@ -234,7 +234,7 @@ func (r *Repository) disbursementListBaseQuery(ctx context.Context, phoneKeyword
|
||||
}
|
||||
|
||||
if phoneKeyword = strings.TrimSpace(phoneKeyword); isPhoneKeyword(phoneKeyword) {
|
||||
withdrawal = withdrawal.Where("wr.user_id = (SELECT id FROM users WHERE phone = ?)", phoneKeyword)
|
||||
withdrawal = withdrawal.Where("wr.user_id IN (SELECT id FROM users WHERE phone = ?)", phoneKeyword)
|
||||
return db.Table("(?) AS d", withdrawal)
|
||||
}
|
||||
|
||||
@@ -298,8 +298,8 @@ func (r *Repository) applyDisbursementFilters(db *gorm.DB, query DisbursementQue
|
||||
if query.Keyword != "" {
|
||||
keyword := strings.TrimSpace(query.Keyword)
|
||||
if isPhoneKeyword(keyword) {
|
||||
// 完整手机号只可能命中提现收款人,先经 users(phone) 唯一索引解析用户。
|
||||
db = db.Where("d.source_type = ? AND d.user_id = (SELECT id FROM users WHERE phone = ?)", "withdrawal", keyword)
|
||||
// 完整手机号只匹配提现收款人,先查询该手机号对应的所有租户和站点账号。
|
||||
db = db.Where("d.source_type = ? AND d.user_id IN (SELECT id FROM users WHERE phone = ?)", "withdrawal", keyword)
|
||||
} else {
|
||||
like := "%" + keyword + "%"
|
||||
db = db.Where(`(d.business_no LIKE ? OR d.payee_name LIKE ? OR d.payee_phone LIKE ?
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package adminfinance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestDisbursementPhoneFilterWithDuplicateAccounts(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
const phone = "13812345678"
|
||||
users := []model.User{
|
||||
{TenantID: 101, Phone: phone},
|
||||
{TenantID: 202, Phone: phone},
|
||||
{TenantID: 101, DistributionSiteID: 7, Phone: phone},
|
||||
{TenantID: 101, Phone: "13912345678"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
withdrawals := make([]model.WithdrawalRequest, len(users))
|
||||
for i, user := range users {
|
||||
withdrawals[i] = model.WithdrawalRequest{
|
||||
WithdrawNo: fmt.Sprintf("W-PHONE-%d", i), UserID: user.ID, TenantID: user.TenantID,
|
||||
DistributionSiteID: user.DistributionSiteID, AccountType: "alipay",
|
||||
AccountName: "测试", AccountNo: "test", Status: "pending",
|
||||
AmountCent: 100, ActualAmountCent: 100,
|
||||
}
|
||||
}
|
||||
if err := db.Create(&withdrawals).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
tenantID uint64
|
||||
keyword string
|
||||
want map[uint64]bool
|
||||
}{
|
||||
{"tenant A excludes site wallets", 101, phone, map[uint64]bool{withdrawals[0].ID: true}},
|
||||
{"tenant B", 202, phone, map[uint64]bool{withdrawals[1].ID: true}},
|
||||
{"platform matches every account", 0, phone, map[uint64]bool{withdrawals[0].ID: true, withdrawals[1].ID: true, withdrawals[2].ID: true}},
|
||||
{"unknown phone", 101, "13712345678", map[uint64]bool{}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := repo.Disbursements(financeTenantContext(tc.tenantID), DisbursementQuery{
|
||||
Keyword: tc.keyword, Page: 1, PageSize: 20,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Total != int64(len(tc.want)) || len(got.Items) != len(tc.want) ||
|
||||
got.Summary.ReviewPendingAmountCent != int64(len(tc.want))*100 {
|
||||
t.Fatalf("unexpected phone-filtered result: %+v", got)
|
||||
}
|
||||
for _, item := range got.Items {
|
||||
if item.SourceType != "withdrawal" || !tc.want[item.WithdrawalID] {
|
||||
t.Fatalf("unexpected withdrawal: %+v", item)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user