Files
pricing_review/CostReview/app/core/integrity.py

92 lines
3.3 KiB
Python
Raw 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 -*-
"""决策 292:工料机引用完整性(全格式一条方法)。
源集 = 全项目工料机汇总源行;引用集 = 定额消耗 / 组成子级 / 汇总子级里的引用。
导入端清孤儿在 import_job.save;本模块负责导出端最后一道闸:
最终引用地仍有码找不到源 → ExportIntegrityError,拦截导出,绝不补占位。
格式只差标签:
COS 源 LabourMaterialsEquipmentsMachinesSummary@Number
引用 LabourMaterialsEquipmentsMachinesElement@Number
ESCC 源 ResourceItem@Code
引用 ResElementItem@ResCode
SWT 源 ResourceItem@Code
引用 NormResUsageItem@ResourceID(按单位 Resource.ID→Code 映射)
"""
import re
class ExportIntegrityError(RuntimeError):
"""导出工料机引用完整性闸门:引用地有码、全项目源集没有。"""
_SUM = re.compile(r'<LabourMaterialsEquipmentsMachinesSummary\b[^>]*?\bNumber="([^"]*)"')
_ELEM = re.compile(r'<LabourMaterialsEquipmentsMachinesElement\b[^>]*?\bNumber="([^"]*)"')
def cos_sets(txt):
src = {m.group(1) for m in _SUM.finditer(txt) if m.group(1)}
refs = {m.group(1) for m in _ELEM.finditer(txt) if m.group(1)}
return src, refs
def escc_sets(txt):
src = {m.group(1) for m in re.finditer(
r'<ResourceItem\b[^>]*?\bCode="([^"]*)"', txt) if m.group(1)}
refs = {m.group(1) for m in re.finditer(
r'<ResElementItem\b[^>]*?\bResCode="([^"]*)"', txt) if m.group(1)}
return src, refs
def swt_sets(txt):
"""SWT:ResourceItem@Code 是源;NormResUsageItem@ResourceID 是引用地,
Resource@ID→Code 是映射。映射不全时只校验能映射的引用,不误杀。"""
src = {m.group(1) for m in re.finditer(
r'<ResourceItem\b[^>]*?\bCode="([^"]*)"', txt) if m.group(1)}
id2code = {}
for m in re.finditer(r'<Resource\b[^>]*?>', txt):
ot = m.group(0)
rid = re.search(r'\bID="([^"]*)"', ot)
if not rid:
continue
seg = txt[m.end():m.end() + 6000]
cm = re.search(r'<ResourceItem\b[^>]*?\bCode="([^"]*)"', seg)
if cm:
id2code[rid.group(1)] = cm.group(1)
refs = set()
for m in re.finditer(r'<NormResUsageItem\b[^>]*?\bResourceID="([^"]*)"', txt):
rid = m.group(1)
if rid in id2code:
refs.add(id2code[rid])
return src, refs
def _raise_if_missing(kind, src, refs):
missing = sorted(refs - src)
if missing:
raise ExportIntegrityError(
"%s 导出引用完整性失败:引用地 %d 个码找不到全项目工料机源,缺 %d 个(例:%s)。"
"按决策 292 拦截导出,不补占位。" % (kind, len(refs), len(missing), "、".join(missing[:8])))
def validate_cos(txts, where=""):
src, refs = set(), set()
for t in txts:
s, r = cos_sets(t)
src |= s
refs |= r
_raise_if_missing("COS/COS4" + (" " + where if where else ""), src, refs)
return len(src), len(refs)
def validate_escc(txt, where=""):
src, refs = escc_sets(txt)
_raise_if_missing("ESCC/XML" + (" " + where if where else ""), src, refs)
return len(src), len(refs)
def validate_swt(txt, where=""):
src, refs = swt_sets(txt)
_raise_if_missing("SWT/SWZ" + (" " + where if where else ""), src, refs)
return len(src), len(refs)