947 lines
29 KiB
C#
947 lines
29 KiB
C#
using BaseFunction;
|
||
using NSAnalysis.DAL;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using Telerik.WinControls;
|
||
using Telerik.WinControls.UI;
|
||
|
||
namespace NSAnalysis
|
||
{
|
||
/// <summary>
|
||
///
|
||
///
|
||
///
|
||
/// 2025.08.28
|
||
/// ----------------------------------------------
|
||
/// 1、当解析文件时,增加备份到根目录
|
||
/// 2、解析失败或提取有效内容失败,移动到源文件夹的Error文件夹
|
||
/// 3、修复查询结果,显示错误问题
|
||
///
|
||
/// 2025.08.31
|
||
/// --------------------------------------------
|
||
/// 1、新增将客户报告 默认生成到备份文件夹,然后拷贝到目标路径,并记录错误日志
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
///
|
||
/// </summary>
|
||
|
||
public partial class FormMain : Telerik.WinControls.UI.ShapedForm
|
||
{
|
||
private Timer fileSortTimer = new Timer(); // 定时器,用于定时分发任务
|
||
private FileSorter fileSorter = new FileSorter();
|
||
|
||
#region 全局变量
|
||
|
||
private DataTable dtCSVContent = new DataTable();
|
||
|
||
private CjlrDAL tmdal = new CjlrDAL();
|
||
|
||
private bool bReadCSVFlag = false;
|
||
|
||
private int iLastMesureCount = 0;
|
||
|
||
#region 分页全局变量
|
||
|
||
private DataTable PageTable = new DataTable();
|
||
|
||
/// <summary>
|
||
/// 每页记录数
|
||
/// </summary>
|
||
public int pageSize = 20;
|
||
|
||
/// <summary>
|
||
/// 总记录数
|
||
/// </summary>
|
||
public int recordCount = 0;
|
||
|
||
/// <summary>
|
||
/// 总页数
|
||
/// </summary>
|
||
public int pageCount = 0;
|
||
|
||
/// <summary>
|
||
/// 当前页
|
||
/// </summary>
|
||
public int currentPage = 0;
|
||
|
||
#endregion 分页全局变量
|
||
|
||
private Dictionary<Control, PointF> originalRatios = new Dictionary<Control, PointF>();
|
||
private Dictionary<Control, SizeF> originalSizes = new Dictionary<Control, SizeF>();
|
||
|
||
#endregion 全局变量
|
||
|
||
#region 主窗口事件
|
||
|
||
public FormMain()
|
||
{
|
||
InitializeComponent();
|
||
SQLHelper.connStr = DatabaseDfn.SqlConnectStr();
|
||
|
||
//加载皮肤
|
||
ThemeResolutionService.LoadPackageFile(ConfigDfn.strTheme);
|
||
this.ThemeName = ConfigDfn.strSkinName.Substring(0, ConfigDfn.strSkinName.Length - 5);
|
||
RPV.ThemeName = ConfigDfn.strSkinName.Substring(0, ConfigDfn.strSkinName.Length - 5);
|
||
}
|
||
|
||
private void FormMain_Load(object sender, EventArgs e)
|
||
{
|
||
//初始化全局变量
|
||
|
||
//MyBase.rleMessage = rleMessage;
|
||
rdtpStartTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
|
||
rdtpEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
|
||
|
||
//绑定按钮
|
||
lpcSoftwareSetup.labPicture.Click += new EventHandler(lpcSoftwareSetup_Click);
|
||
lpcSoftwareSetup.labText.Click += new EventHandler(lpcSoftwareSetup_Click);
|
||
|
||
lpcToleranceSetup.labPicture.Click += new EventHandler(lpcToleranceSetup_Click);
|
||
lpcToleranceSetup.labText.Click += new EventHandler(lpcToleranceSetup_Click);
|
||
|
||
lpcAboutSoftware.labPicture.Click += new EventHandler(lpcAboutSoftware_Click);
|
||
lpcAboutSoftware.labText.Click += new EventHandler(lpcAboutSoftware_Click);
|
||
|
||
lpcShowLog.labPicture.Click += new EventHandler(lpcShowLog_Click);
|
||
lpcShowLog.labText.Click += new EventHandler(lpcShowLog_Click);
|
||
|
||
iLastMesureCount = tmdal.SelectTMeasureResultCount();
|
||
|
||
ClearLabel(); //清空信息
|
||
InitTableColumns();
|
||
StoreControlRatios(this);
|
||
}
|
||
|
||
private void FileSorter_OnProcessStep(string obj)
|
||
{
|
||
if (lbCSVFiles.InvokeRequired)
|
||
{
|
||
lbCSVFiles.Invoke(new Action<string>(FileSorter_OnProcessStep), obj);
|
||
return;
|
||
}
|
||
|
||
// 控件初始化检查
|
||
if (lbCSVFiles == null)
|
||
return;
|
||
|
||
// 超过100条时清空
|
||
if (lbCSVFiles.Items.Count > 100)
|
||
{
|
||
lbCSVFiles.Items.Clear();
|
||
}
|
||
|
||
lbCSVFiles.Items.Add(obj);
|
||
|
||
// 滚动到底部
|
||
if (lbCSVFiles.Items.Count > 0)
|
||
{
|
||
lbCSVFiles.TopIndex = lbCSVFiles.Items.Count - 1;
|
||
}
|
||
}
|
||
|
||
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
|
||
{
|
||
MyBase.TraceWriteLine("-------------海克斯康面隙分析软件程序关闭---------------------");
|
||
LogDebugDfn.strEndTime = DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss");
|
||
string CopyFileName = ConfigDfn.strFileFolder + "\\DebugFiles\\" + "Debug(" + LogDebugDfn.strStartTime + " To " + LogDebugDfn.strEndTime + ")" + ".txt";
|
||
if (File.Exists(LogDebugDfn.strDebugFile))
|
||
File.Copy(LogDebugDfn.strDebugFile, CopyFileName);
|
||
System.Environment.Exit(0);
|
||
}
|
||
|
||
private void FormMain_Resize(object sender, EventArgs e)
|
||
{
|
||
ScaleControls(this);
|
||
}
|
||
|
||
private void FormMain_Shown(object sender, EventArgs e)
|
||
{
|
||
// 定时处理任务
|
||
fileSortTimer.Interval = 15000; // 每60秒检查一次分发任务
|
||
fileSortTimer.Tick += FileSortTimer_Tick;
|
||
fileSortTimer.Start();
|
||
|
||
// 订阅处理中事件
|
||
fileSorter.OnFileParsed += FileSorter_OnFileParsed;
|
||
fileSorter.OnProcessStep += FileSorter_OnProcessStep;
|
||
|
||
//记录到日志
|
||
MyBase.TraceWriteLine("-------------海克斯康面隙分析软件程序启动---------------------");
|
||
}
|
||
|
||
#endregion 主窗口事件
|
||
|
||
#region 方法
|
||
|
||
private void ClearLabel()
|
||
{
|
||
labVIN.Text = "";
|
||
labCarType.Text = "";
|
||
|
||
labOKCount_L.Text = "";
|
||
labNGCount_L.Text = "";
|
||
labRejectCount_L.Text = "";
|
||
labSumMeasureCount_L.Text = "";
|
||
labMeaTime_L.Text = "";
|
||
}
|
||
|
||
private void InitTableColumns()
|
||
{
|
||
#region 测量数据表始化表
|
||
|
||
//创建虚拟表
|
||
DataColumn column2 = new DataColumn("CarID", Type.GetType("System.String"));
|
||
DataColumn dcCarType = new DataColumn("CarType", Type.GetType("System.String"));
|
||
DataColumn column3 = new DataColumn("MeasPointName", Type.GetType("System.String"));
|
||
DataColumn column4 = new DataColumn("DimensionName", Type.GetType("System.String"));
|
||
DataColumn column5 = new DataColumn("NormalValue", Type.GetType("System.String"));
|
||
DataColumn column6 = new DataColumn("LowerTolVal", Type.GetType("System.String"));
|
||
DataColumn column7 = new DataColumn("UpperTolVal", Type.GetType("System.String"));
|
||
DataColumn column8 = new DataColumn("MeasureValue", Type.GetType("System.String"));
|
||
DataColumn column10 = new DataColumn("MeasureItemResult", Type.GetType("System.String"));
|
||
DataColumn column9 = new DataColumn("MeasureDate", Type.GetType("System.DateTime"));
|
||
DataColumn column1 = new DataColumn("Remark", Type.GetType("System.String"));
|
||
//将列添加到table表中
|
||
|
||
dtCSVContent.Columns.Add(column2);
|
||
dtCSVContent.Columns.Add(dcCarType);
|
||
dtCSVContent.Columns.Add(column3);
|
||
dtCSVContent.Columns.Add(column4);
|
||
dtCSVContent.Columns.Add(column5);
|
||
dtCSVContent.Columns.Add(column6);
|
||
dtCSVContent.Columns.Add(column7);
|
||
dtCSVContent.Columns.Add(column8);
|
||
dtCSVContent.Columns.Add(column10);
|
||
dtCSVContent.Columns.Add(column9);
|
||
dtCSVContent.Columns.Add(column1);
|
||
|
||
#endregion 测量数据表始化表
|
||
|
||
#region 极差数据Table初始化
|
||
|
||
//创建虚拟表
|
||
DataColumn dcCarID = new DataColumn("CarID", Type.GetType("System.String"));
|
||
DataColumn dcRangeName = new DataColumn("RangeName", Type.GetType("System.String"));
|
||
DataColumn dcRangeValue = new DataColumn("RangeValue", Type.GetType("System.String"));
|
||
DataColumn dcRangeLowUpp = new DataColumn("RangeLowUpp", Type.GetType("System.String"));
|
||
DataColumn dcResult = new DataColumn("Result", Type.GetType("System.String"));
|
||
DataColumn dcCreateTime = new DataColumn("CreateTime", Type.GetType("System.DateTime"));
|
||
DataColumn dcRemark = new DataColumn("Remark", Type.GetType("System.String"));
|
||
//将列添加到table表中
|
||
|
||
#endregion 极差数据Table初始化
|
||
|
||
#region 分页相关
|
||
|
||
//创建分页虚拟表
|
||
DataColumn column11 = new DataColumn("CarID", Type.GetType("System.String"));
|
||
DataColumn column12 = new DataColumn("MeasPointName", Type.GetType("System.String"));
|
||
DataColumn column13 = new DataColumn("DimensionName", Type.GetType("System.String"));
|
||
DataColumn column14 = new DataColumn("NormalValue", Type.GetType("System.String"));
|
||
DataColumn column15 = new DataColumn("LowerTolVal", Type.GetType("System.String"));
|
||
DataColumn column16 = new DataColumn("UpperTolVal", Type.GetType("System.String"));
|
||
DataColumn column17 = new DataColumn("MeasureValue", Type.GetType("System.String"));
|
||
DataColumn column18 = new DataColumn("MeasureDate", Type.GetType("System.String"));
|
||
DataColumn column19 = new DataColumn("MeasureItemResult", Type.GetType("System.String"));
|
||
|
||
//将列添加到table表中
|
||
PageTable.Columns.Add(column11);
|
||
PageTable.Columns.Add(column12);
|
||
PageTable.Columns.Add(column13);
|
||
PageTable.Columns.Add(column14);
|
||
PageTable.Columns.Add(column15);
|
||
PageTable.Columns.Add(column16);
|
||
PageTable.Columns.Add(column17);
|
||
PageTable.Columns.Add(column18);
|
||
PageTable.Columns.Add(column19);
|
||
|
||
labCurrentPage.Text = "当前页:" + currentPage.ToString(); //当前页
|
||
labSumPages.Text = "共 " + pageCount.ToString() + " 页";//总页数
|
||
labSumRecorders.Text = "总共 " + recordCount.ToString() + " 条记录";//总记录数
|
||
rddlPageRecorderCount.SelectedIndex = 0;
|
||
|
||
#endregion 分页相关
|
||
}
|
||
|
||
private void DisplayMeasureData(string strCarID) // 其中 string strCarID 是车身ID_L 或 车身ID_R
|
||
{
|
||
try
|
||
{
|
||
DataTable sampleData = tmdal.SelectMeasureResultByCarID(strCarID);
|
||
if (sampleData == null || sampleData.Rows.Count == 0)
|
||
{
|
||
MyBase.TraceWriteLine("[DisplayMeasureData] 没有找到车ID为:" + strCarID + " 的测量数据。");
|
||
return;
|
||
}
|
||
AnalysisResult analysis = AnalysisResult.AnalyzeMeasureData(sampleData);
|
||
//AnalysisResult.DisplayAnalysisResult(analysis);
|
||
|
||
//表格行数
|
||
int dtRowCount = analysis.TotalCount;
|
||
//超差个数
|
||
double OutCount = analysis.OutCount;
|
||
//Ok个数
|
||
double OKCount = analysis.OKCount;
|
||
//异常个数
|
||
double RejectedCount = analysis.RejectedCount;
|
||
//合格率
|
||
double FPYPercent = analysis.FPYPercent;
|
||
// 更新统计信息显示
|
||
labVIN.Text = strCarID;
|
||
labCarType.Text = ConfigDfn.strCarModel;
|
||
//labPosition_L.Text = "左侧";
|
||
labOKCount_L.Text = OKCount.ToString();
|
||
labNGCount_L.Text = OutCount.ToString();
|
||
labRejectCount_L.Text = RejectedCount.ToString();
|
||
labSumMeasureCount_L.Text = dtRowCount.ToString();
|
||
labMeaTime_L.Text = ConfigDfn.strMeasureTime;
|
||
|
||
#region 表单区填充
|
||
|
||
DataTable dataTable = tmdal.SelectMeasureDataByCarID(strCarID);
|
||
|
||
// 判断是否有数据
|
||
if (dataTable == null || dataTable.Rows.Count == 0)
|
||
{
|
||
MyBase.TraceWriteLine("没有找到车ID为:" + strCarID + " 的测量数据。");
|
||
return;
|
||
}
|
||
dgvMeasureContent.DataSource = dataTable;
|
||
|
||
this.dgvMeasureContent.Rows[dgvMeasureContent.Rows.Count - 1].DefaultCellStyle.ForeColor = Color.White;
|
||
if (dgvMeasureContent.Rows.Count > 0)
|
||
{
|
||
SetdgvRowBgColor(dgvMeasureContent);
|
||
for (int i = 0; i < dgvMeasureContent.Rows.Count; i++)
|
||
{
|
||
//if (dgvMeasureContent.Rows[i].Cells["MResult"].Value.ToString().ToLower().Contains("ok"))
|
||
//{
|
||
// dgvMeasureContent.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
|
||
//}
|
||
if (dgvMeasureContent.Rows[i].Cells["Classification"].Value.ToString().ToLower().Contains("ng"))
|
||
{
|
||
dgvMeasureContent.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
|
||
}
|
||
else if (string.IsNullOrEmpty(dgvMeasureContent.Rows[i].Cells["Classification"].Value.ToString()))
|
||
{
|
||
dgvMeasureContent.Rows[i].DefaultCellStyle.BackColor = Color.Red;
|
||
}
|
||
else
|
||
{
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion 表单区填充
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MyBase.TraceWriteLine("[AnalysisNextSenseCSV] 函数分析异常:" + ex.ToString());
|
||
}
|
||
}
|
||
|
||
private void FileSorter_OnFileParsed(string strCarID, string Position) //形如
|
||
{
|
||
// 线程安全更新 UI
|
||
if (InvokeRequired)
|
||
{
|
||
Invoke(new Action<string, string>(FileSorter_OnFileParsed), strCarID, Position);
|
||
return;
|
||
}
|
||
// 显示数据 并更新单侧汇总结果, 此时为左侧或右侧数据
|
||
DisplayMeasureData(strCarID);
|
||
}
|
||
|
||
/// 通过给定的文件流,判断文件的编码类型
|
||
/// <param name="fs">文件流</param>
|
||
/// <returns>文件的编码类型</returns>
|
||
public static Encoding GetType(FileStream fs)
|
||
{
|
||
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
|
||
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
|
||
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
|
||
System.Text.Encoding reVal = System.Text.Encoding.Default;
|
||
|
||
System.IO.BinaryReader r = new System.IO.BinaryReader(fs, System.Text.Encoding.Default);
|
||
int i;
|
||
int.TryParse(fs.Length.ToString(), out i);
|
||
byte[] ss = r.ReadBytes(i);
|
||
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
|
||
{
|
||
reVal = System.Text.Encoding.UTF8;
|
||
}
|
||
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
|
||
{
|
||
reVal = System.Text.Encoding.BigEndianUnicode;
|
||
}
|
||
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
|
||
{
|
||
reVal = System.Text.Encoding.Unicode;
|
||
}
|
||
r.Close();
|
||
return reVal;
|
||
}
|
||
|
||
/// 判断是否是不带 BOM 的 UTF8 格式
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
private static bool IsUTF8Bytes(byte[] data)
|
||
{
|
||
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
|
||
byte curByte; //当前分析的字节.
|
||
for (int i = 0; i < data.Length; i++)
|
||
{
|
||
curByte = data[i];
|
||
if (charByteCounter == 1)
|
||
{
|
||
if (curByte >= 0x80)
|
||
{
|
||
//判断当前
|
||
while (((curByte <<= 1) & 0x80) != 0)
|
||
{
|
||
charByteCounter++;
|
||
}
|
||
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
|
||
if (charByteCounter == 1 || charByteCounter > 6)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//若是UTF-8 此时第一位必须为1
|
||
if ((curByte & 0xC0) != 0x80)
|
||
{
|
||
return false;
|
||
}
|
||
charByteCounter--;
|
||
}
|
||
}
|
||
if (charByteCounter > 1)
|
||
{
|
||
throw new Exception("非预期的byte格式");
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public Encoding GetType(string FileName)
|
||
{
|
||
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
|
||
Encoding er = GetType(fs);
|
||
fs.Close();
|
||
return er;
|
||
}
|
||
|
||
private void btn_MouseHover(object sender, EventArgs e)
|
||
{
|
||
RadButton btn = sender as RadButton;
|
||
btn.BackColor = Color.FromArgb(0, 151, 186);
|
||
}
|
||
|
||
private void btn_MouseLeave(object sender, EventArgs e)
|
||
{
|
||
RadButton btn = sender as RadButton;
|
||
btn.BackColor = Color.FromArgb(19, 46, 53);
|
||
}
|
||
|
||
public void SetdgvRowBgColor(DataGridView dgv) //设置DataGridView各行变色
|
||
{
|
||
if (dgv.Rows.Count > 0)
|
||
{
|
||
foreach (DataGridViewRow item in dgv.Rows)
|
||
{
|
||
if (item.Index % 2 == 0)
|
||
{
|
||
item.DefaultCellStyle.BackColor = Color.FromArgb(19, 46, 53);
|
||
}
|
||
else
|
||
{
|
||
item.DefaultCellStyle.BackColor = Color.FromArgb(27, 60, 68);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void StoreControlRatios(Control container)
|
||
{
|
||
foreach (Control ctrl in container.Controls)
|
||
{
|
||
// 存储位置比例
|
||
originalRatios.Add(ctrl,
|
||
new PointF((float)ctrl.Left / container.Width,
|
||
(float)ctrl.Top / container.Height));
|
||
|
||
// 存储尺寸比例
|
||
originalSizes.Add(ctrl,
|
||
new SizeF((float)ctrl.Width / container.Width,
|
||
(float)ctrl.Height / container.Height));
|
||
|
||
// 递归处理容器控件
|
||
if (ctrl.HasChildren) StoreControlRatios(ctrl);
|
||
}
|
||
}
|
||
|
||
private void ScaleControls(Control container)
|
||
{
|
||
foreach (var item in originalRatios)
|
||
{
|
||
if (!container.Controls.Contains(item.Key)) continue;
|
||
|
||
// 调整位置
|
||
item.Key.Left = (int)(container.Width * item.Value.X);
|
||
item.Key.Top = (int)(container.Height * item.Value.Y);
|
||
|
||
// 调整大小
|
||
if (originalSizes.TryGetValue(item.Key, out SizeF sizeRatio))
|
||
{
|
||
item.Key.Width = (int)(container.Width * sizeRatio.Width);
|
||
item.Key.Height = (int)(container.Height * sizeRatio.Height);
|
||
}
|
||
|
||
// 递归处理容器控件
|
||
if (item.Key.HasChildren) ScaleControls(item.Key);
|
||
}
|
||
}
|
||
|
||
public static void ExportdgvDataToCsv(DataGridView dataGridView, string filePath)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
|
||
// 添加列标题
|
||
foreach (DataGridViewColumn column in dataGridView.Columns)
|
||
{
|
||
sb.Append(column.HeaderText);
|
||
sb.Append(",");
|
||
}
|
||
sb.Remove(sb.Length - 1, 1);
|
||
sb.AppendLine();
|
||
|
||
// 添加行数据
|
||
foreach (DataGridViewRow row in dataGridView.Rows)
|
||
{
|
||
foreach (DataGridViewCell cell in row.Cells)
|
||
{
|
||
sb.Append(cell.Value);
|
||
sb.Append(",");
|
||
}
|
||
sb.Remove(sb.Length - 1, 1);
|
||
sb.AppendLine();
|
||
}
|
||
|
||
// 将数据写入CSV文件
|
||
File.WriteAllText(filePath, sb.ToString(), Encoding.GetEncoding("GBK"));
|
||
}
|
||
|
||
public static void ExportDataTableDataToCsv(DataTable dt, string filePath)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
|
||
// 添加列标题
|
||
foreach (DataColumn column in dt.Columns)
|
||
{
|
||
sb.Append(column.ColumnName);
|
||
sb.Append(",");
|
||
}
|
||
sb.Remove(sb.Length - 1, 1);
|
||
sb.AppendLine();
|
||
|
||
// 添加行数据
|
||
foreach (DataRow row in dt.Rows)
|
||
{
|
||
foreach (object item in row.ItemArray)
|
||
{
|
||
sb.Append(item);
|
||
sb.Append(",");
|
||
}
|
||
sb.Remove(sb.Length - 1, 1);
|
||
sb.AppendLine();
|
||
}
|
||
|
||
// 将数据写入CSV文件
|
||
File.WriteAllText(filePath, sb.ToString());
|
||
}
|
||
|
||
#endregion 方法
|
||
|
||
#region datagridview分页功能
|
||
|
||
private void LoadPage()
|
||
{
|
||
if (currentPage < 1) currentPage = 1;
|
||
if (currentPage > pageCount) currentPage = pageCount;
|
||
|
||
int beginRecord; //开始指针
|
||
int endRecord; //结束指针
|
||
DataTable dtTemp;
|
||
dtTemp = PageTable.Clone();//Clone方法只会复制DataTable的结构(列定义),而不会复制数据行的引用。
|
||
|
||
beginRecord = pageSize * (currentPage - 1);
|
||
if (currentPage == 1) beginRecord = 0;
|
||
endRecord = pageSize * currentPage;
|
||
|
||
if (currentPage == pageCount) endRecord = recordCount;
|
||
for (int i = beginRecord; i < endRecord; i++)
|
||
{
|
||
// 使用ImportRow方法将源PageTable的行导入到目标dtTemp
|
||
dtTemp.ImportRow(PageTable.Rows[i]);
|
||
}
|
||
|
||
dgvSelectMeasureData.Rows.Clear();
|
||
|
||
for (int i = 0; i < dtTemp.Rows.Count; i++)
|
||
{
|
||
dgvSelectMeasureData.Rows.Add(new object[] { dtTemp.Rows[i][0], dtTemp.Rows[i][1], dtTemp.Rows[i][2], dtTemp.Rows[i][3], dtTemp.Rows[i][4], dtTemp.Rows[i][5], dtTemp.Rows[i][6], dtTemp.Rows[i][7], dtTemp.Rows[i][8], dtTemp.Rows[i][9] });
|
||
}
|
||
|
||
SetdgvRowBgColor(dgvSelectMeasureData);
|
||
for (int i = 0; i < dgvSelectMeasureData.Rows.Count; i++)
|
||
{
|
||
string strResult = dgvSelectMeasureData.Rows[i].Cells["MeasureItemResult"].Value.ToString();
|
||
if (strResult.ToLower().Contains("ng"))
|
||
{
|
||
//dgvSelectMeasureData.Rows[i].Cells["MeasureItemResult"].Style.ForeColor = Color.Orange;
|
||
dgvSelectMeasureData.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
|
||
}
|
||
if (strResult.ToLower().Contains("rej"))
|
||
{
|
||
dgvSelectMeasureData.Rows[i].Cells["MeasureItemResult"].Style.ForeColor = Color.Red;
|
||
}
|
||
}
|
||
labCurrentPage.Text = "当前页:" + currentPage.ToString(); //当前页
|
||
labSumPages.Text = "共 " + pageCount.ToString() + " 页";//总页数
|
||
labSumRecorders.Text = "总共 " + recordCount.ToString() + " 条记录";//总记录数
|
||
}
|
||
|
||
private void rbtnFirtstPage_Click(object sender, EventArgs e) //首页
|
||
{
|
||
if (currentPage == 1)
|
||
{ return; }
|
||
currentPage = 1;
|
||
LoadPage();
|
||
}
|
||
|
||
private void rbtnPrevPage_Click(object sender, EventArgs e) //上一页
|
||
{
|
||
if (currentPage == 1)
|
||
{ return; }
|
||
currentPage--;
|
||
LoadPage();
|
||
}
|
||
|
||
private void rbtnNextPage_Click(object sender, EventArgs e) //下一页
|
||
{
|
||
if (currentPage == pageCount)
|
||
{ return; }
|
||
currentPage++;
|
||
LoadPage();
|
||
}
|
||
|
||
private void rbtnLastPage_Click(object sender, EventArgs e) //尾页
|
||
{
|
||
if (currentPage == pageCount)
|
||
{ return; }
|
||
currentPage = pageCount;
|
||
LoadPage();
|
||
}
|
||
|
||
private void rddlPageRecorderCount_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
|
||
{
|
||
pageSize = int.Parse(rddlPageRecorderCount.Text.Trim());
|
||
//PageSorter();
|
||
}
|
||
|
||
#endregion datagridview分页功能
|
||
|
||
private void tmSystem_Tick(object sender, EventArgs e)
|
||
{
|
||
rleTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
if (ConfigDfn.iAnalysisCSVFlag == 1)
|
||
rleReadCSVStatus.ForeColor = bReadCSVFlag ? Color.Green : Color.White;
|
||
bReadCSVFlag = !bReadCSVFlag;
|
||
}
|
||
|
||
private void FileSortTimer_Tick(object sender, EventArgs e) //分发定时器
|
||
{
|
||
// 后台运行,避免阻塞UI
|
||
Task.Run(() =>
|
||
{
|
||
try
|
||
{
|
||
fileSorter.ProcessFiles();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MyBase.TraceWriteLine($"分发任务异常: {ex.Message}");
|
||
}
|
||
});
|
||
}
|
||
|
||
#region 事件
|
||
|
||
private void rbtnSearchMeaserData_Click(object sender, EventArgs e) //查询测量数据
|
||
{
|
||
dgvFPYResult.Visible = false;
|
||
chartFPYLine.Visible = false;
|
||
rbtnExportCSVReport.Visible = false;
|
||
string strStartTime = rdtpStartTime.Value.ToString("yyyy-MM-dd") + " 00:00:00";
|
||
string strEndTime = rdtpEndTime.Value.ToString("yyyy-MM-dd") + " 23:59:59";
|
||
string strCarID = rtbPartID.Text.Trim();
|
||
PageTable.Clear();
|
||
PageTable = tmdal.SelectTMeasureDataByCarIDAndTime(strCarID, strStartTime, strEndTime);
|
||
if (PageTable.Rows.Count > 0)
|
||
{
|
||
recordCount = PageTable.Rows.Count; //记录总行数
|
||
pageCount = (recordCount / pageSize);
|
||
if ((recordCount % pageSize) > 0)
|
||
{
|
||
pageCount++;
|
||
}
|
||
|
||
//默认第一页
|
||
currentPage = 1;
|
||
|
||
LoadPage();//调用加载数据的方法
|
||
dgvSelectMeasureData.Visible = true;
|
||
rbtnExportCSVReport.Visible = true;
|
||
pnlPage.Visible = true;
|
||
labSearchResult.Visible = false;
|
||
pnlPage.Visible = true;
|
||
}
|
||
else
|
||
{
|
||
dgvSelectMeasureData.Visible = false;
|
||
labSearchResult.Visible = true;
|
||
rbtnExportCSVReport.Visible = false;
|
||
pnlPage.Visible = false;
|
||
}
|
||
}
|
||
|
||
private void rbtSelectFPY_Click(object sender, EventArgs e) //查询合格率
|
||
{
|
||
dgvSelectMeasureData.Visible = false;
|
||
rbtnExportCSVReport.Visible = false;
|
||
pnlPage.Visible = false;
|
||
string strStartTime = rdtpStartTime.Value.ToString("yyyy-MM-dd") + " 00:00:00";
|
||
string strEndTime = rdtpEndTime.Value.ToString("yyyy-MM-dd") + " 23:59:59";
|
||
string strCarID = rtbPartID.Text.Trim();
|
||
DataTable dtFPY = new DataTable();
|
||
dtFPY = tmdal.SelectTMeasureResultByTime(strCarID, strStartTime, strEndTime);
|
||
if (dtFPY.Rows.Count > 0)
|
||
{
|
||
dgvFPYResult.Visible = true;
|
||
labSearchResult.Visible = false;
|
||
rbtnExportCSVReport.Visible = true;
|
||
chartFPYLine.Visible = true;
|
||
dgvFPYResult.DataSource = dtFPY;
|
||
SetdgvRowBgColor(dgvFPYResult);
|
||
for (int i = 0; i < dgvFPYResult.Rows.Count; i++)
|
||
{
|
||
if (dgvFPYResult.Rows[i].Cells["SMResult"].Value.ToString().ToLower().Contains("不合格"))
|
||
{
|
||
dgvFPYResult.Rows[i].Cells["SMResult"].Style.ForeColor = Color.Red;
|
||
}
|
||
}
|
||
|
||
#region 合格率折线图
|
||
|
||
//合格率折线图
|
||
string[] strXDate = new string[dtFPY.Rows.Count];
|
||
double[] dFPYResult = new double[dtFPY.Rows.Count];
|
||
for (int i = 0; i < dtFPY.Rows.Count; i++)
|
||
{
|
||
strXDate[i] = dtFPY.Rows[i]["CarID"].ToString();
|
||
}
|
||
for (int i = 0; i < dtFPY.Rows.Count; i++)
|
||
{
|
||
double dResult = double.Parse(dtFPY.Rows[i]["FPY"].ToString());
|
||
dFPYResult[i] = dResult * 100.00;
|
||
}
|
||
chartFPYLine.ChartAreas[0].AxisX.LabelStyle.Angle = -50; //X轴标签倾斜角度设置
|
||
chartFPYLine.ChartAreas[0].AxisY.LabelStyle.Format = "{0:F2}" + "%"; //设置Y轴值的格式化 带%
|
||
chartFPYLine.Series[0].Label = "#VAL%"; //设置显示Y的值
|
||
chartFPYLine.Series[0].LabelForeColor = Color.White;
|
||
chartFPYLine.Series[0].ToolTip = "车身编号 : #VALX \r\n合格率 : #VAL%"; //鼠标移动到对应点显示数值
|
||
chartFPYLine.Series[0].Points.DataBindXY(strXDate, dFPYResult);
|
||
|
||
#endregion 合格率折线图
|
||
}
|
||
else
|
||
{
|
||
dgvFPYResult.Visible = false;
|
||
labSearchResult.Visible = true;
|
||
rbtnExportCSVReport.Visible = false;
|
||
chartFPYLine.Visible = false;
|
||
}
|
||
}
|
||
|
||
private void rbtnExportCSVReport_Click(object sender, EventArgs e) //导出CSV报表
|
||
{
|
||
string strCSVExportPath = "";
|
||
string strExportTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||
FolderBrowserDialog m_Folder = new FolderBrowserDialog();
|
||
m_Folder.Description = "请选择要导出的CSV文件的路径";
|
||
m_Folder.RootFolder = Environment.SpecialFolder.Desktop;
|
||
if (dgvFPYResult.Visible == true && dgvFPYResult.Rows.Count > 0)
|
||
{
|
||
m_Folder.ShowDialog();
|
||
if (Directory.Exists(m_Folder.SelectedPath))
|
||
{
|
||
strCSVExportPath = m_Folder.SelectedPath + "\\合格率" + strExportTime + ".CSV";
|
||
|
||
try
|
||
{
|
||
ExportdgvDataToCsv(dgvFPYResult, strCSVExportPath);
|
||
MyBase.TraceWriteLine("合格率数据导出成功,路径为:" + strCSVExportPath);
|
||
//弹窗 提示
|
||
MessageBox.Show("合格率数据导出成功,路径为:" + strCSVExportPath, "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MyBase.TraceWriteLine("导出合格率数据时发生错误:" + ex.Message);
|
||
}
|
||
}
|
||
}
|
||
if (dgvSelectMeasureData.Visible == true && dgvSelectMeasureData.Rows.Count > 0)
|
||
{
|
||
m_Folder.ShowDialog();
|
||
if (Directory.Exists(m_Folder.SelectedPath))
|
||
{
|
||
strCSVExportPath = m_Folder.SelectedPath + "\\车身尺寸数据" + strExportTime + ".CSV";
|
||
|
||
try
|
||
{
|
||
ExportDataTableDataToCsv(PageTable, strCSVExportPath);
|
||
MyBase.TraceWriteLine("车身尺寸数据导出成功,路径为:" + strCSVExportPath);
|
||
|
||
//弹窗 提示
|
||
MessageBox.Show("车身尺寸数据导出成功,路径为:" + strCSVExportPath, "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MyBase.TraceWriteLine("导出车身尺寸数据时发生错误:" + ex.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void dgvFPYResult_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
|
||
{
|
||
e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
|
||
}
|
||
|
||
private void dgvSelectMeasureData_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
|
||
{
|
||
e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
|
||
}
|
||
|
||
private void lpcSoftwareSetup_Click(object sender, EventArgs e) //软件设置
|
||
{
|
||
//string strInputPwd = MyBase.InputBox("密码", "请输入密码 : ", "", "确定", "取消");
|
||
//if (strInputPwd.Contains("-999.999"))
|
||
//{
|
||
// return;
|
||
//}
|
||
//else if (strInputPwd == ConfigDfn.strPwd)
|
||
{
|
||
FSoftwareSetup fss = new FSoftwareSetup();
|
||
fss.ShowDialog(this);
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("密码错误! ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
//}
|
||
}
|
||
|
||
private void lpcToleranceSetup_Click(object sender, EventArgs e) //分发设置
|
||
{
|
||
//string strInputPwd = MyBase.InputBox("密码", "请输入密码 : ", "", "确定", "取消");
|
||
//if (strInputPwd.Contains("-999.999"))
|
||
//{
|
||
// return;
|
||
//}
|
||
//else if (strInputPwd == ConfigDfn.strPwd)
|
||
{
|
||
FConfigSetup fts = new FConfigSetup();
|
||
fts.ShowDialog(this);
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("密码错误! ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
//}
|
||
}
|
||
|
||
private void lpcAboutSoftware_Click(object sender, EventArgs e) //关于软件
|
||
{
|
||
AboutSoftware asi = new AboutSoftware();
|
||
asi.ShowDialog();
|
||
}
|
||
|
||
private void lpcShowLog_Click(object sender, EventArgs e) //查看日志
|
||
{
|
||
System.Diagnostics.Process.Start("notepad.exe", LogDebugDfn.strDebugFile);
|
||
}
|
||
|
||
#endregion 事件
|
||
}
|
||
|
||
public class AnalysisResult
|
||
{
|
||
public string CarID { get; set; }
|
||
public int TotalCount { get; set; }
|
||
public int OutCount { get; set; }
|
||
public int OKCount { get; set; }
|
||
public int RejectedCount { get; set; }
|
||
public double FPYPercent { get; set; }
|
||
|
||
public static AnalysisResult AnalyzeMeasureData(DataTable dt)
|
||
{
|
||
var result = new AnalysisResult();
|
||
|
||
if (dt == null || dt.Rows.Count == 0)
|
||
return result;
|
||
|
||
// Accessing the first row of the DataTable to retrieve values
|
||
var firstRow = dt.Rows[0];
|
||
|
||
result.CarID = firstRow["CarID"].ToString();
|
||
result.TotalCount = Convert.ToInt32(firstRow["TotalCount"]);
|
||
result.OKCount = Convert.ToInt32(firstRow["PassCount"]);
|
||
result.OutCount = Convert.ToInt32(firstRow["NGCount"]);
|
||
result.RejectedCount = Convert.ToInt32(firstRow["RejectCount"]);
|
||
result.FPYPercent = Convert.ToDouble(firstRow["FPY"]);
|
||
|
||
return result;
|
||
}
|
||
|
||
public static void DisplayAnalysisResult(AnalysisResult result)
|
||
{
|
||
Console.WriteLine("===== 测量结果分析 =====");
|
||
Console.WriteLine($"车身编号: {result.CarID}");
|
||
Console.WriteLine($"总测量项: {result.TotalCount}");
|
||
Console.WriteLine($"合格项(OK): {result.OKCount}");
|
||
Console.WriteLine($"超差项(Out): {result.OutCount}");
|
||
Console.WriteLine($"异常项(Rejected): {result.RejectedCount}");
|
||
Console.WriteLine($"首次通过率(FPY): {result.FPYPercent:F2}%");
|
||
}
|
||
}
|
||
} |