# -*- coding: utf-8 -*- """第二批接线:①箱号粘贴/方案库管尾 RecalcAllFromDatabase → 刷新项目汇总 ②PasteYuanjianRowsToDb 建新表后建触发器 ③RecalcAllAfterReload 落库段退役 ④SaveAllQuoteTablesIfDirty 置空(保留 EndEdit) ⑤SaveAllToDatabaseSilent 置空 ⑥EnterProjectCore 挂触发器重建 ⑦保存按钮改实时保存提示""" import io ROOT = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\成套报价软件" n = 0 def edit(path, pairs, tag): global n s = io.open(path, encoding="utf-8-sig").read() for old, new in pairs: if new in s and old not in s: continue assert old in s, tag + " 锚点缺失: " + old[:60] s = s.replace(old, new, 1) n += 1 io.open(path, "w", encoding="utf-8-sig").write(s) print(tag, "ok") # ①②③ 菜单与库.cs p1 = ROOT + r"\主窗口.菜单与库.cs" edit(p1, [ # 箱号粘贴管尾 (''' // ★ 全量重算:箱号聚合元件价格、图号聚合箱号(管线 INSERT 公式列是 0) RecalcAllFromDatabase(pid);''', ''' // ★ 触发器已自动算好箱号/图号聚合;只补跨库项目汇总 计算引擎.刷新项目汇总(dbPath, pid, TuhaoTableName);'''), # 方案库两处 (''' RecalcAllFromDatabase(_currentProjectId); // ★ 重算箱号/图号/项目汇总价''', ''' 计算引擎.刷新项目汇总(GetQuoteDbPath(_currentProjectId), _currentProjectId, TuhaoTableName); // 触发器已算明细,补跨库项目汇总'''), # PasteYuanjianRowsToDb 建新表后建触发器 (''' using (var cmd = new System.Data.SQLite.SQLiteCommand( "INSERT INTO 表索引 (类型, 图号, 箱号, 实际表名) VALUES ('元件', @th, @xh, @real);", conn, tx)) { cmd.Parameters.AddWithValue("@th", tuhao); cmd.Parameters.AddWithValue("@xh", xhId); cmd.Parameters.AddWithValue("@real", tblName); cmd.ExecuteNonQuery(); }''', ''' using (var cmd = new System.Data.SQLite.SQLiteCommand( "INSERT INTO 表索引 (类型, 图号, 箱号, 实际表名) VALUES ('元件', @th, @xh, @real);", conn, tx)) { cmd.Parameters.AddWithValue("@th", tuhao); cmd.Parameters.AddWithValue("@xh", xhId); cmd.Parameters.AddWithValue("@real", tblName); cmd.ExecuteNonQuery(); } // ★ 新元件表:建公式触发器(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, "图号")); } }'''), ], "菜单与库.cs") # ④⑤⑥ 公式重算.cs + 主窗口.cs p2 = ROOT + r"\主窗口.公式重算.cs" edit(p2, [ # RecalcAllAfterReload 尾部落库退役(直写库架构:内存只读,不回写) (''' SaveYuanjianTableIfDirty(); SaveXianghaoTableIfDirty(); SaveTuhaoTableIfDirty(); if (_yuanjianTable != null) _yuanjianTable.AcceptChanges();''', ''' // ★ 直写库架构:派生值由数据库触发器算好并经"读回"刷新缓存,内存不再回写库 if (_yuanjianTable != null) _yuanjianTable.AcceptChanges();'''), ], "公式重算.cs") p3 = ROOT + r"\主窗口.cs" edit(p3, [ # 选型页回报价页:触发器已算好聚合,去掉全量重算 (''' // ★ 步骤①:先调 RecalcAllFromDatabase 重算数据库里的箱号/图号/项目汇总 // (此时数据库里的元件派生列已是新值,Recalc 会按新元件数据聚合) // 必须先重算再 Reload,否则 Reload 把旧聚合值读到内存,显示还是旧的 RecalcAllFromDatabase(_currentProjectId);''', ''' // ★ 直写库+触发器:元件写库时聚合已自动更新,这里只补跨库项目汇总 计算引擎.刷新项目汇总(GetQuoteDbPath(_currentProjectId), _currentProjectId, TuhaoTableName);'''), # SaveAllQuoteTablesIfDirty 置空(保留 EndEdit——把 UI 编辑提交进直写队列) (''' private void SaveAllQuoteTablesIfDirty(bool force = true) { if (string.IsNullOrWhiteSpace(_currentProjectId)) return; try { // 先提交所有正在编辑的单元格,否则最后一笔编辑没进 DataTable if (this.advancedDataGridView5 != null) this.advancedDataGridView5.EndEdit(); if (this.advancedDataGridView4 != null) this.advancedDataGridView4.EndEdit(); if (this.advancedDataGridView9 != null) this.advancedDataGridView9.EndEdit();''', ''' private void SaveAllQuoteTablesIfDirty(bool force = true) { // ★ 直写库架构退役:编辑已实时 UPDATE 库(触发器自动计算),无"未保存"数据。 // 仅保留 EndEdit——把正在编辑的单元格提交进 CellValuePushed(排队直写库)。 if (string.IsNullOrWhiteSpace(_currentProjectId)) return; try { if (this.advancedDataGridView5 != null) this.advancedDataGridView5.EndEdit(); if (this.advancedDataGridView4 != null) this.advancedDataGridView4.EndEdit(); if (this.advancedDataGridView9 != null) this.advancedDataGridView9.EndEdit(); return;'''), # SaveAllToDatabaseSilent 主体置空(实时架构无保存动作;保留项目汇总刷新+项目列表刷新) (''' private void SaveAllToDatabaseSilent() { if (string.IsNullOrWhiteSpace(_currentProjectId)) return; // 先提交所有正在编辑的单元格到内存 DataTable if (this.advancedDataGridView5 != null) this.advancedDataGridView5.EndEdit(); if (this.advancedDataGridView4 != null) this.advancedDataGridView4.EndEdit(); if (this.advancedDataGridView9 != null) this.advancedDataGridView9.EndEdit();''', ''' private void SaveAllToDatabaseSilent() { // ★ 直写库架构退役:数据已实时在库(触发器自动计算),保存动作不复存在。 // 只提交编辑队列 + 刷新跨库项目汇总(供项目列表页显示)。 if (string.IsNullOrWhiteSpace(_currentProjectId)) return; if (this.advancedDataGridView5 != null) this.advancedDataGridView5.EndEdit(); if (this.advancedDataGridView4 != null) this.advancedDataGridView4.EndEdit(); if (this.advancedDataGridView9 != null) this.advancedDataGridView9.EndEdit(); try { 计算引擎.刷新项目汇总(GetQuoteDbPath(_currentProjectId), _currentProjectId, TuhaoTableName); } catch (Exception exSummary) { Log("刷新项目汇总异常: " + exSummary.Message); } return; if (this.advancedDataGridView5 != null) this.advancedDataGridView5.EndEdit(); if (this.advancedDataGridView4 != null) this.advancedDataGridView4.EndEdit(); if (this.advancedDataGridView9 != null) this.advancedDataGridView9.EndEdit();'''), ], "主窗口.cs") print("合计", n, "处")