Files
pricing_review/AdminCost/_cos4_tree.py

287 lines
14 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.
"""以两份 COS4 为准,重建目录树(三层各建一套元素节点)。
口径(用户 2026-10-29 定):
· 不再参考规范,数据结构完全以 2 份 COS4 为准,字段取两家的并集;
· 项目层 / 单项工程层 / 单位工程层各建一套节点,一层套一层;
· COS4 元素 45 个全建(含易达独有的 7 个)。
用法:
python _cos4_tree.py 预演(不提交)
python _cos4_tree.py --apply 落库
"""
import sys
import app
from _cos4_source import field_def, props_of
# (key, 节点名, 元素, 父 key, 节点类型, 复用的 tab id)
# 复用 tab 的节点会沿用该 tab 的列与数据;其余新建。
TREE = [
# ------------------------------------------------ 工具页
("base", "基本设置", "", None, "fixed", 1),
# ------------------------------------------------ A 建设项目文件
("A", "建设项目", "ConstructionProject", None, "project", 11),
("A1", "系统信息", "SystemInfo", "A", "table", 16),
("A2", "工程信息", "ConstructionInfo", "A", "table", 17),
("A2.1", "编审信息", "ProjectInfo", "A2", "table", 19),
("A2.2", "招标信息", "TendereeInfo", "A2", "table", 20),
("A2.3", "投标信息", "BidderInfo", "A2", "table", 21),
("A2.4", "工程特征", "AttrInfo", "A2", "table", None),
("A2.4.1", "工程特征项", "AttrInfoItem", "A2.4", "table", 22),
("A2.5", "补充信息", "AddiInfo", "A2", "table", None),
("A2.5.1", "补充信息项", "AddiInfoItem", "A2.5", "table", 23),
("A2.6", "费用精度", "Option", "A2", "table", 18),
("A3", "项目费用汇总", "SummaryOfCost", "A", "table", 24),
("A3.1", "项目其他费用", "AddiCost", "A3", "table", None),
("A4", "建筑安装工程费", "ProjectInstallationWorkCost", "A", "install", 13),
("A4.1", "建安费费用汇总", "ProjectInstallationWorkCostSummary", "A4", "table", None),
("A4.1.1", "建安费汇总组", "ProjectInstallationWorkCostSummaryGroup", "A4.1", "table", None),
("A4.1.2", "建安费汇总项", "ProjectInstallationWorkCostSummaryItem", "A4.1", "table", 25),
("A4.2", "措施项目", "Preliminaries", "A4", "table", None),
("A4.2.1", "措施项目要素", "PreliminaryWorkElement", "A4.2", "table", 5),
("A4.2.2", "措施基本费用汇总", "SummaryOfBasicCost", "A4.2", "table", None),
("A4.2.3", "措施分部分项", "PreliminaryDivisionalWorks", "A4.2", "table", None),
("A4.2.3.1", "措施分部分项要素", "PreliminaryWorkElement", "A4.2.3", "table", None),
("A4.2.3.2", "措施分部分项基本费用", "SummaryOfBasicCost", "A4.2.3", "table", None),
("A4.3", "其他项目", "Sundry", "A4", "table", 26),
("A4.3.1", "其他项目费", "SundryCosts", "A4.3", "table", 27),
("A4.3.1.1", "其他项目费明细", "SundryCostsItem", "A4.3.1", "table", None),
("A4.3.2", "暂列金额", "ProvisionalSums", "A4.3", "table", 28),
("A4.3.2.1", "暂列金额组", "ProvisionalSumsGroup", "A4.3.2", "table", None),
("A4.3.2.2", "暂列金额明细", "ProvisionalSumsItem", "A4.3.2", "table", None),
("A4.3.3", "专业工程暂估价", "SpecialtyProvisionalPrice", "A4.3", "table", 29),
("A4.3.4", "计日工", "DayWorkRate", "A4.3", "table", 30),
("A4.3.4.1", "计日工组", "DayWorkRateGroup", "A4.3.4", "table", None),
("A4.3.5", "总承包服务费", "MainContractorAttendance", "A4.3", "table", 31),
("A4.3.5.1", "总承包服务明细", "MainContractorAttendanceItem", "A4.3.5", "table", None),
("A4.4", "材料暂估价", "ProvisionalMaterialEquipment", "A4", "table", 33),
("A4.5", "增值税", "Tax", "A4", "table", 32),
("A4.6", "建安费工料机汇总", "LabourMaterialsEquipmentsMachinesSummary", "A4", "table", 2),
("A4.6.1", "建安费工料机", "LabourMaterialsEquipmentsMachinesElement", "A4.6", "table", 8),
("A4.7", "变更费用", "ChangeCost", "A4", "table", None),
("A4.8", "索赔费用", "ClaimsCost", "A4", "table", None),
("A4.9", "法规政策变化", "LawsRegulationsAndPolicyChangeProjects", "A4", "table", None),
# ------------------------------------------------ B 单项工程层
("B", "单项工程", "SectionalWorks", None, "sectional", 14),
("B1", "单项工程特征", "AttrInfo", "B", "table", None),
("B1.1", "单项工程特征项", "AttrInfoItem", "B1", "table", None),
("B2", "单项工程费用汇总", "SummaryOfCost", "B", "table", None),
("B2.1", "单项工程其他费用", "AddiCost", "B2", "table", None),
# ------------------------------------------------ C 单位工程层
("C", "单位工程", "UnitWorks", None, "unit", 15),
("C1", "单位工程特征", "AttrInfo", "C", "table", None),
("C1.1", "单位工程特征项", "AttrInfoItem", "C1", "table", None),
("C2", "单位工程补充信息", "AddiInfo", "C", "table", None),
("C2.1", "单位工程补充信息项", "AddiInfoItem", "C2", "table", None),
("C3", "分部分项", "DivisionalAndElementalWorks", "C", "table", None),
("C3.1", "分部工程", "DivisionalWorks", "C3", "table", None),
("C3.2", "清单项目", "WorkElement", "C3", "table", 3),
("C3.2.1", "工作内容", "WorkContent", "C3.2", "table", None),
("C3.2.1.1", "清单定额", "Norm", "C3.2.1", "table", 4),
("C3.2.1.1.1", "清单单价计算", "UnitPriceCalculationOfItem", "C3.2.1.1", "table", None),
("C3.2.1.1.2", "清单定额工料机", "LabourMaterialsEquipmentsMachinesElement", "C3.2.1.1", "table", None),
("C3.2.1.1.3", "含量组成", "ExpressElement", "C3.2.1.1", "table", None),
("C3.2.1.1.4", "定额调整", "NormAdjustmentList", "C3.2.1.1", "table", None),
("C3.2.2", "清单基本费用汇总", "SummaryOfBasicCost", "C3.2", "table", None),
("C3.3", "分部分项基本费用汇总", "SummaryOfBasicCost", "C3", "table", None),
("C4", "单位工程措施项目", "Preliminaries", "C", "table", None),
("C4.1", "单位工程措施项目要素", "UnitWorksPreliminaryWorkElement", "C4", "table", None),
("C4.1.1", "措施要素定额", "Norm", "C4.1", "table", 6),
("C4.1.2", "措施要素工料机", "LabourMaterialsEquipmentsMachinesElement", "C4.1", "table", None),
("C4.1.3", "措施要素单价计算", "UnitPriceCalculationOfItem", "C4.1", "table", None),
("C4.1.4", "措施要素基本费用", "SummaryOfBasicCost", "C4.1", "table", None),
("C4.2", "单位工程措施基本费用", "SummaryOfBasicCost", "C4", "table", None),
("C5", "工料机汇总", "LabourMaterialsEquipmentsMachinesSummary", "C", "table", None),
("C5.1", "工料机", "LabourMaterialsEquipmentsMachinesElement", "C5", "table", None),
("C6", "单位工程其他项目", "Sundry", "C", "table", None),
("C6.1", "单位工程其他项目费", "SundryCosts", "C6", "table", None),
("C6.1.1", "单位工程其他项目费明细", "SundryCostsItem", "C6.1", "table", None),
("C6.2", "单位工程暂列金额", "ProvisionalSums", "C6", "table", None),
("C6.2.1", "单位工程暂列金额组", "ProvisionalSumsGroup", "C6.2", "table", None),
("C6.2.2", "单位工程暂列金额明细", "ProvisionalSumsItem", "C6.2", "table", None),
("C6.3", "单位工程专业暂估价", "SpecialtyProvisionalPrice", "C6", "table", None),
("C6.4", "单位工程计日工", "DayWorkRate", "C6", "table", None),
("C6.4.1", "单位工程计日工组", "DayWorkRateGroup", "C6.4", "table", None),
("C6.5", "单位工程总承包服务费", "MainContractorAttendance", "C6", "table", None),
("C6.5.1", "单位工程总承包服务明细", "MainContractorAttendanceItem", "C6.5", "table", None),
("C7", "单位工程材料暂估价", "ProvisionalMaterialEquipment", "C", "table", None),
("C8", "单位工程增值税", "Tax", "C", "table", None),
("C9", "单位工程费用汇总", "SummaryOfCost", "C", "table", 34),
("C9.1", "单位工程其他费用", "AddiCost", "C9", "table", None),
("C10", "单位工程汇总", "UnitWorksSummary", "C", "table", None),
("C10.1", "单位工程汇总组", "UnitWorksSummaryGroup", "C10", "table", None),
("C10.2", "单位工程汇总项", "UnitWorksSummaryItem", "C10", "table", 10),
]
NO_COLUMN_KINDS = ("index", "suffix", "action", "anchorline", "anchor")
def plan_columns(conn, element, src_tab):
"""该节点应有的列:[(name, std, typ, req, note, width)]"""
cols, seen = [], set()
# 1) 复用 tab 自己的列(保序,含手工列)
if src_tab:
for r in conn.execute(
"SELECT name, std_name, std_type, required, std_note, width, kind FROM columns"
" WHERE tab_id = ? ORDER BY sort_order ASC, id ASC",
(src_tab,),
):
if r["kind"] in NO_COLUMN_KINDS:
continue
cols.append((r["name"], (r["std_name"] or "").strip(), r["std_type"],
r["required"], r["std_note"], r["width"]))
if r["std_name"]:
seen.add(r["std_name"].strip())
# 2) 同元素的其它 tab 作模板
for r in conn.execute(
"SELECT c.name, c.std_name, c.std_type, c.required, c.std_note, c.width"
" FROM columns c JOIN tabs t ON t.id = c.tab_id"
" WHERE t.element = ? AND c.kind = 'custom' AND c.std_name IS NOT NULL"
" AND c.std_name <> '' ORDER BY c.sort_order ASC, c.id ASC",
(element,),
):
std = r["std_name"].strip()
if std in seen:
continue
cols.append((r["name"], std, r["std_type"], r["required"], r["std_note"], r["width"]))
seen.add(std)
# 3) 兜底:按 COS4 属性全集补
for std in props_of(element):
if std in seen:
continue
cn, typ, req, note = field_def(std)
cols.append((cn, std, typ, req, note, 0))
seen.add(std)
return cols
def main(apply=False):
app.init_db()
conn = app.connect()
try:
names = [t[1] for t in TREE]
dup = {n for n in names if names.count(n) > 1}
assert not dup, "节点名重复:%s" % dup
keys = [t[0] for t in TREE]
assert len(keys) == len(set(keys)), "key 重复"
# 先把现有 tab 名改成占位名,避免改名时撞 UNIQUE
for r in conn.execute("SELECT id, name FROM tabs"):
conn.execute("UPDATE tabs SET name = ? WHERE id = ?", ("~t%d~" % r["id"], r["id"]))
# 字段分组名同样是 UNIQUE,一起腾位(顺序最后交给 sync_field_groups 统一)
for r in conn.execute("SELECT id FROM field_groups"):
conn.execute("UPDATE field_groups SET name = ? WHERE id = ?", ("~g%d~" % r["id"], r["id"]))
id_of = {}
for idx, (key, name, element, parent, kind, src) in enumerate(TREE):
if src:
tab_id = src
conn.execute(
"UPDATE tabs SET name = ?, element = ?, node_kind = ?, sort_order = ?,"
" is_fixed = ? WHERE id = ?",
(name, element, kind, idx, 1 if kind == "fixed" else 0, tab_id),
)
else:
cur = conn.execute(
"INSERT INTO tabs(name, is_fixed, sort_order, created_at, parent_id,"
" node_kind, collapsed, element) VALUES(?,?,?,?,?,?,0,?)",
(name, 0, idx, app.now_str(), None, kind, element),
)
tab_id = cur.lastrowid
id_of[key] = tab_id
print("%-10s %-2s %-38s %-46s tab%d" % (key, "", name, element or "-", tab_id))
for idx, (key, name, element, parent, kind, src) in enumerate(TREE):
conn.execute(
"UPDATE tabs SET parent_id = ? WHERE id = ?",
(id_of[parent] if parent else None, id_of[key]),
)
# 列补齐
print("\n" + "=" * 76)
gid_cache = {}
for key, name, element, parent, kind, src in TREE:
tab_id = id_of[key]
g = conn.execute("SELECT id FROM field_groups WHERE tab_id = ?", (tab_id,)).fetchone()
if g is None:
cur = conn.execute(
"INSERT INTO field_groups(name, sort_order, tab_id, created_at)"
" VALUES(?,?,?,?)",
(name, 0, tab_id, app.now_str()),
)
gid = cur.lastrowid
else:
gid = g["id"]
gid_cache[tab_id] = gid
if not element:
continue
want = plan_columns(conn, element, src)
have = {
(r["std_name"] or "").strip(): r["id"]
for r in conn.execute(
"SELECT id, std_name FROM columns WHERE tab_id = ? AND kind = 'custom'",
(tab_id,),
)
}
order, added = 2, []
for cn, std, typ, req, note, width in want:
order += 1
cid = have.get(std) if std else None
if cid:
conn.execute(
"UPDATE columns SET sort_order = ? WHERE id = ?", (order, cid)
)
continue
if conn.execute(
"SELECT 1 FROM columns WHERE tab_id = ? AND name = ?", (tab_id, cn)
).fetchone():
continue
if conn.execute(
"SELECT 1 FROM fields WHERE group_id = ? AND name = ?", (gid, cn)
).fetchone() is None:
conn.execute(
"INSERT INTO fields(name, note, is_value, sort_order, created_at, group_id)"
" VALUES(?,?,0,?,?,?)",
(cn, note or "", order, app.now_str(), gid),
)
conn.execute(
"INSERT INTO columns(tab_id, name, kind, sort_order, width, created_at,"
" std_name, std_type, required, std_note) VALUES(?,?,?,?,?,?,?,?,?,?)",
(tab_id, cn, "custom", order, width or 0, app.now_str(),
std or None, typ, req, note),
)
added.append(cn)
flag = "" if not added else " +%d列" % len(added)
print("%-46s tab%-4d 列%-3d%s" % (name, tab_id, len(want), flag))
app.sync_field_groups(conn)
if apply:
conn.commit()
print("\n已提交")
else:
conn.rollback()
print("\n预演结束(未提交)")
finally:
conn.close()
if __name__ == "__main__":
main(apply="--apply" in sys.argv)