feat: PDD 商品数据独立成表 (#16)
原来 pdd_data 是 shopee_products 上的一个 JSON 字段,两个蝦皮商品指向 同一个 PDD 链接时会各存一份、各采一次;collect_status 描述的是 PDD 商品的 状态,却挂在蝦皮商品上,两份可能不一致。 更要紧的是 PDD 商品变动频繁(A 下架就得换 B),而 sku_mappings 只按 shopee_sku_id 做键——换商品后旧映射还在,B 恰好有同名规格但完全是另一件货 时会静默买错,事后查不出来。 改动 - 新增 pdd_products 表:id 主键 + goods_id UNIQUE + 4 个状态值(去掉 no_link,「未填链接」改由 shopee_products.pdd_goods_id 为空表达)+ 软删除可复活 - shopee_products 去掉 pdd_data / collect_status / collect_error / collected_at,pdd_goods_id 改为引用 - sku_mappings 主键改为 (shopee_sku_id, pdd_goods_id),新增 pdd_option_key。 查映射永远带上当前 PDD 商品,换商品后天然查不到旧映射,不需要删数据; 换回原商品时旧映射直接复用 - 新增 OptionKey():用 json.Marshal 实现(Go 序列化 map 按键名排序, 天然规范化),不自己拼字符串——规格文字里可能含 = 或 ;。 存映射和查 SKU 必须用同一个函数,各写一遍会静默算出不同结果 - 采集结果改落 pdd_products,新增两条校验: 返回的 goods_id 与请求不符 → 整体回滚拒绝(422),不静默存下; skus 为空数组 → 置 failed 而非 collected,否则界面显示"已采集" 但数据毫无用处 实施时超出工单但必要的三处 - TaskExists 重构为 GetTaskInfo:原函数只返回蝦皮 goods_id, 而采集结果要按 PDD goods_id 落库,不改取不到正确的键 - 复活时一并清空旧采集结果(skus_json / collect_msg / collected_at), 否则复活后会显示"已采集"但数据是删除前的 - 删除 repository/shopee.go:两个函数签名全变且已迁到 pdd.go,留着是死代码 已验证(Go 1.23.0) - go vet / gofmt / go test 全过,55 个测试 - 端到端补验了工单未覆盖的 HTTP 层:goods_id 不符返回 422 COLLECT_GOODS_MISMATCH 且整体回滚(skus_json 空、任务仍 claimed、 幂等记录 0 条);skus 为空返回 200 但状态 failed 遗留 - MarkCollecting / SoftDeletePddProduct 暂无调用方,等界面工单接上 - artifact_ref 存 diagnostics 原始 JSON,未按 client-001:artifacts/... 规范化, 因 Client 侧尚未定义 diagnostics 结构 - 界面未实现(工单明确排除),四个页面仍为骨架 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+78
-12
@@ -20,8 +20,47 @@ var (
|
||||
ErrNeverClaimed = errors.New("该任务从未分配给这个客户端")
|
||||
// ErrIdempotencyConflict 同一个键提交了不同内容 -> 409
|
||||
ErrIdempotencyConflict = repository.ErrIdempotencyConflict
|
||||
// ErrCollectMismatch 客户端采回来的商品不是请求的那个 -> 422
|
||||
ErrCollectMismatch = errors.New("采集结果与请求的商品不一致")
|
||||
)
|
||||
|
||||
// collectedData 是采集结果里 Admin 关心的那几项。
|
||||
//
|
||||
// 客户端提交的完整内容会原样存进 tasks.result_data,这里只挑业务要用的解析出来。
|
||||
// 用不到的字段不写进结构体,多余的 JSON 字段会被忽略,不影响向前兼容。
|
||||
type collectedData struct {
|
||||
GoodsID string `json:"goods_id"`
|
||||
Title string `json:"title"`
|
||||
SKUs []struct {
|
||||
Options map[string]string `json:"options"`
|
||||
PriceCent *int64 `json:"price_cent"` // 指针:采不到价格时是 null,不是 0
|
||||
Available bool `json:"available"`
|
||||
RawPrice string `json:"raw_price"`
|
||||
} `json:"skus"`
|
||||
}
|
||||
|
||||
// parseCollected 解析采集结果。解析不了直接报错,不要当成"采到 0 个规格"——
|
||||
// 那是两回事:一个是客户端发的东西有问题,一个是商品确实没规格。
|
||||
func parseCollected(raw string) (*collectedData, error) {
|
||||
var c collectedData
|
||||
if err := json.Unmarshal([]byte(raw), &c); err != nil {
|
||||
return nil, fmt.Errorf("采集结果不是合法结构: %w", err)
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// artifactRef 从失败上报里取诊断产物位置。
|
||||
//
|
||||
// 按已定案的 Artifact 策略(docs/client/04 §10 待确认 #3),
|
||||
// 客户端**只报本地引用、不上传文件**。所以这里存的是一个位置字符串,
|
||||
// 告诉操作员去哪台客户端的哪个目录捞截图,Admin 自己显示不了图。
|
||||
func artifactRef(req FailureRequest) string {
|
||||
if len(req.Diagnostics) == 0 {
|
||||
return ""
|
||||
}
|
||||
return string(req.Diagnostics)
|
||||
}
|
||||
|
||||
// ResultRequest 是客户端提交成功结果的请求体。
|
||||
type ResultRequest struct {
|
||||
TaskVersion int `json:"task_version"`
|
||||
@@ -70,7 +109,7 @@ func SubmitResult(db *sql.DB, taskID, clientID, idemKey string, rawBody []byte)
|
||||
}
|
||||
|
||||
return submitInTx(db, taskID, clientID, idemKey, rawBody,
|
||||
func(tx *sql.Tx, taskType model.TaskType, goodsID string) (map[string]any, error) {
|
||||
func(tx *sql.Tx, info *repository.TaskInfo) (map[string]any, error) {
|
||||
pddData := string(req.PddData)
|
||||
if strings.TrimSpace(pddData) == "" {
|
||||
pddData = "{}"
|
||||
@@ -79,9 +118,35 @@ func SubmitResult(db *sql.DB, taskID, clientID, idemKey string, rawBody []byte)
|
||||
if err := repository.MarkTaskSucceeded(tx, taskID, pddData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 采集任务的结果还要落到商品级,供后续规格匹配使用
|
||||
if taskType == model.TaskCollect {
|
||||
if err := repository.SetCollectResult(tx, goodsID, pddData); err != nil {
|
||||
|
||||
// 采集任务的结果还要落到 PDD 商品上,供后续规格匹配使用。
|
||||
// 注意用的是 PddGoodsID —— 被采集的是 PDD 商品,不是蝦皮商品。
|
||||
if info.TaskType == model.TaskCollect {
|
||||
collected, err := parseCollected(pddData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 校验一:客户端采回来的商品必须就是我们要的那个。
|
||||
// 链接跳转、采错商品时如果不拦,会把 B 的规格价格
|
||||
// 存到 A 名下,后面按它下单就是买错东西。
|
||||
if info.PddGoodsID != "" && collected.GoodsID != "" &&
|
||||
collected.GoodsID != info.PddGoodsID {
|
||||
return nil, fmt.Errorf(
|
||||
"%w: 请求采集的是商品 %s,客户端返回的却是 %s",
|
||||
ErrCollectMismatch, info.PddGoodsID, collected.GoodsID)
|
||||
}
|
||||
|
||||
// 校验二:一个规格都没采到,对业务毫无用处
|
||||
// (商品下架、页面改版、解析器没认出来)。
|
||||
// 这种情况必须算失败,不能显示"已采集"让操作员空欢喜。
|
||||
if len(collected.SKUs) == 0 {
|
||||
if err := repository.SetCollectFailed(tx, info.PddGoodsID,
|
||||
"未采集到任何规格,请检查商品是否已下架", ""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err := repository.SetCollectResult(
|
||||
tx, info.PddGoodsID, collected.Title, pddData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -109,18 +174,19 @@ func SubmitFailure(db *sql.DB, taskID, clientID, idemKey string, rawBody []byte)
|
||||
}
|
||||
|
||||
return submitInTx(db, taskID, clientID, idemKey, rawBody,
|
||||
func(tx *sql.Tx, taskType model.TaskType, goodsID string) (map[string]any, error) {
|
||||
func(tx *sql.Tx, info *repository.TaskInfo) (map[string]any, error) {
|
||||
if err := repository.MarkTaskFailure(
|
||||
tx, taskID, newStatus, req.Error.Code, req.Error.Message); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 采集失败要让操作员在蝦皮数据页看得见原因
|
||||
if taskType == model.TaskCollect {
|
||||
// 采集失败要让操作员看得见原因和诊断产物位置
|
||||
if info.TaskType == model.TaskCollect {
|
||||
msg := req.Error.Message
|
||||
if req.Error.Code != "" {
|
||||
msg = req.Error.Code + ": " + msg
|
||||
}
|
||||
if err := repository.SetCollectFailed(tx, goodsID, msg); err != nil {
|
||||
if err := repository.SetCollectFailed(
|
||||
tx, info.PddGoodsID, msg, artifactRef(req)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -157,7 +223,7 @@ func mapFailureStatus(reported string) (model.TaskStatus, bool) {
|
||||
// 业务写入部分由 apply 提供,它拿到的 tx 和外层是同一个。
|
||||
func submitInTx(
|
||||
db *sql.DB, taskID, clientID, idemKey string, rawBody []byte,
|
||||
apply func(tx *sql.Tx, taskType model.TaskType, goodsID string) (map[string]any, error),
|
||||
apply func(tx *sql.Tx, info *repository.TaskInfo) (map[string]any, error),
|
||||
) (string, error) {
|
||||
|
||||
hash := repository.HashRequest(rawBody)
|
||||
@@ -176,11 +242,11 @@ func submitInTx(
|
||||
}
|
||||
|
||||
// 2. 任务得存在
|
||||
exists, taskType, goodsID, err := repository.TaskExists(tx, taskID)
|
||||
info, err := repository.GetTaskInfo(tx, taskID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !exists {
|
||||
if info == nil {
|
||||
return "", ErrTaskNotFound
|
||||
}
|
||||
|
||||
@@ -195,7 +261,7 @@ func submitInTx(
|
||||
}
|
||||
|
||||
// 4. 业务写入
|
||||
resp, err := apply(tx, taskType, goodsID)
|
||||
resp, err := apply(tx, info)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user