feat(costreview): 升级导出克隆完整块、GCL 前缀公式与导入换行保留,并配套界面与规格更新
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
M3-1 后端服务:零依赖(Python 标准库 http.server)
|
||||
提供后台管理界面需要的接口,并托管前端页面。
|
||||
"""
|
||||
import io, os, re, json, sqlite3, urllib.parse, mimetypes, hashlib, time, threading, base64, sys
|
||||
import io, os, re, json, sqlite3, urllib.parse, mimetypes, hashlib, time, threading, base64, sys, zipfile
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
from itertools import combinations
|
||||
from collections import Counter, OrderedDict
|
||||
@@ -1690,13 +1690,13 @@ TBL = {
|
||||
# lme_proj=项目级工料机汇总(决策 40,2026-10-11):「项目工料机」标签两侧统一改读**项目级汇总**
|
||||
# (工作台 s_lme_proj / 后台 lme_proj),过滤规则不变(LME_LIST_WHERE);会话侧备注列
|
||||
# lme_remark=s_lme_proj.x_remark(采纳落点随之平移,见 _migrate_lme_adoption),批次侧恒 NULL。
|
||||
"session": {"proj": "sessions", "pname": "name", "sections": "s_sections",
|
||||
"session": {"proj": "sessions", "pname": "name", "sections": "s_sections", "src": "src_name",
|
||||
"uworks": "s_unit_works", "divides": "s_divides", "bills": "s_bills",
|
||||
"norms": "s_norms", "lmes": "s_lmes", "norm_res": "s_norm_res", "lme_res": "s_lme_res",
|
||||
"lme_lib": "s_lme_lib", "lme_proj": "s_lme_proj", "lme_uw": "s_lme_uw",
|
||||
"idcol": "session_id", "remark": "b.x_remark", "lme_remark": "x_remark",
|
||||
"normstate": 1, "lmemerge": 1},
|
||||
"batch": {"proj": "batches", "pname": "Name", "sections": "sections",
|
||||
"batch": {"proj": "batches", "pname": "Name", "sections": "sections", "src": "x_src_hash",
|
||||
"uworks": "unit_works", "divides": "divides", "bills": "bills",
|
||||
"norms": "norms", "lmes": "lmes", "norm_res": "norm_res", "lme_res": "lme_res",
|
||||
"lme_lib": "lme_lib", "lme_proj": "lme_proj", "lme_uw": "lme_uw",
|
||||
@@ -1712,7 +1712,35 @@ def _raw_tree(con, T, oid):
|
||||
% (T["sections"], T["idcol"]), (oid,)).fetchall()
|
||||
uws = con.execute("SELECT id, section_id, Number, Name, Total, Specialty FROM %s WHERE %s=?"
|
||||
% (T["uworks"], T["idcol"]), (oid,)).fetchall()
|
||||
uws = sorted(uws, key=lambda r: _natkey(r["Number"])) # 原文件顺序不靠谱,按编号自然序
|
||||
# 决策 103:单位工程顺序 = 源文件 Project.xml 的引用序(易达目录序)——
|
||||
# 编号自然序会打乱「A 系列 + 数字系列」混编工程的原有先后(前山河二期 81 栋实测:
|
||||
# 原生序 A1→A19/A20 → 数字 21→80 → 市政管网 0001→0004;自然序把数字排前、A10/A11 排 A2 前)。
|
||||
# 排序键:Project.xml 引用序(编号缺失的排最后、按自然序);源文件读不到/解析失败 → 退回自然序。
|
||||
nat = {r["id"]: _natkey(r["Number"]) for r in uws}
|
||||
key = {r["id"]: (0, nat[r["id"]]) for r in uws}
|
||||
try:
|
||||
srow = con.execute("SELECT %(src)s FROM %(pr)s WHERE id=?" %
|
||||
{"src": T["src"], "pr": T["proj"]}, (oid,)).fetchone()
|
||||
srcp = os.path.join(UPLOAD_DIR, (srow[0] or "").strip()) if srow and srow[0] else None
|
||||
if srcp and os.path.isfile(srcp):
|
||||
ptxt = None
|
||||
with zipfile.ZipFile(srcp) as zf:
|
||||
for i in zf.infolist():
|
||||
if i.filename.lower().endswith("project.xml"):
|
||||
ptxt = zf.read(i.filename).decode("utf-8", "replace")
|
||||
break
|
||||
if ptxt:
|
||||
seq = []
|
||||
for m in re.finditer(r"<UnitWorks\b[^>]*>", ptxt):
|
||||
c = re.search(r'Number="([^"]*)"', m.group(0))
|
||||
if c and c.group(1) not in seq:
|
||||
seq.append(c.group(1))
|
||||
for r in uws:
|
||||
key[r["id"]] = ((0, seq.index(r["Number"]), nat[r["id"]])
|
||||
if r["Number"] in seq else (1, 0, nat[r["id"]]))
|
||||
except Exception:
|
||||
pass # 源文件读不到/解析失败:退回编号自然序
|
||||
uws = sorted(uws, key=lambda r: key[r["id"]])
|
||||
by_sec, free = {}, []
|
||||
for u in uws:
|
||||
it = {"id": u["id"], "Number": u["Number"], "Name": u["Name"], "Total": u["Total"],
|
||||
@@ -2595,11 +2623,15 @@ def _cmp_bills(con, T, oid):
|
||||
"idc": T["idcol"], "remark": T.get("remark", "NULL")}, (oid,)).fetchall()
|
||||
|
||||
|
||||
def _inconsistent(rows, rowfn):
|
||||
def _inconsistent(rows, rowfn, sid=None):
|
||||
"""组价不一致:同名同规格(名称归一 + 特征归一)但单价不同的组(**两侧唯一实现**)。
|
||||
|
||||
⚠️ 必须在**已过滤**的 rows 上算(措施费、单价 0 已排除),否则 pmin/pmax 会被措施费拉高,
|
||||
得出虚高价差 —— 后台曾用 SQL 聚合值(含措施费),与工作台口径不一致。
|
||||
规格 20(2026-10-17 主人定):
|
||||
①组顺序在编辑过程**稳定不变**——顺序号存会话设置(incon_ord),新产生的组追加在最后;
|
||||
②全部组价一致后组**不消失**:组内清单数不变、consistent=1(前端目录组名变绿);
|
||||
编辑再产生价差 → 自动恢复原色。从未出现过价差的组不进目录。
|
||||
"""
|
||||
g = {}
|
||||
for r in rows:
|
||||
@@ -2607,16 +2639,37 @@ def _inconsistent(rows, rowfn):
|
||||
if not any(key):
|
||||
continue
|
||||
g.setdefault(key, []).append(r)
|
||||
ordmap = {}
|
||||
if sid:
|
||||
raw_o = _setting_get("session:%d" % sid, "incon_ord")
|
||||
if raw_o:
|
||||
try:
|
||||
ordmap = {tuple(k.split(chr(31))): int(v)
|
||||
for k, v in json.loads(raw_o).items()}
|
||||
except Exception:
|
||||
ordmap = {}
|
||||
nxt = (max(ordmap.values()) + 1) if ordmap else 1
|
||||
out = []
|
||||
for _key, rs in g.items():
|
||||
for key, rs in g.items():
|
||||
prices = {str(_col(x, "Price")) for x in rs}
|
||||
if len(prices) < 2:
|
||||
continue
|
||||
consistent = len(prices) < 2
|
||||
if key not in ordmap:
|
||||
if consistent:
|
||||
continue # 从未出现过价差的一致组:不进目录
|
||||
ordmap[key] = nxt
|
||||
nxt += 1
|
||||
out.append((ordmap[key], key, rs, consistent))
|
||||
if sid:
|
||||
_setting_set("session:%d" % sid, "incon_ord",
|
||||
json.dumps({chr(31).join(k): v for v, k, _r, _c in out}, ensure_ascii=False))
|
||||
out.sort(key=lambda t: t[0])
|
||||
groups = []
|
||||
for _o, key, rs, consistent in out:
|
||||
d = _grpstat(rs)
|
||||
d["rows"] = [rowfn(x) for x in rs]
|
||||
out.append(d)
|
||||
out.sort(key=lambda x: -(x.get("spread") or 0))
|
||||
return {"total": len(out), "groups": out, "extra": []}
|
||||
d["consistent"] = 1 if consistent else 0
|
||||
groups.append(d)
|
||||
return {"total": len(groups), "groups": groups, "extra": []}
|
||||
|
||||
|
||||
def api_session_groups(sid, kind):
|
||||
@@ -2632,7 +2685,7 @@ def api_session_groups(sid, kind):
|
||||
con.close()
|
||||
|
||||
if kind == "inconsistent":
|
||||
return _inconsistent(rows, BILLROW)
|
||||
return _inconsistent(rows, BILLROW, sid)
|
||||
|
||||
# ---- similar:v2 顺序管线(决策 29/30)——关键词层读「分组设置」表(global.grp_rules)----
|
||||
return _group_v2_output(rows)
|
||||
@@ -2794,11 +2847,9 @@ def api_norm_op(sc, body):
|
||||
def _gcl_of(bill_qty, unit):
|
||||
"""GCL 数值(规格 2026-10-16 工具栏节同款算法,采纳与行内编辑共用):
|
||||
清单工程量 ÷ 定额单位从左到右数字前缀(到字母处截断,如 10t→10);纯字母/无数字
|
||||
单位(t、m、空)则 GCL=清单工程量;清单工程量空按 0。round 6 位(源文件规律)。"""
|
||||
m = re.match(r"\s*(\d+(?:\.\d+)?)", unit or "")
|
||||
div = float(m.group(1)) if m else 1.0
|
||||
if div <= 0:
|
||||
div = 1.0
|
||||
单位(t、m、空)则 GCL=清单工程量;清单工程量空按 0。round 6 位(源文件规律)。
|
||||
单位前缀解析收口在核心层 job.unit_prefix(系数迁移同一把尺,口径不分叉)。"""
|
||||
div = job.unit_prefix(unit)
|
||||
return round((float(bill_qty) if bill_qty is not None else 0.0) / div, 6)
|
||||
|
||||
|
||||
@@ -3042,6 +3093,24 @@ def f_(x):
|
||||
return None
|
||||
|
||||
|
||||
def _gcl_coef_expr(norm_qty, bill_qty, coef=None, unit=None):
|
||||
"""规格 19(2026-10-17 主人定·采纳逻辑变化):采纳定额默认公式 = **GCL×系数**,
|
||||
系数 = 历史定额量 ÷ 历史GCL(GCL=历史清单量÷定额单位数字前缀;例:历史清单量 500、
|
||||
定额量 300 → GCL*0.6000)——采纳到本清单后定额量按比例跟随本清单工程量。
|
||||
系数**导入时已预存**(norms.x_qty_coef,主人定:导入算好、采纳直接用),优先取用;
|
||||
老数据缺失时现算兜底。历史清单量缺失/为 0/定额量缺失 → 退回裸 GCL(决策 90 口径)。
|
||||
2026-10-17 决策 106 修正:系数一律 **GCL 口径**(定额量÷历史GCL)。旧口径
|
||||
(定额量÷清单量)在定额单位带数字前缀(10m/100m…)时把单位换算双算两遍——
|
||||
GCL 求值已除过一次前缀,前山河 040501004001(10m 定额)导出量少 10 倍实测。
|
||||
现算兜底同样按 GCL 口径(传入单位)。"""
|
||||
if coef is None:
|
||||
nq, bq = f_(norm_qty), f_(bill_qty)
|
||||
if nq is None or bq in (None, 0):
|
||||
return "GCL"
|
||||
coef = nq / _gcl_of(bq, unit)
|
||||
return "GCL*%.4f" % coef
|
||||
|
||||
|
||||
def api_bill_adopt(sc, hc, body):
|
||||
"""采纳(数据落地)。body={session_id, bill_id, src_bill_id}
|
||||
9.1 新增清单(x_new=1):清单字段照入库项目覆盖、工程量0;定额整组替换(工程量=GCL公式/单价照带/合价0),
|
||||
@@ -3060,16 +3129,17 @@ def api_bill_adopt(sc, hc, body):
|
||||
return {"ok": False, "msg": "清单不存在(可能已被删除,请刷新)"}
|
||||
if sb["x_tdel"]:
|
||||
return {"ok": False, "msg": "临时删除状态不可以采纳(规格 9.2)——先取消临时删除"}
|
||||
bb = hc.execute("SELECT id, Number, Name, Attr, Unit, Price FROM bills WHERE id=?", (src_id,)).fetchone()
|
||||
bb = hc.execute("SELECT id, Number, Name, Attr, Unit, Price, Quantity FROM bills WHERE id=?", (src_id,)).fetchone()
|
||||
if not bb:
|
||||
return {"ok": False, "msg": "入库项目清单不存在(批次可能已删除)"}
|
||||
bnorms = hc.execute("""SELECT id, Number, Name, Unit, Quantity, Price, Total
|
||||
bnorms = hc.execute("""SELECT id, Number, Name, Unit, Quantity, Price, Total, x_qty_coef
|
||||
FROM norms WHERE bill_id=? ORDER BY id""", (src_id,)).fetchall()
|
||||
is_new = bool(sb["x_new"])
|
||||
# 决策 90(2026-10-17):新采纳的定额,工程量列默认挂公式 **GCL 原文**(=清单工程量÷单位数字前缀,
|
||||
# 纯字母单位=清单工程量;导出时按当时清单量求值——采纳后清单量再改,定额量跟着走)。
|
||||
# 库里 Quantity 留 NULL;9.1 新增清单清单量按 0 计 → GCL=0,导出侧批次回填口径不变。
|
||||
# 规格 19(2026-10-17 主人定·采纳逻辑变化):采纳定额工程量默认公式 = **GCL×系数**,
|
||||
# 系数 = 历史定额量 ÷ 历史清单量(例:历史清单量 500、定额量 300 → GCL*0.6000)——
|
||||
# 换本清单工程量后定额量按比例走。历史清单量缺失/为 0 / 定额量缺失 → 退回裸 GCL。
|
||||
new_price = bb["Price"]
|
||||
src_qty = f_(bb["Quantity"])
|
||||
if is_new:
|
||||
# 9.1 整体覆盖
|
||||
sc.execute("""UPDATE s_bills SET Number=?, Name=?, Attr=?, Unit=?, Price=?, Quantity=0,
|
||||
@@ -3079,8 +3149,9 @@ def api_bill_adopt(sc, hc, body):
|
||||
sc.execute("DELETE FROM s_norms WHERE bill_id=?", (bid,))
|
||||
for nn in bnorms:
|
||||
cur = sc.execute("""INSERT INTO s_norms(bill_id, Number, Name, Unit, Quantity, Price, Total,
|
||||
x_adopt, x_src_norm_id, qty_expr) VALUES(?,?,?,?,NULL,?,NULL,1,?,'GCL')""",
|
||||
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"], nn["id"]))
|
||||
x_adopt, x_src_norm_id, qty_expr) VALUES(?,?,?,?,NULL,?,NULL,1,?,?)""",
|
||||
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"], nn["id"],
|
||||
_gcl_coef_expr(nn["Quantity"], bb["Quantity"], nn["x_qty_coef"], nn["Unit"])))
|
||||
for rr in hc.execute("SELECT res_code, qty FROM norm_res WHERE norm_id=? ORDER BY id", (nn["id"],)):
|
||||
sc.execute("INSERT INTO s_norm_res(norm_id, res_code, qty) VALUES(?,?,?)",
|
||||
(cur.lastrowid, rr["res_code"], rr["qty"]))
|
||||
@@ -3091,8 +3162,9 @@ def api_bill_adopt(sc, hc, body):
|
||||
sc.execute("UPDATE s_norms SET x_tdel=1 WHERE bill_id=?", (bid,)) # 原有定额 → 临删
|
||||
for nn in bnorms:
|
||||
cur = sc.execute("""INSERT INTO s_norms(bill_id, Number, Name, Unit, Quantity, Price, Total,
|
||||
x_adopt, x_src_norm_id, qty_expr) VALUES(?,?,?,?,NULL,?,NULL,1,?,'GCL')""",
|
||||
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"], nn["id"]))
|
||||
x_adopt, x_src_norm_id, qty_expr) VALUES(?,?,?,?,NULL,?,NULL,1,?,?)""",
|
||||
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"], nn["id"],
|
||||
_gcl_coef_expr(nn["Quantity"], bb["Quantity"], nn["x_qty_coef"], nn["Unit"])))
|
||||
for rr in hc.execute("SELECT res_code, qty FROM norm_res WHERE norm_id=? ORDER BY id", (nn["id"],)):
|
||||
sc.execute("INSERT INTO s_norm_res(norm_id, res_code, qty) VALUES(?,?,?)",
|
||||
(cur.lastrowid, rr["res_code"], rr["qty"]))
|
||||
@@ -3654,9 +3726,27 @@ def api_search(body):
|
||||
row["x_hash_semi"] = job.md5(row["Name"], row["Unit"])
|
||||
else:
|
||||
row = sc.execute("SELECT * FROM %s WHERE id=?" % tbl, (rid,)).fetchone()
|
||||
# 规格 20(2026-10-17 主人定):组价不一致对比默认只显示**本组其他清单**的记录
|
||||
# (第 N 组 3 条 → 只显示另外 2 条)。本组 = 同名称集(x_name_norm/x_attr_norm)的
|
||||
# 其他会话清单;其历史记录 = 本批次里同编码同名的批次清单。
|
||||
scope_self = (body.get("scope") == "self_group") and typ == "bill"
|
||||
members = None # [(Number, Name), ...];None=非组价不一致对比
|
||||
batch_id = None
|
||||
if scope_self and typ == "bill" and row is not None and row["x_name_norm"] is not None:
|
||||
batch_id = s["x_batch_id"] if "x_batch_id" in s.keys() else None
|
||||
if batch_id:
|
||||
members = [(m["Number"] or "", m["Name"] or "", m["UWNum"] or "") for m in sc.execute(
|
||||
"""SELECT b.Number AS Number, b.Name AS Name, u.Number AS UWNum FROM s_bills b
|
||||
JOIN s_unit_works u ON u.id = b.uw_id
|
||||
WHERE b.session_id=? AND b.x_name_norm IS ? AND b.x_attr_norm IS ? AND b.id<>?""",
|
||||
(sid, row["x_name_norm"], row["x_attr_norm"], rid)).fetchall()]
|
||||
sc.close()
|
||||
if not row:
|
||||
return {"ok": False, "msg": "找不到这一行"}
|
||||
# 规格 18(2026-10-17 主人定):**组价不一致**清单的对比默认只看本项目——
|
||||
# 同源文件(本组各楼栋/部位)+ 同名项目;别的项目不展示;独立检索(工具栏查询)不受限。
|
||||
# 实现口令 scope=self_group:匹配时不再排除本项目(self_name/self_src 传空),
|
||||
# 拿到全量后按"本组其他清单"过滤(见 members/allowed)。
|
||||
# 历史匹配过滤词(全局设置):剥掉这些词后再算相似度
|
||||
sw = _setting_get("global", "match_stopwords")
|
||||
try:
|
||||
@@ -3690,8 +3780,18 @@ def api_search(body):
|
||||
else:
|
||||
res = match.match_bill(hist, row, base,
|
||||
body.get("file_kinds") or [], body.get("bulid_units") or [],
|
||||
body.get("classes") or [], limit, srcs, pf, pt, self_name, self_src,
|
||||
body.get("classes") or [], limit, srcs, pf, pt,
|
||||
None if scope_self else self_name,
|
||||
None if scope_self else self_src,
|
||||
area)
|
||||
# 规格 20:本组其他清单的批次记录(bill_id 集合)——组价不一致对比按它过滤
|
||||
allowed = None
|
||||
if scope_self and members is not None:
|
||||
allowed = set()
|
||||
for mnum, mname in members:
|
||||
for h in hist.execute("SELECT id FROM bills WHERE batch_id=? AND Number=? AND Name=?",
|
||||
(batch_id, mnum, mname)).fetchall():
|
||||
allowed.add(h[0])
|
||||
finally:
|
||||
hist.close()
|
||||
# 屏蔽词(2026-10-11 主人定):筛选栏输入,命中 名称/特征(规格)/单位 任一词的历史行不显示。
|
||||
@@ -3705,6 +3805,11 @@ def api_search(body):
|
||||
for r in res: # 单价类型码 → 名称(0/空=未标注 → 前端显示 —)
|
||||
fk = r.get("fkind")
|
||||
r["fkind_name"] = FILEKIND.get(fk) if fk not in (None, "", 0) else None
|
||||
if allowed is not None:
|
||||
# 规格 20(定稿):组价不一致对比默认只显示**本组其他清单**的历史记录
|
||||
# (第 N 组 3 条 → 只显示另外 2 条);别的项目/别的历史记录不展示。
|
||||
# 工具栏独立检索(/api/hist/search)不受限。被点清单自己的记录亦不在本组集合内。
|
||||
res = [r for r in res if r.get("bill_id") in allowed]
|
||||
return {"ok": True, "base_period": match.ym(pf) + ("~" + match.ym(pt) if pt != pf else ""),
|
||||
"rows": res, "total": len(res)}
|
||||
|
||||
@@ -4561,6 +4666,53 @@ def _ensure_cols(con, table, cols):
|
||||
print("补列失败 %s.%s:%s" % (table, name, e), flush=True)
|
||||
|
||||
|
||||
def _repair_session_exprs():
|
||||
"""决策 106 一次性修复:会话库存量采纳定额的 qty_expr 由**旧口径系数**(定额量÷清单量)
|
||||
生成,单位带数字前缀(10m/100m…)的定额导出量少 N 倍(前山河 040501004001 实测)。
|
||||
只重写**本软件自动生成形态**(GCL*±数字,恒 4 位小数)的公式——主人手改过的原文不动;
|
||||
系数取批次库 x_qty_coef(启动迁移已换算 GCL 口径),批次行缺失时按 GCL 口径现算兜底。
|
||||
幂等:修完的公式重算结果不变;s_settings 标记防每次启动空跑。"""
|
||||
pat = re.compile(r"^GCL\*-?\d+\.\d{4}$")
|
||||
try:
|
||||
if _setting_get("sys", "mig_session_expr_gcl"):
|
||||
return 0
|
||||
hc = conn()
|
||||
try:
|
||||
sc = sess.connect()
|
||||
try:
|
||||
rows = sc.execute("""SELECT id, x_src_norm_id, qty_expr FROM s_norms
|
||||
WHERE x_adopt=1 AND x_src_norm_id IS NOT NULL""").fetchall()
|
||||
fixes = []
|
||||
for r in rows:
|
||||
if not pat.match((r["qty_expr"] or "").strip()):
|
||||
continue
|
||||
n = hc.execute("SELECT Quantity, Unit, x_qty_coef, bill_id FROM norms WHERE id=?",
|
||||
(r["x_src_norm_id"],)).fetchone()
|
||||
if n is None:
|
||||
continue
|
||||
coef = n["x_qty_coef"]
|
||||
if coef is None:
|
||||
bq = hc.execute("SELECT Quantity FROM bills WHERE id=?",
|
||||
(n["bill_id"],)).fetchone()
|
||||
if n["Quantity"] is None or not bq or bq["Quantity"] in (None, 0):
|
||||
continue
|
||||
coef = n["Quantity"] / _gcl_of(bq["Quantity"], n["Unit"])
|
||||
fixes.append(("GCL*%.4f" % coef, r["id"]))
|
||||
if fixes:
|
||||
sc.executemany("UPDATE s_norms SET qty_expr=? WHERE id=?", fixes)
|
||||
sc.commit()
|
||||
finally:
|
||||
sc.close()
|
||||
finally:
|
||||
hc.close()
|
||||
_setting_set("sys", "mig_session_expr_gcl", "1")
|
||||
print("工程量公式口径修复:%d 条" % len(fixes), flush=True)
|
||||
return len(fixes)
|
||||
except Exception as e:
|
||||
print("!! 会话工程量公式口径修复失败:%s" % e, flush=True)
|
||||
return 0
|
||||
|
||||
|
||||
def _ensure_batch_schema():
|
||||
"""后台批次库的**唯一建表入口**(幂等,可安全重复执行)。
|
||||
|
||||
@@ -4582,7 +4734,17 @@ def _ensure_batch_schema():
|
||||
("Quantity", "REAL"), ("Total", "REAL"),
|
||||
# 源文件备注(清单节点 Remark 属性,2026-10-08;只显示在「原数据」)
|
||||
("src_remark", "TEXT")])
|
||||
_ensure_cols(c, "norms", [("Quantity", "REAL"), ("Total", "REAL")])
|
||||
_ensure_cols(c, "norms", [("Quantity", "REAL"), ("Total", "REAL"),
|
||||
# 规格 19:定额工程量系数(**GCL 口径** = 定额量÷历史GCL,
|
||||
# 导入时算好存库,采纳时直接取用生成 GCL*系数 公式;主人 2026-10-17 定)
|
||||
("x_qty_coef", "REAL")])
|
||||
# 存量批次一次性补算(幂等:只填 NULL;一条 SQL 集合运算,毫秒级无感)
|
||||
c.execute("""UPDATE norms SET x_qty_coef = ROUND(norms.Quantity /
|
||||
(SELECT b.Quantity FROM bills b WHERE b.id = norms.bill_id), 6)
|
||||
WHERE x_qty_coef IS NULL
|
||||
AND norms.Quantity IS NOT NULL
|
||||
AND EXISTS (SELECT 1 FROM bills b WHERE b.id = norms.bill_id
|
||||
AND b.Quantity IS NOT NULL AND b.Quantity != 0)""")
|
||||
_ensure_cols(c, "lmes", [("Quantity", "REAL"), ("Kind", "INTEGER")])
|
||||
# x_info / x_feat:项目级「基本信息」「项目特征信息」两张卡的数据(与 sessions 对齐)
|
||||
# x_parser_ver:导入时的解析器版本,用于启动比对「数据是否过期需重导」
|
||||
@@ -4596,9 +4758,23 @@ def _ensure_batch_schema():
|
||||
CASE WHEN kind='project' THEN ? ELSE ? END
|
||||
WHERE x_parser_ver IS NULL""",
|
||||
(job.PARSER_VER, price.PARSER_VER))
|
||||
# 决策 106(2026-10-17):x_qty_coef 旧口径(定额量÷清单量)→ **GCL 口径**
|
||||
# (定额量÷历史GCL,GCL=清单量÷定额单位数字前缀)一次性全量重算——旧值在
|
||||
# 10m/100m 类单位上把单位换算双算两遍(前山河 040501004001 导出量少 10 倍实测)。
|
||||
# 设置标记防重入(全表重算不该每次启动都跑);导入侧 import_job 已同口径写入。
|
||||
if not c.execute("SELECT 1 FROM settings WHERE k='mig_x_qty_coef_gcl'").fetchone():
|
||||
_rows = c.execute("""SELECT n.id, n.Quantity, n.Unit, b.Quantity
|
||||
FROM norms n JOIN bills b ON b.id = n.bill_id
|
||||
WHERE n.Quantity IS NOT NULL AND b.Quantity IS NOT NULL
|
||||
AND b.Quantity != 0""").fetchall()
|
||||
c.executemany("UPDATE norms SET x_qty_coef=? WHERE id=?",
|
||||
[(round(nq * job.unit_prefix(un) / bq, 6), _nid)
|
||||
for _nid, nq, un, bq in _rows])
|
||||
c.execute("INSERT OR REPLACE INTO settings(k,v,updated_at) VALUES('mig_x_qty_coef_gcl','1',datetime('now'))")
|
||||
c.commit()
|
||||
c.close()
|
||||
_BATCH_SCHEMA_ONCE = True
|
||||
_BATCH_SCHEMA_ONCE = True # 先落标志再跑会话修复——repair 里的 conn() 会重入本函数
|
||||
_repair_session_exprs()
|
||||
except Exception as e:
|
||||
# 2026-10-07:此前这里静默 pass —— 建表/补列失败会让后续所有接口莫名的报错,
|
||||
# 却查不到任何线索(overMaxMsg 同类教训)。至少要把失败喊出来。
|
||||
|
||||
Reference in New Issue
Block a user