fix(plugin): XQ0034 同步 AI 表电话字段改名,XQ0052 台账写入事务化

XQ0034 v1.0.31:AI 表字段「电子签接收电话号码」已改名为「电子签接收员工本人电话」,插件版与 workflows 版同步,避免写旧字段 404。XQ0052 v0.4.3:台账写入改事务顺序(明细 -> 新办卡 -> 交班行 -> 余额 -> 一次保存),交班行作为补数进度提交点,中途失败整簿不落盘;返回值 days 改 isoformat 修复 exit=0 但结果被静默丢弃;导出落盘遇文件占用(WPS/Excel 打开)改重试 3 次并给中文提示;台账已含某营业日时改为核对明细覆盖度。
This commit is contained in:
2026-11-11 12:48:55 +08:00
parent db1ab3abdf
commit 602d9dd4f3
9 changed files with 249 additions and 38 deletions
@@ -20,6 +20,7 @@ from __future__ import annotations
import json
import os
import time
from webhost.backend.plugins.base import PLUGINS, ScenePlugin
@@ -36,12 +37,42 @@ def _sdk_versions() -> dict:
return {}
#: 导出表落盘重试:目标 xlsx 正被 WPS/Excel 打开时 openpyxl 会 PermissionError
_EXPORT_RETRIES = 3
_EXPORT_RETRY_DELAY = 2.0
def _save_xlsx(path: str, writer, ctx, what: str):
"""写一份导出表;被别的程序占用(财务开着那份 xlsx)时等一下重试,仍失败给中文提示。
原来的表现:直接抛 `PermissionError: [Errno 13] Permission denied: '...会员卡余额_xxx.xlsx'`
加一串 openpyxl 清理临时文件的 `Exception ignored`,财务完全看不出是"表被打开着"。
"""
name = os.path.basename(path)
last = None
for attempt in range(1, _EXPORT_RETRIES + 1):
try:
return writer()
except PermissionError as e:
last = e
if attempt < _EXPORT_RETRIES:
wait = _EXPORT_RETRY_DELAY * attempt
ctx.log("%s写不进去:「%s」正被别的程序占用(多半是 WPS/Excel 开着它),"
"%g 秒后重试(%d/%d)"
% (what, name, wait, attempt, _EXPORT_RETRIES), "WARN")
time.sleep(wait)
raise RuntimeError(
"写不了%s「%s」:文件被其它程序独占(请关掉 WPS/Excel 里打开着的这份表,"
"或结束残留的 et.exe / wps.exe 进程,再重跑一次)。原始错误:%s"
% (what, path, last)) from last
class HupanStoredCardPlugin(ScenePlugin):
"""湖畔储值卡每日导出统计(需求 XQ0052)。"""
id = "xq0052"
name = "湖畔储值卡每日导出统计"
version = "0.4.0"
version = "0.4.3"
description = ("按营业日导出会员卡统计(余额 + 明细),并维护会员卡统计台账 Excel:"
"交班时间逐日追加、余额覆盖、明细/新办卡追加、汇总核对")
platform = ""
@@ -229,8 +260,9 @@ class HupanStoredCardPlugin(ScenePlugin):
ctx.log("取会员卡余额:截至 %s" % window["end"])
balance = hotel_report.collect.collect_member_balance(
window["end"], cfg, window=(window["start"], window["end"]))
bpath = hotel_report.render.write_member_balance_xlsx(
balance, os.path.join(save_dir, "会员卡余额_%s.xlsx" % stamp))
btarget = os.path.join(save_dir, "会员卡余额_%s.xlsx" % stamp)
bpath = _save_xlsx(btarget, lambda: hotel_report.render.write_member_balance_xlsx(
balance, btarget), ctx, "会员卡余额表")
ctx.log("会员卡余额已导出:%d 人,卡内余额合计 %.2f → %s"
% (balance.count, (balance.totals or {}).get("卡内余额", 0.0), bpath))
ctx.progress(45)
@@ -238,8 +270,9 @@ class HupanStoredCardPlugin(ScenePlugin):
ctx.log("取会员卡明细:%s ~ %s" % (window["start"], window["end"]))
detail = hotel_report.collect.collect_member_detail(
window["start"], window["end"], cfg, window=(window["start"], window["end"]))
dpath = hotel_report.render.write_member_detail_xlsx(
detail, os.path.join(save_dir, "会员卡明细_%s.xlsx" % stamp))
dtarget = os.path.join(save_dir, "会员卡明细_%s.xlsx" % stamp)
dpath = _save_xlsx(dtarget, lambda: hotel_report.render.write_member_detail_xlsx(
detail, dtarget), ctx, "会员卡明细表")
ctx.log("会员卡明细已导出:%d 行 × %d 列 → %s"
% (detail.count, len(detail.headers), dpath))
ctx.progress(55)
@@ -274,13 +307,22 @@ class HupanStoredCardPlugin(ScenePlugin):
backup=backup, recalc=recalc, month_reset=month_reset)
provider = SnapshotProvider(hotel_report, cfg, snapshot, log=ctx.log)
try:
return maint.run(day, provider)
res = maint.run(day, provider)
except LedgerError as e:
ctx.log(_RETRY_TIP, "WARN")
raise RuntimeError("台账维护失败:%s" % e) from e
except Exception: # noqa: BLE001 含 WPS 的 com_error
ctx.log(_RETRY_TIP, "WARN")
raise
# 兜底:整趟返回值要经壳子 `json.dumps` 回传前端,任何不可序列化的对象(date、COM 对象…)
# 都会在壳子侧抛错并被静默吞掉 → 前端只看到「执行进程异常退出(exit=0)且未返回结果」,
# 而台账其实已经保存成功(v0.4.1 的坑)。这里先自检一遍,坏字段转文本,别让结果凭空消失。
try:
json.dumps(res)
except (TypeError, ValueError) as e:
ctx.log("台账结果含无法回传的字段(%s),已按文本处理" % e, "WARN")
res = json.loads(json.dumps(res, default=str))
return res
#: 台账阶段失败时的下一步提示(导出成果已经落盘,不用重导)