feat(core): 工料机子级按单位工程源声明入库,不跨单位并集

This commit is contained in:
lianjianwei2024
2026-11-15 21:18:10 +08:00
parent 9bce08d685
commit 384498bf12
16 changed files with 1014 additions and 300 deletions

View File

@@ -138,15 +138,45 @@ def _consumption_by_code(sc, sid, uw_id=None, by_uw=False):
return out
def _children_map(sc, sid):
"""{母码: [(res_code, qty含量, name), ...]} 按 seq。"""
out = {}
for r in sc.execute("""SELECT p_code, res_code, qty, name FROM s_lme_res
WHERE session_id=? ORDER BY p_code, seq, id""", (sid,)):
out.setdefault(r["p_code"], []).append((r["res_code"], r["qty"], r["name"]))
def _children_map(sc, sid, uw_id=None):
"""{母码: [(res_code, qty含量, name), ...]} 按 seq。
决策 351(2026-11-15 主人定):工料机子级**按单位工程取本单位源声明**,行数/码/含量
逐行等于源声明;本单位没有→项目级声明;**不跨单位并集**。
uw_id 有值:本单位边 → 项目级边(NULL);两边都没有→该母码无子级。
uw_id=None:项目级视图——项目级边优先,没有则取**第一个有边的单位**做代表(仍是一份声明)。
"""
by_uw, proj = _children_maps(sc, sid)
if uw_id is not None:
mine = by_uw.get(int(uw_id)) or {}
return {p: (mine.get(p) or proj.get(p) or []) for p in set(mine) | set(proj)}
# 项目级视图:项目级边优先;没有的母码取第一个有该母码的单位的声明(不并集)
out = {p: list(rows) for p, rows in proj.items()}
for u in sorted(by_uw):
for p, rows in (by_uw.get(u) or {}).items():
if p not in out:
out[p] = list(rows)
return out
def _children_maps(sc, sid):
"""({uw_id: {母码: [(子码,含量,名)]}}, {母码: [...]})——一次读全,按单位分桶。
决策 351:边带 uw_id;项目级声明(uw_id NULL)单独一桶,单位桶里没有的母码才回退。
"""
by_uw, proj = {}, {}
try:
_cols = {x[1] for x in sc.execute("PRAGMA table_info(s_lme_res)")}
except Exception:
_cols = set()
_uw = "uw_id" if "uw_id" in _cols else "NULL AS uw_id" # 假库/老库无归属列 → 全按项目级
for r in sc.execute("""SELECT p_code, res_code, qty, name, %s FROM s_lme_res
WHERE session_id=? ORDER BY p_code, seq, id""" % _uw, (sid,)):
d = proj if r["uw_id"] is None else by_uw.setdefault(r["uw_id"], {})
d.setdefault(r["p_code"], []).append((r["res_code"], r["qty"], r["name"]))
return by_uw, proj
def _price_map(sc, sid):
"""{code: {name,spec,unit,kind,ntp,taxrate,taxp,qty,id,hide,x_remark}} 项目级现值快照(价格/名规单/备注真值)。
决策 219 起降为**兜底**(缺码/缺价才取)+行id/备注/隐藏标记来源;主取价源=单位级 _uw_price_rows。"""
@@ -167,21 +197,22 @@ def _uw_price_rows(sc, sid):
{code: [行dict(按 uw+成员 去重、id 序)]},行含 uw_id/名规单/类别/三价/六价扩展/flags。
项目级汇总(_dyn_rows uw_id=None)以这张表为分组基础——单位级先对,项目级才对。"""
out = {}
seen = set() # 2026-11-15 性能:去重改键控集合(原「列表线性 any」=O(n²))
for r in sc.execute("""SELECT id, uw_id, code, name, spec, unit, kind, ntp, taxrate, taxp,
ntp_org, ntp_expr, taxp_org, taxp_expr, x_flags
FROM s_lme_uw WHERE session_id=? AND COALESCE(x_adopt_hidden,0)=0
ORDER BY id""", (sid,)):
if not r["code"]:
continue
lst = out.setdefault(r["code"], [])
key = (str(r["uw_id"]), r["name"], r["spec"], r["unit"], r["kind"],
key = (r["code"], str(r["uw_id"]), r["name"], r["spec"], r["unit"], r["kind"],
r["ntp"], r["taxrate"], r["taxp"],
r["ntp_org"], r["ntp_expr"], r["taxp_org"], r["taxp_expr"], r["x_flags"])
if any(m.get("_mk") == key for m in lst):
if key in seen:
continue # 同单位同成员(字段全同)→ 合成一行
seen.add(key)
d = dict(r)
d["_mk"] = key
lst.append(d)
d["_mk"] = key[1:] # 与旧实现同形(码之外的那段键)
out.setdefault(r["code"], []).append(d)
return out
@@ -270,7 +301,7 @@ def dyn_rows(sc, sid, uw_id=None, keep_zero=False):
def _dyn_rows(sc, sid, uw_id=None, keep_zero=False):
kids = _children_map(sc, sid)
kids = _children_map(sc, sid, uw_id) # 决策 351:本单位源声明;无则项目级
pm = _price_map(sc, sid)
ov = _overrides(sc, sid)
uwp = _uw_price_rows(sc, sid) # 决策 219:主取价源=单位级(每单位每码认第一行)
@@ -315,17 +346,9 @@ def _dyn_rows(sc, sid, uw_id=None, keep_zero=False):
_pk(_pk(r and r["taxp"], p.get("taxp")), lb[1]))
def _one_price_set(ch):
"""同一母码可能按单位工程挂多套组成(ESCC:一处无后缀套、别处 -0003 换算套)——
母价 Σ 只取**第一套**(按子码基码去重),避免两套相加把台班单价翻倍;
数量传导仍用全量(变体子码各自得量)。"""
seen, out = set(), []
for c, q, nm in ch:
base = (c or "").split("-")[0]
if base in seen:
continue
seen.add(base)
out.append((c, q, nm))
return out
"""母价 Σ 的取数集(决策 351):边已按本单位源声明存(_children_map),
源声明几行就 Σ 几行;@1/-0001 都是独立码,不折基码、不再额外去重。"""
return list(ch)
def computed_mother(code, uw=None):
"""带子级母行 Σ 单价(除税,含税);任一子级无价→None(算不齐退行值)。"""
@@ -582,9 +605,14 @@ def _split_remap_by_uw(sc, sid):
def _qmap_build_all(sc, sid):
consume = _consumption_by_code(sc, sid, None, by_uw=True)
kids = _children_map(sc, sid)
by_uw, proj = _children_maps(sc, sid) # 决策 351:量传导按本单位源声明;无则项目级
remaps = _split_remap_by_uw(sc, sid)
return {u: _propagate(m, kids, remaps.get(u)) for u, m in consume.items()}
out = {}
for u, m in consume.items():
mine = by_uw.get(u) or {}
kids = {p: (mine.get(p) or proj.get(p) or []) for p in set(mine) | set(proj)}
out[u] = _propagate(m, kids, remaps.get(u))
return out
def qmap_all_cached(sc, sid):
@@ -620,7 +648,7 @@ def qmap_touch_uw(sc, sid, uw_id, pre_mt=None):
if ent is None:
return
try:
umap = _propagate(_consumption_by_code(sc, sid, uw_id), _children_map(sc, sid),
umap = _propagate(_consumption_by_code(sc, sid, uw_id), _children_map(sc, sid, uw_id),
_split_remap_by_uw(sc, sid).get(uw_id))
except Exception:
with _QLOCK: