# -*- coding: utf-8 -*- """管线接入触发器:①图号粘贴管线建 xh/yj 表后即建触发器 ②管尾 RecalcAllFromDatabase → 计算引擎.刷新项目汇总 ③管线开头加载公式。幂等(带标记检查)。""" import io import re P = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\成套报价软件\主窗口.菜单与库.cs" s = io.open(P, encoding="utf-8-sig").read() n = 0 # 0) 管线开头加载公式(conn.Open 后、journal_mode 日志后):图号粘贴专属段 anchor = 'Log("[粘贴图号] 库=" + dbPath + " journal_mode=" + dcmd.ExecuteScalar());' add = anchor + ''' // ★ 公式加载(Excel 式公式表):触发器生成与管线内联计算共用 using (var fakConn = new System.Data.SQLite.SQLiteConnection(GetConnStringStatic( System.IO.Path.Combine(System.IO.Path.GetDirectoryName(dbPath), "fak.db")))) { fakConn.Open(); 计算引擎.EnsureFormulaTable(fakConn); _公式_元件 = 计算引擎.加载公式(fakConn, "元件"); _公式_箱号 = 计算引擎.加载公式(fakConn, "箱号"); _公式_图号 = 计算引擎.加载公式(fakConn, "图号"); }''' if "_公式_元件 = 计算引擎.加载公式" not in s: assert anchor in s, "管线开头锚点缺失" s = s.replace(anchor, add, 1) n += 1 # 1) CREATE xh + 表索引登记后 → 建箱号表触发器 a1 = '''INSERT INTO 表索引 (类型, 图号, 箱号, 实际表名) VALUES ('箱号', @th, '', @real);", conn, tx)) { cmd.Parameters.AddWithValue("@th", nt); cmd.Parameters.AddWithValue("@real", xhTbl); cmd.ExecuteNonQuery(); }''' add1 = a1 + ''' // ★ 建箱号表触发器:INSERT/UPDATE/DELETE 自动算行内公式+刷新图号聚合 计算引擎.建箱号表触发器(conn, tx, xhTbl, nt, TuhaoTableName, _公式_箱号, _公式_图号);''' if "计算引擎.建箱号表触发器(conn, tx, xhTbl" not in s: assert a1 in s, "xh 锚点缺失" s = s.replace(a1, add1, 1) n += 1 # 2) 图号管线 CREATE yj + 表索引登记后 → 建元件表触发器 a2 = '''INSERT INTO 表索引 (类型, 图号, 箱号, 实际表名) VALUES ('元件', @th, @xh, @real);", conn, tx)) { cmd.Parameters.AddWithValue("@th", nt); cmd.Parameters.AddWithValue("@xh", nx); cmd.Parameters.AddWithValue("@real", yjTbl); cmd.ExecuteNonQuery(); }''' add2 = a2 + ''' // ★ 建元件表触发器:INSERT/UPDATE/DELETE 自动算派生价+刷新箱号/图号聚合 计算引擎.建元件表触发器(conn, tx, yjTbl, xhTbl, nx, nt, TuhaoTableName, _公式_元件, _公式_箱号, _公式_图号);''' if "计算引擎.建元件表触发器(conn, tx, yjTbl" not in s: assert a2 in s, "yj 锚点缺失" s = s.replace(a2, add2, 1) n += 1 # 3) 管尾 RecalcAllFromDatabase(pid) (图号粘贴 657) → 刷新项目汇总 old3 = ''' // ★ 全量重算:箱号聚合元件价格、图号聚合箱号、项目聚合图号 // (管线 INSERT 的公式列是 0,必须重算;纯数据库操作,后台线程安全) RecalcAllFromDatabase(pid);''' new3 = ''' // ★ 触发器已在 INSERT 时自动算好派生列与箱号/图号聚合;只补跨库项目汇总 计算引擎.刷新项目汇总(dbPath, pid, TuhaoTableName);''' if old3 in s: s = s.replace(old3, new3, 1) n += 1 io.open(P, "w", encoding="utf-8-sig").write(s) print("图号管线接入完成", n, "处")