feat(lme): 恢复定额按决策316刷新工料机数据链并支持合并后同码单行导出

This commit is contained in:
lianjianwei2024
2026-11-13 11:14:37 +08:00
parent 8e0f3b31dc
commit 5d86c95402
3 changed files with 100 additions and 0 deletions

View File

@@ -3279,6 +3279,80 @@ def _bill_rehash(sc, bid):
job.md5(num9, row["Unit"] or ""), bid))
def _restore_refresh_chain(sc, sid, uw_id, norm_ids, hc=None):
"""决策 316(2026-11-13,主人定):恢复(取消临时删除 59 / 定额恢复)=按**采纳方式**
刷新恢复范围的工料机数据链——三同按最新单价、属性值全部按最新(含量 qty 保留原值,
296「含量属结构」)、不能出现同码异物(295 三同匹配顺序+303/304 一码一身份约束)。
①恢复定额的耗码在本项目找同名规单行(本单位排除自身→其他单位首个,295 取码顺序)
→ 命中其他码 → 耗引用改挂到命中码(属性随命中行=工作台现值;含量不动)——
身份参照取码行,码行不在(被取消采纳物理删)退 s_lme_lib 快照(取消不删 lib);
②改挂后按显示口径刷新两码 qty=Σ 未临删定额耗量(临删不算,与显示一致);
③带子级母行复用 _mothers_reprice 按 288/300 Σ 子级回算;
④尾部 _chain_survey_repair 兜底(项目全无同名规单的码按 297 勘察补源/登记)。
返回 {remapped, requant};任何一步失败不挡恢复本身(恢复动作已先行完成)。"""
norm_ids = [int(n) for n in (norm_ids or []) if n]
if not norm_ids:
return {"remapped": [], "requant": []}
_ph = ",".join("?" * len(norm_ids))
codes = [r[0] for r in sc.execute(
"""SELECT DISTINCT res_code FROM s_norm_res WHERE norm_id IN (%s)
AND res_code IS NOT NULL AND res_code<>''""" % _ph, norm_ids).fetchall()]
remapped, touched = [], set()
for c in codes:
# 身份参照:码行 → lib 快照 →(都无则无从判三同,留给勘察④)
row = sc.execute(
"SELECT name, spec, unit FROM s_lme_uw WHERE session_id=? AND code=? ORDER BY id LIMIT 1",
(sid, c)).fetchone()
if row is None:
row = sc.execute(
"SELECT name, spec, unit FROM s_lme_lib WHERE session_id=? AND code=? LIMIT 1",
(sid, c)).fetchone()
if row is None:
continue
ident = tuple("" if v is None else v for v in (row["name"], row["spec"], row["unit"]))
# 295 取码顺序:本单位(排除自身)→ 其他单位,各取首个
hit = sc.execute(
"""SELECT code FROM s_lme_uw WHERE session_id=? AND uw_id=? AND code<>?
AND COALESCE(name,'') IS ? AND COALESCE(spec,'') IS ? AND COALESCE(unit,'') IS ?
ORDER BY id LIMIT 1""", (sid, uw_id, c) + ident).fetchone()
if hit is None:
hit = sc.execute(
"""SELECT code FROM s_lme_uw WHERE session_id=? AND uw_id<>? AND code<>?
AND COALESCE(name,'') IS ? AND COALESCE(spec,'') IS ? AND COALESCE(unit,'') IS ?
ORDER BY uw_id, id LIMIT 1""", (sid, uw_id, c) + ident).fetchone()
if hit is None:
continue
nc = hit["code"]
sc.execute(
"UPDATE s_norm_res SET res_code=? WHERE norm_id IN (%s) AND res_code=?" % _ph,
[nc] + norm_ids + [c])
remapped.append({"from": c, "to": nc})
touched.add(c)
touched.add(nc)
# ② qty 刷新(显示口径:未临删定额的耗量合计)
for c in sorted(touched):
sc.execute(
"""UPDATE s_lme_uw SET qty=(SELECT COALESCE(SUM(nr.qty),0) 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=? AND b.uw_id=s_lme_uw.uw_id AND nr.res_code=s_lme_uw.code
AND COALESCE(n.x_tdel,0)=0)
WHERE session_id=? AND code=?""", (sid, sid, c))
# ③ 带子级母行 Σ 回算(改挂码作为子码上溯;288 修/300 口径)
if touched:
try:
_mothers_reprice(sc, sid, sorted(touched), as_children=True)
except Exception:
pass
# ④ 勘察兜底(缺源补行/登记;hc=批次库连接,获取失败不挡恢复)
try:
if hc is None:
hc = conn()
_chain_survey_repair(sc, sid, hc)
except Exception:
pass
return {"remapped": remapped, "requant": sorted(touched)}
def api_bill_op(sc, body):
"""工作台清单操作(批次A 五个动作 + 行内编辑),body={op, bill_id, fields?, session_id?}。
返回 ok=False 时带 msg(前端打提示);成功带回 sid 供路由层失效分组缓存。"""
@@ -3339,8 +3413,14 @@ def api_bill_op(sc, body):
# 无采纳的清单全部恢复。
if sc.execute("SELECT 1 FROM s_norms WHERE bill_id=? AND x_adopt=1 LIMIT 1", (bid,)).fetchone():
sc.execute("UPDATE s_norms SET x_tdel=0 WHERE bill_id=? AND x_adopt=1", (bid,))
_rn = [x[0] for x in sc.execute(
"SELECT id FROM s_norms WHERE bill_id=? AND x_adopt=1", (bid,)).fetchall()]
else:
sc.execute("UPDATE s_norms SET x_tdel=0 WHERE bill_id=?", (bid,))
_rn = [x[0] for x in sc.execute(
"SELECT id FROM s_norms WHERE bill_id=?", (bid,)).fetchall()]
# 决策 316:恢复=按采纳方式刷新恢复范围工料机链(三同最新价/属性最新/含量除外)
_restore_refresh_chain(sc, sid, r["uw_id"], _rn)
return {"ok": True, "sid": sid}
if op == "edit":
fields = body.get("fields") if isinstance(body.get("fields"), dict) else {}
@@ -3410,6 +3490,8 @@ def api_norm_op(sc, body):
return {"ok": True, "sid": sid}
if op == "unntdel":
sc.execute("UPDATE s_norms SET x_tdel=0 WHERE id=?", (nid,))
# 决策 316:恢复=按采纳方式刷新恢复范围工料机链(三同最新价/属性最新/含量除外)
_restore_refresh_chain(sc, sid, b["uw_id"], [nid])
return {"ok": True, "sid": sid}
if op == "unadopt":
if not n["x_adopt"]: