From d71c7fef675f7cbe33fc2d4b8dad9945f1b96f26 Mon Sep 17 00:00:00 2001 From: Hermes CI Fix Date: Sun, 6 Sep 2026 16:57:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(data):=20=E8=87=AA=E5=8A=A8=E5=BD=92?= =?UTF-8?q?=E9=9B=86=E7=A9=BA=E6=B1=87=E6=80=BB=E4=B8=8D=E5=86=990=20+=20E?= =?UTF-8?q?xcel=E5=AF=BC=E5=85=A5=E7=A4=BA=E4=BE=8B=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=98=B2=E7=BA=BF=20(DAMA=E5=AE=A1=E8=AE=A120260906=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=B1=BB)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/cash.py | 2 ++ backend/scripts/kpi_value_collector.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/app/api/cash.py b/backend/app/api/cash.py index 7f2878e4..6e0b2fb0 100644 --- a/backend/app/api/cash.py +++ b/backend/app/api/cash.py @@ -945,6 +945,8 @@ async def api_import_vouchers( content = await file.read() fname = (file.filename or "网银流水.xlsx").strip() + if any(kw in fname.lower() for kw in ("示例", "sample", "test", "demo", "测试")): + raise HTTPException(400, f"文件名含测试标识,拒绝导入生产库: {fname}") try: df = pd.read_excel(io.BytesIO(content)) except Exception as e: diff --git a/backend/scripts/kpi_value_collector.py b/backend/scripts/kpi_value_collector.py index 20991b00..769c4055 100644 --- a/backend/scripts/kpi_value_collector.py +++ b/backend/scripts/kpi_value_collector.py @@ -123,7 +123,10 @@ def collect_for_mapping(db, mapping: KPIValueSource, period: str, write_kpi: boo q = q.filter(model.carry_forward == 0) value = q.scalar() - value = float(value or 0) + if value is None: + # 源表无记录 → 不写 0,交由 run_collector 记 skipped,避免污染 KPI 实际值 + return None, "源表无记录" + value = float(value) value = round(value * unit, 2) message = f"表{table}.{field} {aggregate}(period={period}) × {unit}" @@ -185,6 +188,18 @@ def run_collector(db, entity_id: Optional[int] = None, period: Optional[str] = N for m in mappings: try: value, message = collect_for_mapping(db, m, period, write_kpi=True) + if value is None: + # 源表无记录 → 跳过,不落 pending=0 的 KPIValue + db.add(KPIValueCollectLog( + entity_id=m.entity_id, + kpi_id=m.kpi_id, + period=period, + source_table=m.source_table, + collected_value=None, + status="skipped", + message=message, + )) + continue db.add(KPIValueCollectLog( entity_id=m.entity_id, kpi_id=m.kpi_id,