using System; using System.Data; using System.IO; namespace HMI.Simple.Util { public class ProductModelHelper { public static bool GetModelBySNR(string snr, out string model, out string msg) { model = string.Empty; msg = string.Empty; SqliteHelper sqliteHelper = null; try { string appFolder = RegUtil.GetLocaltion(); string dbFilePath = Path.Combine(appFolder, "Config", "ModelManage", "data.db"); if (!File.Exists(dbFilePath)) { msg = "数据库文件不存在"; return false; } sqliteHelper = new SqliteHelper(dbFilePath); int codeLength = snr.Length; var sql = $@"SELECT Model, CodeLength, StartIndex, Fixcode FROM ModelData WHERE CodeLength = {codeLength} AND SUBSTR('{snr}', StartIndex, LENGTH(Fixcode) + StartIndex + 1) = Fixcode"; //var sql = $@"SELECT Model, CodeLength, StartIndex, Length, Fixcode FROM ModelData WHERE CodeLength={codeLength} AND '{snr}' LIKE '%'||Fixcode||'%'"; DataTable dt = sqliteHelper.GetDataTable(sql); if (dt.Rows.Count <= 0) { msg = "没有匹配的型号"; return false; } foreach (DataRow dr in dt.Rows) { var startIndex = int.Parse(dr["StartIndex"].ToString()); var fixcode = dr["Fixcode"].ToString(); if (string.Equals(snr.Substring(startIndex, fixcode.Length), fixcode, StringComparison.CurrentCultureIgnoreCase)) { model = dr["Model"].ToString(); return true; } } msg = "没有匹配的型号"; return false; } catch (Exception ex) { msg = ex.Message; return false; } finally { sqliteHelper?.Dispose(); } } public static bool GetModelBySNR(string dbFilePath, string snr, out string model, out string msg) { model = string.Empty; msg = string.Empty; SqliteHelper sqliteHelper = null; try { if (!File.Exists(dbFilePath)) { msg = "数据库文件不存在"; return false; } sqliteHelper = new SqliteHelper(dbFilePath); int codeLength = snr.Length; var sql = $@"SELECT Model, CodeLength, StartIndex, Fixcode FROM ModelData WHERE CodeLength = {codeLength} AND SUBSTR('{snr}', StartIndex, LENGTH(Fixcode) + StartIndex + 1) = Fixcode"; DataTable dt = sqliteHelper.GetDataTable(sql); if (dt.Rows.Count <= 0) { msg = "没有匹配的型号"; return false; } foreach (DataRow dr in dt.Rows) { var startIndex = int.Parse(dr["StartIndex"].ToString()); var fixcode = dr["Fixcode"].ToString(); if (string.Equals(snr.Substring(startIndex, fixcode.Length), fixcode, StringComparison.CurrentCultureIgnoreCase)) { model = dr["Model"].ToString(); return true; } } msg = "没有匹配的型号"; return false; } catch (Exception ex) { msg = ex.Message; return false; } finally { sqliteHelper?.Dispose(); } } } }