This commit is contained in:
@@ -76,12 +76,12 @@ def _fetch_candidates(conn, limit: int) -> List[Dict[str, Any]]:
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"商品ID" AS sku,
|
||||
SUM(CASE WHEN snapshot_date >= CURRENT_DATE - 7
|
||||
SUM(CASE WHEN snapshot_date::date >= CURRENT_DATE - 7
|
||||
THEN COALESCE("銷售金額"::numeric, 0) ELSE 0 END) AS sales_7d,
|
||||
SUM(CASE WHEN snapshot_date >= CURRENT_DATE - 14
|
||||
AND snapshot_date < CURRENT_DATE - 7
|
||||
SUM(CASE WHEN snapshot_date::date >= CURRENT_DATE - 14
|
||||
AND snapshot_date::date < CURRENT_DATE - 7
|
||||
THEN COALESCE("銷售金額"::numeric, 0) ELSE 0 END) AS sales_prev_7d,
|
||||
SUM(CASE WHEN snapshot_date >= CURRENT_DATE - 7
|
||||
SUM(CASE WHEN snapshot_date::date >= CURRENT_DATE - 7
|
||||
THEN COALESCE("數量"::numeric, 0) ELSE 0 END) AS qty_7d
|
||||
FROM daily_sales_snapshot
|
||||
GROUP BY "商品ID"
|
||||
@@ -157,6 +157,10 @@ def _fetch_candidates(conn, limit: int) -> List[Dict[str, Any]]:
|
||||
return [dict(row) for row in conn.execute(sql, {"limit": max(limit * 6, 100)}).mappings().all()]
|
||||
except Exception as exc:
|
||||
logger.warning("[ProductPickAgent] sales-aware query failed, fallback without sales: %s", exc)
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
fallback = text("""
|
||||
WITH latest_momo AS (
|
||||
SELECT
|
||||
@@ -315,8 +319,10 @@ def _write_pick(conn, pick: Dict[str, Any]) -> None:
|
||||
def generate_product_pick_list(engine, limit: int = 30) -> ProductPickResult:
|
||||
"""產生並保存 AI 建議挑品清單。"""
|
||||
generated_at = datetime.now().isoformat(timespec="seconds")
|
||||
with engine.begin() as conn:
|
||||
with engine.connect() as conn:
|
||||
rows = _fetch_candidates(conn, limit)
|
||||
|
||||
with engine.begin() as conn:
|
||||
scored = [_score_candidate(row) for row in rows if _to_float(row.get("pchome_price")) > 0]
|
||||
picks = [
|
||||
pick for pick in scored
|
||||
|
||||
Reference in New Issue
Block a user