feat: 蝦皮数据页分页与状态筛选 (#43)
导入真实样本后 /shopee 一次吐 3.4MB / 5195 行。但只加分页会把问题从 「5195 行糊在一起」变成「260 页里藏着 6 个」——那 6 个待补规格的商品 仍然找不到。所以分页和状态筛选一起做。 HTML 3.4MB → 16.7KB。 状态条显示全量而不是本页:「共 5195 个商品 · 第 1/260 页」。 显示「共 20 个商品」会让操作员以为总共就 20 个。筛选后显示筛选结果 总数:「待补规格:6 个商品」。 列表查询和 COUNT 共用同一套筛选条件拼装。分开写两份 WHERE,迟早 有天忘了给 COUNT 也加条件,页码算错而且没人发现(#19 踩过一次)。 page 越界兜到最后一页而不是显示空表格——空表格会让操作员以为数据没了。 总数为 0 时显示「第 1/1 页」,不出现「第 1/0 页」。 「待补规格」用 EXISTS 不用 JOIN+DISTINCT:一个商品有多个失败 SKU 时 JOIN 会出重复行,DISTINCT 又让 LIMIT/OFFSET 的行为难推理。 分页控件是 <a href> 纯 GET,浏览器前进后退和书签都正常。首末页用 <span class="disabled"> 禁用,语义上不再是链接,不只靠颜色区分。 这是全项目第一个分页页面,通用逻辑单独放 service/pagination.go 供 后面四页复用,规则写进 05 §3.2 而不是蝦皮页那一节(#34 踩过这个错)。 05 §3 的每页条数从「建议 50」改为「统一 20」并写明理由。 实现踩到 html/template 的 URL 上下文转义:夹在字面量 & 中间的动态内容 会被整体当成一个参数值转义,?/= 变成 %3F/%3D 让链接失效。改为在 Go 里 把整段 URL 拼好,模板作为单个 pipeline 输出,并加了回归测试。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"cmautobuy/admin/repository"
|
||||
@@ -47,7 +48,7 @@ func TestListShopeeProducts_商品级一行(t *testing.T) {
|
||||
seedShopeeSKU(t, db, "sku-2", "24420648774", "", "黑色", "L", "50-60公斤", true)
|
||||
seedShopeeSKU(t, db, "sku-3", "24420648774", "", "白色", "M", "40-50公斤", true)
|
||||
|
||||
result, err := ListShopeeProducts(db, "")
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -82,7 +83,7 @@ func TestListShopeeProducts_颜色尺码只统计parse_ok(t *testing.T) {
|
||||
seedShopeeSKU(t, db, "s4", "26886533818", "紅色,3XL建議80-90公斤】", "", "", "", false)
|
||||
seedShopeeSKU(t, db, "s5", "26886533818", "粉色,3XL【寬鬆版 82.5-92.5kg", "", "", "", false)
|
||||
|
||||
result, err := ListShopeeProducts(db, "")
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -109,7 +110,7 @@ func TestListShopeeProducts_待补大于0整行标黄(t *testing.T) {
|
||||
seedShopeeProduct(t, db, "1001", "商品A")
|
||||
seedShopeeSKU(t, db, "s1", "1001", "坏数据", "", "", "", false)
|
||||
|
||||
result, err := ListShopeeProducts(db, "")
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -123,7 +124,7 @@ func TestListShopeeProducts_PDD链接未填写(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedShopeeProduct(t, db, "1001", "商品A")
|
||||
|
||||
result, err := ListShopeeProducts(db, "")
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -148,7 +149,7 @@ func TestListShopeeProducts_采集状态联查(t *testing.T) {
|
||||
}
|
||||
setShopeePddLink(t, db, "1001", "9001", "https://mobile.yangkeduo.com/goods.html?goods_id=9001")
|
||||
|
||||
result, err := ListShopeeProducts(db, "")
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -168,7 +169,7 @@ func TestListShopeeProducts_关键字匹配商品ID和名称_不匹配颜色尺
|
||||
seedShopeeSKU(t, db, "s1", "1001", "", "黑色", "M", "", true)
|
||||
|
||||
// 按商品 ID 匹配
|
||||
result, err := ListShopeeProducts(db, "1001")
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "1001"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -177,7 +178,7 @@ func TestListShopeeProducts_关键字匹配商品ID和名称_不匹配颜色尺
|
||||
}
|
||||
|
||||
// 按商品名称匹配
|
||||
result, err = ListShopeeProducts(db, "背心")
|
||||
result, err = ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "背心"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -186,7 +187,7 @@ func TestListShopeeProducts_关键字匹配商品ID和名称_不匹配颜色尺
|
||||
}
|
||||
|
||||
// 颜色/尺码不参与匹配:搜"黑色"应该搜不到任何商品。
|
||||
result, err = ListShopeeProducts(db, "黑色")
|
||||
result, err = ListShopeeProducts(db, repository.ShopeeFilter{Keyword: "黑色"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
@@ -250,3 +251,162 @@ func TestGetShopeeProductDetail_商品不存在返回nil(t *testing.T) {
|
||||
t.Fatalf("不存在的商品应该返回 nil,得到 %+v", detail)
|
||||
}
|
||||
}
|
||||
|
||||
// ── 分页(工单 #43) ──────────────────────────────────
|
||||
|
||||
func seedShopeeProducts(t *testing.T, db *sql.DB, n int) {
|
||||
t.Helper()
|
||||
for i := 0; i < n; i++ {
|
||||
seedShopeeProduct(t, db, fmt.Sprintf("g%04d", i), fmt.Sprintf("商品%d", i))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_每页固定20条(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedShopeeProducts(t, db, 25)
|
||||
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if len(result.Rows) != 20 {
|
||||
t.Errorf("第 1 页行数 = %d,想要 20", len(result.Rows))
|
||||
}
|
||||
if result.Total != 25 {
|
||||
t.Errorf("Total = %d,想要 25(全量,不是本页数)", result.Total)
|
||||
}
|
||||
if result.TotalPages != 2 {
|
||||
t.Errorf("TotalPages = %d,想要 2", result.TotalPages)
|
||||
}
|
||||
|
||||
result2, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if len(result2.Rows) != 5 {
|
||||
t.Errorf("第 2 页行数 = %d,想要 5(25 条剩下的)", len(result2.Rows))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_page越界兜到最后一页(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedShopeeProducts(t, db, 25)
|
||||
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 9999)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if result.Page != 2 {
|
||||
t.Errorf("Page = %d,想要兜到最后一页 2", result.Page)
|
||||
}
|
||||
if len(result.Rows) != 5 {
|
||||
t.Errorf("越界后应显示最后一页的数据,行数 = %d,想要 5,不是空表格", len(result.Rows))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_page小于1或非数字当作第1页(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedShopeeProducts(t, db, 3)
|
||||
|
||||
for _, p := range []int{0, -1, -100} {
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, p)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if result.Page != 1 {
|
||||
t.Errorf("page=%d 时 Page = %d,想要 1", p, result.Page)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_总数为0时第1页共1页(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if result.TotalPages != 1 {
|
||||
t.Errorf("总数为 0 时 TotalPages = %d,想要 1(不能出现『第 1/0 页』)", result.TotalPages)
|
||||
}
|
||||
if result.Page != 1 {
|
||||
t.Errorf("Page = %d,想要 1", result.Page)
|
||||
}
|
||||
}
|
||||
|
||||
// ── 状态筛选(工单 #43) ──────────────────────────────
|
||||
|
||||
func TestListShopeeProducts_待补规格筛选_一个商品多个失败SKU只出现一行(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
// 同一商品两条解析失败的 SKU:用 JOIN + DISTINCT 会因为多行匹配 EXISTS
|
||||
// 子查询而出重复,这里验证过滤用 EXISTS 时该商品只出现一次。
|
||||
seedShopeeProduct(t, db, "1001", "待补商品")
|
||||
seedShopeeSKU(t, db, "s1", "1001", "坏数据1", "", "", "", false)
|
||||
seedShopeeSKU(t, db, "s2", "1001", "坏数据2", "", "", "", false)
|
||||
seedShopeeProduct(t, db, "1002", "正常商品")
|
||||
seedShopeeSKU(t, db, "s3", "1002", "", "黑色", "M", "", true)
|
||||
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{Status: "pending_spec"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if result.Total != 1 {
|
||||
t.Fatalf("Total = %d,想要 1(不因为两条失败 SKU 出重复行)", result.Total)
|
||||
}
|
||||
if len(result.Rows) != 1 || result.Rows[0].GoodsID != "1001" {
|
||||
t.Fatalf("筛选结果不对: %+v", result.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_未填链接和已填链接筛选(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedShopeeProduct(t, db, "1001", "未填链接商品")
|
||||
seedShopeeProduct(t, db, "1002", "已填链接商品")
|
||||
setShopeePddLink(t, db, "1002", "9001", "https://mobile.yangkeduo.com/goods.html?goods_id=9001")
|
||||
|
||||
noLink, err := ListShopeeProducts(db, repository.ShopeeFilter{Status: "no_link"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if noLink.Total != 1 || noLink.Rows[0].GoodsID != "1001" {
|
||||
t.Fatalf("未填链接筛选结果不对: %+v", noLink.Rows)
|
||||
}
|
||||
|
||||
hasLink, err := ListShopeeProducts(db, repository.ShopeeFilter{Status: "has_link"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if hasLink.Total != 1 || hasLink.Rows[0].GoodsID != "1002" {
|
||||
t.Fatalf("已填链接筛选结果不对: %+v", hasLink.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListShopeeProducts_筛选参数认不出来当全部(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedShopeeProducts(t, db, 3)
|
||||
|
||||
result, err := ListShopeeProducts(db, repository.ShopeeFilter{Status: "not_a_real_status"}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
// repository 层不做校验,直接透传的话这个取值走不进 switch 的任何分支,
|
||||
// 等价于不筛选——校验(认不出来当全部)在 service.ParseShopeeStatus,
|
||||
// 这里验证 repository 端不会因为陌生取值报错或漏数据。
|
||||
if result.Total != 3 {
|
||||
t.Errorf("Total = %d,想要 3(陌生取值不应影响结果)", result.Total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseShopeeStatus_认不出来当全部(t *testing.T) {
|
||||
cases := []string{"", "not_a_real_status", " ", "PENDING_SPEC"}
|
||||
for _, c := range cases {
|
||||
if got := ParseShopeeStatus(c); got != "" {
|
||||
t.Errorf("ParseShopeeStatus(%q) = %q,想要空串(当全部)", c, got)
|
||||
}
|
||||
}
|
||||
for _, c := range []string{"pending_spec", "no_link", "has_link"} {
|
||||
if got := ParseShopeeStatus(c); got != c {
|
||||
t.Errorf("ParseShopeeStatus(%q) = %q,想要原样返回", c, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user