fix(manual): 手工导出骨架与导入边界修复,单位按目录自然序排列

- 上传口骨架收集改为空操作,骨架仅经升级入库(决策 231/补记十四)
- s_lme_lib 价格快照垫底,修手工会话组成子行价格落 0
- 导出编辑相交消解:删除块与闭合前插入冲突时重映射,防采纳定额丢失/碎 XML
- _span 识别自闭合标签,防容器配对失败留孤儿闭合
- 手工导入放开空名/同名/深层级校验
- 单位顺序改按左侧目录树序(单项入库序+组内编号自然序)
- 补齐手工导出/导入/MCP 切换相关测试
This commit is contained in:
lianjianwei2024
2026-10-28 16:20:34 +08:00
parent 34b14e74ec
commit 8250a265fb
29 changed files with 4033 additions and 1128 deletions

View File

@@ -41,8 +41,8 @@ import price_xlsx # 信息价 / 市场价 Excel 解析器(2026-1
import session_job as sess # 会话库(工作台)
import dyn_lme # 决策 188:动态工料机汇总引擎(方案A 现算,2026-10-24)
import lme_skeleton # 决策 197:工料机骨架库(后缀+字段签名组,代表件大骨架)
import classic_skeleton # 决策 229:经典整件骨架库(手工工程导出模板,连保留值存)
import export_manual # 决策 229:手工工程导出 cos/cos4(经典骨架当模板填空)
import classic_skeleton # 决策 231:经典整件骨架库(手工工程导出模板,连保留值存)
import export_manual # 决策 231:手工工程导出 cos/cos4(经典骨架当模板填空)
import match # 比对引擎(M4)
import export_cos as xcos # 导出回写(批次D,决策 70)
import formats # 导出格式适配器(2026-10-19:SWT/SWZ 骨架/填空)
@@ -554,7 +554,7 @@ def api_upload(body):
_sk.close()
except Exception:
pass
# 决策 229:经典整件骨架收集——后台导入与工作台打开工程同走本口(失败绝不挡上传)
# 决策 231:经典整件骨架收集——后台导入与工作台打开工程同走本口(失败绝不挡上传)
if classic_skeleton.suffix_of(name):
try:
_sk = conn()
@@ -3593,6 +3593,7 @@ def _adopt_summary_rows(sc, hc, sid, uw_id, src_bill_id):
# 本会话该母码已有子级则不覆盖(先采纳的/本项目原的子级为准)。取消采纳回收时随行撤下。
if not sc.execute("SELECT 1 FROM s_lme_res WHERE session_id=? AND p_code=? LIMIT 1",
(sid, code)).fetchone():
_lib_need = set()
for r in hc.execute("""SELECT res_code, qty, seq, name FROM lme_res
WHERE batch_id=(SELECT batch_id FROM bills WHERE id=?)
AND p_code=? ORDER BY seq, id""", (src_bill_id, code)):
@@ -3600,6 +3601,21 @@ def _adopt_summary_rows(sc, hc, sid, uw_id, src_bill_id):
continue
sc.execute("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name) VALUES(?,?,?,?,?,?)",
(sid, code, r["res_code"], r["qty"], r["seq"], r["name"]))
_lib_need.add(r["res_code"])
# 组成子码的名称/规格/单位/类别/单价快照(批次 lme_lib → 会话 s_lme_lib):
# 手工会话没有源文件、s_lme_lib 是空的——导出时这些码要各有一行自己的价
# (2026-10-28 主人实测易达映射源里组成子行编制价全空;原生件铁证:同表自有行)
if _lib_need and hc.execute(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='lme_lib'").fetchone():
for lr in hc.execute("""SELECT code, name, spec, unit, kind, ntp, taxp FROM lme_lib
WHERE batch_id=(SELECT batch_id FROM bills WHERE id=?)
AND code IN (%s)""" % ",".join("?" * len(_lib_need)),
[src_bill_id] + sorted(_lib_need)):
sc.execute("""INSERT OR IGNORE INTO s_lme_lib
(session_id, code, name, spec, unit, kind, ntp, taxp)
VALUES(?,?,?,?,?,?,?,?)""",
(sid, lr["code"], lr["name"], lr["spec"], lr["unit"],
lr["kind"], lr["ntp"], lr["taxp"]))
return n, code_map
@@ -3775,6 +3791,26 @@ def api_bill_adopt(sc, hc, body):
if new and new != rr["res_code"]:
sc.execute("UPDATE s_norm_res SET res_code=? WHERE id=?", (new, rr["id"]))
n_recode += 1
# 决策 231 补记五(主人 2026-10-28 定「采纳全部数据,包含子工料机」):把批次里
# 台班/配合比的**下一级组成**(lme_res,批次库)补进会话 s_lme_res(会话库)——
# 本会话没有该码的组成才补(源文件工程解析时已带,不重复);手工工程从此有子级数据。
try:
_codes = [r[0] for r in sc.execute(
"SELECT DISTINCT nr.res_code FROM s_norm_res nr "
"JOIN s_norms n ON n.id=nr.norm_id "
"JOIN s_bills b ON b.id=n.bill_id WHERE b.session_id=?", (sid,))]
_have = {r[0] for r in sc.execute(
"SELECT DISTINCT p_code FROM s_lme_res WHERE session_id=?", (sid,))}
_todo = [c for c in _codes if c and c not in _have]
if _todo:
_ph = ",".join("?" * len(_todo))
for r in hc.execute(
"SELECT p_code, res_code, qty, seq, name FROM lme_res "
"WHERE p_code IN (%s) ORDER BY p_code, seq, id" % _ph, _todo):
sc.execute("INSERT INTO s_lme_res(session_id,p_code,res_code,qty,seq,name) "
"VALUES(?,?,?,?,?,?)", (sid, r[0], r[1], r[2], r[3], r[4]))
except Exception as _e:
print("!! 采纳补子级组成失败:%s" % _e, flush=True)
_pk = _pre.keys() if _pre is not None else []
_price0 = (_pre["x_price0"] if _pre is not None and "x_price0" in _pk and _pre["x_price0"] is not None
else (_pre["Price"] if _pre is not None else None))
@@ -3793,7 +3829,7 @@ _EXPORT_JOBS = {}
def _export_meta(sc, sid, fmt=""):
"""导出建议名(决策 92:先弹另存为再生成——对话框要预填 名字-审核版.原后缀,不能等生成完)。
决策 229:手工工程导出 fmt=cos4/cos/xlsx 三选(xlsx=每单位工程一个 Excel 的 zip 包)。"""
决策 231:手工工程导出 fmt=cos4/cos/xlsx 三选(xlsx=每单位工程一个 Excel 的 zip 包)。"""
srow = sc.execute("SELECT * FROM sessions WHERE id=?", (sid,)).fetchone()
if not srow:
return {"ok": False, "msg": "会话不存在"}
@@ -3807,6 +3843,8 @@ def _export_meta(sc, sid, fmt=""):
return {"ok": False, "msg": "手工工程导出请选格式:cos4 / cos / Excel"}
if not (srow["src_name"] or ""):
return {"ok": False, "msg": "没有源文件,无法按原格式导出"}
if (fmt or "").lower() == "cos4": # 补记十二:普通工程 cos4 也走经典骨架
return {"ok": True, "name": (proj_name or "工程") + "-审核版.cos4"}
ext = xcos._source_ext(srow["src_name"])
return {"ok": True, "name": (proj_name or "工程") + "-审核版" + ext}
@@ -3815,7 +3853,7 @@ def _export_session(sc, sid, fmt="", progress=None):
"""导出=拿原文件当模板外科手术。返回 (字节, 文件名, 错误)。
2026-10-19:SWT/SWZ 等新格式走 formats 适配器(骨架+填空);COS4/.COS 继续用
export_cos 的专用引擎(保留逐节点保真)。
决策 229:手工工程(无源文件)走经典整件骨架——fmt=cos4/cos 取骨架库代表件填空,
决策 231:手工工程(无源文件)走经典整件骨架——fmt=cos4/cos 取骨架库代表件填空,
fmt=xlsx 走 manual_import.export_excel_bytes(每单位工程一个 Excel 的 zip 包)。"""
srow = sc.execute("SELECT * FROM sessions WHERE id=?", (sid,)).fetchone()
if not srow:
@@ -3832,6 +3870,15 @@ def _export_session(sc, sid, fmt="", progress=None):
_h.close()
return None, None, "手工工程导出请选格式:cos4 / cos / Excel"
path = _src_path(srow["src_name"])
# 补记十二(2026-10-28 主人定「推翻:审核版不再基于原文件」):cos4 全量走经典骨架
# (骨架管导出、数据归软件);其余格式维持原格式导出。源文件在=按字段相近挑骨架,
# 不在也照导(挑最近收藏的骨架)。
if (fmt or "").lower() == "cos4":
_h = conn()
try:
return export_manual.export(sc, _h, sid, "cos4", progress, src_path=path)
finally:
_h.close()
if not path or not os.path.exists(path):
return None, None, "原文件已不在软件里(可能被清理),无法导出"
try:
@@ -5199,6 +5246,8 @@ def api_schema():
{"name*": "工程名", "period_from*": "年*12+月", "period_to": "止期次",
"tree*": "[{name, units:[{name, file?}]}],file=上传的 xlsx 路径(整单位工程覆盖)"},
{"session_id": 5, "imported": "[{uw,n_div,n_item,n_prelim}]"}),
r("GET", "/api/session/manual/export_unit", "单单位工程导 xlsx(决策 233;格式=231 Excel 通道单文件,可回导)",
{"session_id*": "会话id", "uw*": "单位工程id"}),
],
"查询": [
r("GET", "/api/session/raw/tree", "单位工程树",
@@ -5254,9 +5303,9 @@ def api_schema():
r("GET", "/api/session/edit/log", "字段编辑历史(红三角批注)"),
],
"导出": [
r("GET", "/api/session/export", "导出审核版(zip 流;文件名=工程名-审核版.原后缀;手工工程 fmt=cos4/cos/xlsx 三选,决策 229)",
r("GET", "/api/session/export", "导出审核版(zip 流;文件名=工程名-审核版.原后缀;手工工程 fmt=cos4/cos/xlsx 三选,决策 231)",
{"session_id*": "", "token": "可选:进度上报令牌(决策 92)", "fmt": "可选:手工工程导出格式 cos4/cos/xlsx"}),
r("GET", "/api/session/export/meta", "导出建议名(先弹另存为再生成,决策 92;手工工程带 fmt,决策 229)",
r("GET", "/api/session/export/meta", "导出建议名(先弹另存为再生成,决策 92;手工工程带 fmt,决策 231)",
{"session_id*": "", "fmt": "可选:手工工程导出格式 cos4/cos/xlsx"}),
r("GET", "/api/session/export/progress", "导出逐条目进度(轮询,决策 92)",
{"token*": ""}),
@@ -7813,6 +7862,22 @@ class Handler(SimpleHTTPRequestHandler):
self.send_header("Content-Disposition", self._cd(fn, "manual_import_template.xlsx"))
self.end_headers()
self.wfile.write(raw)
if p == "/api/session/manual/export_unit": # 手工工程:单单位工程导 xlsx(决策 233 方案a)
con = sess.connect()
try:
raw, fn, err = mimp.export_unit_xlsx(
con, int(one.get("session_id", 0) or 0), int(one.get("uw", 0) or 0))
finally:
con.close()
if err or not raw:
return self._json({"error": err or "导出失败"}, 400)
self._allow_cache = True # 下载不能带 no-store(同模板下载)
self.send_response(200)
self.send_header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
self.send_header("Content-Length", str(len(raw)))
self.send_header("Content-Disposition", self._cd(fn or "单位工程.xlsx", "manual_unit_export.xlsx"))
self.end_headers()
self.wfile.write(raw)
# ---- 询价附件(项目工料机专用)----
m = re.match(r"^/api/admin/batch/(\d+)/inq/lmes$", p) # 该项目全部工料机 + 绑定状态
if m: