audit_products.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. """机器验收:8 款产品数据的字段完整性 + 数字自洽性"""
  3. import json, glob, os, sys
  4. D = "/opt/data/projects/hardware-lab/site/data/products"
  5. fails, warns = [], []
  6. REQ = ["id","name","latin","tagline","category","craftTags","summary",
  7. "shape","materials","parts","electronics","consumables","bom",
  8. "processes","moq","cost","detail","dimensions","weight","priceBasis"]
  9. rows = []
  10. for f in sorted(glob.glob(os.path.join(D, "*.json"))):
  11. if os.path.basename(f) == "INDEX.json": continue
  12. d = json.load(open(f))
  13. pid = os.path.basename(f)[:-5]
  14. e = []
  15. for k in REQ:
  16. if k not in d or d[k] in (None, "", [], {}): e.append(f"缺字段 {k}")
  17. if d.get("id") != pid: e.append(f"id 不一致: {d.get('id')} vs 文件名 {pid}")
  18. bom = d.get("bom", {})
  19. brow = bom.get("rows", [])
  20. if not brow: e.append("BOM 无 rows")
  21. # BOM 小计自洽
  22. bad = 0
  23. for r in brow:
  24. p, q, s = r.get("price"), r.get("qty"), r.get("subtotal")
  25. if p is None or q is None or s is None: bad += 1; continue
  26. if abs(p * q - s) > 0.02: bad += 1
  27. if bad: e.append(f"BOM 小计不自洽 {bad} 行")
  28. bsum = round(sum(r.get("subtotal", 0) for r in brow), 2)
  29. cost = d.get("cost", {})
  30. mat = cost.get("material")
  31. if mat is not None and abs(mat - bsum) > max(1.0, bsum * 0.03):
  32. warns.append(f"{pid}: cost.material {mat} ≠ BOM合计 {bsum}")
  33. items = cost.get("items", [])
  34. isum = round(sum(i.get("amount", 0) for i in items), 2)
  35. uc = cost.get("unitCost")
  36. if uc is not None and items and abs(isum - uc) > max(0.5, uc * 0.02):
  37. e.append(f"成本各项相加 {isum} ≠ unitCost {uc}")
  38. sp = cost.get("suggestedPrice")
  39. gm = cost.get("grossMarginPct")
  40. if uc and sp:
  41. realgm = (sp - uc) / sp * 100
  42. if gm is not None and abs(realgm - gm) > 1.0:
  43. e.append(f"毛利率不自洽: 写了 {gm}% 实际 {realgm:.1f}%")
  44. if realgm < 20 or realgm > 70:
  45. warns.append(f"{pid}: 毛利率 {realgm:.1f}% 超出 20~70% 合理带")
  46. rl, rh = cost.get("retailLow"), cost.get("retailHigh")
  47. if rl and rh and sp and abs(sp - (rl + rh) / 2) > max(30, sp * 0.25):
  48. warns.append(f"{pid}: 建议价 {sp} 与零售带 {rl}~{rh} 差距大")
  49. procs = d.get("processes", [])
  50. if len(procs) < 4: e.append(f"工艺仅 {len(procs)} 道(要求≥4)")
  51. for p in procs:
  52. if not p.get("equipment"): e.append(f"工艺[{p.get('name')}] 缺设备")
  53. if p.get("yield") is None: e.append(f"工艺[{p.get('name')}] 缺良率")
  54. if not p.get("moq"): e.append(f"工艺[{p.get('name')}] 缺 MOQ")
  55. moq = d.get("moq", {})
  56. tiers = moq.get("tiers", []) if isinstance(moq, dict) else []
  57. if len(tiers) < 3: e.append(f"MOQ 阶梯仅 {len(tiers)} 档(要求≥3)")
  58. det = d.get("detail", {})
  59. if isinstance(det, dict) and not det.get("sections") and len(det) < 3:
  60. warns.append(f"{pid}: detail 结构偏薄")
  61. # parts 是几何渲染用的结构件(role/material/craft),数量归 bom.rows 管
  62. parts = d.get("parts", [])
  63. if len(parts) < 4: e.append(f"parts 仅 {len(parts)} 件(几何至少 4 件)")
  64. for p in parts:
  65. if not p.get("name"): e.append("零件缺 name")
  66. if not p.get("role"): e.append(f"零件[{p.get('name')}] 缺 role")
  67. if not p.get("material"): e.append(f"零件[{p.get('name')}] 缺 material")
  68. rows.append((pid, d.get("name"), bsum, uc, sp, gm, len(brow), len(procs), len(tiers), e))
  69. print(f"{'产品':<16}{'BOM合计':>10}{'单台成本':>10}{'建议价':>9}{'毛利':>8}{'BOM行':>7}{'工艺':>5}{'MOQ档':>6}")
  70. print("-" * 78)
  71. for r in rows:
  72. print(f"{r[0]:<16}{r[2]:>10.2f}{(r[3] or 0):>10.2f}{(r[4] or 0):>9}{(r[5] or 0):>7.1f}%{r[6]:>7}{r[7]:>5}{r[8]:>6}")
  73. print(f"\n=== 硬错误 {sum(len(r[9]) for r in rows)} ===")
  74. for r in rows:
  75. for e in r[9][:6]:
  76. print(f" [{r[0]}] {e}")
  77. print(f"\n=== 警告 {len(warns)} ===")
  78. for w in warns[:15]: print(f" {w}")
  79. sys.exit(1 if sum(len(r[9]) for r in rows) else 0)