Files
FullAutoWaterCheck/全自动水压检测仪/ScanImport.cs

327 lines
11 KiB
C#
Raw Normal View History

2026-01-24 13:49:39 +08:00
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ;
namespace
{
2026-01-24 14:07:01 +08:00
public partial class ScanImport : UIForm
2026-01-24 13:49:39 +08:00
{
2026-01-24 16:55:48 +08:00
private NormalTemperatureMode _normalTemperatureMode;
2026-01-24 13:49:39 +08:00
private ConductivityRepository _repository;
public ScanImport()
{
InitializeComponent();
_repository = new ConductivityRepository();
uiDataGridView1.AutoGenerateColumns = false;
}
2026-01-26 15:41:42 +08:00
private void SafeInvoke(Action action)
{
if (action == null) return;
// 检查窗体是否已释放/句柄是否创建
if (this.IsDisposed || !this.IsHandleCreated)
{
System.Diagnostics.Debug.WriteLine("ScanImport窗体已释放或句柄未创建跳过UI操作");
return;
}
try
{
// 如果已经在UI线程直接执行否则Invoke
if (this.InvokeRequired)
{
this.Invoke(action);
}
else
{
action();
}
}
catch (ObjectDisposedException ex)
{
System.Diagnostics.Debug.WriteLine($"ScanImport UI操作时控件已释放: {ex.Message}");
}
catch (InvalidOperationException ex)
{
System.Diagnostics.Debug.WriteLine($"ScanImport UI操作异常: {ex.Message}");
}
}
2026-01-24 13:49:39 +08:00
private void save_Click(object sender, EventArgs e)
{
2026-01-26 15:41:42 +08:00
SafeInvoke(() =>
2026-01-24 13:49:39 +08:00
{
2026-01-26 15:41:42 +08:00
float diffpressure = 0, exit_temperature = 0, dwelltime = 0, temperature = 0;
2026-02-04 20:05:55 +08:00
float pressureDifference = 0, standardError = 0;
int quantity = 0;
2026-01-26 15:41:42 +08:00
float.TryParse(uiTextBox4?.Text ?? "", out diffpressure);
float.TryParse(uiTextBox9?.Text ?? "", out exit_temperature);
float.TryParse(uiTextBox5?.Text ?? "", out dwelltime);
float.TryParse(uiTextBox1?.Text ?? "", out temperature);
2026-02-04 20:05:55 +08:00
float.TryParse(uiTextBoxPressureDiff?.Text ?? "", out pressureDifference);
float.TryParse(uiTextBoxStandardError?.Text ?? "", out standardError);
int.TryParse(uiTextBoxQuantity?.Text ?? "", out quantity);
2026-01-26 15:41:42 +08:00
string temperatureMode = "";
if (uiCheckBox1?.Checked == true)
{
temperatureMode = "常温模式";
}
else if (uiCheckBox2?.Checked == true)
{
temperatureMode = "高温模式";
}
2026-01-24 13:59:21 +08:00
2026-02-04 20:05:55 +08:00
// 获取联络单号、件号、刻字号和数量
2026-02-03 14:53:11 +08:00
string contactNumber = uiTextBox2?.Text?.Trim() ?? "";
string itemNumber = uiTextBox11?.Text?.Trim() ?? "";
2026-02-04 20:05:55 +08:00
string engravingNumber = uiTextBoxEngravingNumber?.Text?.Trim() ?? "";
2026-02-03 14:53:11 +08:00
if (string.IsNullOrEmpty(contactNumber))
{
MessageBox.Show("请输入联络单号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
2026-02-04 20:16:27 +08:00
2026-02-03 14:53:11 +08:00
if (string.IsNullOrEmpty(itemNumber))
{
MessageBox.Show("请输入件号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
2026-02-04 20:16:27 +08:00
2026-02-04 09:46:06 +08:00
// 组合条码用于兼容性(可选)
2026-02-03 14:53:11 +08:00
string barcode = $"{contactNumber}-{itemNumber}";
2026-01-26 15:41:42 +08:00
try
{
_repository.InsertScanItems(new ScanData
{
2026-02-03 14:53:11 +08:00
barcode = barcode,
2026-02-04 09:46:06 +08:00
ContactNumber = contactNumber,
ItemNumber = itemNumber,
2026-02-04 20:05:55 +08:00
EngravingNumber = engravingNumber,
Quantity = quantity,
2026-01-26 15:41:42 +08:00
diffpressure = diffpressure,
exit_temperature = exit_temperature,
dwelltime = dwelltime,
IsHighMode = temperature > 0,
temperature = temperature,
2026-02-04 20:05:55 +08:00
PressureDifference = pressureDifference,
StandardError = standardError,
2026-01-26 15:41:42 +08:00
TemperatureMode = temperatureMode
});
// 重新加载数据
LoadData();
// 可选:清空输入框
ClearInputFields();
}
catch (Exception ex)
{
MessageBox.Show($"保存失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2026-01-24 13:49:39 +08:00
});
2026-01-26 15:41:42 +08:00
}
2026-01-26 17:05:33 +08:00
2026-01-26 15:41:42 +08:00
private void ClearInputFields()
{
if (uiTextBox1 != null) uiTextBox1.Text = "";
if (uiTextBox2 != null) uiTextBox2.Text = "";
2026-02-03 14:53:11 +08:00
if (uiTextBox11 != null) uiTextBox11.Text = "";
2026-02-04 20:05:55 +08:00
if (uiTextBoxEngravingNumber != null) uiTextBoxEngravingNumber.Text = "";
if (uiTextBoxQuantity != null) uiTextBoxQuantity.Text = "";
2026-01-26 15:41:42 +08:00
if (uiTextBox4 != null) uiTextBox4.Text = "";
if (uiTextBox5 != null) uiTextBox5.Text = "";
if (uiTextBox9 != null) uiTextBox9.Text = "";
2026-02-04 20:05:55 +08:00
if (uiTextBoxPressureDiff != null) uiTextBoxPressureDiff.Text = "";
if (uiTextBoxStandardError != null) uiTextBoxStandardError.Text = "";
2026-01-26 15:41:42 +08:00
if (uiCheckBox1 != null) uiCheckBox1.Checked = false;
if (uiCheckBox2 != null) uiCheckBox2.Checked = false;
2026-01-24 13:49:39 +08:00
}
private void ScanImport_Load(object sender, EventArgs e)
{
2026-01-26 15:41:42 +08:00
SafeInvoke(() =>
{
LoadData();
});
2026-01-24 13:49:39 +08:00
}
2026-01-26 17:05:33 +08:00
// 设置行号的方法
private void SetRowNumbers()
{
SafeInvoke(() =>
{
if (uiDataGridView1 == null || uiDataGridView1.IsDisposed)
return;
// 为每一行设置序号
for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
{
uiDataGridView1.Rows[i].Cells["SerialNumber"].Value = (i + 1).ToString();
}
});
}
2026-01-24 13:49:39 +08:00
private void LoadData()
{
2026-01-26 15:41:42 +08:00
try
{
// 将数据查询操作移到后台线程避免UI卡顿
System.Threading.Tasks.Task.Run(() =>
{
var data = _repository.GetScanData();
2026-01-24 13:49:39 +08:00
2026-01-26 15:41:42 +08:00
// 安全更新DataGridView
SafeInvoke(() =>
{
// 避免重复绑定导致的异常
if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed)
{
// 先解绑再重新绑定
uiDataGridView1.DataSource = null;
uiDataGridView1.DataSource = data;
2026-02-04 20:16:27 +08:00
2026-01-26 17:05:33 +08:00
if (uiDataGridView1.Columns.Contains("Column1"))
{
uiDataGridView1.Columns["Column1"].Visible = false;
}
SetRowNumbers();
2026-01-26 15:41:42 +08:00
uiDataGridView1.Refresh();
2026-01-26 17:05:33 +08:00
2026-01-26 15:41:42 +08:00
}
});
});
}
catch (Exception ex)
{
SafeInvoke(() =>
{
MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
2026-01-24 13:49:39 +08:00
}
2026-01-24 16:55:48 +08:00
2026-01-26 15:41:42 +08:00
//private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
//{
// if (windowInstance == null || windowInstance.IsDisposed)
// {
// windowInstance = createFunc();
// windowInstance.FormClosed += (s, e) =>
// {
// this.Invoke(new Action(() =>
// {
// this.Show();
// this.Activate();
// }));
// };
// }
// else
// {
// windowInstance.Activate();
// return;
// }
// this.Hide();
// windowInstance.Show();
//}
private void SwitchWindow<T>(T windowInstance, Func<T> createFunc) where T : UIForm
2026-01-24 16:55:48 +08:00
{
2026-01-26 15:41:42 +08:00
// 需要将 windowInstance 声明为局部变量
T localInstance = windowInstance;
// 复用窗口实例
if (localInstance == null || localInstance.IsDisposed)
2026-01-24 16:55:48 +08:00
{
2026-01-26 15:41:42 +08:00
localInstance = createFunc();
localInstance.FormClosed += (s, e) =>
2026-01-24 16:55:48 +08:00
{
2026-01-26 15:41:42 +08:00
SafeInvoke(() =>
2026-01-24 16:55:48 +08:00
{
this.Show();
this.Activate();
2026-01-26 15:41:42 +08:00
});
2026-01-24 16:55:48 +08:00
};
}
else
{
2026-01-26 15:41:42 +08:00
SafeInvoke(() => localInstance.Activate());
2026-01-24 16:55:48 +08:00
return;
}
2026-01-26 15:41:42 +08:00
SafeInvoke(() =>
{
this.Hide();
localInstance.Show();
});
// 如果需要更新外部引用,可以返回实例
// return localInstance;
}
2026-01-24 16:55:48 +08:00
private void uiButton1_Click(object sender, EventArgs e)
{
2026-01-26 15:41:42 +08:00
SwitchWindow(_normalTemperatureMode, () => new NormalTemperatureMode());
}
//清除
private void uiButton2_Click(object sender, EventArgs e)
{
2026-01-26 15:57:13 +08:00
var ids = new List<int>();
foreach (DataGridViewRow row in uiDataGridView1.SelectedRows)
{
if (row.DataBoundItem is ScanData scanData && scanData.Id > 0)
{
ids.Add(scanData.Id);
}
}
_repository.DeleteScanItems(ids[0]);
LoadData();
2026-01-24 16:55:48 +08:00
}
2026-01-26 17:05:33 +08:00
private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
// 当添加新行时更新序号
SetRowNumbers();
}
private void uiDataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
// 当移除行时更新序号
SetRowNumbers();
}
private void uiDataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
// 当移除行时更新序号
SetRowNumbers();
}
2026-01-27 13:39:10 +08:00
private void uiButton3_Click(object sender, EventArgs e)
{
Application.Restart();
// 关闭当前窗口
Environment.Exit(0);
}
2026-01-24 13:49:39 +08:00
}
}