# -*- coding: utf-8 -*- """触发器创建挪到数据写完后统一建。幂等(基于标记检查)。""" import io P = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\成套报价软件\主窗口.菜单与库.cs" ROOT = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件" n = 0 s = io.open(P, encoding="utf-8-sig").read() def rep(old, new, tag): global s, n if old not in s: if new in s: print(tag, "已有(跳过)") return raise AssertionError(tag + " 锚点缺失: " + old[:70]) s = s.replace(old, new, 1) n += 1 print(tag, "ok") # 1) 字段(编辑直写库.cs, tab 缩进) p2 = ROOT + r"\编辑直写库.cs" s2 = io.open(p2, encoding="utf-8-sig").read() old_f = "\t\t/// 当前 Reload 耗时测量(进 Reload 方法时重启)。" new_f = ("\t\t/// 待建触发器清单 [yj表, 箱号表, 箱号ID, 图号ID](数据写完后统一建,避免事务内 DDL 拖慢批量 INSERT)。\n" "\t\tprivate readonly System.Collections.Generic.List _待建yj触发器 = new System.Collections.Generic.List();\n" "\t\t/// 待建箱号表触发器 [箱号表, 图号ID]。\n" "\t\tprivate readonly System.Collections.Generic.List _待建xh触发器 = new System.Collections.Generic.List();\n\n" "\t\t/// 当前 Reload 耗时测量(进 Reload 方法时重启)。") if old_f in s2 and "_待建yj触发器" not in s2: s2 = s2.replace(old_f, new_f, 1) io.open(p2, "w", encoding="utf-8-sig").write(s2) n += 1 print("字段 ok") else: print("字段 已有(跳过)") # 2) 图号管线循环内:建元件表触发器 → 收集 rep(''' // ★ 建元件表触发器:INSERT/UPDATE/DELETE 自动算派生价+刷新箱号/图号聚合 计算引擎.建元件表触发器(conn, tx, yjTbl, xhTbl, nx, nt, TuhaoTableName, _公式_元件, _公式_箱号, _公式_图号);''', ''' // ★ 登记待建触发器(数据全部写完后统一建,避免事务内 DDL 拖慢 INSERT) _待建yj触发器.Add(new[] { yjTbl, xhTbl, nx, nt });''', "图号管线yj收集") # 3) 图号管线:建箱号表触发器 → 收集 rep(''' // ★ 建箱号表触发器:INSERT/UPDATE/DELETE 自动算行内公式+刷新图号聚合 计算引擎.建箱号表触发器(conn, tx, xhTbl, nt, TuhaoTableName, new List(), _公式_箱号, _公式_图号);''', ''' // 箱号表触发器管尾统一建(行级依赖各行的元件表名) if (!_待建xh触发器.Exists(x => x[0] == xhTbl)) _待建xh触发器.Add(new[] { xhTbl, nt });''', "图号管线xh收集") # 4) 图号管线管尾:统一建(开关=0 前) rep(''' // ★ 恢复触发器(编辑自动计算) 计算引擎.开关设(conn, tx, 0);''', ''' // ★ 数据全部写完,统一建触发器(此后无批量 DML) foreach (var t in _待建yj触发器) 计算引擎.建元件表触发器(conn, tx, t[0], t[1], t[2], t[3], TuhaoTableName, _公式_元件, _公式_箱号, _公式_图号); foreach (var t in _待建xh触发器) { var rows = new System.Collections.Generic.List(); foreach (var y in _待建yj触发器) if (y[1] == t[0]) rows.Add(new[] { y[2], y[0] }); 计算引擎.建箱号表触发器(conn, tx, t[0], t[1], TuhaoTableName, rows, _公式_箱号, _公式_图号); } _待建yj触发器.Clear(); _待建xh触发器.Clear(); // ★ 恢复触发器(编辑自动计算) 计算引擎.开关设(conn, tx, 0);''', "图号管线管尾") # 5) PasteYuanjianRowsToDb 建新表:触发器 → 收集 rep(''' // ★ 新元件表:建公式触发器(INSERT/UPDATE/DELETE 自动算派生价+刷聚合) string _xhTblReal = LookupTableByIndex("箱号", tuhao, "", conn); if (!string.IsNullOrEmpty(_xhTblReal)) { using (var fakConn = new System.Data.SQLite.SQLiteConnection(GetConnStringStatic( System.IO.Path.Combine(System.IO.Path.GetDirectoryName(GetQuoteDbPath(projectId)), "fak.db")))) { fakConn.Open(); 计算引擎.EnsureFormulaTable(fakConn); 计算引擎.建元件表触发器(conn, tx, tblName, _xhTblReal, xhId, tuhao, TuhaoTableName, 计算引擎.加载公式(fakConn, "元件"), 计算引擎.加载公式(fakConn, "箱号"), 计算引擎.加载公式(fakConn, "图号")); } }''', ''' // ★ 登记待建触发器(管尾统一建;行内派生值已随行写入,无需即时触发) string _xhTblReal = LookupTableByIndex("箱号", tuhao, "", conn); if (!string.IsNullOrEmpty(_xhTblReal)) { bool exists = false; foreach (var t in _待建yj触发器) if (t[2] == xhId && t[3] == tuhao) { exists = true; break; } if (!exists) _待建yj触发器.Add(new[] { tblName, _xhTblReal, xhId, tuhao }); if (!_待建xh触发器.Exists(x => x[0] == _xhTblReal && x[1] == tuhao)) _待建xh触发器.Add(new[] { _xhTblReal, tuhao }); }''', "PasteYuanjian收集") # 6) 箱号管线管尾:统一建(开关=0 前) —— 锚点用 刷新图号行聚合 调用行 rep(''' // 箱号行聚合抄源行已正确;仅图号行聚合(全箱号表 SUM)需刷新一次 计算引擎.刷新图号行聚合(conn, tx, dbPath, th, TuhaoTableName);''', ''' // 箱号行聚合抄源行已正确;统一建触发器 + 刷一次图号行聚合 foreach (var t in _待建yj触发器) 计算引擎.建元件表触发器(conn, tx, t[0], t[1], t[2], t[3], TuhaoTableName, _公式_元件, _公式_箱号, _公式_图号); foreach (var t in _待建xh触发器) { var rows = new System.Collections.Generic.List(); foreach (var y in _待建yj触发器) if (y[1] == t[0] && y[3] == t[1]) rows.Add(new[] { y[2], y[0] }); 计算引擎.建箱号表触发器(conn, tx, t[0], t[1], TuhaoTableName, rows, _公式_箱号, _公式_图号); } _待建yj触发器.Clear(); _待建xh触发器.Clear(); 计算引擎.刷新图号行聚合(conn, tx, dbPath, th, TuhaoTableName);''', "箱号管线管尾") io.open(P, "w", encoding="utf-8-sig").write(s) print("合计", n, "处")