feat: 增加蝦皮店铺图片筛选 (#150)
This commit is contained in:
@@ -100,7 +100,7 @@ func ListShopeeProducts(db *sql.DB, filter repository.ShopeeFilter, page int) (*
|
||||
Rows: make([]ShopeeProductView, 0, len(rows)),
|
||||
Total: total,
|
||||
HasAnyProducts: hasAny > 0,
|
||||
IsFiltered: strings.TrimSpace(filter.Keyword) != "" || filter.Status != "",
|
||||
IsFiltered: strings.TrimSpace(filter.Keyword) != "" || filter.Status != "" || filter.Shop != "" || filter.Image != "",
|
||||
Page: page,
|
||||
PageSize: PageSize,
|
||||
TotalPages: totalPages,
|
||||
@@ -188,6 +188,25 @@ func ShopeeStatusLabel(status string) string {
|
||||
return shopeeStatusTexts[status]
|
||||
}
|
||||
|
||||
// ShopeePresenceOptions 返回资料字段的三态选项。使用下拉框而不是复选框,
|
||||
// 让“全部”和“无资料”都有明确表达,不把未勾选误解成任意状态。
|
||||
func ShopeePresenceOptions(hasText, missingText string) []ShopeeStatusOption {
|
||||
return []ShopeeStatusOption{
|
||||
{"", "全部"},
|
||||
{"has", hasText},
|
||||
{"missing", missingText},
|
||||
}
|
||||
}
|
||||
|
||||
// ParseShopeePresence 校验店铺/图片筛选参数;未知值按“全部”处理。
|
||||
func ParseShopeePresence(s string) string {
|
||||
value := strings.TrimSpace(s)
|
||||
if value == "has" || value == "missing" {
|
||||
return value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ---------- 弹窗 ----------
|
||||
|
||||
// ShopeeSpecView 是弹窗规格表的一行。
|
||||
|
||||
@@ -474,3 +474,70 @@ func TestParseShopeeStatus_认不出来当全部(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_店铺图片三态及组合筛选(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
for _, product := range []struct {
|
||||
id, title, shop, image string
|
||||
}{
|
||||
{"A", "店铺有图", "店铺 A", "https://example.invalid/a.jpg"},
|
||||
{"B", "店铺无图", "店铺 B", ""},
|
||||
{"C", "无店有图", "", "https://example.invalid/c.jpg"},
|
||||
{"D", "资料为空白", " ", " "},
|
||||
} {
|
||||
seedShopeeProduct(t, db, product.id, product.title)
|
||||
if _, err := db.Exec(`UPDATE shopee_products SET shopee_shop_name=?,image_url=? WHERE goods_id=?`, product.shop, product.image, product.id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
filter repository.ShopeeFilter
|
||||
want []string
|
||||
}{
|
||||
{"有店铺", repository.ShopeeFilter{Shop: "has"}, []string{"A", "B"}},
|
||||
{"无店铺", repository.ShopeeFilter{Shop: "missing"}, []string{"C", "D"}},
|
||||
{"有图片", repository.ShopeeFilter{Image: "has"}, []string{"A", "C"}},
|
||||
{"无图片", repository.ShopeeFilter{Image: "missing"}, []string{"B", "D"}},
|
||||
{"有店铺且有图片", repository.ShopeeFilter{Shop: "has", Image: "has"}, []string{"A"}},
|
||||
{"无店铺且无图片", repository.ShopeeFilter{Shop: "missing", Image: "missing"}, []string{"D"}},
|
||||
{"组合关键词", repository.ShopeeFilter{Keyword: "店铺有图", Shop: "has", Image: "has"}, []string{"A"}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := ListShopeeProducts(db, tc.filter, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Total != len(tc.want) || len(result.Rows) != len(tc.want) {
|
||||
t.Fatalf("Total=%d rows=%d,期望 %v", result.Total, len(result.Rows), tc.want)
|
||||
}
|
||||
for index, want := range tc.want {
|
||||
if result.Rows[index].GoodsID != want {
|
||||
t.Fatalf("第 %d 行=%s,期望 %s", index, result.Rows[index].GoodsID, want)
|
||||
}
|
||||
}
|
||||
if !result.IsFiltered {
|
||||
t.Fatal("使用店铺/图片条件时 IsFiltered 应为 true")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseShopeePresence_三态参数(t *testing.T) {
|
||||
for _, value := range []string{"has", "missing"} {
|
||||
if got := ParseShopeePresence(value); got != value {
|
||||
t.Errorf("ParseShopeePresence(%q)=%q", value, got)
|
||||
}
|
||||
}
|
||||
for _, value := range []string{"", "all", "HAS", "unknown", " "} {
|
||||
if got := ParseShopeePresence(value); got != "" {
|
||||
t.Errorf("ParseShopeePresence(%q)=%q,期望全部", value, got)
|
||||
}
|
||||
}
|
||||
options := ShopeePresenceOptions("有店铺", "无店铺")
|
||||
if len(options) != 3 || options[0].Value != "" || options[1].Value != "has" || options[2].Value != "missing" {
|
||||
t.Fatalf("三态选项不正确:%+v", options)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user