fix(order): 修复重复手机号筛选报错
This commit is contained in:
@@ -337,10 +337,11 @@ func applyAdminOrderFilters(db *gorm.DB, query AdminOrderQuery) *gorm.DB {
|
|||||||
if keyword := strings.TrimSpace(query.Keyword); keyword != "" {
|
if keyword := strings.TrimSpace(query.Keyword); keyword != "" {
|
||||||
upper := strings.ToUpper(keyword)
|
upper := strings.ToUpper(keyword)
|
||||||
if isPhoneKeyword(keyword) {
|
if isPhoneKeyword(keyword) {
|
||||||
// 先由 users(phone) 唯一索引解析用户,再让订单表 owner_id/renter_id 索引驱动。
|
// 同一手机号可在不同分销站点各有一个账号,必须按用户集合匹配;
|
||||||
|
// 标量子查询在命中多个用户时会直接报 SQL 错误。
|
||||||
// 不能直接写 owner.phone/renter.phone:跨表别名 OR 会让 MySQL 倒序扫描全部订单。
|
// 不能直接写 owner.phone/renter.phone:跨表别名 OR 会让 MySQL 倒序扫描全部订单。
|
||||||
db = db.Where(
|
db = db.Where(
|
||||||
"(o.owner_id = (SELECT id FROM users WHERE phone = ?) OR o.renter_id = (SELECT id FROM users WHERE phone = ?))",
|
"(o.owner_id IN (SELECT id FROM users WHERE phone = ?) OR o.renter_id IN (SELECT id FROM users WHERE phone = ?))",
|
||||||
keyword,
|
keyword,
|
||||||
keyword,
|
keyword,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -2375,6 +2376,75 @@ func TestListAdminFiltersByCreatedAtRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListAdminFiltersByPhoneWhenSamePhoneHasMultipleUsers(t *testing.T) {
|
||||||
|
db := setupOrderTestDB(t)
|
||||||
|
repo := NewRepository(db)
|
||||||
|
const phone = "16673815551"
|
||||||
|
|
||||||
|
ownerMain := model.User{Phone: phone, DistributionSiteID: 0}
|
||||||
|
ownerDistribution := model.User{Phone: phone, DistributionSiteID: 1}
|
||||||
|
renter := model.User{Phone: "13900004003"}
|
||||||
|
if err := db.Create(&ownerMain).Error; err != nil {
|
||||||
|
t.Fatalf("create main-site owner failed: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&ownerDistribution).Error; err != nil {
|
||||||
|
t.Fatalf("create distribution-site owner failed: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&renter).Error; err != nil {
|
||||||
|
t.Fatalf("create renter failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for index, owner := range []model.User{ownerMain, ownerDistribution} {
|
||||||
|
suffix := strconv.Itoa(index + 1)
|
||||||
|
account := model.GameAccount{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
Status: accountStatusRented,
|
||||||
|
ServerRegion: "国服",
|
||||||
|
LoginPlatform: "steam",
|
||||||
|
Title: "手机号跨站点订单测试账号 " + suffix,
|
||||||
|
}
|
||||||
|
if err := db.Create(&account).Error; err != nil {
|
||||||
|
t.Fatalf("create account %s failed: %v", suffix, err)
|
||||||
|
}
|
||||||
|
listing := model.RentalListing{
|
||||||
|
ListingNo: "L-PHONE-SCOPE-" + suffix,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
AccountID: account.ID,
|
||||||
|
Status: listingStatusRented,
|
||||||
|
ReviewStatus: listingReviewStatusApproved,
|
||||||
|
InTransaction: true,
|
||||||
|
PriceCent: 10000,
|
||||||
|
}
|
||||||
|
if err := db.Create(&listing).Error; err != nil {
|
||||||
|
t.Fatalf("create listing %s failed: %v", suffix, err)
|
||||||
|
}
|
||||||
|
order := model.RentalOrder{
|
||||||
|
OrderNo: "RO-PHONE-SCOPE-" + suffix,
|
||||||
|
ListingID: listing.ID,
|
||||||
|
AccountID: account.ID,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
RenterID: renter.ID,
|
||||||
|
RentAmountCent: 10000,
|
||||||
|
Status: orderStatusRenting,
|
||||||
|
}
|
||||||
|
if err := db.Create(&order).Error; err != nil {
|
||||||
|
t.Fatalf("create order %s failed: %v", suffix, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := repo.ListAdmin(t.Context(), AdminOrderQuery{
|
||||||
|
Page: 1,
|
||||||
|
PageSize: 1,
|
||||||
|
Keyword: phone,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListAdmin() by duplicate phone error = %v", err)
|
||||||
|
}
|
||||||
|
if result.Total != 2 || len(result.Items) != 1 {
|
||||||
|
t.Fatalf("ListAdmin() by duplicate phone total/items = %d/%d, want 2/1", result.Total, len(result.Items))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAdminPlatformCheckoutConfirmRequiresExplicitConfirmationForZeroOwnerIncome(t *testing.T) {
|
func TestAdminPlatformCheckoutConfirmRequiresExplicitConfirmationForZeroOwnerIncome(t *testing.T) {
|
||||||
db := setupOrderTestDB(t)
|
db := setupOrderTestDB(t)
|
||||||
repo := NewRepository(db, Dependencies{
|
repo := NewRepository(db, Dependencies{
|
||||||
|
|||||||
@@ -515,7 +515,7 @@ func TestApplyAdminOrderFiltersSmartRouting(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "11位手机号先解析用户再走订单角色索引",
|
name: "11位手机号先解析用户再走订单角色索引",
|
||||||
query: AdminOrderQuery{Keyword: "13800138000"},
|
query: AdminOrderQuery{Keyword: "13800138000"},
|
||||||
wantSQLFrag: "o.owner_id = (SELECT id FROM users WHERE phone = ?)",
|
wantSQLFrag: "o.owner_id IN (SELECT id FROM users WHERE phone = ?)",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "RO前缀订单号路由至rental_orders(order_no)",
|
name: "RO前缀订单号路由至rental_orders(order_no)",
|
||||||
|
|||||||
Reference in New Issue
Block a user