// 蝦皮数据列表页的查询和界面文字组装。 // // 分成单独文件是因为它和 shopee_import.go 职责不同:这里只读,不写库。 package service import ( "database/sql" "cmautobuy/admin/model" "cmautobuy/admin/repository" ) // ShopeeSKUView 是列表页一行要显示的全部内容,全部已经是字符串, // 模板里不做判断和格式化。 type ShopeeSKUView struct { GoodsID string Title string SKUID string Color string Size string Advice string // ParseFailed 为 true 时整行标黄,提示需要人工补颜色/尺码/建议, // 见 docs/admin/05-ui-specification.md §4.2。 ParseFailed bool // PddURL 为空且 PddMissing 为 true 时,模板显示"未填写"并标红—— // 这是最需要操作员注意的状态,见 §4.2。 PddURL string PddMissing bool StatusText string IsManual bool UpdatedAt string } // ShopeeListResult 是列表页要的全部数据。 type ShopeeListResult struct { Rows []ShopeeSKUView Total int // shopee_products 的总商品数(不受筛选影响),用于判断"是否已导入过" IsFiltered bool } // ListShopeeSKUs 查蝦皮 SKU 列表并把每一行翻成界面文字。 // // 采集状态来自 pdd_products(联查得到),不是蝦皮自己的字段, // 见 docs/admin/01-requirements.md §6.1: // // shopee_products.pdd_goods_id 为空 -> "未填链接" // pdd_products.collect_status -> 未采集 / 采集中 / 已采集 / 采集失败 func ListShopeeSKUs(db *sql.DB, keyword string) (*ShopeeListResult, error) { rows, err := repository.ListShopeeSKUs(db, keyword) if err != nil { return nil, err } total, err := repository.CountShopeeProducts(db) if err != nil { return nil, err } result := &ShopeeListResult{ Rows: make([]ShopeeSKUView, 0, len(rows)), Total: total, IsFiltered: keyword != "", } for _, r := range rows { v := ShopeeSKUView{ GoodsID: r.GoodsID, Title: r.ProductTitle, SKUID: r.SKUID, IsManual: r.IsManual, UpdatedAt: formatLocalTime(r.UpdatedAt), } if r.ParseOK { v.Color, v.Size, v.Advice = r.Color, r.Size, r.Advice if v.Advice == "" { v.Advice = placeholder } } else { v.Color, v.Size, v.Advice = placeholder, placeholder, placeholder v.ParseFailed = true } if r.PddGoodsID == "" { v.PddMissing = true v.PddURL = "未填写" v.StatusText = "未填链接" } else { v.PddURL = r.PddGoodsURL if r.CollectStatus.Valid { v.StatusText = collectStatusText(model.CollectStatus(r.CollectStatus.String)) } else { // pdd_goods_id 有值但 pdd_products 里查不到对应行 // (比如那条 PDD 商品被软删除了),如实说明,不要装作"未采集"。 v.StatusText = "PDD 商品缺失" } } result.Rows = append(result.Rows, v) } return result, nil }