Files
puddy 9873eb1963 restore: 恢复 11-06~11-09 本地未提交改动
- libs: 新增 hr_sdk / hotel_report / dingtalk,更新 desktopauto
- controlplane-next: SDK 管理模块(controller/entity/migrations)与前端视图
- electron-shell: 插件同步、注册表、runner 调整
- plugins: XQ0035/XQ0041/XQ0042/XQ0045_XQ0046/XQ0048/XQ0076/XQ0078 等版本更新
- tests/tools: 新增 SDK 兼容性检查与打包工具(sdk_pack.py、sdk_compat_check.py)
2026-11-09 17:27:10 +08:00

156 lines
5.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0052 端到端验收:SDK `hotel_report` 0.3.0 会员卡统计 vs legacy 直连,并对产出 xlsx 做结构校验。
覆盖:
1. settle_window(前一日最晚总交班 ~ 当日最晚总交班)
2. 余额:SDK collect vs legacy fetch_balance(按会员聚合逐项比对)
3. 明细:SDK collect vs legacy fetch_detail(36 列逐格比对)
4. export_member 产出两份 xlsx:行数 / 表头 / 合计行位置
用法(项目根,.venv 已装 pyodbc):
.venv\\Scripts\\python.exe tests\\xq0052_湖畔储值卡\\e2e_sdk_check.py
"""
from __future__ import annotations
import os
import sys
import tempfile
import time
BASE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(BASE, "..", ".."))
SDK_ROOT = os.path.join(ROOT, "libs", "hotel_report")
sys.path.insert(0, SDK_ROOT)
sys.path.insert(0, os.path.join(SDK_ROOT, "legacy"))
sys.path.insert(0, BASE)
os.environ.setdefault(
"GS_CONN",
"DRIVER={SQL Server};SERVER=172.22.43.254,1433;UID=sa;PWD=Goldensoft5829;DATABASE=golden")
os.environ["GS_SKIP_CLIENT_CHECK"] = "1"
import hotel_report # noqa: E402
import export_member # noqa: E402
from compare_member import norm # noqa: E402
def check_balance(cfg, w) -> bool:
t0 = time.time()
table = hotel_report.collect.collect_member_balance(w["end"], cfg)
print(f" SDK 余额: {table.count} 人 / {time.time() - t0:.1f}s,卡内合计 {table.totals['卡内余额']:,.2f}")
t0 = time.time()
old = export_member.fetch_balance(w["end"])
print(f" legacy余额: {len(old)} 人 / {time.time() - t0:.1f}s")
if table.count != len(old):
print(f" ✗ 行数不一致 SDK={table.count} legacy={len(old)}")
return False
old_agg: dict = {}
for d in old:
key = str(d["memberid"]).strip()
item = old_agg.setdefault(key, [0, 0.0, 0.0, 0.0])
item[0] += 1
item[1] += float(d["leavemoney"] or 0)
item[2] += float(d["griftleave"] or 0)
item[3] += float(d["factleave"] or 0)
new_agg: dict = {}
for r in table.rows:
item = new_agg.setdefault(r.member_id.strip(), [0, 0.0, 0.0, 0.0])
item[0] += 1
item[1] += float(r.leavemoney or 0)
item[2] += float(r.griftleave or 0)
item[3] += float(r.factleave or 0)
diff = [k for k in set(old_agg) | set(new_agg)
if old_agg.get(k, [0, 0, 0, 0])[0] != new_agg.get(k, [0, 0, 0, 0])[0]
or any(abs(old_agg.get(k, [0, 0, 0, 0])[i] - new_agg.get(k, [0, 0, 0, 0])[i]) > 0.011
for i in (1, 2, 3))]
print(f" 差异会员: {len(diff)}" + (f",示例 {diff[:3]}" if diff else ""))
return not diff
def check_detail(cfg, w) -> bool:
t0 = time.time()
table = hotel_report.collect.collect_member_detail(w["start"], w["end"], cfg)
print(f" SDK 明细: {table.count} 行 × {len(table.headers)} 列 / {time.time() - t0:.1f}s")
old = export_member.fetch_detail(w["start"], w["end"])
print(f" legacy明细: {len(old)} 行 / 36 列")
if table.count != len(old):
print(f" ✗ 行数不一致 SDK={table.count} legacy={len(old)}")
return False
diff = 0
for i, (o, n) in enumerate(zip(old, table.rows)):
for j in range(36):
a, b = norm(o[j]), norm(n.values[j])
if a == b:
continue
if isinstance(a, float) and isinstance(b, float) and abs(a - b) < 0.011:
continue
diff += 1
if diff <= 5:
print(f" 行{i} 列{j}: legacy={a!r} sdk={b!r}")
print(f" 差异单元格: {diff}")
return diff == 0
def check_files(res) -> bool:
"""write_only 产物没有 dimension,直接读 sheet XML 数行 / 表头非空单元格。"""
import re
import zipfile
ok = True
for path in res["files"]:
name = os.path.basename(path)
with zipfile.ZipFile(path) as z:
xml = z.read("xl/worksheets/sheet1.xml").decode("utf-8")
rows = xml.count("<row ")
row_xml = re.findall(r"<row[^>]*>(.*?)</row>", xml, re.S)
if "余额" in name:
head = row_xml[2] # 第 3 行才是列名
expect_rows = res["balance"]["count"] + 4 # 标题 + 区间 + 表头 + 数据 + 合计
expect_cells = 11 # B~L
else:
head = row_xml[1] # 第 2 行是列名
expect_rows = res["detail"]["count"] + 3 # 标题 + 表头 + 数据 + 合计
expect_cells = len(res["detail"]["headers"])
cells = head.count("<c ")
good = rows == expect_rows and cells == expect_cells
print(f" {name}: {rows} 行 / 表头 {cells} 个非空单元格"
f"(期望 {expect_rows} 行 / {expect_cells}){'OK' if good else '✗'}")
ok &= good
return ok
def main() -> int:
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
cfg = hotel_report.HotelConfig.resolve(timeout=300)
from datetime import datetime
today = datetime.now().strftime("%Y-%m-%d")
w = hotel_report.settle_window(cfg, today)
print(f"WCF: {cfg.wcf_url}")
print(f"结算窗口: {w['start']} ~ {w['end']}(closed={w['closed']})")
if not w["closed"]:
print("✗ 当日尚未交班,无法验收")
return 1
ok1 = check_balance(cfg, w)
ok2 = check_detail(cfg, w)
ok3 = True
with tempfile.TemporaryDirectory(prefix="xq0052_e2e_") as d:
t0 = time.time()
res = hotel_report.export_member(cfg, today, d)
print(f"\n export_member 落盘: {time.time() - t0:.1f}s")
ok3 = check_files(res)
print(f"\n结论: 余额={'一致' if ok1 else '有差异'} 明细={'一致' if ok2 else '有差异'} "
f"文件={'正常' if ok3 else '异常'}")
return 0 if (ok1 and ok2 and ok3) else 2
if __name__ == "__main__":
sys.exit(main())