# -*- coding: utf-8 -*- """把扁平的 8 个标签改造成「标准层级目录树」。 树形(按《建设工程造价数据交换标准》4.1.1 / 4.1.13 / 4.1.14 / 4.1.23 / 4.2.1): 建设项目 └ 工程费用 └ 建筑安装工程费 └ 单项工程 └ 单位工程 ├ 单位工程费用汇总 ├ 分部分项清单 ├ 分部分项定额 ├ 措施项目清单 ├ 措施项目定额 └ 工料机汇总 └ 工料机含量明细 """ import os import sqlite3 import datetime BASE = os.path.dirname(os.path.abspath(__file__)) DB = os.path.join(BASE, "data.db") def now_str(): return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") def has_col(conn, table, col): return any(r[1] == col for r in conn.execute("PRAGMA table_info(%s)" % table)) def main(): conn = sqlite3.connect(DB) conn.row_factory = sqlite3.Row try: for col, ddl in ( ("parent_id", "INTEGER REFERENCES tabs(id) ON DELETE CASCADE"), ("node_kind", "TEXT NOT NULL DEFAULT 'table'"), ("collapsed", "INTEGER NOT NULL DEFAULT 0"), ): if not has_col(conn, "tabs", col): conn.execute("ALTER TABLE tabs ADD COLUMN %s %s" % (col, ddl)) print("++ tabs.%s" % col) def find(name): r = conn.execute("SELECT id FROM tabs WHERE name = ?", (name,)).fetchone() return r["id"] if r else None def add(name, parent, kind, order): tid = find(name) if tid is None: cur = conn.execute( "INSERT INTO tabs(name, is_fixed, sort_order, created_at, parent_id," " node_kind, collapsed) VALUES(?,0,?,?,?,?,0)", (name, order, now_str(), parent, kind), ) tid = cur.lastrowid print("++ 节点:%s(parent=%s, %s)" % (name, parent, kind)) else: conn.execute( "UPDATE tabs SET parent_id=?, node_kind=?, sort_order=? WHERE id=?", (parent, kind, order, tid), ) print("~~ 更新节点:%s(parent=%s)" % (name, parent)) return tid t_proj = add("建设项目", None, "project", 1) t_cost = add("工程费用", t_proj, "cost", 1) t_inst = add("建筑安装工程费", t_cost, "install", 1) t_sect = add("单项工程", t_inst, "sectional", 1) t_unit = add("单位工程", t_sect, "unit", 1) # 现有数据标签挂到「单位工程」下 leaves = [ ("单位工程费用汇总", 1), ("分部分项清单", 2), ("分部分项定额", 3), ("措施项目清单", 4), ("措施项目定额", 5), ("工料机汇总", 6), ] t_sum = None for name, order in leaves: tid = find(name) if tid is None: print("!! 找不到标签:%s" % name) continue if name == "工料机汇总": t_sum = tid conn.execute( "UPDATE tabs SET parent_id=?, node_kind='table', sort_order=? WHERE id=?", (t_unit, order, tid), ) print("-> 挂载:%s → 单位工程(序 %d)" % (name, order)) # 含量明细挂到工料机汇总下 tid = find("工料机含量明细") if tid is not None and t_sum is not None: conn.execute( "UPDATE tabs SET parent_id=?, node_kind='table', sort_order=1 WHERE id=?", (t_sum, tid), ) print("-> 挂载:工料机含量明细 → 工料机汇总") # 固定标签不属于树 conn.execute( "UPDATE tabs SET parent_id=NULL, node_kind='fixed' WHERE is_fixed=1" ) conn.commit() print("\n==== 目录树 ====") def walk(pid, depth, prefix): rows = list( conn.execute( "SELECT id, name, node_kind, is_fixed FROM tabs" " WHERE (parent_id IS ? OR parent_id = ?)" " AND (is_fixed=0 OR ? IS NULL)" " ORDER BY sort_order ASC, id ASC", (pid, pid, pid), ) ) for i, r in enumerate(rows, 1): no = ("%s%d" % (prefix, i)) if prefix else str(i) print(" " * depth + "%-10s %s [%s]" % (no, r["name"], r["node_kind"])) walk(r["id"], depth + 1, no + ".") walk(None, 0, "") finally: conn.close() if __name__ == "__main__": main()