fix(export): 措施清单嵌章节节点导出改价丢失——按编码兜底配对;定额工程量界面改为永远显示数字(决策 96/97)

This commit is contained in:
lianjianwei2024
2026-10-17 15:38:40 +08:00
parent a4772bc97b
commit 9269541ee6
5 changed files with 139 additions and 28 deletions

View File

@@ -231,23 +231,70 @@ def transform_unit(txt, uwkey, pairs, news, norms_by_bill, res_by_norm):
news = {"item": [bill...], "prelim": [...]} # x_new 行(bill["_anchor"]=锚点源序位)
norms_by_bill = {bill_id: {"source": [norm...], "append": [norm...]}} # 与文件/插入序一致
res_by_norm = {norm_id: [(code, qty), ...]}
"""
决策 97:配对两段式——①按 XML 标签数配对(原有主路径);②数对不上时按 **编码(Number+Name)
在文档序里认领** 兜底。起因:珠海等格式把措施清单用 <WorkElement> 标签嵌在措施章节节点里
(ChapterKind=4),解析正确归为 prelim,但按标签数配对 139 块 vs 108+31 必然失衡 →
整文件跳过、采纳/改价/汇总同步全丢(金湾区 8#楼实测)。兜底有一个编码认领不上就放弃,
维持「整组不动」护栏,绝不硬凑。"""
def uv(mk):
return (_attr(mk["uw"], "Number"), _attr(mk["uw"], "Name"))
edits = [] # (start, end, replacement)
blocks = _scan_bills(txt)
norm_tmpl = _full_norm_template(txt)
CLOSE = {"WorkElement": _CLOSE_WE, "UnitWorksPreliminaryWorkElement": _CLOSE_PRE}
# ---- ① 按标签数配对(主路径)----
paired = {} # kind -> [(源序位, bill, 文件块)]
left = {} # kind -> [(源序位, bill)] 标签数配不上的
fblocks_by = {} # kind -> 该标签的文件块(新增插入的锚点池)
for kind, tag in (("item", "WorkElement"), ("prelim", "UnitWorksPreliminaryWorkElement")):
pairs_k = pairs.get(kind) or []
fblocks = [mk for mk in blocks if mk["tag"] == tag and uv(mk) == uwkey]
fblocks_by[kind] = fblocks
if not pairs_k and not (news.get(kind) or []):
continue
if len(fblocks) == len(pairs_k):
paired[kind] = [(sp, b, mk) for (sp, b), mk in zip(pairs_k, fblocks)]
else:
left[kind] = list(pairs_k)
# ---- ② 编码兜底(决策 97):两种标签的块合并在文档序里按 (Number,Name) 认领 ----
if left:
want = {}
for kind, ks in left.items():
for sp, b in ks:
want.setdefault(((b.get("Number") or ""), (b.get("Name") or "")), []).append((kind, sp, b))
claimed = {id(mk) for lst in paired.values() for (_sp, _b, mk) in lst}
ok = True
for mk in [mk for mk in blocks if uv(mk) == uwkey]:
if id(mk) in claimed:
continue
k2 = ((_attr(mk["opentag"], "Number") or ""), (_attr(mk["opentag"], "Name") or ""))
q = want.get(k2)
if not q:
continue # 文件有、会话没有的块:不在改动范围,跳过
kind, sp, b = q.pop(0)
if not q:
del want[k2]
claimed.add(id(mk))
paired.setdefault(kind, []).append((sp, b, mk))
if any(want.values()):
ok = False # 有会话清单没在文件里找到:宁可不改,回滚兜底
if not ok:
for kind in left:
paired.pop(kind, None)
# ---- 动刀(主路径与兜底共用同一套编辑体)----
for kind, tag, close in (("item", "WorkElement", _CLOSE_WE),
("prelim", "UnitWorksPreliminaryWorkElement", _CLOSE_PRE)):
pairs_k = pairs.get(kind) or []
fblocks = [mk for mk in blocks if mk["tag"] == tag and uv(mk) == uwkey]
if not pairs_k and not (news.get(kind) or []):
continue
if len(fblocks) != len(pairs_k):
continue # 数对不上:这一组整体不动(护栏)
paired = list(zip(pairs_k, fblocks)) # [((源序位, bill), 文件块)]
for (srcpos, b), mk in paired:
plist = paired.get(kind) or []
if len(plist) != len(pairs_k):
continue # 护栏(含兜底放弃):整组不动
for srcpos, b, mk in plist:
if b["x_tdel"]:
edits.append((mk["s"], mk["end"], ""))
continue
@@ -303,19 +350,15 @@ def transform_unit(txt, uwkey, pairs, news, norms_by_bill, res_by_norm):
if ' NormIdentity="' in nsp["opentag"]:
tmpl = nsp["opentag"]
break
ci = mk["end"] - len(close)
ci = mk["end"] - len(CLOSE[mk["tag"]])
edits.append((ci, ci, "".join(_norm_xml(nn, res_by_norm.get(nn["id"], []), tmpl)
for nn in napp)))
# 新增清单:锚点块之后插入(锚点=源序位;同锚点按传入序依次跟随)
srcpos_of = {id(mk): sp for (sp, _b), mk in paired}
srcpos_of = {sp: mk for sp, _b, mk in plist}
for b in news.get(kind) or []:
tgt = None
for mk in fblocks:
if srcpos_of.get(id(mk)) == b.get("_anchor"):
tgt = mk
break
tgt = srcpos_of.get(b.get("_anchor")) or (plist[-1][2] if plist else None)
if tgt is None:
tgt = fblocks[-1]
continue
tmpl = norm_tmpl
for nsp in _norm_spans(txt[tgt["s"]:tgt["end"]]):
if ' NormIdentity="' in nsp["opentag"]: