fix(contract): 合同付款预警bug修复
This commit is contained in:
1 parent
5330e08281
commit
161666c0e7
26 files changed
+1599
-117
No files matched your search
@@ -3,7 +3,7 @@ services/contract_payment/alert_service.py
|
||||
|
||||
用途:按付款阶段生成待发送预警,在每周五按收件人汇总发送企业微信和 Excel 明细,并提供工作台待发送摘要。
|
||||
依赖:合同付款模型、用户目录、openpyxl、企业微信通知服务、OA 上级矩阵、排程时区工具。
|
||||
逻辑:每日任务只写入 pending 审计;周汇总任务锁定本周收件人批次,发送一条汇总卡片和一份逐笔 Excel;摘要接口与周五发送使用同一 pending 口径。
|
||||
逻辑:每日任务只写入 pending 审计;周汇总任务锁定本周收件人批次,发送一条汇总卡片和一份逐笔 Excel;发送失败时把原因写入批次审计。摘要接口与周五发送使用同一 pending 口径。
|
||||
第一轮按台账申请人 `real_name` 唯一匹配系统用户;第二、三轮查 OA HrmResource + Matrixtable_3,用 zz/jfl 作为企微 userid,不向第一轮兜底。
|
||||
"""
|
||||
|
||||
@@ -152,11 +152,106 @@ def _resolve_pending_alerts(*, node_id: int) -> int:
|
||||
return len(rows)
|
||||
|
||||
|
||||
_FAILURE_REASON_LABELS: dict[str, str] = {
|
||||
"notify_disabled": "企业微信通知未启用",
|
||||
"event_disabled": "合同付款预警通知事件未启用",
|
||||
"user_unbound": "收件人未绑定企业微信账号",
|
||||
"textcard_failed": "企业微信卡片发送失败",
|
||||
"file_upload_failed": "企业微信附件上传失败",
|
||||
"file_failed": "企业微信附件发送失败",
|
||||
"unknown": "发送失败",
|
||||
}
|
||||
|
||||
|
||||
def _append_failure_reason(reasons: list[dict[str, str]], code: str, detail: str = "") -> None:
|
||||
"""向失败原因列表追加一条去重后的记录。
|
||||
|
||||
核心逻辑:压缩 detail 空白并截断到 500 字;code 与 detail 都相同则跳过。
|
||||
|
||||
Args:
|
||||
reasons: 正在组装的原因列表,就地修改。
|
||||
code: 前端可翻译的原因编码。
|
||||
detail: 企微或异常返回的补充说明,可为空。
|
||||
"""
|
||||
normalized = " ".join(str(detail or "").split())[:500]
|
||||
if any(item["code"] == code and item["detail"] == normalized for item in reasons):
|
||||
return
|
||||
reasons.append({"code": code, "detail": normalized})
|
||||
|
||||
|
||||
def describe_batch_failure(trace: dict | None, *, fallback: str | None = None) -> list[dict[str, str]]:
|
||||
"""把周汇总企微发送轨迹整理成可展示的失败原因。
|
||||
|
||||
核心逻辑:优先识别卡片失败、附件上传异常和附件发送失败;「未绑定企微」只保留一条。
|
||||
轨迹无法识别时回退到已保存的 error_message,避免历史失败批次没有原因可看。
|
||||
|
||||
Args:
|
||||
trace: 批次 wecom_trace_json。非字典时忽略。
|
||||
fallback: 轨迹无法识别时使用的已存错误文本。
|
||||
|
||||
Returns:
|
||||
原因列表。每项含 code 与 detail;没有可展示原因时返回空列表。
|
||||
"""
|
||||
reasons: list[dict[str, str]] = []
|
||||
# [STEP 1] 卡片发送失败优先按已知原因码归类,企微 errmsg 放在 detail。
|
||||
if isinstance(trace, dict):
|
||||
textcard = trace.get("textcard") if isinstance(trace.get("textcard"), dict) else {}
|
||||
if textcard and not textcard.get("ok"):
|
||||
reason = str(textcard.get("reason") or "").strip()
|
||||
raw = textcard.get("raw") if isinstance(textcard.get("raw"), dict) else {}
|
||||
errmsg = str(raw.get("errmsg") or "").strip()
|
||||
if reason in {"notify_disabled", "event_disabled", "user_unbound"}:
|
||||
_append_failure_reason(reasons, reason)
|
||||
else:
|
||||
_append_failure_reason(reasons, "textcard_failed", errmsg)
|
||||
|
||||
# [STEP 2] 附件失败与卡片失败分开记录;未绑定账号不重复展示。
|
||||
file_error = str(trace.get("file_error") or "").strip()
|
||||
if file_error:
|
||||
_append_failure_reason(reasons, "file_upload_failed", file_error)
|
||||
else:
|
||||
file_result = trace.get("file") if isinstance(trace.get("file"), dict) else {}
|
||||
errcode = file_result.get("errcode")
|
||||
if errcode not in (None, 0, "0"):
|
||||
errmsg = str(file_result.get("errmsg") or "").strip()
|
||||
if errmsg == "user_unbound":
|
||||
_append_failure_reason(reasons, "user_unbound")
|
||||
else:
|
||||
_append_failure_reason(reasons, "file_failed", errmsg or str(errcode))
|
||||
|
||||
if reasons:
|
||||
return reasons
|
||||
stored = " ".join(str(fallback or "").split())
|
||||
if stored:
|
||||
_append_failure_reason(reasons, "unknown", stored[:2000])
|
||||
return reasons
|
||||
|
||||
|
||||
def render_batch_failure_message(reasons: list[dict[str, str]]) -> str:
|
||||
"""把失败原因编码拼成写入批次审计字段的中文摘要。
|
||||
|
||||
核心逻辑:使用固定中文标签,detail 非空时接在标签后,多条以分号连接。
|
||||
|
||||
Args:
|
||||
reasons: describe_batch_failure 的返回值。
|
||||
|
||||
Returns:
|
||||
最长 2000 字的中文摘要;没有原因时返回空字符串。
|
||||
"""
|
||||
parts: list[str] = []
|
||||
for item in reasons:
|
||||
label = _FAILURE_REASON_LABELS.get(str(item.get("code") or ""), "发送失败")
|
||||
detail = str(item.get("detail") or "").strip()
|
||||
parts.append(f"{label}:{detail}" if detail else label)
|
||||
return ";".join(parts)[:2000]
|
||||
|
||||
|
||||
def send_weekly_alerts(*, today: date | None = None) -> dict[str, int]:
|
||||
"""按收件人发送本周合同付款预警汇总及 Excel 明细。
|
||||
|
||||
AI 触发时机:每周五 18:00 的排程或管理员补发时调用。
|
||||
核心逻辑:每位收件人每周只产生一个批次;企微正文仅发送总笔数和阶段数量,逐笔信息写入附件。
|
||||
发送失败时把卡片和附件的原因写入 error_message,并把 sent_at 记为最近一次尝试时间,供周汇总页查看。
|
||||
Args:
|
||||
today: 用于确定 ISO 周标识,测试可显式传入。
|
||||
Returns:
|
||||
@@ -231,7 +326,9 @@ def send_weekly_alerts(*, today: date | None = None) -> dict[str, int]:
|
||||
sent += 1
|
||||
else:
|
||||
batch.status = "failed"
|
||||
batch.error_message = str(trace)[:2000]
|
||||
# 失败也记录尝试时刻,周汇总「发送时间」不再空白;重试时覆盖为最近一次。
|
||||
batch.sent_at = datetime.utcnow()
|
||||
batch.error_message = render_batch_failure_message(describe_batch_failure(trace)) or str(trace)[:2000]
|
||||
failed += 1
|
||||
db.session.commit()
|
||||
return {"batches": batches, "sent": sent, "failed": failed, "alerts": detail_count}
|
||||
|
||||
Reference in new issue
Block a user