Commit d54f0e09 authored by wenwen.tang's avatar wenwen.tang 😕

fix(tools): 合同导入按文件只落盘一次,多单元共用同一份

原先每个单元都调一次 contracts.save(),导致多单元合同(1101、1115 / 124.155 /
132&227 / 307&603 / 501-502 / 601A&702)在服务器上重复存了多份:
29 个源文件变成 35 份存储(127MB vs 源 93MB),也与「多行共享同一份」的设计相悖。
改为按文件名缓存 meta,同一份只落盘一次、多个单元挂同一个 id;
失败也按文件记一次,不再每个单元重复报。
parent 53b23899
...@@ -133,14 +133,20 @@ def main(): ...@@ -133,14 +133,20 @@ def main():
return 0 return 0
print("\n开始导入(%s)…" % ("覆盖" if args.replace else "追加")) print("\n开始导入(%s)…" % ("覆盖" if args.replace else "追加"))
updates, done, failed = [], 0, [] updates, done, failed, bad = [], 0, [], set()
cache = {} # 文件名 → meta:多单元共用同一份文件,不重复落盘
for p in plan: for p in plan:
fp = os.path.join(src, p["file"]) if p["file"] in bad:
continue
try: try:
raw = open(fp, "rb").read() if p["file"] not in cache:
meta = CT.save("data:application/octet-stream;base64," + base64.b64encode(raw).decode(), raw = open(os.path.join(src, p["file"]), "rb").read()
p["file"], "合同导入") cache[p["file"]] = CT.save(
"data:application/octet-stream;base64," + base64.b64encode(raw).decode(),
p["file"], "合同导入")
meta = cache[p["file"]]
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
bad.add(p["file"])
failed.append((p["file"], str(e))) failed.append((p["file"], str(e)))
continue continue
row = [r for r in t["rows"] if r.get("_id") == p["row_id"]][0] row = [r for r in t["rows"] if r.get("_id") == p["row_id"]][0]
...@@ -152,7 +158,8 @@ def main(): ...@@ -152,7 +158,8 @@ def main():
if updates: if updates:
res = tables.update_cells(tables.RENT_KEY, updates, who="合同导入") res = tables.update_cells(tables.RENT_KEY, updates, who="合同导入")
print("已写入 %d 处(其中内容有变化 %d 处)" % (len(updates), res["updated"])) print("已写入 %d 个单元(其中内容有变化 %d 处)" % (len(updates), res["updated"]))
print("落盘 %d 份文件(多单元共用同一份,不重复存)" % len(cache))
if failed: if failed:
print("\n失败:") print("\n失败:")
for fn, why in failed: for fn, why in failed:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment