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

@@ -770,18 +770,53 @@ def _norm_res(con, T, norm_id):
out.append([r["res_code"], name, spec, unit, kind,
r["qty"], ntp, taxp, 1 if r["has_sub"] else 0,
_rate])
return {"rows": out}
# 决策 351:把定额所属单位工程带回前端——子级组成按本单位源声明取,不跨单位并集
return {"rows": out, "uw": (_uw[0] if _uw and _uw[0] is not None else None)}
def _adopt_drop_rows(meta):
"""按会话 id 删采纳事件 + 失掉最后引用的附件出目录(只碰 history.db)。幂等。"""
"""按会话 id 删采纳事件 + 失掉最后引用的附件出目录(只碰 history.db)。幂等。
决策 242 补记(2026-11-15 主人定):附件双库引用计数——删会话后若同一 sha 还有别的
adopt_hits(别的会话)或活批次(inquiry_files.batch_id<>0),附件保留;最后一个引用
消失才删 batch_id=0 的保留行和磁盘文件。"""
hc = conn()
try:
try:
_ensure_cols(hc, "inquiry_files", [("x_retained", "INTEGER DEFAULT 0")])
except Exception:
pass
sids = sorted({int(m["sid"]) for m in meta if m.get("sid") is not None})
shas = []
if sids:
_ph = ",".join("?" * len(sids))
shas = [r[0] for r in hc.execute(
"SELECT DISTINCT sha256 FROM adopt_hits WHERE sid IN (%s)" % _ph, tuple(sids))]
rows = []
for m in meta:
rows += [x[0] for x in hc.execute("SELECT rowid FROM adopt_hits WHERE sid=?", (m["sid"],))]
if rows:
hc.executemany("DELETE FROM adopt_hits WHERE rowid=?", [(x,) for x in rows])
_adopt_purge_orphans(hc)
for sha in shas:
if not sha:
continue
if hc.execute("SELECT 1 FROM adopt_hits WHERE sha256=? LIMIT 1", (sha,)).fetchone():
continue # 别的会话还引用 → 保留
ret = hc.execute("SELECT stored_path FROM inquiry_files WHERE sha256=? AND batch_id=0",
(sha,)).fetchall()
if not ret:
continue
hc.execute("DELETE FROM inquiry_files WHERE sha256=? AND batch_id=0", (sha,))
if hc.execute("SELECT 1 FROM inquiry_files WHERE sha256=? LIMIT 1", (sha,)).fetchone():
continue # 活批次还持有同内容 → 文件留着
for r in ret:
if not r[0]:
continue
fp = os.path.join(DATA_DIR, r[0])
try:
if os.path.exists(fp):
os.remove(fp)
except OSError:
pass
hc.commit()
except Exception:
try:
@@ -1533,7 +1568,6 @@ def api_bill_adopt(sc, hc, body):
if not _new:
return mo.group(0)
return mo.group(0).replace('Number="%s"' % mo.group(1), 'Number="%s"' % _new, 1)
_adopted = []
for nn in bnorms:
# 决策322(2026-11-14 主人拍板):锚四列落地——差额法不变式的起点
# (Price=锚价+(现合计−锚合计);改价/含量编辑按 Δ=差额×含量 增量维护,见 _norm_price_cascade)。
@@ -1603,21 +1637,15 @@ def api_bill_adopt(sc, hc, body):
if _va is not None and _vt is not None:
sc.execute("UPDATE s_norms SET Price=?, x_lme_total=? WHERE id=?",
(_r2(float(_va) + (_cur_total - float(_vt))), _cur_total, cur.lastrowid))
_cf0 = nn["x_qty_coef"] if "x_qty_coef" in nn.keys() else None
if _cf0 in (None, 0) and bb["Quantity"] not in (None, 0):
_cf0 = float(nn["Quantity"] or 0) / float(bb["Quantity"])
_adopted.append((cur.lastrowid, float(_cf0 or 0)))
# 决策 333(2026-11-14 斗门莲洲实证):清单价=Σ(定额单位含量×定额价),定额价=
# 决策 333(2026-11-14/15 斗门莲洲实证):清单价=Σ(定额单位含量×定额价),定额价=
# 322 差额法价(上段已写);合价=清单价×清单量,量空/0→价、合价留空。
# 单位含量=定额求值量÷清单量(决策 333 2026-11-15 补正:不是 GCL 系数 x_qty_coef——
# 100m 定额的 x_qty_coef=1.0,直接乘会把 2123.17 当成每 m 单价;1.7201÷172.01=0.01)。
# 与显示 232 现算、导出写穿共用 _bill_price_333,杜绝「显示 30.93 / 库 2123.17」两套口径。
_qb = _pre["Quantity"]
_sp333, _ok333 = 0.0, False
for _nid2, _cf2 in _adopted:
_prow = sc.execute("SELECT Price FROM s_norms WHERE id=?", (_nid2,)).fetchone()
if _prow and _prow[0] is not None:
_sp333 += _cf2 * float(_prow[0])
_ok333 = True
if _ok333 and _qb not in (None, 0):
new_price = _r2(_sp333)
_new_total = None
if _qb not in (None, 0):
new_price, _new_total = _bill_price_333(sc, bid)
else:
new_price = None
# —— 清单字段统一口径(2026-11-07 主人定):编码/名称/项目特征/单位/工程量
@@ -1637,7 +1665,8 @@ def api_bill_adopt(sc, hc, body):
if _cq is not None and _cq != 0:
sets.append("Quantity=?"); vals.append(_cq) # 利旧
sets.append("Total=?")
vals.append(round(float(_cq) * (float(new_price) if new_price is not None else 0.0), 2))
vals.append(_new_total if _new_total is not None
else _r2(float(_cq) * (float(new_price) if new_price is not None else 0.0)))
else:
sets.append("Total=?"); vals.append(None) # 量空→合价空
sets.append("x_adopt=1")
@@ -1886,6 +1915,50 @@ def _r2(v):
(round(2.675, 2)=2.67),与界面/原软件的「四舍五入」口径不符。"""
return float(Decimal(str(v)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
def _bill_price_333(sc, bill_id):
"""决策 333(2026-11-15 主人定「清单价=Σ(定额单位含量×定额价)」):按**当前**定额价
现算清单单价/合价,采纳落库与 322 改价/改含量级联共用同一把尺(显示 232 现算=库=导出)。
单位含量=定额求值量÷清单量(formula_qty,GCL=清单原值、结果÷单位前缀;
不是 GCL 系数 x_qty_coef——100m 定额该系数=1.0,会把 2123.17 当成每 m 单价)。
未临删/未藏的定额参与;量空/0 或无一条可算定额 → (None, None)(276 空槽)。"""
have = _have_col_set(sc, "s_norms")
if not {"Quantity", "Price"} <= have:
return None, None
if "Quantity" not in _have_col_set(sc, "s_bills"):
return None, None
b = sc.execute("SELECT Quantity FROM s_bills WHERE id=?", (bill_id,)).fetchone()
if b is None:
return None, None
q = b["Quantity"]
if q in (None, 0):
return None, None
sel = ["Quantity", "Price", "Unit" if "Unit" in have else "NULL AS Unit",
"qty_expr" if "qty_expr" in have else "NULL AS qty_expr"]
where = ["bill_id=?"]
if "x_tdel" in have:
where.append("COALESCE(x_tdel,0)=0")
if "x_adopt_hidden" in have:
where.append("COALESCE(x_adopt_hidden,0)=0")
tot, ok = 0.0, False
for n in sc.execute("SELECT %s FROM s_norms WHERE %s"
% (", ".join(sel), " AND ".join(where)), (bill_id,)):
if n["Price"] is None:
continue
nq = None
if n["qty_expr"]:
nq, _msg = dyn_lme.formula_qty(n["qty_expr"], q, n["Unit"])
if nq is None:
nq = n["Quantity"]
if nq is None:
continue
tot += _r2(float(nq) * float(n["Price"]))
ok = True
if not ok:
return None, None
px = _r2(tot / float(q))
return px, _r2(px * float(q))
def _extend_adopt_leaves(sc, hc, sid, code, src_bill_id, _seen=None):
"""决策 304 补记四(主人定「一直延伸叶子节点」):反藏码的组成按**本次来源批次**重写
(p_code=码 的旧边整组替换为来源包内该码的边),链上子码的隐藏采纳行一并反藏显示,
@@ -2085,10 +2158,15 @@ def _ensure_batch_schema():
("Quantity", "REAL"), ("Total", "REAL"),
# 源文件备注(清单节点 Remark 属性,2026-10-08;只显示在「原数据」)
("src_remark", "TEXT")])
# 决策 349(2026-11-15 主人定):批次删除两段式——先 x_deleted=1 摘入口,明细后台清。
_ensure_cols(c, "batches", [("x_deleted", "INTEGER DEFAULT 0")])
_ensure_cols(c, "norms", [("Quantity", "REAL"), ("Total", "REAL"),
# 规格 19:定额工程量系数(**GCL 口径** = 定额量÷历史GCL,
# 导入时算好存库,采纳时直接取用生成 GCL*系数 公式;主人 2026-10-17 定)
("x_qty_coef", "REAL")])
# 决策 242 补记(2026-11-15 主人定):附件双库引用计数——批次删时若工作台仍引用,
# inquiry_files 保留一行 batch_id=0/x_retained=1;最后一个工作台引用消失才真删。
_ensure_cols(c, "inquiry_files", [("x_retained", "INTEGER DEFAULT 0")])
# 决策 279:原文块库(幂等;schema.sql 已含建表,这里保旧库也能有)
c.execute("""CREATE TABLE IF NOT EXISTS block_bank(
id INTEGER PRIMARY KEY, batch_id INTEGER, bill_id INTEGER, norm_id INTEGER,
@@ -2106,6 +2184,8 @@ def _ensure_batch_schema():
_ensure_cols(c, "norm_res", [("express", "TEXT")]) # 含量算式(换算调整式,补记十二)
_ensure_cols(c, "norms", [("x_adjust", "TEXT")]) # 换算调整记录(JSON,补记十三)
_ensure_cols(c, "lmes", [("Quantity", "REAL"), ("Kind", "INTEGER")])
# 决策 351(2026-11-15 主人定):工料机子级按单位工程原样存——批次库 lme_res 补边归属
_ensure_cols(c, "lme_res", [("uw_id", "INTEGER")])
# 决策 244(2026-11-01):四张汇总卡——单位工程表补 代号/单位/建设规模/总造价 四列
_ensure_cols(c, "unit_works", [("Code", "TEXT"), ("Unit", "TEXT"),
("Scale", "REAL"), ("ztotal", "REAL")])
@@ -2149,5 +2229,13 @@ def conn():
c = sqlite3.connect(DB, timeout=30.0)
c.row_factory = sqlite3.Row
c.execute("PRAGMA busy_timeout=30000")
# 2026-11-15 性能(主人拍板「开干」):批次库已 985MB,默认 2MB 页缓存下导入一份
# 71.9MB 大件要写 10s 级。这里**只加大页缓存与内存临时表**——journal_mode(本库已是 WAL)
# 与 synchronous(保持默认 FULL)都不动:history.db 是长期资产,每次提交照旧完整落盘。
try:
c.execute("PRAGMA cache_size=-131072") # 128MB 页缓存
c.execute("PRAGMA temp_store=MEMORY")
except Exception:
pass
return c