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)
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0052 对拍:legacy 直连(pyodbc)vs SDK WCF 通道,验证会员卡余额/明细口径一致。
|
||||
|
||||
余额:legacy 多语句批(#临时表)跑在 jgmember ↔ 新单条 CTE 跑在 golden(四段式)
|
||||
明细:同一条 DETAIL_SQL,两条通道各跑一次,逐列比对
|
||||
支付方式:同上
|
||||
|
||||
用法(项目根,.venv 已装 pyodbc):
|
||||
.venv\\Scripts\\python.exe tests\\xq0052_湖畔储值卡\\compare_member.py
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
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"))
|
||||
|
||||
# legacy 直连通道(只读)
|
||||
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"
|
||||
|
||||
from hotel_report import HotelConfig, latest_shift_time # noqa: E402
|
||||
import balance_cte_probe as probe # noqa: E402
|
||||
import db # noqa: E402
|
||||
import export_member # noqa: E402
|
||||
|
||||
|
||||
def norm(v):
|
||||
"""两条通道的值归一化:数值→float(4),时间→'YYYY-MM-DD HH:MM:SS',其余→str。"""
|
||||
if v is None:
|
||||
return None
|
||||
if isinstance(v, datetime):
|
||||
return v.strftime("%Y-%m-%d %H:%M:%S")
|
||||
if isinstance(v, (int, float)):
|
||||
return round(float(v), 4)
|
||||
if isinstance(v, str):
|
||||
s = v.strip()
|
||||
if s == "":
|
||||
return None
|
||||
if len(s) >= 19 and s[4] == "-" and s[10] in "T ":
|
||||
return s[:10] + " " + s[11:19]
|
||||
try:
|
||||
return round(float(s), 4) # WCF 通道的数值列是字符串
|
||||
except ValueError:
|
||||
return s
|
||||
try:
|
||||
return round(float(v), 4)
|
||||
except (TypeError, ValueError):
|
||||
return str(v)
|
||||
|
||||
|
||||
def compare_balance(cfg, end):
|
||||
print("\n===== 余额对拍")
|
||||
t0 = time.time()
|
||||
old = export_member.fetch_balance(end)
|
||||
print(f" legacy 直连: {len(old)} 行 / {time.time() - t0:.1f}s")
|
||||
t0 = time.time()
|
||||
new = probe.raw_query(";" + probe.BALANCE_CTE.format(endtime=end, top=""), cfg)
|
||||
print(f" WCF CTE : {len(new)} 行 / {time.time() - t0:.1f}s")
|
||||
|
||||
def agg_old(rows):
|
||||
out = {}
|
||||
for d in rows:
|
||||
k = str(d["memberid"]).strip()
|
||||
a = out.setdefault(k, [0, 0.0, 0.0, 0.0, 0.0])
|
||||
a[0] += 1
|
||||
for i, key in ((1, "leavemoney"), (2, "griftleave"), (3, "factleave"), (4, "partition")):
|
||||
a[i] += float(d[key] or 0)
|
||||
return out
|
||||
|
||||
def agg_new(rows):
|
||||
out = {}
|
||||
for d in rows:
|
||||
k = str(d["Memberid"]).strip()
|
||||
a = out.setdefault(k, [0, 0.0, 0.0, 0.0, 0.0])
|
||||
a[0] += 1
|
||||
for i, key in ((1, "leavemoney"), (2, "griftleave"), (3, "factleave"),
|
||||
(4, "PartitionLeaveMoney")):
|
||||
a[i] += float(d[key] or 0)
|
||||
return out
|
||||
|
||||
a, b = agg_old(old), agg_new(new)
|
||||
diff = []
|
||||
for k in set(a) | set(b):
|
||||
x = a.get(k, [0, 0.0, 0.0, 0.0, 0.0])
|
||||
y = b.get(k, [0, 0.0, 0.0, 0.0, 0.0])
|
||||
if x[0] != y[0] or any(abs(x[i] - y[i]) > 0.011 for i in (1, 2, 3, 4)):
|
||||
diff.append((k, x, y))
|
||||
print(f" 会员数 legacy={len(a)} wcf={len(b)};差异 {len(diff)} 个会员")
|
||||
for k, x, y in diff[:10]:
|
||||
print(f" {k}: legacy={x} wcf={y}")
|
||||
tot_old, tot_new = sum(v[1] for v in a.values()), sum(v[1] for v in b.values())
|
||||
print(f" 卡内余额合计 legacy={tot_old:,.2f} wcf={tot_new:,.2f} 差={tot_new - tot_old:+.2f}")
|
||||
return len(diff) == 0
|
||||
|
||||
|
||||
def compare_rows(label, old_rows, new_rows, key_cols):
|
||||
print(f"\n===== {label} 对拍")
|
||||
print(f" 行数 legacy={len(old_rows)} wcf={len(new_rows)}")
|
||||
if len(old_rows) != len(new_rows):
|
||||
print(" 行数不一致,跳过逐行比对")
|
||||
return False
|
||||
cols = list(new_rows[0].keys()) if new_rows else []
|
||||
none_cnt = {}
|
||||
diff = 0
|
||||
for i, (o, n) in enumerate(zip(old_rows, new_rows)):
|
||||
for j, col in enumerate(cols):
|
||||
vo, vn = norm(o[j]), norm(n[col])
|
||||
if vo == vn:
|
||||
continue
|
||||
if isinstance(vo, float) and isinstance(vn, float) and abs(vo - vn) < 0.011:
|
||||
continue
|
||||
diff += 1
|
||||
if diff <= 8:
|
||||
print(f" 行{i} 列[{j}]{col}: legacy={vo!r} wcf={vn!r}")
|
||||
none_cnt[col] = none_cnt.get(col, 0) + 1
|
||||
print(f" 列={len(cols)};差异单元格 {diff}")
|
||||
if none_cnt:
|
||||
print(" 差异列分布: " + ", ".join(f"{k}×{v}" for k, v in none_cnt.items()))
|
||||
return diff == 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if hasattr(sys.stdout, "reconfigure"):
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
cfg = HotelConfig.resolve(timeout=300)
|
||||
today = datetime.now()
|
||||
cur = latest_shift_time(cfg, today)
|
||||
prev = latest_shift_time(cfg, today.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
- __import__("datetime").timedelta(days=1))
|
||||
end = cur["latest"] or today.strftime("%Y-%m-%d %H:%M:%S")
|
||||
start = prev["latest"] or (today - __import__("datetime").timedelta(days=1)).strftime(
|
||||
"%Y-%m-%d 00:00:00")
|
||||
print(f"WCF: {cfg.wcf_url}\n窗口: {start} ~ {end}")
|
||||
|
||||
ok1 = compare_balance(cfg, end)
|
||||
|
||||
detail_sql = export_member.DETAIL_SQL.format(starttime=start, endtime=end, feetype="全部")
|
||||
t0 = time.time()
|
||||
old = db.fetch_all(detail_sql)
|
||||
print(f"\n明细 legacy 直连: {len(old)} 行 / {time.time() - t0:.1f}s")
|
||||
t0 = time.time()
|
||||
new = probe.raw_query(detail_sql, cfg)
|
||||
print(f"明细 WCF : {len(new)} 行 / {time.time() - t0:.1f}s")
|
||||
ok2 = compare_rows("明细", old, new, ("memberid", "feeindate"))
|
||||
|
||||
pay_sql = ("select a.Billid, b.MethodName, sum(b.PayAmount) as amt "
|
||||
"from jgmember.dbo.dt_membercardcomein a "
|
||||
"left join v_snchangeclass b on b.BillId = a.Billid "
|
||||
"where a.feeindate >= ? and a.feeindate <= ? and a.feetype = '充值' "
|
||||
"group by a.Billid, b.MethodName")
|
||||
old_pay = db.fetch_all(pay_sql, start, end)
|
||||
new_pay = probe.raw_query(pay_sql.replace("?", f"'{start}'", 1).replace("?", f"'{end}'", 1), cfg)
|
||||
print(f"\n支付方式 legacy={len(old_pay)} wcf={len(new_pay)}")
|
||||
ok3 = compare_rows("支付方式", old_pay, new_pay, ("Billid", "MethodName"))
|
||||
|
||||
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())
|
||||
Reference in New Issue
Block a user