databaseProc.cs
16.5 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
public class databaseProc
{
SQLiteHelper sh = new SQLiteHelper();
IdWorker id = new IdWorker(1, 1);
static databaseProc _databaseProc;
public static databaseProc Current
{
get
{
if (_databaseProc == null)
{
_databaseProc = new databaseProc();
}
return _databaseProc;
}
}
public long GetID()
{
return id.nextId();
}
private databaseProc()
{
SQLiteHelper.SetConnectionString(Application.StartupPath + "\\sys.db");
string sql;
//添加MaterialQuantity表
sql = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='ReelInfo';";
int hastable = (int)(long)sh.ExecuteScalar(sql);
if (hastable == 0)
{
sql = @"
CREATE TABLE ""ReelInfo"" (
""ID"" integer NOT NULL COLLATE BINARY,
""PN"" text,
""RI"" TEXT,
""ISNG"" integer,
""NGMSG"" TEXT,
""QTY"" integer,
""LBLState"" TEXT,
""OMSG"" TEXT,
""2D_Barcode"" TEXT,
""DateTime"" date,
PRIMARY KEY (""ID"")
);
";
sh.ExecuteNonQuery(sql);
}
else
{
//添加后期添加的字段
sql = "select sql from sqlite_master where name='ReelInfo';";
string sc = (string)sh.ExecuteScalar(sql);
sc = sc.Replace("\"", "");
if (!sc.Contains("Slot integer"))
{
sql = "alter table ReelInfo add column Slot integer;";
//sh.ExecuteNonQuery(sql);
}
if (!sc.Contains("imgfileid TEXT"))
{
sql = "alter table ReelInfo add column imgfileid TEXT;";
sh.ExecuteNonQuery(sql);
sql = "alter table ReelInfo add column resultimage TEXT;";
sh.ExecuteNonQuery(sql);
}
if (!sc.Contains("algo TEXT"))
{
sql = "alter table ReelInfo add column algo TEXT;";
sh.ExecuteNonQuery(sql);
}
}
}
public void InsertOrUpdateRI(long uid, string PN, string RI, bool isng, string ngmsg, int quantity, string lblstate, string OMSG, string tdbarcode, string XRayFile, string ResultFile, string algo)
{
string sql = "SELECT COUNT(*) FROM ReelInfo WHERE ID = " + uid.ToString();
long sc = (long)sh.ExecuteScalar(sql);
if (sc == 0)
{
sql = "INSERT INTO ReelInfo VALUES(@id, @PN, @RI, @isng, @ngmsg,@quantity,@lblstate,@OMSG,@tdbarcode,@datetime,@imgfileid,@resultimage,@algo)";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@id",uid),
new SQLiteParameter("@PN",PN),
new SQLiteParameter("@RI",RI),
new SQLiteParameter("@isng",isng),
new SQLiteParameter("@ngmsg",ngmsg),
new SQLiteParameter("@quantity",quantity),
new SQLiteParameter("@lblstate",lblstate),
new SQLiteParameter("@OMSG",OMSG),
new SQLiteParameter("@tdbarcode",tdbarcode),
new SQLiteParameter("@datetime",DateTime.Now),
new SQLiteParameter("@imgfileid",XRayFile),
new SQLiteParameter("@resultimage",ResultFile),
new SQLiteParameter("@algo",algo)
};
sh.ExecuteNonQuery(sql, para);
}
else
{
sql = @"
UPDATE ReelInfo
SET
ISNG=@ISNG,
NGMSG=@NGMSG,
QTY=@QTY,
LBLState=@LBLState,
OMSG=@OMSG
WHERE
id =@id";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@id",uid),
new SQLiteParameter("@ISNG",isng),
new SQLiteParameter("@NGMSG",ngmsg),
new SQLiteParameter("@QTY",quantity),
new SQLiteParameter("@LBLState",lblstate),
new SQLiteParameter("@OMSG",OMSG),
};
sh.ExecuteNonQuery(sql, para);
}
}
public void DeleteDataByDate(int day)
{
string sql = "DELETE FROM ReelInfo WHERE DateTime < date('now', '-" + day + " days');";
SQLiteParameter[] para = new SQLiteParameter[] { };
sh.ExecuteNonQuery(sql, para);
}
public void InsertLog(string device, string action, object state, string log = "")
{
string sql = "INSERT INTO ReelInfo VALUES(NULL, @device,@action, @state, @log,@datetime,NULL)";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@device",device),
new SQLiteParameter("@action",action),
new SQLiteParameter("@state",state.ToString()),
new SQLiteParameter("@log",log),
new SQLiteParameter("@datetime",DateTime.Now)
};
sh.ExecuteNonQuery(sql, para);
}
public long insertImage(byte[] b)
{
long ids = id.nextId();
string sql = "INSERT INTO img VALUES(@id, @imgblob, @datetime)";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@id",ids),
new SQLiteParameter("@imgblob",b),
new SQLiteParameter("@datetime",DateTime.Now)
};
sh.ExecuteNonQuery(sql, para);
return ids;
}
public Image getImg(long ids)
{
string sql = @"SELECT imgblob FROM img WHERE id = @id LIMIT 1";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@id",ids)
};
byte[] buffer = (byte[])sh.ExecuteScalar(sql, para);
MemoryStream ms = new MemoryStream(buffer);
Image i = Image.FromStream(ms);
ms.Close();
return i;
}
public DataTable GetDatabydate(DateTime startData, DateTime endDate, bool SortDESC = false)
{
string where = "";
string sql = @"SELECT * FROM ReelInfo WHERE
(datetime BETWEEN @startData AND @endDate) " + where + " ORDER BY datetime " + (SortDESC ? "DESC" : "ASC");
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@startData",startData),
new SQLiteParameter("@endDate",endDate)
};
DataTable dt = sh.ExecuteQuery(sql, para);
return dt;
}
public DataTable GetDatabyWhereString(string where, bool SortDESC = false)
{
string sql = @"SELECT * FROM ReelInfo WHERE " + where + " ORDER BY datetime " + (SortDESC ? "DESC" : "ASC");
DataTable dt = sh.ExecuteQuery(sql);
return dt;
}
public DataTable GetLogbydate(DateTime startData, DateTime endDate)
{
string sql = @"select device,action,state,log,datetime from logs WHERE datetime BETWEEN @startData AND @endDate ORDER BY datetime desc";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@startData",startData),
new SQLiteParameter("@endDate",endDate)
};
DataTable dt = sh.ExecuteQuery(sql, para);
return dt;
}
public DataTable GetDaySUM(string user)
{
string sql = "SELECT count(id) as count,substr(datetime,1,10) as date FROM MaterialQuantity GROUP BY date ORDER BY datetime desc";
if (!string.IsNullOrEmpty(user))
sql = "SELECT count(id) as count,substr(datetime,1,10) as date FROM MaterialQuantity where JobNumber=@JobNumber GROUP BY date ORDER BY datetime desc";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@JobNumber",user)
};
DataTable dt = sh.ExecuteQuery(sql, para);
return dt;
}
public bool AddUser(string username, string jobnumber, string password, int role)
{
if (string.IsNullOrEmpty(username))
username = jobnumber;
string sql = "INSERT INTO Users VALUES(@username, @jobnumber, @password,@role,@createdate,@lastlogindate)";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@username",username),
new SQLiteParameter("@jobnumber",jobnumber),
new SQLiteParameter("@password",password),
new SQLiteParameter("@role",role),
new SQLiteParameter("@createdate",DateTime.Now),
new SQLiteParameter("@lastlogindate",DateTime.Now)
};
int c = sh.ExecuteNonQuery(sql, para);
if (c == 1)
{
return true;
}
else
return false;
}
public bool TestUser(string username, string jobnumber, string password, out int role)
{
role = 0;
string sqlpart = " jobnumber = @jobnumber ";
if (string.IsNullOrEmpty(jobnumber))
sqlpart = " username = @username ";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@username",username),
new SQLiteParameter("@jobnumber",jobnumber),
new SQLiteParameter("@password",password),
};
string sql = @"select username,jobnumber,role from users WHERE " + sqlpart + " and password=@password";
var dt = sh.ExecuteQuery(sql, para);
if (dt.Rows.Count == 0)
{
return false;
}
else
{
role = (int)(long)dt.Rows[0]["role"];
sql = @"update users set lastlogindate = @lastlogindate WHERE " + sqlpart;
para = new SQLiteParameter[] {
new SQLiteParameter("@username",username),
new SQLiteParameter("@jobnumber",jobnumber),
new SQLiteParameter("@lastlogindate",DateTime.Now),
};
sh.ExecuteNonQuery(sql, para);
return true;
}
}
public void ModifyUser(string username, string jobnumber, string password, int role)
{
string sqlpart = " jobnumber = @jobnumber ";
if (string.IsNullOrEmpty(jobnumber))
sqlpart = " username = @username ";
string sql = @"update users set password = @password,jobnumber=@jobnumber,role=@role WHERE " + sqlpart;
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@username",username),
new SQLiteParameter("@jobnumber",jobnumber),
new SQLiteParameter("@password",password),
new SQLiteParameter("@role",role)
};
sh.ExecuteNonQuery(sql, para);
}
public void DeleteUser(string username)
{
string sql = @"delete from Users WHERE username = @username";
SQLiteParameter[] para = new SQLiteParameter[] {
new SQLiteParameter("@username",username),
};
sh.ExecuteNonQuery(sql, para);
}
public DataTable GetUserList()
{
string sql = @"select * from users";
return sh.ExecuteQuery(sql);
}
public void backup()
{
Directory.CreateDirectory("dbbackup");
var n = DateTime.Now.Month - 1;
var bckfile = "dbbackup\\Sys-" + n + ".bck";
if (File.Exists(bckfile))
{
return;
}
File.Copy("sys.db", bckfile);
var d = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01");
//string sql = "delete FROM MaterialQuantity WHERE datetime < '"+ d + "';delete FROM logs WHERE datetime < '" + d + "';";
//sh.ExecuteNonQuery(sql);
}
HashSet<string> AlgoPnlist = new HashSet<string>();
public void LoadDISTINCTPn()
{
if (!File.Exists("Config\\PnList.txt"))
{
string sql = @"SELECT DISTINCT ReelInfo.PN FROM ReelInfo";
var dt = sh.ExecuteQuery(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
AlgoPnlist.Add(dt.Rows[i][0].ToString());
}
File.AppendAllLines("Config\\PnList.txt", AlgoPnlist.ToArray());
}
else {
var fls = File.ReadAllLines("Config\\PnList.txt");
for (int i = 0; i < fls.Length; i++)
{
AlgoPnlist.Add(fls[i].ToString());
}
}
Pn_Algo_Match.OnloadAlgoPN += Pn_Algo_Match_OnloadAlgoPN;
}
private void Pn_Algo_Match_OnloadAlgoPN(object sender, string[] e)
{
foreach (var s in e)
AppendPn(s);
}
public void AppendPn(string pn) {
if (!AlgoPnlist.TryGetValue(pn, out _))
{
File.AppendAllText("Config\\PnList.txt", pn + "\r\n");
AlgoPnlist.Add(pn);
}
}
public bool HasPn(string pn) {
return AlgoPnlist.TryGetValue(pn, out _);
}
}
public class IdWorker
{
private long workerId;
private long datacenterId;
private long sequence = 0L;
private static long twepoch = 1288834974657L;
private static long workerIdBits = 5L;
private static long datacenterIdBits = 5L;
private static long maxWorkerId = -1L ^ (-1L << (int)workerIdBits);
private static long maxDatacenterId = -1L ^ (-1L << (int)datacenterIdBits);
private static long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << (int)sequenceBits);
private long lastTimestamp = -1L;
private static object syncRoot = new object();
public IdWorker(long workerId, long datacenterId)
{
// sanity check for workerId
if (workerId > maxWorkerId || workerId < 0)
{
throw new ArgumentException(string.Format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0)
{
throw new ArgumentException(string.Format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public long nextId()
{
lock (syncRoot)
{
long timestamp = timeGen();
if (timestamp < lastTimestamp)
{
throw new ApplicationException(string.Format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp)
{
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0)
{
timestamp = tilNextMillis(lastTimestamp);
}
}
else
{
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << (int)timestampLeftShift) | (datacenterId << (int)datacenterIdShift) | (workerId << (int)workerIdShift) | sequence;
}
}
protected long tilNextMillis(long lastTimestamp)
{
long timestamp = timeGen();
while (timestamp <= lastTimestamp)
{
timestamp = timeGen();
}
return timestamp;
}
protected long timeGen()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
}