fix: 应收预警重复生成Bug — 按plan_id去重替代全文匹配
根因: 到期未收款预警消息含'已逾期X天'动态文本, _exists()按全文匹配 每天逾期天数变化→消息不同→去重永远失败→每天生成重复预警(68条) 修复: 到期未收款循环改为按plan_id查已有pending预警去重 验证: 连续触发2次均新增0条 附带修复: - 清理159条金额单位错误预警(元→万元, 40980万→4.1万) - 删除137条budget_sync虚拟收付款计划 - 作废129条孤儿预警(指向已删除plan) - 清理68条重复预警
This commit is contained in:
@@ -680,7 +680,21 @@ def check_cash_alerts(db: Session, entity_id: int = 1) -> dict:
|
|||||||
days_late = (today - p.plan_date).days
|
days_late = (today - p.plan_date).days
|
||||||
msg = (f"【到期未收款】应收款{p.counterparty or '客户'} {p.amount:.1f}万 "
|
msg = (f"【到期未收款】应收款{p.counterparty or '客户'} {p.amount:.1f}万 "
|
||||||
f"原计划{p.plan_date.strftime('%Y-%m-%d')}到期,已逾期{days_late}天未收回")
|
f"原计划{p.plan_date.strftime('%Y-%m-%d')}到期,已逾期{days_late}天未收回")
|
||||||
if _exists(msg):
|
# 2026-08-30修复: 按plan_id去重(消息含"已逾期X天"动态文本,全文匹配会每天生成重复预警)
|
||||||
|
existing_plan_alert = db.query(KPIAlert).filter(
|
||||||
|
KPIAlert.alert_type == "cash_plan",
|
||||||
|
KPIAlert.status.in_(["pending", "processing"]),
|
||||||
|
).all()
|
||||||
|
dup = False
|
||||||
|
for ea in existing_plan_alert:
|
||||||
|
try:
|
||||||
|
es = _json.loads(ea.suggestion) if ea.suggestion else {}
|
||||||
|
if isinstance(es, dict) and es.get("plan_id") == p.id:
|
||||||
|
dup = True
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
if dup:
|
||||||
continue
|
continue
|
||||||
alert = KPIAlert(
|
alert = KPIAlert(
|
||||||
kpi_id=ar_kpi.id,
|
kpi_id=ar_kpi.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user