backup: 价格bug修复 + 订单批量识别设计文档

This commit is contained in:
2026-07-19 16:17:12 +08:00
parent 968a6f8d22
commit cc66448327
6 changed files with 473 additions and 35 deletions
+32 -10
View File
@@ -178,18 +178,30 @@ class ProductDatabase:
try:
row = conn.execute("SELECT avg_price FROM products WHERE barcode=?",
(str(barcode).strip(),)).fetchone()
return row[0] if row and row[0] else None
if not row:
return None
return row[0] if row[0] is not None else 0.0
finally:
conn.close()
def get_prices(self, barcodes: List[str]) -> Dict[str, float]:
def get_prices(self, barcodes: List[str], column: str = 'price') -> Dict[str, float]:
"""批量查进货价。
Args:
barcodes: 条码列表
column: 价字段,默认 'price'(Excel 导入的进货价,按商家主单位),
可选 'avg_price'OCR 学来的历史均价,调试用)。
"""
if not barcodes:
return {}
valid = {'price', 'avg_price', 'min_price', 'max_price'}
if column not in valid:
raise ValueError(f"column must be one of {valid}")
conn = self._connect()
try:
placeholders = ','.join('?' * len(barcodes))
rows = conn.execute(
f"SELECT barcode, avg_price FROM products WHERE barcode IN ({placeholders})",
f"SELECT barcode, {column} FROM products WHERE barcode IN ({placeholders})",
[str(b).strip() for b in barcodes]).fetchall()
return {r[0]: r[1] for r in rows if r[1]}
finally:
@@ -357,13 +369,23 @@ class ProductDatabase:
else:
# 高可信度源全字段覆盖;低可信度仅填空
if source in ('template', 'user_confirmed') or new_conf > 50:
conn.execute(
"UPDATE products SET name=?, specification=?, unit=?, price=?, "
"source=?, confidence=?, usage_count=?, last_seen=?, updated_at=?, "
"avg_price=?, min_price=?, max_price=?, price_count=? WHERE barcode=?",
(name or old_name, spec or old_spec, unit or old_unit, price,
source, new_conf, new_count, now, now,
new_avg, new_min, new_max, new_pc, barcode))
# OCR 来源不覆盖 priceprice 是商品资料 Excel 导入的进货价,应作为基准不被 OCR 学习的"按箱单价"覆盖)
if source == 'ocr':
conn.execute(
"UPDATE products SET name=?, specification=?, unit=?, "
"source=?, confidence=?, usage_count=?, last_seen=?, updated_at=?, "
"avg_price=?, min_price=?, max_price=?, price_count=? WHERE barcode=?",
(name or old_name, spec or old_spec, unit or old_unit,
source, new_conf, new_count, now, now,
new_avg, new_min, new_max, new_pc, barcode))
else:
conn.execute(
"UPDATE products SET name=?, specification=?, unit=?, price=?, "
"source=?, confidence=?, usage_count=?, last_seen=?, updated_at=?, "
"avg_price=?, min_price=?, max_price=?, price_count=? WHERE barcode=?",
(name or old_name, spec or old_spec, unit or old_unit, price,
source, new_conf, new_count, now, now,
new_avg, new_min, new_max, new_pc, barcode))
else:
conn.execute(
"UPDATE products SET "