using System; using System.Data; using System.IO; namespace HMI.Simple.Util { public class SerialDataHelper { public static bool GetFailCountBySNR(string snr, out int count, out string msg) { count = -1; msg = string.Empty; SqliteHelper sqliteHelper = null; try { string dataLogPath = ""; var value = ParameterUtil.GetCommonParameterValue("TS_DataLogPath", out msg); if (!string.IsNullOrEmpty(msg)) { string appFolder = RegUtil.GetLocaltion(); dataLogPath = Path.Combine(appFolder, "Config", "DataBoard", "DataLog.db"); } if (!File.Exists(dataLogPath)) { msg = "数据库文件不存在"; return false; } sqliteHelper = new SqliteHelper(dataLogPath); var sql = $@"SELECT COUNT(*) as num FROM testResult WHERE sn = '{snr}' AND result = 'Failed';"; DataTable dt = sqliteHelper.GetDataTable(sql); if (dt.Rows.Count <= 0) { count = 0; return true; } count = int.Parse(dt.Rows[0]["num"].ToString()); return true; } catch (Exception ex) { msg = ex.Message; return false; } finally { sqliteHelper?.Dispose(); } } public static bool GetCountBySNRAndPosition(string snr, string position, out int okCount, out int ngCount, out string status, out string msg) { okCount = -1; ngCount = -1; status = string.Empty; msg = string.Empty; SqliteHelper sqliteHelper = null; try { string dataLogPath = ""; var value = ParameterUtil.GetCommonParameterValue("TS_DataLogPath", out msg); if (!string.IsNullOrEmpty(msg)) { string appFolder = RegUtil.GetLocaltion(); dataLogPath = Path.Combine(appFolder, "Config", "DataBoard", "DataLog.db"); } if (!File.Exists(dataLogPath)) { msg = "数据库文件不存在"; return false; } sqliteHelper = new SqliteHelper(dataLogPath); var sql = $@"SELECT COUNT(*) as num FROM testResult WHERE sn = '{snr}' AND position = '{position}' AND result = 'Failed';"; DataTable dt = sqliteHelper.GetDataTable(sql); if (dt.Rows.Count <= 0) { ngCount = 0; return true; } ngCount = int.Parse(dt.Rows[0]["num"].ToString()); sql = $@"SELECT COUNT(*) as num FROM testResult WHERE sn = '{snr}' AND position = '{position}' AND result = 'Passed';"; dt = sqliteHelper.GetDataTable(sql); if (dt.Rows.Count <= 0) { okCount = 0; return true; } okCount = int.Parse(dt.Rows[0]["num"].ToString()); sql = $@"SELECT status as status FROM testResult WHERE sn = '{snr}' ORDER BY time DESC LIMIT 0,1;"; dt = sqliteHelper.GetDataTable(sql); if (dt.Rows.Count <= 0) { status = string.Empty; return true; } status = dt.Rows[0]["status"] == DBNull.Value ? string.Empty : dt.Rows[0]["status"].ToString(); return true; } catch (Exception ex) { msg = ex.Message; return false; } finally { sqliteHelper?.Dispose(); } } } }