FrmRetrospect.cs
16.1 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Asa.FaceControl;
using System.Windows.Forms;
using System.Reflection;
using System.Web.UI.WebControls;
using DocumentFormat.OpenXml.ExtendedProperties;
using DocumentFormat.OpenXml.Wordprocessing;
using BLL;
using System.Configuration;
using System.Diagnostics;
using System.IO;
namespace SmartScan
{
public partial class FrmRetrospect : FaceFormFixed
{
private List<HistoryPath> fileFull = new();
public FrmRetrospect()
{
InitializeComponent();
}
private class HistoryPath
{
public string cameraPath = "";
public string labelPath = "";
public string codePath = "";
public int Count
{
get
{
int n = 0;
if (cameraPath != "") n++;
if (labelPath != "") n++;
if (codePath != "") n++;
return n;
}
}
}
private string ExportIndex(int index)
{
return exportOriginalFormat(index);
}
string exportOriginalFormat(int index)
{
HistoryPath path = fileFull[index];
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
string content = "";
if (dic.ContainsKey("Code"))
{
object[] obj = (object[])dic["Code"];
string[] items = new string[obj.Length];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
items[i] = dicCode["Text"].ToString().Replace("\r", "");
items[i] = items[i].Replace("\n", "");
}
content += "Code," + string.Join(",", items) + "\r\n";
}
if (dic.ContainsKey("Label"))
{
object[] obj = (object[])dic["Label"];
string[] items = new string[obj.Length];
for (int i = 0; i < items.Length; i++)
items[i] = obj[i].ToString();
content += "Label," + string.Join(",", items);
}
if (dic.ContainsKey("Content"))
{
object[] obj = (object[])dic["Content"];
string[] items = new string[obj.Length];
for (int i = 0; i < items.Length; i++)
items[i] = obj[i].ToString();
content += "Content," + string.Join(",", items);
}
return content;
}
private List<HistoryPath> CheckFiles(string[] files)
{
string[] affix = new string[3] { "_camera", "_label", "_code" };
List<HistoryPath> result = new();
for (int i = 0; i < files.Length; i++)
{
if (files[i] == "") continue;
HistoryPath temp = new();
string name = System.IO.Path.GetFileNameWithoutExtension(files[i]);
string[] arr = name.Split('_');
string prefix = arr[0];
int index = Array.FindIndex(files, match => prefix + affix[0] == System.IO.Path.GetFileNameWithoutExtension(match));
if (index > -1)
{
temp.cameraPath = files[index];
files[index] = "";
}
index = Array.FindIndex(files, match => prefix + affix[1] == System.IO.Path.GetFileNameWithoutExtension(match));
if (index > -1)
{
temp.labelPath = files[index];
files[index] = "";
}
index = Array.FindIndex(files, match => prefix + affix[2] == System.IO.Path.GetFileNameWithoutExtension(match));
if (index > -1)
{
temp.codePath = files[index];
files[index] = "";
}
if (temp.Count == affix.Length)
result.Add(temp);
}
return result;
}
private void FrmRetrospect_Load(object sender, EventArgs e)
{
DtpLower.Value = DateTime.Now.Date;
DtpUpper.Value = DateTime.Now.Date;
if (ConfigurationManager.AppSettings["CustomerName"] == "1181")
{
butt_startservice.Visible=true;
LstRecord.Height = 206;
}
}
private void BtnSelect_Click(object sender, EventArgs e)
{
List<string> directory = new();
string[] dirs = System.IO.Directory.GetDirectories(Model.FilePath.RETROSPECT_DIR);
for (int i = 0; i < dirs.Length; i++)
{
System.IO.DirectoryInfo info = new(dirs[i]);
DateTime dt = DateTime.ParseExact(info.Name, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
if (DtpUpper.Checked)
{
if (dt < DtpUpper.Value)
continue;
}
if (DtpLower.Checked)
{
if (dt > DtpLower.Value)
continue;
}
directory.Add(dirs[i]);
}
fileFull = new();
for (int i = 0; i < directory.Count; i++)
{
string[] files = System.IO.Directory.GetFiles(directory[i]);
fileFull.AddRange(CheckFiles(files));
}
int index = 0;
string text = TxtCode.Text;
if (text.Length > 0)
{
while (index < fileFull.Count)
{
string allText = System.IO.File.ReadAllText(fileFull[index].codePath);
if (allText.IndexOf(text) == -1) //任意位置出现,相同用=
fileFull.RemoveAt(index);
else
index++;
}
}
string[] items = new string[fileFull.Count];
for (int i = 0; i < fileFull.Count; i++)
{
string name = System.IO.Path.GetFileNameWithoutExtension(fileFull[i].codePath);
string[] arr = name.Split('_');
System.IO.DirectoryInfo info = new(fileFull[i].codePath);
items[i] = info.Parent.Name + " " + arr[0];
}
LstRecord.Items.Clear();
LstRecord.Items.AddRange(items);
LstRecord.SelectedFirst();
}
private void LstRecord_SelectedIndexChanged(object sender, EventArgs e)
{
if (LstRecord.Items.Count == 0)
return;
if (LstRecord.SelectedIndex < 0)
return;
HistoryPath path = fileFull[LstRecord.SelectedIndex];
PicShow.Image = new Bitmap(path.cameraPath);
PicLabel.Image = new Bitmap(path.labelPath);
LstCode.Items.Clear();
PicShow.CodeCenterClear();
LstLabel.Items.Clear();
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic.ContainsKey("Code"))
{
object[] obj = (object[])dic["Code"];
string[] items = new string[obj.Length];
PointF[] center = new PointF[obj.Length];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
items[i] = dicCode["Text"].ToString().Replace("\r", "");
items[i] = items[i].Replace("\n", "");
items[i] = string.Format("({0}) {1}", i + 1, items[i]);
center[i] = new(Convert.ToSingle(dicCode["X"]), Convert.ToSingle(dicCode["Y"]));
}
LstCode.Items.AddRange(items);
PicShow.AddCodeCenter(center);
LstCode.SelectedIndex = -1;
}
if (dic.ContainsKey("Label"))
{
object[] obj = (object[])dic["Label"];
string[] items = new string[obj.Length];
for (int i = 0; i < items.Length; i++)
items[i] = obj[i].ToString();
LstLabel.Items.AddRange(items);
LstLabel.SelectedIndex = -1;
}
}
/// <summary>
/// 导出文件格式
/// </summary>
int exportFileFormat = ConfigHelper.Config.Get("ExportFileFormat", 1);
private void BtnExport_Click(object sender, EventArgs e)
{
if (LstRecord.SelectedIndex == -1) return;
switch (exportFileFormat)
{
case 1:
exportByMacroKey(LstRecord.SelectedIndex);
break;
default:
SaveFileDialog dlg = new() { Filter = "TXT文件|*.txt" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
string content = ExportIndex(LstRecord.SelectedIndex);
System.IO.File.WriteAllText(dlg.FileName, content, Encoding.UTF8);
break;
}
}
string genLine(Dictionary<string, object> pairs)
{
List<string> lines = new List<string>();
foreach (var item in BLLCommon.macroKey)
{
if (pairs.ContainsKey(item))
{
lines.Add(pairs[item].ToString());
}
else
{
lines.Add("");
}
}
return string.Join(",", lines);
}
string genLine(string txt)
{
List<string> lines = new List<string>();
string[] fields = txt.Split(';');
if (fields != null && fields.Length > 0)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (var item in fields)
{
string[] pairs = item.Split(':');
if (pairs != null && pairs.Length > 0)
{
if (BLLCommon.macroKey.Contains(pairs[0]))
{
dic[pairs[0]] = pairs[1];
}
}
}
foreach (var item in BLLCommon.macroKey)
{
if (dic.ContainsKey(item))
{
lines.Add(dic[item]);
}
else
{
lines.Add("");
}
}
}
return string.Join(",", lines);
}
bool checkHasMacroKey(string content)
{
string[] fields = content.Split(';');
if (fields != null && fields.Length > 0)
{
for (int i = 0; i < fields.Length; i++)
{
string[] pairs = fields[i].Split(':');
if (pairs != null && pairs.Length > 0)
{
if (i > 0)
return true;
else if (!BLLCommon.macroKey.Contains(pairs[0])) { return false; }
}
}
}
return true;
}
bool checkHasMacroKey(Dictionary<string, object> content)
{
for (int i = 0; i < BLLCommon.macroKey.Count; i++)
{
if (i > 0)
return true;
else if (!content.ContainsKey(BLLCommon.macroKey[i])) { return false; }
}
return true;
}
void exportByMacroKey(int index)
{
SaveFileDialog dlg = new() { Filter = "CSV文件|*.csv" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
StringBuilder content = new StringBuilder();
content.AppendLine(string.Join(",", BLLCommon.macroKey));
var path = fileFull[index];
{
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic.ContainsKey("Content"))
{
object[] obj = (object[])dic["Content"];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
if (checkHasMacroKey(dicCode))
{
content.AppendLine(genLine(dicCode));
break;
}
}
}
}
System.IO.File.WriteAllText(dlg.FileName, content.ToString(), Encoding.UTF8);
}
void exportAllByMacroKey()
{
SaveFileDialog dlg = new() { Filter = "CSV文件|*.csv" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
StringBuilder content = new StringBuilder();
content.AppendLine(string.Join(",", BLLCommon.macroKey));
foreach (var path in fileFull)
{
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic.ContainsKey("Content"))
{
object[] obj = (object[])dic["Content"];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
if (checkHasMacroKey(dicCode))
{
content.AppendLine(genLine(dicCode));
break;
}
}
}
}
System.IO.File.WriteAllText(dlg.FileName, content.ToString(), Encoding.UTF8);
}
private void BtnExportAll_Click(object sender, EventArgs e)
{
switch (exportFileFormat)
{
case 1:
exportAllByMacroKey();
break;
default:
SaveFileDialog dlg = new() { Filter = "TXT文件|*.txt" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
string[] content = new string[fileFull.Count];
for (int i = 0; i < fileFull.Count; i++)
content[i] = ExportIndex(i);
System.IO.File.WriteAllLines(dlg.FileName, content, Encoding.UTF8);
break;
}
}
private void butt_startservice_Click(object sender, EventArgs e)
{
Process p = new Process();
var pss = Process.GetProcessesByName(ConfigurationManager.AppSettings["StartServiceName"]);
if (pss.Length > 0)
return;
var f = ConfigurationManager.AppSettings["StartServicePath"];
if (!File.Exists(f))
throw new Exception("未找到服务文件!");
p.StartInfo = new ProcessStartInfo
{
FileName = f,
Verb = "runas" // 这里设置为 "runas" 表示以管理员身份运行
};
p.Start();
}
}
}