# -*- coding: utf-8 -*- """合并工料机 v2 防回归(决策 171,2026-10-22 主人定:视野=单位工程×n+项目级)。 钉死六件事: ①分组:项目级 + 各单位级(按码聚合,标注 units/uw_ids)同 名称|规格|单位 归组; 组内 ≥2 个不同编码才成组——D800(@3,不同名)不成组、同码无处不在也不成组。 ②编码不可编辑:keep 里传 code 一律忽略,项目级行码原样不动(取代决策 75 编码可编辑)。 ③项目级隐藏:x_merged_hide=1;被隐藏码进变体表。 ④单位级变体:hide {uw, code, uw_ids} → 这些单位的该变体码物理换成组保留码; **无跳过**(单位同时持有保留码与变体码 → 同码双行,镜像源文件多行现实,主人定「只显示保留码」)。 ⑤定额消耗量:变体码**精确**换码;非变体(-0009 同基础码但不在组内)不误伤(防 D800 陷阱)。 ⑥子级组成:变体码的子级被保留行子级整字段替换;recode_map 含全部变体(导出件换码用)。 """ import sqlite3 import json import sys, os import pytest _HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "core")) sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "server")) import api_server as srv # noqa: E402 SID = 1 K = "17290080" # 保留码(编码最短 → 默认勾选) P = "17290070" # 项目级码(无后缀,源文件项目级形态) V15 = "17290070@15" # 单位级变体(源文件撞码号) V1 = "17290070@1" D800 = "17290070@3" # 不同名(D800)——基础码相同也绝不能误伤 def _mk_sess(): sc = sqlite3.connect(":memory:") sc.row_factory = sqlite3.Row c = sc.cursor() c.execute("""CREATE TABLE s_lme_proj(id INTEGER PRIMARY KEY, session_id INT, code TEXT, name TEXT, spec TEXT, unit TEXT, kind INT, qty REAL, ntp REAL, taxrate REAL, taxp REAL, x_adopt_added INTEGER DEFAULT 0, x_merged_hide INTEGER DEFAULT 0, x_remark TEXT, ntp_org REAL, ntp_expr REAL, taxp_org REAL, taxp_expr REAL)""") c.execute("""CREATE TABLE s_lme_uw(id INTEGER PRIMARY KEY, session_id INT, uw_id INT, code TEXT, name TEXT, spec TEXT, unit TEXT, kind INT, qty REAL, ntp REAL, taxrate REAL, taxp REAL, x_adopt_added INTEGER DEFAULT 0, ntp_org REAL, ntp_expr REAL, taxp_org REAL, taxp_expr REAL)""") c.execute("""CREATE TABLE s_lme_res(id INTEGER PRIMARY KEY, session_id INT, p_code TEXT, res_code TEXT, qty REAL, seq INTEGER, name TEXT)""") c.execute("""CREATE TABLE s_settings(scope TEXT NOT NULL, k TEXT NOT NULL, v TEXT, PRIMARY KEY(scope,k))""") c.execute("CREATE TABLE s_bills(id INTEGER PRIMARY KEY, session_id INT, uw_id INT)") c.execute("CREATE TABLE s_norms(id INTEGER PRIMARY KEY, bill_id INT)") c.execute("""CREATE TABLE s_norm_res(id INTEGER PRIMARY KEY, norm_id INT, res_code TEXT, qty REAL, express TEXT)""") # 项目级:K 保留码 + P 项目码(同 名称|规格|单位、价不同 → 成组);D800 另名(不成组) c.executemany("""INSERT INTO s_lme_proj(id, session_id, code, name, spec, unit, kind, qty, ntp, taxrate, taxp) VALUES(?,1,?,?, '', 'm',2, ?, ?, 0, ?)""", [(1, K, "II级钢筋混凝土管 D300", 11.286, 58.88, 58.88), (2, P, "II级钢筋混凝土管 D300", 617.11, 126.96, 126.96), (3, D800, "II级钢筋混凝土管 D800", 5.0, 90.0, 90.0)]) # 单位级:101/102 有 K、@15;102 另有 P;103 有 @1;101 有 D800 c.executemany("""INSERT INTO s_lme_uw(id, session_id, uw_id, code, name, spec, unit, kind, qty, ntp, taxrate, taxp) VALUES(?,1,?,?,?, '', 'm', 2, ?, ?, 0, ?)""", [(11, 101, K, "II级钢筋混凝土管 D300", 50.0, 58.88, 58.88), (12, 101, V15, "II级钢筋混凝土管 D300", 100.0, 126.96, 126.96), (13, 101, D800, "II级钢筋混凝土管 D800", 5.0, 90.0, 90.0), (14, 102, P, "II级钢筋混凝土管 D300", 200.0, 126.96, 126.96), (15, 102, V15, "II级钢筋混凝土管 D300", 300.0, 130.0, 130.0), (16, 102, K, "II级钢筋混凝土管 D300", 60.0, 58.88, 58.88), (17, 103, V1, "II级钢筋混凝土管 D300", 7.0, 99.0, 99.0)]) # 子级组成:K 有子级(保留行),@15 也有子级(变体合并后应以保留行子级为准) c.executemany("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq) VALUES(1,?,?,?,?)", [(K, "00010003", 2.0, 1), (V15, "99990001", 63.0, 1)]) # 定额消耗量:@15(变体,应换)、-0009(同基础码但非变体,不误伤)、K(本码,不动) c.executemany("INSERT INTO s_bills(id, session_id, uw_id) VALUES(?,?,?)", [(1, 1, 101), (2, 1, 102)]) c.executemany("INSERT INTO s_norms(id, bill_id) VALUES(?,?)", [(10, 1), (11, 2)]) c.executemany("INSERT INTO s_norm_res(norm_id, res_code, qty, express) VALUES(?,?,?,?)", [(10, V15, 1.5, None), (11, "17290070-0009", 2.0, None), (10, K, 0.5, None)]) sc.commit() return sc def _uw_codes(sc, uw): return sorted(x["code"] for x in sc.execute( "SELECT code FROM s_lme_uw WHERE session_id=? AND uw_id=?", (SID, uw))) def test_groups_unify_levels(): """①分组:D300 组 = 项目级 2 行 + 单位级按码聚合 4 行(K×2单位、@15×2单位、P×1、@1×1); D800 单码不成组;uw 聚合行带 units/uw_ids。""" sc = _mk_sess() d = srv._lme_merge_groups(sc, SID) g = [x for x in d["groups"] if x["name"] == "II级钢筋混凝土管 D300"] assert len(g) == 1 g = g[0] proj = [r for r in g["rows"] if r["lvl"] == "proj"] uws = [r for r in g["rows"] if r["lvl"] == "uw"] assert {r["code"] for r in proj} == {K, P} assert {r["code"] for r in uws} == {K, P, V15, V1} krow = [r for r in uws if r["code"] == K][0] assert krow["units"] == 2 and sorted(krow["uw_ids"]) == [101, 102] assert krow["qty"] == 110.0 # 50 + 60 v15row = [r for r in uws if r["code"] == V15][0] assert v15row["units"] == 2 and v15row["ntp"] == 126.96 # 2 条取低(126.96/130.0) assert not any(x["name"] == "II级钢筋混凝土管 D800" for x in d["groups"]) # 单码不成组 assert g["rows"][0]["def"] is True and g["rows"][0]["code"] == K # 默认勾编码最短 def test_merge_v2_unify_and_no_code_edit(): """②③④⑤⑥⑦:编码不收不改(传假码也原样);项目级隐藏;单位变体(含双码单位)全部换保留码 且三价统一为保留行三价;D800 与 -0009 不误伤;定额变体精确换码;子级替换 + recode_map 全量。""" sc = _mk_sess() r = srv.api_lme_merge(sc, {"session_id": SID, "groups": [{ "sel": {"id": 1, "ntp": 58.88, "taxrate": 0, "taxp": 58.88}, "hide": [{"id": 2}, {"uw": 1, "code": V15, "uw_ids": [101, 102]}, {"uw": 1, "code": V1, "uw_ids": [103]}, {"uw": 1, "code": P, "uw_ids": [102]}]}]}) assert r["ok"] is True # ② 编码不可编辑:传了 WRONG-EDITED-CODE 也原样 assert sc.execute("SELECT code, ntp FROM s_lme_proj WHERE id=1").fetchone()[0:2] == (K, 58.88) # ③ 项目级隐藏 assert sc.execute("SELECT COALESCE(x_merged_hide,0) FROM s_lme_proj WHERE id=2").fetchone()[0] == 1 # ④ 全单位只显示保留码:无跳过——单位同时持有保留码与变体码时为同码双行(镜像源文件多行现实) assert _uw_codes(sc, 101) == sorted([K, K, D800]) assert _uw_codes(sc, 102) == [K, K, K] assert _uw_codes(sc, 103) == [K] # ⑦ 三价同步(主人定「三价要同步更新」):换码的变体行三价=保留行三价,不留新旧两价并存 # uw101 K×2 + uw102 K×3 + uw103 K×1 = 6 行,全部 58.88/0/58.88 ntps = sorted(x["ntp"] for x in sc.execute( "SELECT ntp FROM s_lme_uw WHERE session_id=? AND code=?", (SID, K))) assert ntps == [58.88] * 6 assert all(x["taxp"] == 58.88 and x["taxrate"] == 0 for x in sc.execute( "SELECT taxrate, taxp FROM s_lme_uw WHERE session_id=? AND code=?", (SID, K))) # ⑤ 定额消耗量:变体精确换码;-0009(非变体)不误伤 nr = {x["res_code"]: x["qty"] for x in sc.execute("SELECT res_code, qty FROM s_norm_res")} assert set(nr) == {K, "17290070-0009"} assert nr["17290070-0009"] == 2.0 # ⑥ 子级替换 + recode_map kids = sorted(x["res_code"] for x in sc.execute( "SELECT res_code FROM s_lme_res WHERE session_id=? AND p_code=?", (SID, V15))) assert kids == ["00010003"] # 变体子级已被保留行子级整字段替换 assert sc.execute("SELECT COUNT(*) FROM s_lme_res WHERE session_id=? AND p_code=?", (SID, K)).fetchone()[0] == 1 rm = json.loads(sc.execute("SELECT v FROM s_settings WHERE scope='session:1' AND k='recode_map'").fetchone()[0]) assert rm == {V15: K, V1: K, P: K} # D800 项目级行完好无损 assert sc.execute("SELECT name FROM s_lme_proj WHERE id=3").fetchone()[0] == "II级钢筋混凝土管 D800" def test_merge_keep_uw_only_no_proj(): """勾单位级聚合行:全组替换成聚合码/展示三价——项目级组内旧行全部隐藏, **确保项目级有一行 保留码/替换三价**(没有则新增,数量=被隐藏项目行数量合计); 全部单位变体换成聚合码、三价统一。""" sc = _mk_sess() # 预置一条**早已隐藏**的同码项目行(实测报障:v3 曾对它走 INSERT 撞唯一索引) sc.execute("INSERT INTO s_lme_proj(id, session_id, code, name, spec, unit, kind, qty, ntp, taxrate, taxp, x_merged_hide)" " VALUES(9,1,'17290070@15','II级钢筋混凝土管 D300','', 'm',2, 9.9, 120.0, 0, 126.0, 1)") r = srv.api_lme_merge(sc, {"session_id": SID, "groups": [{ "sel": {"uw": 1, "code": V15, "uw_ids": [101, 102], "ntp": 126.96, "taxrate": 0, "taxp": 126.96, "name": "II级钢筋混凝土管 D300", "spec": "", "unit": "m", "kind": 2, "qty": 628.396}, # sel.qty 由前端传(被隐藏项目行合计);本用例直接给定 "hide": [{"id": 1}, {"id": 2}, {"uw": 1, "code": K, "uw_ids": [101, 102]}, {"uw": 1, "code": P, "uw_ids": [102]}, {"uw": 1, "code": V1, "uw_ids": [103]}]}]}) assert r["ok"] is True assert _uw_codes(sc, 101) == sorted([V15, V15, D800]) assert _uw_codes(sc, 102) == [V15, V15, V15] # 原 K/P/@15 三行全归保留码(无跳过) assert _uw_codes(sc, 103) == [V15] # 三价统一为替换三价(M=126.96):全部 V15 行 126.96 assert all(x["ntp"] == 126.96 for x in sc.execute( "SELECT ntp FROM s_lme_uw WHERE session_id=? AND code=?", (SID, V15))) # 项目级确保有一行 保留码/替换三价(新增行,数量=被隐藏项目行合计 628.396);旧行隐藏 prows = [tuple(x) for x in sc.execute( "SELECT code, qty, ntp, COALESCE(x_merged_hide,0) FROM s_lme_proj WHERE session_id=? ORDER BY id", (SID,))] assert prows == [(K, 11.286, 58.88, 1), (P, 617.11, 126.96, 1), (D800, 5.0, 90.0, 0), (V15, 628.396, 126.96, 0)] # D800 完好 assert sc.execute("SELECT name FROM s_lme_proj WHERE id=3").fetchone()[0] == "II级钢筋混凝土管 D800" rm = json.loads(sc.execute("SELECT v FROM s_settings WHERE scope='session:1' AND k='recode_map'").fetchone()[0]) assert rm == {K: V15, P: V15, V1: V15}