Files
pricing_review/test/_tmp_ui227_prep.py

115 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""决策 227 无头实测·隔离环境(临时库,生产库零接触)。
起服务 → 建会话 → 库内造「两个单位工程同名清单、同码工料机不同价」场景 →
API 实测:本项目内采纳=浮层价随来源(后采纳的为准);取消采纳恢复;再采纳重落。打印结果后保持运行。"""
import os, shutil, socket, sqlite3, subprocess, sys, tempfile, time
HERE = os.path.dirname(os.path.abspath(__file__))
APP_SRC = os.path.abspath(os.path.join(HERE, "..", "CostReview", "app"))
sys.path.insert(0, os.path.join(APP_SRC, "tests"))
from api_harness import make_cos4 # noqa: E402
import httpx
BILLS = [
("010101001001", "地下室墙身保温隔热", "1、30厚聚苯板", "m2", 100, 33.35),
("010101002001", "地下室墙身保温隔热", "1、30厚聚苯板", "m2", 200, 33.35),
]
root = tempfile.mkdtemp(prefix="ui227_")
app_dst = os.path.join(root, "CostReview", "app")
shutil.copytree(APP_SRC, app_dst, ignore=shutil.ignore_patterns("__pycache__", "_deprecated", "tests", "输出"))
src_dir = os.path.join(root, "后台需要导入的项目")
os.makedirs(src_dir)
lic = os.path.join(root, "lic")
os.makedirs(lic)
open(os.path.join(lic, "config.json"), "w").write('{"enforce": false}')
s = socket.socket(); s.bind(("127.0.0.1", 0)); port = s.getsockname()[1]; s.close()
env = dict(os.environ, PR_PORT=str(port), LICENSE_DATA_DIR=lic, PYTHONIOENCODING="utf-8")
log = open(os.path.join(root, "server.log"), "wb")
proc = subprocess.Popen([sys.executable, "run_server_headless.py"], cwd=app_dst,
env=env, stdout=log, stderr=subprocess.STDOUT)
base = "http://127.0.0.1:%d" % port
cli = httpx.Client(base_url=base, timeout=60)
for _ in range(100):
try:
if cli.get("/api/license/status").status_code == 200:
break
except Exception:
time.sleep(0.3)
else:
print("SERVER NOT READY"); sys.exit(1)
pa = os.path.join(src_dir, "ui227.cos4")
make_cos4(pa, "采纳价锚实测", BILLS)
r = cli.post("/api/audit", json={
"path": pa, "name": "采纳价锚实测",
"province": "广东省", "city": "珠海市", "district": "香洲区",
"period_from": 2026 * 12 + 7, "file_kind": 1}).json()
assert r.get("session_id"), r
sid = r["session_id"]
db = os.path.join(root, "CostReview", "data", "session.db")
sc = sqlite3.connect(db)
sc.row_factory = sqlite3.Row
# 造场景:第二个单位工程 + 清单2 挪过去;两 uw 的 EPS 板(02110160)各挂各的价
uw1 = sc.execute("SELECT id FROM s_unit_works WHERE session_id=? ORDER BY id LIMIT 1", (sid,)).fetchone()["id"]
sc.execute("INSERT INTO s_unit_works(session_id, Number, Name) VALUES(?,?,?)",
(sid, "UW2", "2#大礼堂"))
uw2 = sc.execute("SELECT MAX(id) FROM s_unit_works WHERE session_id=?", (sid,)).fetchone()[0]
b1 = sc.execute("SELECT id FROM s_bills WHERE session_id=? AND Number='010101001001'", (sid,)).fetchone()["id"]
b2 = sc.execute("SELECT id FROM s_bills WHERE session_id=? AND Number='010101002001'", (sid,)).fetchone()["id"]
sc.execute("UPDATE s_bills SET uw_id=? WHERE id=?", (uw2, b2))
# 两 uw 的工料机汇总:需先有 s_lme_uw 行(借用 lmes 的 EPS 板编码 02110160)
lmes = sc.execute("SELECT id FROM s_lmes WHERE session_id=? LIMIT 1", (sid,)).fetchall()
# s_lme_uw 结构探测
cols = [x[1] for x in sc.execute("PRAGMA table_info(s_lme_uw)")]
def _ins_uw(uw, ntp, taxp):
vals = {"session_id": sid, "uw_id": uw, "code": "02110160", "name": "EPS聚苯板",
"spec": "", "unit": "m3", "kind": 2, "ntp": ntp, "taxp": taxp}
ks = [k for k in vals if k in cols]
ph = ",".join(ks)
q = ",".join("?" for _ in ks)
sc.execute("INSERT INTO s_lme_uw(%s) VALUES(%s)" % (ph, q), [vals[k] for k in ks])
_ins_uw(uw1, 205.00, 238.87)
_ins_uw(uw2, 252.00, 293.63)
sc.commit()
def norm_of(bid):
return sc.execute("SELECT id FROM s_norms WHERE bill_id=? ORDER BY id LIMIT 1",
(bid,)).fetchone()["id"]
n_src, n_dst = norm_of(b1), norm_of(b2)
def res_price(nid):
rr = sc.execute("SELECT res_code, qty, x_ntp, x_taxp FROM s_norm_res WHERE norm_id=? ORDER BY id", (nid,)).fetchall()
return [(x["res_code"], x["qty"], x["x_ntp"], x["x_taxp"]) for x in rr]
print("PRE src res:", res_price(n_src))
print("PRE dst res:", res_price(n_dst))
# 浮层(链价)改前:目标定额(uw2)应显示 252
f0 = cli.get("/api/session/norm/%d/res" % n_dst).json()
print("FLOAT dst before:", [(x[0], x[5], x[6], x[7]) for x in f0["rows"]])
# 本项目内采纳:清单2 采纳 清单1
ad = cli.post("/api/session/bill/op", json={"op": "adopt", "bill_id": b2, "src_self_bill_id": b1}).json()
print("ADOPT:", {k: ad.get(k) for k in ("ok", "mode", "price", "n_norm")})
sc2 = sqlite3.connect(db); sc2.row_factory = sqlite3.Row
n_dst2 = sc2.execute("SELECT id FROM s_norms WHERE bill_id=? AND x_adopt=1 ORDER BY id DESC LIMIT 1", (b2,)).fetchone()["id"]
fa = cli.get("/api/session/norm/%d/res" % n_dst2).json()
print("FLOAT dst after adopt:", [(x[0], x[5], x[6], x[7]) for x in fa["rows"]])
anc = sc2.execute("SELECT x_ntp, x_taxp FROM s_norm_res WHERE norm_id=?", (n_dst2,)).fetchall()
print("ANCHOR rows:", [(x["x_ntp"], x["x_taxp"]) for x in anc])
sc2.close()
# 取消采纳 → 锚随采纳定额整组删,恢复原定额 → 浮层回 252
un = cli.post("/api/session/bill/op", json={"op": "unadopt", "bill_id": b2}).json()
print("UNADOPT:", {k: un.get(k) for k in ("ok",)})
n_dst3 = norm_of(b2)
f2 = cli.get("/api/session/norm/%d/res" % n_dst3).json()
print("FLOAT dst after unadopt:", [(x[0], x[5], x[6], x[7]) for x in f2["rows"]])
print("BASE=%s SID=%s B1=%s B2=%s" % (base, sid, b1, b2))
print("ROOT=%s" % root)
sys.stdout.flush()
time.sleep(3600)