# -*- coding: utf-8 -*-
"""项目右键"加入方案库"插入段修正版:在 复制图号到方案库 方法前插入 项目右键处理方法。幂等。"""
import io
P = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\成套报价软件\主窗口.菜单与库.cs"
s = io.open(P, encoding="utf-8-sig").read()
anchor = " /// 复制一个图号(含箱号/元件子树)从项目库到方案库。供单图号加入与整项目加入共用。"
assert anchor in s, "插入锚缺失"
if "MenuProject_AddToFak_Click" in s:
print("已存在,跳过")
else:
proj_click = ''' /// 项目表右键「加入方案库」:把整个项目(全部图号+箱号+元件+项目信息)复制到方案库。
private void MenuProject_AddToFak_Click(object sender, EventArgs e)
{
var row = GetFirstSelectedRow(this.advancedDataGridView6);
if (row == null)
{
MessageBox.Show(this, "请先在项目列表中选择要加入方案库的项目。",
"加入方案库", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
string projectId = GetBoundValueAsString(row, "项目编号");
if (string.IsNullOrWhiteSpace(projectId))
{
MessageBox.Show(this, "选中行没有项目编号,无法加入方案库。",
"加入方案库", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string srcDbPath = GetQuoteDbPath(projectId);
if (!System.IO.File.Exists(srcDbPath))
{
MessageBox.Show(this, "项目报价库不存在:\\n" + srcDbPath,
"加入方案库", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string fakDbPath = GetDbFullPath(@"sjk\\fak.db");
var sw = System.Diagnostics.Stopwatch.StartNew();
Cursor = Cursors.WaitCursor;
try
{
var 图号们 = new System.Collections.Generic.List();
using (var conn = new System.Data.SQLite.SQLiteConnection(GetConnString(srcDbPath)))
{
conn.Open();
using (var cmd = new System.Data.SQLite.SQLiteCommand(
"SELECT 内部ID FROM " + TuhaoTableName + " ORDER BY CAST(序号 AS INTEGER);", conn))
using (var r = cmd.ExecuteReader())
while (r.Read())
{
if (!r.IsDBNull(0))
{
string v = r.GetString(0);
if (!string.IsNullOrEmpty(v)) 图号们.Add(v);
}
}
}
if (图号们.Count == 0)
{
MessageBox.Show(this, "项目 [" + projectId + "] 没有图号数据,无法加入方案库。",
"加入方案库", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
foreach (var thId in 图号们)
复制图号到方案库(projectId, thId, srcDbPath, fakDbPath);
string projTbl = ProjectInfoTableName;
using (var srcConn = new System.Data.SQLite.SQLiteConnection(GetConnString(srcDbPath)))
using (var dstConn = new System.Data.SQLite.SQLiteConnection(GetConnString(fakDbPath)))
{
srcConn.Open();
dstConn.Open();
var infoRow = new DataTable();
using (var cmd = new System.Data.SQLite.SQLiteCommand(
"SELECT * FROM " + projTbl + " WHERE 项目编号=@id LIMIT 1;", srcConn))
{
cmd.Parameters.AddWithValue("@id", projectId);
using (var ad = new System.Data.SQLite.SQLiteDataAdapter(cmd)) ad.Fill(infoRow);
}
var cols = new System.Collections.Generic.List();
foreach (DataColumn c in infoRow.Columns) cols.Add(c.ColumnName);
using (var tx = dstConn.BeginTransaction())
{
Exec(dstConn, tx, "DELETE FROM " + projTbl + ";");
foreach (DataRow r in infoRow.Rows)
InsertRowByColumns(r, projTbl, cols, dstConn, tx);
tx.Commit();
}
}
sw.Stop();
Log("[加入方案库] 整项目完成: 项目=" + projectId + " 图号=" + 图号们.Count + " 耗时 " + sw.ElapsedMilliseconds + "ms");
ReloadFakData();
MessageBox.Show(this, "已把项目 [" + projectId + "] 加入方案库。\\n共 " + 图号们.Count + " 个图号,耗时 " +
sw.ElapsedMilliseconds + "ms。", "加入方案库",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exAdd)
{
Log("[加入方案库] 整项目失败: " + exAdd);
MessageBox.Show(this, "加入方案库失败:\\n" + exAdd.Message, "加入方案库",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor = Cursors.Default;
}
}
'''
s = s.replace(anchor, proj_click + anchor, 1)
io.open(P, "w", encoding="utf-8-sig").write(s)
print("项目右键方法已插入")