"""按两份 COS4 校准子表节点的列。 口径(2026-10-29 用户定):数据结构以 2 份 COS4 为准,结合标准规范;COS4 里没有的,我们也不建。 本脚本做三件事: 1. 工料机汇总 34 个属性补上英文属性名(std_name),并删掉 COS4 没有的 风险系数 ProviderRate / 基本价格指数 BasicPrice / 现行价格指数 CurrentPrice 2. 含量明细只有 4 个属性,删掉 COS4 没有的不计价材料 NOCost 3. 五张空表按 COS4 对应元素(含子元素并集)补齐属性列 用法:python _cos4_sub.py [--apply] """ import sys import app # 中文列名 -> COS4 属性名 CN2STD = { "工料机编码": "Number", "名称": "Name", "型号规格": "Specification", "单位": "Unit", "损耗率": "LossRate", "数量": "Quantity", "基准单价": "ProviderBase", "除税定额价": "NoTaxOrgPrice", "除税价表达式": "NoTaxPriceExpress", "除税编制价": "NoTaxPrice", "含税定额价": "TaxOrgPrice", "含税价表达式": "TaxPriceExpress", "含税编制价": "TaxPrice", "除税定额价合价": "NoTaxOrgTotal", "除税编制价合价": "NoTaxTotal", "含税定额价合价": "TaxOrgTotal", "含税编制价合价": "TaxTotal", "除税率": "TaxRate", "工料机归属": "Class", "商品砼": "Concrete", "工料机类型": "Kind", "扩展属性": "ExtKind", "附加属性": "AddiKind", "主要材料": "MainMaterial", "暂估价材料": "ProvisionalMaterial", "供料方式": "Provider", "变值权重": "Weight", "价格来源": "PriceSource", "交货方式": "Delivery", "送达地点": "Location", "产地": "ProducingArea", "厂家": "Supplier", "质量要求": "Character", "汇总备注": "Remark", # 含量明细(LabourMaterialsEquipmentsMachinesElement)只有 4 个属性 "引用编码": "Number", "数量表达式": "Express", "消耗量": "Quantity", "组成备注": "Remark", } # COS4 里没有、要删掉的列 DROP = { 2: ["风险系数", "基本价格指数", "现行价格指数"], 8: ["不计价材料"], } # 属性中文名(标准表里有就用标准表,没有的按 COS4 语义拟名) EN2CN = { "Attr": ("特征", "String"), "ChapterKind": ("章节类型", "Integer"), "Chapter": ("章", "Integer"), "Code": ("代号", "String"), "Kind": ("类型", "Integer"), "Name": ("名称", "String"), "Number": ("编号", "String"), "Quantity": ("数量", "Double"), "QtyFormula": ("计算基数", "String"), "QuantityCalculationRules": ("工程量计算规则", "String"), "Remark": ("备注", "String"), "TechnicalAndEconomicIndex": ("技术经济指标", "Double"), "Total": ("合价(元)", "Double"), "Unit": ("单位", "String"), "WorkContent": ("工作内容", "String"), "CalcType": ("计算方式", "Integer"), "CalculationKind": ("计算类型", "Integer"), "CostKind": ("费用类型", "Integer"), "EquipmentPrice": ("设备单价(元)", "Double"), "EquipmentTotal": ("设备合价(元)", "Double"), "ListingIdentity": ("清单标识", "String"), "LockKind": ("锁定类型", "Integer"), "Major": ("主要清单", "Boolean"), "Price": ("单价(元)", "Double"), "PriceHigh": ("最高限价(元)", "Double"), "PriceType": ("价格类型", "Integer"), "Rate": ("费率(%)", "Double"), "Specialty": ("专业类别", "String"), "SupplementaryList": ("补充清单", "String"), "Type": ("类别", "Integer"), "InitialFee": ("初期费用(元)", "Double"), "InitialPercent": ("初期费率(%)", "Double"), "MediumFee": ("中期费用(元)", "Double"), "MediumPercent": ("中期费率(%)", "Double"), "LastFee": ("末期费用(元)", "Double"), "LastPercent": ("末期费率(%)", "Double"), "Ratios": ("占比(%)", "Double"), "NormIdentity": ("定额标识", "String"), "Volume": ("册", "Integer"), "IsNorm": ("是否定额", "Boolean"), "EfficiencyKind": ("降效类型", "Integer"), "IncFeeKind": ("子目增加费类型", "Integer"), "Predict": ("", "String"), } DW = "Attr ChapterKind Code Kind Name Number Quantity QuantityCalculationRules Remark TechnicalAndEconomicIndex Total Unit WorkContent".split() WE = ("Attr CalcType CalculationKind Code CostKind EquipmentPrice EquipmentTotal Kind ListingIdentity LockKind" " Major Name Number Price PriceHigh PriceType QtyFormula Quantity QuantityCalculationRules Rate Remark" " Specialty SupplementaryList Total Type Unit WorkContent").split() NORM = ("CalculationKind Chapter CostKind EfficiencyKind EquipmentPrice EquipmentTotal IncFeeKind IsNorm Name" " NormIdentity Number Price Quantity Remark Specialty Total Type Unit Volume").split() USG = "ChapterKind Code Kind Name Number Quantity Ratios Remark TechnicalAndEconomicIndex Total Unit".split() USI = "ChapterKind Code Kind Name Number QtyFormula Quantity Rate Ratios Remark TechnicalAndEconomicIndex Total Unit".split() PWE = ("CalcType Code InitialFee InitialPercent Kind LastFee LastPercent ListingIdentity LockKind MediumFee" " MediumPercent Name Number Price QtyFormula Quantity Rate Remark Specialty SupplementaryList Total" " Unit WorkContent").split() UPWE = PWE + ["PriceType"] PDW = "Code Kind Name Number Quantity Remark TechnicalAndEconomicIndex Total Unit WorkContent".split() # tab_id -> [(英文属性, 说明)] SUB_LAYERS = { 10: ("UnitWorksSummary", USG + [x for x in USI if x not in USG]), 3: ("DivisionalAndElementalWorks", DW + [x for x in WE if x not in DW]), 4: ("Norm", NORM), 5: ("Preliminaries", PWE + [x for x in UPWE if x not in PWE] + [x for x in PDW if x not in PWE]), 6: ("Norm", NORM), } def main(apply=False): app.init_db() conn = app.connect() try: # 1) 工料机汇总 / 含量明细:补属性名,删 COS4 没有的 for tab_id, group_id in ((2, 4), (8, 3)): tab = conn.execute("SELECT name FROM tabs WHERE id = ?", (tab_id,)).fetchone() fixed = ("index", "suffix", "action", "anchorline", "anchor") ph = ",".join("?" * len(fixed)) for r in conn.execute( "SELECT id, name FROM columns WHERE tab_id = ? AND kind NOT IN (%s)" % ph, (tab_id,) + fixed, ).fetchall(): std = CN2STD.get(r["name"]) if std: conn.execute("UPDATE columns SET std_name = ? WHERE id = ?", (std, r["id"])) for cn in DROP.get(tab_id, []): row = conn.execute( "SELECT id FROM columns WHERE tab_id = ? AND name = ?", (tab_id, cn) ).fetchone() if row is None: continue n = conn.execute( "SELECT COUNT(*) AS c FROM row_values WHERE col_id = ? AND TRIM(value) <> ''", (row["id"],), ).fetchone()["c"] conn.execute("DELETE FROM row_values WHERE col_id = ?", (row["id"],)) conn.execute("DELETE FROM fields WHERE group_id = ? AND name = ?", (group_id, cn)) conn.execute("DELETE FROM columns WHERE id = ?", (row["id"],)) print("[%s] 删列:%s(清 %d 个值)" % (tab["name"], cn, n)) # 2) 五张空表按 COS4 元素补属性列 for tab_id, (element, stds) in SUB_LAYERS.items(): tab = conn.execute("SELECT id, name FROM tabs WHERE id = ?", (tab_id,)).fetchone() gid_row = conn.execute( "SELECT id FROM field_groups WHERE tab_id = ?", (tab_id,) ).fetchone() if tab is None or gid_row is None: print("跳过:%s" % tab_id) continue gid = gid_row["id"] conn.execute("UPDATE tabs SET element = ? WHERE id = ?", (element, tab_id)) order = 3 added = [] for std in stds: order += 1 if std not in EN2CN: print(" !! 没有中文名,跳过:%s" % std) continue cn, typ = EN2CN[std] have = conn.execute( "SELECT id FROM columns WHERE tab_id = ? AND std_name = ?", (tab_id, std) ).fetchone() if have: continue if conn.execute( "SELECT id 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, "COS4 属性 %s" % std, 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(?,?,?,?,0,?,?,?,0,?)", (tab_id, cn, "custom", order, app.now_str(), std, typ, "COS4 属性 %s" % std), ) added.append(cn) print("[%s] %s 补 %d 列:%s" % (tab["name"], element, len(added), "、".join(added) or "无")) 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)