feat: 增加店铺规格只读对比工具 (#193)
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cmautobuy/admin/repository"
|
||||
)
|
||||
|
||||
const (
|
||||
SpecCompareUnlinked = "unlinked"
|
||||
SpecComparePDDMissing = "pdd_missing_or_deleted"
|
||||
SpecCompareShopeeUnavailable = "shopee_formal_specs_unavailable"
|
||||
SpecComparePDDUnavailable = "pdd_specs_unavailable"
|
||||
SpecCompareExact = "exact"
|
||||
SpecComparePDDMissingSpecs = "pdd_missing_specs"
|
||||
SpecComparePDDExtraSpecs = "pdd_extra_specs"
|
||||
SpecCompareDifferent = "different_specs"
|
||||
SpecCompareAmbiguousShared = "ambiguous_shared_pdd"
|
||||
)
|
||||
|
||||
// ShopSpecComparison 是 dry-run 报告中的单件商品结果,不包含标题、规格原文和价格。
|
||||
type ShopSpecComparison struct {
|
||||
ShopeeGoodsID string `json:"shopee_goods_id"`
|
||||
PDDGoodsID string `json:"pdd_goods_id,omitempty"`
|
||||
Status string `json:"status"`
|
||||
ShopeeSpecCount int `json:"shopee_spec_count"`
|
||||
PDDSpecCount int `json:"pdd_spec_count"`
|
||||
MissingCount int `json:"missing_count"`
|
||||
ExtraCount int `json:"extra_count"`
|
||||
}
|
||||
|
||||
// ShopSpecReport 是指定店铺的只读规格对比报告。
|
||||
type ShopSpecReport struct {
|
||||
Shop string `json:"shop"`
|
||||
GeneratedAt string `json:"generated_at"`
|
||||
Total int `json:"total"`
|
||||
Summary map[string]int `json:"summary"`
|
||||
Products []ShopSpecComparison `json:"products"`
|
||||
}
|
||||
|
||||
type pddComparableData struct {
|
||||
SKUs []struct {
|
||||
Options map[string]string `json:"options"`
|
||||
} `json:"skus"`
|
||||
}
|
||||
|
||||
// CompareShopSpecs 只在内存中比较规格集合,不写数据库。
|
||||
func CompareShopSpecs(shop string, sources []repository.ShopSpecSource, now time.Time) ShopSpecReport {
|
||||
report := ShopSpecReport{Shop: strings.TrimSpace(shop), GeneratedAt: now.UTC().Format(time.RFC3339), Summary: map[string]int{}}
|
||||
setsByShopee := make(map[string]map[string]struct{}, len(sources))
|
||||
indexesByPDD := make(map[string][]int)
|
||||
for _, source := range sources {
|
||||
shopeeSet := formalShopeeSpecSet(source.ShopeeSpecs)
|
||||
setsByShopee[source.ShopeeGoodsID] = shopeeSet
|
||||
item := compareOne(source, shopeeSet)
|
||||
report.Products = append(report.Products, item)
|
||||
if source.PDDGoodsID != "" {
|
||||
indexesByPDD[source.PDDGoodsID] = append(indexesByPDD[source.PDDGoodsID], len(report.Products)-1)
|
||||
}
|
||||
}
|
||||
|
||||
// 多个蝦皮商品共用同一个 PDD 时,只有正式规格集合完全相同才可作为后续回填候选。
|
||||
for _, indexes := range indexesByPDD {
|
||||
if len(indexes) < 2 {
|
||||
continue
|
||||
}
|
||||
base := setsByShopee[report.Products[indexes[0]].ShopeeGoodsID]
|
||||
for _, index := range indexes[1:] {
|
||||
if !sameStringSet(base, setsByShopee[report.Products[index].ShopeeGoodsID]) {
|
||||
for _, ambiguousIndex := range indexes {
|
||||
report.Products[ambiguousIndex].Status = SpecCompareAmbiguousShared
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(report.Products, func(i, j int) bool { return report.Products[i].ShopeeGoodsID < report.Products[j].ShopeeGoodsID })
|
||||
report.Total = len(report.Products)
|
||||
for _, item := range report.Products {
|
||||
report.Summary[item.Status]++
|
||||
}
|
||||
return report
|
||||
}
|
||||
|
||||
func compareOne(source repository.ShopSpecSource, shopeeSet map[string]struct{}) ShopSpecComparison {
|
||||
item := ShopSpecComparison{ShopeeGoodsID: source.ShopeeGoodsID, PDDGoodsID: source.PDDGoodsID, ShopeeSpecCount: len(shopeeSet)}
|
||||
switch {
|
||||
case strings.TrimSpace(source.PDDGoodsID) == "":
|
||||
item.Status = SpecCompareUnlinked
|
||||
return item
|
||||
case !source.PDDExists || source.PDDDeleted:
|
||||
item.Status = SpecComparePDDMissing
|
||||
return item
|
||||
case len(shopeeSet) == 0:
|
||||
item.Status = SpecCompareShopeeUnavailable
|
||||
return item
|
||||
}
|
||||
pddSet, err := comparablePDDSpecSet(source.PDDSKUsJSON)
|
||||
if err != nil || len(pddSet) == 0 {
|
||||
item.Status = SpecComparePDDUnavailable
|
||||
return item
|
||||
}
|
||||
item.PDDSpecCount = len(pddSet)
|
||||
item.MissingCount = differenceCount(shopeeSet, pddSet)
|
||||
item.ExtraCount = differenceCount(pddSet, shopeeSet)
|
||||
switch {
|
||||
case item.MissingCount == 0 && item.ExtraCount == 0:
|
||||
item.Status = SpecCompareExact
|
||||
case item.MissingCount > 0 && item.ExtraCount == 0:
|
||||
item.Status = SpecComparePDDMissingSpecs
|
||||
case item.MissingCount == 0 && item.ExtraCount > 0:
|
||||
item.Status = SpecComparePDDExtraSpecs
|
||||
default:
|
||||
item.Status = SpecCompareDifferent
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func formalShopeeSpecSet(specs []repository.ComparableShopeeSpec) map[string]struct{} {
|
||||
set := make(map[string]struct{})
|
||||
for _, spec := range specs {
|
||||
if !spec.ParseOK {
|
||||
continue
|
||||
}
|
||||
if key, ok := comparableSpecKey(spec.Color, spec.Size); ok {
|
||||
set[key] = struct{}{}
|
||||
}
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func comparablePDDSpecSet(raw string) (map[string]struct{}, error) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return nil, fmt.Errorf("PDD 规格为空")
|
||||
}
|
||||
var data pddComparableData
|
||||
if err := json.Unmarshal([]byte(raw), &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
set := make(map[string]struct{})
|
||||
for _, sku := range data.SKUs {
|
||||
for key := range sku.Options {
|
||||
if key != "color" && key != "size" {
|
||||
return nil, fmt.Errorf("存在无法比较的 PDD 规格维度 %q", key)
|
||||
}
|
||||
}
|
||||
if key, ok := comparableSpecKey(sku.Options["color"], sku.Options["size"]); ok {
|
||||
set[key] = struct{}{}
|
||||
}
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func comparableSpecKey(color, size string) (string, bool) {
|
||||
color, size = strings.TrimSpace(color), strings.TrimSpace(size)
|
||||
if color == "" && size == "" {
|
||||
return "", false
|
||||
}
|
||||
encoded, _ := json.Marshal([]string{color, size})
|
||||
return string(encoded), true
|
||||
}
|
||||
|
||||
func differenceCount(left, right map[string]struct{}) int {
|
||||
count := 0
|
||||
for value := range left {
|
||||
if _, exists := right[value]; !exists {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func sameStringSet(left, right map[string]struct{}) bool {
|
||||
return len(left) == len(right) && differenceCount(left, right) == 0
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cmautobuy/admin/repository"
|
||||
)
|
||||
|
||||
func TestCompareShopSpecsClassifiesDifferences(t *testing.T) {
|
||||
sources := []repository.ShopSpecSource{
|
||||
{ShopeeGoodsID: "S1", PDDGoodsID: "P1", PDDExists: true,
|
||||
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "黑", Size: "M", ParseOK: true}},
|
||||
PDDSKUsJSON: `{"skus":[{"options":{"color":"黑","size":"M"}}]}`},
|
||||
{ShopeeGoodsID: "S2"},
|
||||
{ShopeeGoodsID: "S3", PDDGoodsID: "P3", PDDExists: true,
|
||||
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "白", Size: "L", ParseOK: true}},
|
||||
PDDSKUsJSON: `{"skus":[{"options":{"color":"白","size":"M"}}]}`},
|
||||
}
|
||||
|
||||
report := CompareShopSpecs(" shop ", sources, time.Date(2026, 8, 12, 0, 0, 0, 0, time.UTC))
|
||||
if report.Shop != "shop" || report.Total != 3 {
|
||||
t.Fatalf("报告基本信息错误: %+v", report)
|
||||
}
|
||||
if report.Summary[SpecCompareExact] != 1 || report.Summary[SpecCompareUnlinked] != 1 || report.Summary[SpecCompareDifferent] != 1 {
|
||||
t.Fatalf("分类统计错误: %+v", report.Summary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareShopSpecsRejectsAmbiguousSharedPDD(t *testing.T) {
|
||||
sources := []repository.ShopSpecSource{
|
||||
{ShopeeGoodsID: "S1", PDDGoodsID: "P1", PDDExists: true,
|
||||
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "黑", Size: "M", ParseOK: true}},
|
||||
PDDSKUsJSON: `{"skus":[{"options":{"color":"黑","size":"M"}}]}`},
|
||||
{ShopeeGoodsID: "S2", PDDGoodsID: "P1", PDDExists: true,
|
||||
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "白", Size: "L", ParseOK: true}},
|
||||
PDDSKUsJSON: `{"skus":[{"options":{"color":"黑","size":"M"}}]}`},
|
||||
}
|
||||
report := CompareShopSpecs("shop", sources, time.Now())
|
||||
if report.Summary[SpecCompareAmbiguousShared] != 2 {
|
||||
t.Fatalf("共用 PDD 且规格不同时应标记歧义: %+v", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareShopSpecsDoesNotExposeRawSpecs(t *testing.T) {
|
||||
source := repository.ShopSpecSource{ShopeeGoodsID: "S1", PDDGoodsID: "P1", PDDExists: true,
|
||||
ShopeeSpecs: []repository.ComparableShopeeSpec{{Color: "秘密颜色", Size: "秘密尺码", ParseOK: true}},
|
||||
PDDSKUsJSON: `{"skus":[{"options":{"color":"别的颜色","size":"别的尺码"},"price_cent":999}]}`}
|
||||
report := CompareShopSpecs("shop", []repository.ShopSpecSource{source}, time.Now())
|
||||
item := report.Products[0]
|
||||
if item.ShopeeSpecCount != 1 || item.PDDSpecCount != 1 || item.MissingCount != 1 || item.ExtraCount != 1 {
|
||||
t.Fatalf("报告只应输出计数: %+v", item)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user