feat(listings): 折算比按整数展示并统一筛选口径
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package listing
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -63,7 +64,9 @@ func matchesPublicQuery(item ListingDTO, query PublicListQuery) bool {
|
||||
if !numberInRange(coinM, NumberRange{Min: query.MinCoin, Max: query.MaxCoin}) {
|
||||
return false
|
||||
}
|
||||
if !numberInRange(ratioFromListing(item), NumberRange{Min: query.MinRatio, Max: query.MaxRatio}) {
|
||||
// 折算比在前端按四舍五入取整展示,筛选必须用同一口径,
|
||||
// 否则会出现「显示 35w 却不在 35-40 区间」的错位。
|
||||
if !numberInRange(math.Round(ratioFromListing(item)), NumberRange{Min: query.MinRatio, Max: query.MaxRatio}) {
|
||||
return false
|
||||
}
|
||||
if !numberInRange(price, NumberRange{Min: query.MinPrice, Max: query.MaxPrice}) {
|
||||
|
||||
@@ -21,6 +21,31 @@ func TestFilterPublicListingsByRatio(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterPublicListingsByRoundedRatio(t *testing.T) {
|
||||
minRatio := 30.0
|
||||
maxRatio := 35.0
|
||||
items := []ListingDTO{
|
||||
{ID: 1, AssetSummary: map[string]any{"publish_ratio": 29.4}}, // 29w,区间外
|
||||
{ID: 2, AssetSummary: map[string]any{"publish_ratio": 34.4}}, // 34w,区间内
|
||||
{ID: 3, AssetSummary: map[string]any{"publish_ratio": 34.6}}, // 35w,区间内
|
||||
{ID: 4, AssetSummary: map[string]any{"publish_ratio": 35.4}}, // 35w,区间内(与展示一致)
|
||||
{ID: 5, AssetSummary: map[string]any{"publish_ratio": 35.6}}, // 36w,区间外
|
||||
// 无 publish_ratio 时按「万币 / 元」兜底计算:34.6w → 35w
|
||||
{ID: 6, HafCoinAmount: 34_600_000, PriceCent: 10_000},
|
||||
}
|
||||
|
||||
filtered := filterPublicListings(items, PublicListQuery{MinRatio: &minRatio, MaxRatio: &maxRatio})
|
||||
|
||||
if len(filtered) != 4 {
|
||||
t.Fatalf("expected 4 listings after rounding, got %#v", filtered)
|
||||
}
|
||||
for i, wantID := range []uint64{2, 3, 4, 6} {
|
||||
if filtered[i].ID != wantID {
|
||||
t.Fatalf("at index %d, expected listing %d, got %d", i, wantID, filtered[i].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterPublicListingsRegionMultiSelectRequiresAll(t *testing.T) {
|
||||
items := []ListingDTO{
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user