alielie
2026-06-15 f08871fe7d148d667f2f11f80f64de71a0d9504c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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();
            }
        }
    }
}