fix(adopt): 取消采纳改为物理删并修复源件锚与重定位

This commit is contained in:
lianjianwei2024
2026-11-12 18:31:20 +08:00
parent 2f6dd3e0ac
commit 9cab80feae
9 changed files with 156 additions and 58 deletions

View File

@@ -418,14 +418,16 @@ def match_bill(hist, row, base, kinds, units, classes, limit=20, srcs=None, pfro
# 口径变更由启动迁移 migrate_match_keys 重算存量,导入侧 import_job 同步。
sql = """SELECT '精确' tier, b.id, b.Number, b.Name, b.Attr, b.Unit, b.Price, bt.Name proj,
bt.x_period_start_m p1, bt.x_period_end_m p2, bt.FileKind, bt.BulidUnit, bt.x_src_hash src,
bt.x_province, bt.x_city, bt.x_district
bt.x_province, bt.x_city, bt.x_district, b.x_hash_exact bhash, b.batch_id batch_id
FROM bills b JOIN batches bt ON bt.id=b.batch_id
WHERE b.x_hash_exact=? AND bt.kind='project' AND %s LIMIT ?""" % w
for x in c.execute(sql, [row["x_hash_exact"]] + args + [limit]):
d = dict(x); out.append(d); seen_ids.add(d["id"])
# L1 次精确:编码+单位(名称护栏 = SEMI_NAME_MIN,挡同码不同物)
if len(out) < limit:
sql2 = sql.replace("x_hash_exact", "x_hash_semi").replace("'精确'", "'次精确'")
sql2 = sql.replace("WHERE b.x_hash_exact=?", "WHERE b.x_hash_semi=?").replace("'精确'", "'次精确'")
# ⚠️ 只换 WHERE 条件——SELECT 里的 b.x_hash_exact bhash 必须保留 exact 哈希:前端采纳重定位(279 延伸)
# 按批次+x_hash_exact 查新 id,bhash 若随档位变 semi 值就永远命不中。
seen = {(x["Name"], x["Attr"]) for x in out}
for x in c.execute(sql2, [row["x_hash_semi"]] + args + [limit]):
d = dict(x)
@@ -442,7 +444,7 @@ def match_bill(hist, row, base, kinds, units, classes, limit=20, srcs=None, pfro
if len(out) < limit and not unit_free:
sqlk = """SELECT '字集' tier, b.id, b.Number, b.Name, b.Attr, b.Unit, b.Price, bt.Name proj,
bt.x_period_start_m p1, bt.x_period_end_m p2, bt.FileKind, bt.BulidUnit, bt.x_src_hash src,
bt.x_province, bt.x_city, bt.x_district
bt.x_province, bt.x_city, bt.x_district, b.x_hash_exact bhash, b.batch_id batch_id
FROM bills b JOIN batches bt ON bt.id=b.batch_id
WHERE b.x_setkey=? AND bt.kind='project' AND %s LIMIT ?""" % w
for x in c.execute(sqlk, [bill_setkey(row["Name"], row["Unit"])] + args + [limit]):
@@ -458,7 +460,7 @@ def match_bill(hist, row, base, kinds, units, classes, limit=20, srcs=None, pfro
if q:
sql3 = """SELECT b.id, b.Number, b.Name, b.Attr, b.Unit, b.Price, bt.Name proj,
bt.x_period_start_m p1, bt.x_period_end_m p2, bt.FileKind, bt.BulidUnit, bt.x_src_hash src,
bt.x_province, bt.x_city, bt.x_district
bt.x_province, bt.x_city, bt.x_district, b.x_hash_exact bhash, b.batch_id batch_id
FROM bills b JOIN batches bt ON bt.id=b.batch_id
WHERE bt.kind='project' AND %s AND b.x_name_norm LIKE ? LIMIT 300""" % w
for x in c.execute(sql3, args + ["%" + q + "%"]):
@@ -487,6 +489,7 @@ def match_bill(hist, row, base, kinds, units, classes, limit=20, srcs=None, pfro
round(d["_sim"], 2) if "_sim" in d else
round(jaccard(qn, norm_match(d["Name"])), 2)), # 次精确=真实名称相似度(决策 27 E)
"src": "工程", "file": "", "src_hash": d.get("src") or "",
"bhash": d.get("bhash") or "", "batch_id": d.get("batch_id") or 0, # 279 延伸:采纳传 批次+内容哈希,id 失效可重定位
})
res = [r for r in res if _in_period(r, pfrom, pto)]
res = _self_filter(res, self_name, self_src) # 排除"被审工程自己"(同一文件/同名项目)
@@ -553,7 +556,7 @@ def hist_search(hist, kinds, units, area, pfrom, pto, kw, limit=200, self_name=N
order = ("kwrank, " if rank else "") + "bt.x_period_end_m DESC, bt.id DESC"
sql = """SELECT b.id, b.Name, b.Attr, b.Unit, b.Price, bt.Name proj, bt.BulidUnit bunit, bt.FileKind,
bt.x_province, bt.x_city, bt.x_district,
bt.x_period_start_m p1, bt.x_period_end_m p2, %s
bt.x_period_start_m p1, bt.x_period_end_m p2, b.x_hash_exact bhash, b.batch_id batch_id, %s
FROM bills b JOIN batches bt ON bt.id=b.batch_id
WHERE %s
ORDER BY %s LIMIT ? OFFSET ?""" % (rank_col, " AND ".join([cond] + extra), order)
@@ -578,6 +581,7 @@ def hist_search(hist, kinds, units, area, pfrom, pto, kw, limit=200, self_name=N
"fkind": x["FileKind"], "region": _region3(x["x_province"], x["x_city"], x["x_district"]),
"period_m": p2 if p2 not in (None, "") else p1,
"dist": 0, "sim": None, "src": "工程", "file": "", "src_hash": "",
"bhash": x["bhash"] or "", "batch_id": x["batch_id"] or 0, # 279 延伸:采纳传 批次+内容哈希,id 失效可重定位
})
return res

View File

@@ -529,7 +529,10 @@ def connect():
"x_price0 REAL",
# 决策 281(2026-11-07 主人定「取消采纳=本清单恢复导入时未编辑的状态」):
# 源件合价锚——取消采纳回滚 Total 用(单价回 x_price0);老库补列后重开工程生效
"x_total0 REAL")),
"x_total0 REAL",
# 决策 304 补记十二:源件原值锚(取消恢复 Number/Name/Attr/Unit=源件原貌;
# 老库补列 NULL=不猜保持现值,重开工程生效)
"x_number0 TEXT", "x_name0 TEXT", "x_attr0 TEXT", "x_unit0 TEXT")),
# 2026-10-08:工料机审核备注(采纳写入,持久副本 s_remarks rkey 前缀 'L:')
# + 次精确档(md5 名称+单位,与历史库 lmes 同口径)
("s_lmes", ("x_remark TEXT", "x_hash_semi TEXT")),
@@ -793,6 +796,15 @@ def open_project(con, files, meta, job, sid=None):
bill_ids.append(cur.lastrowid)
stat["bill"] += 1
# 决策 304 补记十二:源件原值锚回填——Number/Name/Attr/Unit 的导入原值固化
# (取消采纳照锚恢复四字段=源件原貌;幂等:仅全 NULL 行回填,不覆盖已有锚)
# 列探测:夹具/老库无锚列时跳过回填(生产库 connect 补列保证有列)
_anc = {r[1] for r in cur.execute("PRAGMA table_info(s_bills)").fetchall()}
if {"x_number0", "x_name0", "x_attr0", "x_unit0"} <= _anc:
cur.execute("UPDATE s_bills SET x_number0=Number, x_name0=Name, x_attr0=Attr, x_unit0=Unit"
" WHERE session_id=? AND x_number0 IS NULL AND x_name0 IS NULL"
" AND x_attr0 IS NULL AND x_unit0 IS NULL", (sid,))
# 2026-09-29:补 Quantity / Total(源文件有,此前只写 Price,导致工作台定额子行的
# 工程量/合价永远是「—」)。元组顺序:idx, 编码, 名称, 单位, 工程量, 单价, 合价
# 定额入库,同时记下真实 id —— 消耗量要靠它挂(norm_res 里存的是本文件内下标)