chore(AdminCost): 移除 COS4 数据结构迁移的一次性脚本与参考资料

This commit is contained in:
lianjianwei2024
2026-11-01 15:27:27 +08:00
parent 386e1ad624
commit f0b1033b12
69 changed files with 5936 additions and 42964 deletions

View File

@@ -35,7 +35,7 @@ def _free_port():
# ------------------------------------------------------------------ 造数
def make_cos4(path, proj_name, bills, lmes=(), build_unit="测试建设单位",
proj_lmes=None, uw_lmes=None):
proj_lmes=None, uw_lmes=None, zip_ts=None):
"""构造最小可解析的 .cos4(zip 内 Project.xml + 单位工程 xml)。
bills: [(编码, 名称, 特征, 单位, 工程量, 单价)] —— 分部分项清单(自动配 1 条定额 + 定额消耗)
@@ -89,8 +89,16 @@ def make_cos4(path, proj_name, bills, lmes=(), build_unit="测试建设单位",
quoteattr(it[4]), quoteattr(it[5]))
for it in (lmes if uw_lmes is None else uw_lmes))))
with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z:
z.writestr("Project.xml", proj.encode("utf-8"))
z.writestr("UW1.xml", uw.encode("utf-8"))
# zip_ts(决策 243 测试用):同内容不同 zip 时间戳 → 不同 sha256(前后台禁同文件后,
# 「同一份数据」要进两边就得是两个文件;解析结果不变,值断言不受影响)
if zip_ts is None:
z.writestr("Project.xml", proj.encode("utf-8"))
z.writestr("UW1.xml", uw.encode("utf-8"))
else:
for nm, data in (("Project.xml", proj.encode("utf-8")), ("UW1.xml", uw.encode("utf-8"))):
zi = zipfile.ZipInfo(nm, date_time=(2026, 1, 1, 0, 0, zip_ts))
zi.compress_type = zipfile.ZIP_DEFLATED
z.writestr(zi, data)
return path
@@ -248,6 +256,55 @@ def fixture_file(server, name, proj_name, bills, lmes=()):
return make_cos4(str(server["src_dir"] / name), proj_name, bills, lmes)
def twin_file(server, src_path, tag="twin"):
"""决策 243 测试用:把已有 zip 件重打包成**同内容不同哈希**的双胞胎件
(zip 时间戳平移),落 source_files 哈希命名——前后台禁同文件后,
同一份数据要进两边就用这一对(后台用原件、工作台用双胞胎)。"""
import hashlib
import io as _io
raw = open(src_path, "rb").read()
buf = _io.BytesIO()
with zipfile.ZipFile(_io.BytesIO(raw)) as zi, zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zo:
for nm in zi.namelist():
zinfo = zipfile.ZipInfo(nm, date_time=(2026, 1, 1, 0, 0, 7))
zinfo.compress_type = zipfile.ZIP_DEFLATED
zo.writestr(zinfo, zi.read(nm))
tw = buf.getvalue()
dst = os.path.join(str(server["root"]), "CostReview", "data", "source_files",
"%s_%s.cos4" % (hashlib.sha256(tw).hexdigest(), tag))
with open(dst, "wb") as f:
f.write(tw)
return dst
def drop_sessions(server, *names):
"""按工程名删台账(决策 242:导入入口同文件/同名一律拒绝——测试要重导同名,
必须先走真实删除接口清掉旧会话,顺带覆盖删除链路)。
删除是两段式:等后台清理线程把子表/备注清完再返回,避免毫秒级竞态。"""
import sqlite3
import time as _t
con = sqlite3.connect("file:%s?mode=ro" % server["sessdb"], uri=True)
try:
ids = [r[0] for n in names
for r in con.execute("SELECT id FROM sessions WHERE TRIM(name)=?", (n,))]
finally:
con.close()
if not ids:
return
assert post(server, "/api/session/delete", {"ids": ids})["ok"] is True
for _ in range(100): # 最多等 5 秒:子表行清零 = 后台清理完成
con = sqlite3.connect("file:%s?mode=ro" % server["sessdb"], uri=True)
try:
n = con.execute("SELECT COUNT(*) FROM s_bills WHERE session_id IN (%s)"
% ",".join("?" * len(ids)), ids).fetchone()[0]
finally:
con.close()
if n == 0:
return
_t.sleep(0.05)
raise AssertionError("后台清理超时:s_bills 仍有 %d 行" % n)
def price_xlsx_file(server, fname, rows, sheet="价格明细"):
"""造**标准模板** xlsx(2026-10-12 新口径,9 列表头严格)进待导入目录。