chenluhua1980
7 天以前 ceb64b6612309fe384e096dcdc8b5a5e0dfe6cce
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "stdafx.h"
#include "GB2860SQLite.h"
#include "ToolUnits.h"
 
 
CGB2860SQLite::CGB2860SQLite()
{
    m_db = nullptr;
}
 
 
CGB2860SQLite::~CGB2860SQLite()
{
}
 
void CGB2860SQLite::setDbFilepath(const char* pszFilepath)
{
    m_strDbFilepath = pszFilepath;
}
 
int CGB2860SQLite::init(std::string& strError)
{
    char* pszError = nullptr;
    int nRet;
 
 
    // ´ò¿ªÖ¸¶¨µÄÊý¾Ý¿âÎļþ, Èç¹û²»´æÔÚ½«´´½¨Ò»¸öͬÃûµÄÊý¾Ý¿âÎļþ
    nRet = sqlite3_open(m_strDbFilepath.c_str(), &m_db);
    if (nRet != 0) {
        sqlite3_close(m_db);
        m_db = nullptr;
        return -1;
    }
 
 
    // ´´½¨Panel±í, Èç¹û¸Ã±í´æÔÚ£¬Ôò²»´´½¨£¬
    // ²¢¸ø³öÌáʾÐÅÏ¢£¬´æ´¢ÔÚzErrMsg ÖÐ
    char* sql = "CREATE TABLE Panels (ID INTEGER PRIMARY KEY,"        \
        "PanelID VARCHAR(64),"                                        \
        "UnitId INTEGER,"                                            \
        "RecipeName VARCHAR(64),"                                    \
        "ReceivedTime TIME,"                                        \
        "BeginSamplingTime TIME,"                                    \
        "EndSamplingTime TIME,"                                        \
        "RemovedTime TIME,"                                            \
        "Ari1 REAL,"                                                \
        "Ari2 REAL,"                                                \
        "Ari3 REAL,"                                                \
        "Pre1 INTEGER,"                                                \
        "Tmp1 REAL,"                                                \
        "Tmp2 REAL,"                                                \
        "Temp0Max REAL,"                                            \
        "Temp0Min REAL,"                                            \
        "Temp0Ave REAL,"                                            \
        "Temp1Max REAL,"                                            \
        "Temp1Min REAL,"                                            \
        "Temp1Ave REAL,"                                            \
        "Temp2Max REAL,"                                            \
        "Temp2Min REAL,"                                            \
        "Temp2Ave REAL,"                                            \
        "Temp3Max REAL,"                                            \
        "Temp3Min REAL,"                                            \
        "Temp3Ave REAL,"                                            \
        "Temp4Max REAL,"                                            \
        "Temp4Min REAL,"                                            \
        "Temp4Ave REAL,"                                            \
        "Temp5Max REAL,"                                            \
        "Temp5Min REAL,"                                            \
        "Temp5Ave REAL,"                                            \
        "Temp6Max REAL,"                                            \
        "Temp6Min REAL,"                                            \
        "Temp6Ave REAL,"                                            \
        "Temp7Max REAL,"                                            \
        "Temp7Min REAL,"                                            \
        "Temp7Ave REAL,"                                            \
        "Temp8Max REAL,"                                            \
        "Temp8Min REAL,"                                            \
        "Temp8Ave REAL,"                                            \
        "Temp9Max REAL,"                                            \
        "Temp9Min REAL,"                                            \
        "Temp9Ave REAL,"                                            \
        "VacuumMax REAL,"                                        \
        "VacuumMin REAL,"                                        \
        "VacuumAve REAL,"                                        \
        "Pressure0Max REAL,"                                        \
        "Pressure0Min REAL,"                                        \
        "Pressure0Ave REAL,"                                        \
        "Pressure1Max REAL,"                                        \
        "Pressure1Min REAL,"                                        \
        "Pressure1Ave REAL,"                                        \
        "Pressure2Max REAL,"                                        \
        "Pressure2Min REAL,"                                        \
        "Pressure2Ave REAL,"                                        \
        "Pressure3Max REAL,"                                        \
        "Pressure3Min REAL,"                                        \
        "Pressure3Ave REAL,"                                        \
        "Pressure4Max REAL,"                                        \
        "Pressure4Min REAL,"                                        \
        "Pressure4Ave REAL,"                                        \
        "Datas BLOB);";
    nRet = sqlite3_exec(m_db, sql, 0, 0, &pszError);
    if (pszError != nullptr) {
        strError = pszError;
        sqlite3_free(pszError);
    }
 
    return nRet;
}
 
int CGB2860SQLite::term()
{
    if (m_db != nullptr) {
        sqlite3_close(m_db);
        m_db = nullptr;
    }
 
    return 0;
}
 
