feat: 可扩展档案模式框架 + 内置 3C(电子) 模式
- 新增 category.archive_kind 与 kind_field 字段模板表(迁移 0010) - 种子 electronics 字段模板 + 3C 品类树(手机/笔记本/平板等) - 后端按档案模式动态计算完整度/合格:食品沿用 food_detail, 其它模式走 product.attributes + kind_field - 新增 GET /api/kind-fields?kind= 接口 - 后台编辑页按品类模式动态渲染规格参数表单 - 公开详情页/接口输出带标签的规格表 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -27,6 +27,9 @@ type ProductInput struct {
|
||||
NutritionBasis *string `json:"nutrition_basis"`
|
||||
ServingSize *string `json:"serving_size"`
|
||||
NutriScore *string `json:"nutri_score"`
|
||||
// Attributes carries non-food spec values (driven by kind_field) for the
|
||||
// generic archive kinds. Nil means "leave unchanged".
|
||||
Attributes map[string]any `json:"attributes"`
|
||||
}
|
||||
|
||||
func normBrand(name string) string { return strings.Join(strings.Fields(strings.ToLower(name)), " ") }
|
||||
@@ -86,10 +89,11 @@ func (s *Store) UpdateProduct(ctx context.Context, id, actor string, in ProductI
|
||||
brandID = &bid
|
||||
}
|
||||
|
||||
// Resolve category gpc brick code.
|
||||
// Resolve category gpc brick code + archive kind.
|
||||
var gpc *string
|
||||
kind := DefaultKind
|
||||
if in.CategoryID != nil && *in.CategoryID != "" {
|
||||
if err := tx.QueryRow(ctx, "SELECT gpc_brick_code FROM category WHERE id = $1", *in.CategoryID).Scan(&gpc); err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
if err := tx.QueryRow(ctx, "SELECT gpc_brick_code, archive_kind FROM category WHERE id = $1", *in.CategoryID).Scan(&gpc, &kind); err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -116,19 +120,28 @@ WHERE id=$11`,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var nutriJSON []byte
|
||||
if in.Nutriments != nil {
|
||||
nutriJSON, _ = json.Marshal(in.Nutriments)
|
||||
// Non-food spec values live in product.attributes (nil means unchanged).
|
||||
if in.Attributes != nil {
|
||||
attrJSON, _ := json.Marshal(in.Attributes)
|
||||
if _, err = tx.Exec(ctx, "UPDATE product SET attributes=$1 WHERE id=$2", attrJSON, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
allergens := in.Allergens
|
||||
if allergens == nil {
|
||||
allergens = []string{}
|
||||
}
|
||||
additives := in.Additives
|
||||
if additives == nil {
|
||||
additives = []string{}
|
||||
}
|
||||
_, err = tx.Exec(ctx, `
|
||||
|
||||
if kind == FoodKind {
|
||||
var nutriJSON []byte
|
||||
if in.Nutriments != nil {
|
||||
nutriJSON, _ = json.Marshal(in.Nutriments)
|
||||
}
|
||||
allergens := in.Allergens
|
||||
if allergens == nil {
|
||||
allergens = []string{}
|
||||
}
|
||||
additives := in.Additives
|
||||
if additives == nil {
|
||||
additives = []string{}
|
||||
}
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO food_detail (product_id, ingredients_text, allergens, additives,
|
||||
nutriments, nutrition_basis, serving_size, nutri_score)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8)
|
||||
@@ -140,10 +153,11 @@ ON CONFLICT (product_id) DO UPDATE SET
|
||||
nutrition_basis=EXCLUDED.nutrition_basis,
|
||||
serving_size=EXCLUDED.serving_size,
|
||||
nutri_score=EXCLUDED.nutri_score`,
|
||||
id, in.IngredientsText, allergens, additives,
|
||||
nutriJSON, in.NutritionBasis, in.ServingSize, in.NutriScore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
id, in.IngredientsText, allergens, additives,
|
||||
nutriJSON, in.NutritionBasis, in.ServingSize, in.NutriScore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := s.recomputeQualityTx(ctx, tx, id); err != nil {
|
||||
@@ -284,6 +298,9 @@ func diffFields(a, b *ProductDetail) []string {
|
||||
add("nutrition_basis", strEq(a.NutritionBasis, b.NutritionBasis))
|
||||
add("serving_size", strEq(a.ServingSize, b.ServingSize))
|
||||
add("nutri_score", strEq(a.NutriScore, b.NutriScore))
|
||||
aa, _ := json.Marshal(a.Attributes)
|
||||
ab, _ := json.Marshal(b.Attributes)
|
||||
add("attributes", string(aa) == string(ab))
|
||||
return changed
|
||||
}
|
||||
|
||||
@@ -442,6 +459,7 @@ type Category struct {
|
||||
Level int `json:"level"`
|
||||
ParentID *string `json:"parent_id"`
|
||||
GPCBrickCode *string `json:"gpc_brick_code"`
|
||||
ArchiveKind string `json:"archive_kind"`
|
||||
ProductCount int `json:"product_count"`
|
||||
}
|
||||
|
||||
@@ -450,7 +468,7 @@ type Category struct {
|
||||
func (s *Store) ListCategories(ctx context.Context) ([]Category, error) {
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT c.id, c.name_zh, c.name_en, c.path::text, c.level, c.parent_id::text,
|
||||
c.gpc_brick_code,
|
||||
c.gpc_brick_code, c.archive_kind,
|
||||
(SELECT count(*) FROM product p WHERE p.category_id = c.id) AS product_count
|
||||
FROM category c
|
||||
ORDER BY c.path`)
|
||||
@@ -462,7 +480,7 @@ ORDER BY c.path`)
|
||||
for rows.Next() {
|
||||
var c Category
|
||||
if err := rows.Scan(&c.ID, &c.NameZH, &c.NameEN, &c.Path, &c.Level,
|
||||
&c.ParentID, &c.GPCBrickCode, &c.ProductCount); err != nil {
|
||||
&c.ParentID, &c.GPCBrickCode, &c.ArchiveKind, &c.ProductCount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
|
||||
Reference in New Issue
Block a user