int CGB2860SQLite::insertPanel(CPanel* pPanel, std::string& strError)
{
    if (m_db == nullptr) return -1;
 
    //²åÈëÊý¾Ý
    char* pszError = nullptr;
    char szSQL[1024];
 
    UNITDATA1& dataMax = pPanel->getMathData(IMAX);
    UNITDATA1& dataMin = pPanel->getMathData(IMIN);
    UNITDATA1& dataAve = pPanel->getMathData(IAVE);
    sprintf_s(szSQL, 1024, "INSERT INTO Panels VALUES(NULL, '%s', %d, '%s', "        \
        "'%s', '%s', '%s', '%s',"        \
        "%f, %f, %f, %d, %f, %f, "        \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f,"            \
        "%f, %f, %f, ?);",
        pPanel->getQRCode().c_str(),
        pPanel->getUnitId(),
        pPanel->getRecipeName().c_str(),
        CToolUnits::timeToString2(pPanel->getReceivedTime()).c_str(),
        CToolUnits::timeToString2(pPanel->getBeginSamplingTime()).c_str(),
        CToolUnits::timeToString2(pPanel->getEndSamplingTime()).c_str(),
        CToolUnits::timeToString2(pPanel->getRemovedTime()).c_str(),
        pPanel->getAir1(),
        pPanel->getAir2(),
        pPanel->getAir3(),
        pPanel->getPre1(),
        pPanel->getTmp1(),
        pPanel->getTmp2(),
        dataMax.temp[0], dataMin.temp[0], dataAve.temp[0],
        dataMax.temp[1], dataMin.temp[1], dataAve.temp[1],
        dataMax.temp[2], dataMin.temp[2], dataAve.temp[2],
        dataMax.temp[3], dataMin.temp[3], dataAve.temp[3],
        dataMax.temp[4], dataMin.temp[4], dataAve.temp[4],
        dataMax.temp[5], dataMin.temp[5], dataAve.temp[5],
        dataMax.temp[6], dataMin.temp[6], dataAve.temp[6],
        dataMax.temp[7], dataMin.temp[7], dataAve.temp[7],
        dataMax.temp[8], dataMin.temp[8], dataAve.temp[8],
        dataMax.temp[9], dataMin.temp[9], dataAve.temp[9],
        dataMax.vacuum, dataMin.vacuum, dataAve.vacuum,
        dataMax.pressure[0], dataMin.pressure[0], dataAve.pressure[0],
        dataMax.pressure[1], dataMin.pressure[1], dataAve.pressure[1],
        dataMax.pressure[2], dataMin.pressure[2], dataAve.pressure[2],
        dataMax.pressure[3], dataMin.pressure[3], dataAve.pressure[3],
        dataMax.pressure[4], dataMin.pressure[4], dataAve.pressure[4]);
 
    char* pBlod = nullptr;
    int nSize = pPanel->getBlodLen();
    pBlod = new char[nSize];
    pPanel->getBlod(pBlod, nSize);
    sqlite3_stmt *stmt;
    sqlite3_prepare(m_db, szSQL, (int)strlen(szSQL), &stmt, 0);
    sqlite3_bind_blob(stmt, 1, pBlod, nSize, NULL);
    int nRet = sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    return 0;
}
 
int CGB2860SQLite::getPanelList(const char* pszKeyword, int nSearchFlag,
    int dtFlag, const char* pszStartTime, const char* pszEndTime,
    std::list<CPanel*>& list, std::string& strError)
{
    if (m_db == nullptr) return -1;
 
    int nRet;
    CString strSQL = _T("SELECT * FROM Panels Where (1=2");
    if ((nSearchFlag & 0x01) == 0x01) {
        strSQL.AppendFormat(" OR PanelID like '%s%s%s'", "%", pszKeyword, "%");
    }
    strSQL.Append(")");
    if (dtFlag != 0) {
        strSQL.AppendFormat(" AND (ReceivedTime BETWEEN '%s' AND '%s')",
            pszStartTime, pszEndTime);
    }
    strSQL.Append(" ORDER BY ID DESC LIMIT 3000");    // ASC
 
    sqlite3_stmt *stmt;
    sqlite3_prepare(m_db, (LPTSTR)(LPCTSTR)strSQL, strSQL.GetLength(), &stmt, 0);
    nRet = sqlite3_step(stmt);
 
    while (nRet == SQLITE_ROW) {
        int id, unitId;
        double air1, air2, air3, tmp1, tmp2;
        int pre1;
        const unsigned char* pszQRCode, *pszRecipeName;
        const unsigned char* pszReceivedTime, *pszBeginTime, *pszEndTime, *pszRemovedTime;
        const void * pBolbData;
        int nBlodDataLen;
 
        id = sqlite3_column_int(stmt, 0);
        pszQRCode = sqlite3_column_text(stmt, 1);
        unitId = sqlite3_column_int(stmt, 2);
        pszRecipeName = sqlite3_column_text(stmt, 3);
        pszReceivedTime = sqlite3_column_text(stmt, 4);
        pszBeginTime = sqlite3_column_text(stmt, 5);
        pszEndTime = sqlite3_column_text(stmt, 6);
        pszRemovedTime = sqlite3_column_text(stmt, 7);
        air1 = sqlite3_column_double(stmt, 8);
        air2 = sqlite3_column_double(stmt, 9);
        air3 = sqlite3_column_double(stmt, 10);
        pre1 = sqlite3_column_int(stmt, 11);
        tmp1 = sqlite3_column_double(stmt, 12);
        tmp2 = sqlite3_column_double(stmt, 13);
        pBolbData = sqlite3_column_blob(stmt, 62);
        nBlodDataLen = sqlite3_column_bytes(stmt, 62);
 
        CPanel* pPanel = new CPanel();
        pPanel->setQRCode((const char*)pszQRCode);
        pPanel->setUnitId(unitId);
        pPanel->setRecipeName((const char*)pszRecipeName);
        pPanel->setReceivedTime(CToolUnits::stringToTime((const char*)pszReceivedTime));
        pPanel->setBeginSamplingTime(CToolUnits::stringToTime((const char*)pszBeginTime));
        pPanel->setEndSamplingTime(CToolUnits::stringToTime((const char*)pszEndTime));
        pPanel->setRemovedTime(CToolUnits::stringToTime((const char*)pszRemovedTime));
        pPanel->setAir1(air1);
        pPanel->setAir2(air2);
        pPanel->setAir3(air3);
        pPanel->setPre1(pre1);
        pPanel->setTmp1(tmp1);
        pPanel->setTmp2(tmp2);
        pPanel->setBlod((char*)pBolbData, nBlodDataLen);
        list.push_back(pPanel);
 
        nRet = sqlite3_step(stmt);
    }
    sqlite3_finalize(stmt);
 
    return (int)list.size();
}
 
int CGB2860SQLite::getPanelWithPanelId(const char* pszBoxId, CPanel*& pPanel, std::string& strError)
{
    if (m_db == nullptr) return -1;
 
    int nRet;
    CString strSQL;
    strSQL.Format(_T("SELECT * FROM Panels Where PanelID = '%s' ORDER BY ID ASC"), pszBoxId);
    sqlite3_stmt *stmt;
    sqlite3_prepare(m_db, (LPTSTR)(LPCTSTR)strSQL, strSQL.GetLength(), &stmt, 0);
    nRet = sqlite3_step(stmt);
 
    if (nRet == SQLITE_ROW) {
        int id, unitId;
        double air1, air2, air3, tmp1, tmp2;
        int pre1;
        const unsigned char* pszQRCode, *pszRecipeName;
        const unsigned char* pszReceivedTime, *pszBeginTime, *pszEndTime, *pszRemovedTime;
        const void * pBolbData;
        int nBlodDataLen;
 
        id = sqlite3_column_int(stmt, 0);
        pszQRCode = sqlite3_column_text(stmt, 1);
        unitId = sqlite3_column_int(stmt, 2);
        pszRecipeName = sqlite3_column_text(stmt, 3);
        pszReceivedTime = sqlite3_column_text(stmt, 4);
        pszBeginTime = sqlite3_column_text(stmt, 5);
        pszEndTime = sqlite3_column_text(stmt, 6);
        pszRemovedTime = sqlite3_column_text(stmt, 7);
        air1 = sqlite3_column_double(stmt, 8);
        air2 = sqlite3_column_double(stmt, 9);
        air3 = sqlite3_column_double(stmt, 10);
        pre1 = sqlite3_column_int(stmt, 11);
        tmp1 = sqlite3_column_double(stmt, 12);
        tmp2 = sqlite3_column_double(stmt, 13);
        pBolbData = sqlite3_column_blob(stmt, 62);
        nBlodDataLen = sqlite3_column_bytes(stmt, 62);
 
        CPanel* pPanel = new CPanel();
        pPanel->setQRCode((const char*)pszQRCode);
        pPanel->setUnitId(unitId);
        pPanel->setRecipeName((const char*)pszRecipeName);
        pPanel->setReceivedTime(CToolUnits::stringToTime((const char*)pszReceivedTime));
        pPanel->setBeginSamplingTime(CToolUnits::stringToTime((const char*)pszBeginTime));
        pPanel->setEndSamplingTime(CToolUnits::stringToTime((const char*)pszEndTime));
        pPanel->setRemovedTime(CToolUnits::stringToTime((const char*)pszRemovedTime));
        pPanel->setAir1(air1);
        pPanel->setAir2(air2);
        pPanel->setAir3(air3);
        pPanel->setPre1(pre1);
        pPanel->setTmp1(tmp1);
        pPanel->setTmp2(tmp2);
        pPanel->setBlod((char*)pBolbData, nBlodDataLen);
    }
    sqlite3_finalize(stmt);
 
    return 0;
}