Item_General.cs
8.0 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
/*
* kaifa
*
*/
using System;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Model;
using Asa.FaceControl;
using Newtonsoft.Json;
using System.Windows.Forms;
namespace BLL
{
public class General : IExtension
{
public event IExtension.PrintDelegate Printing;
public event IExtension.PrintDelegate SaveRetrospect;
private Config config;
private int reelID = 0;
private List<ExtensionControl> extensions;
public General(Config config)
{
this.config = config;
}
public void Clear()
{
LogNet.log.Info("Clear()");
for (int i = 0; i < extensions.Count; i++)
{
if (!extensions[i].CanClear) continue;
if (extensions[i].Control is FaceComboBox box)
box.Items.Clear();
extensions[i].Control.Text = "";
}
}
public void Dispose()
{
}
public void Load(List<ExtensionControl> extensions)
{
this.extensions = extensions;
ReadReelID();
}
static bool mesResult = false;
Dictionary<string, string> lastkey = null;
//读码后第一步
public void SetKey(string[] originalCode, Dictionary<string, string> key, bool hasMatch)
{
lastkey = key;
mesResult = false;
var now = DateTime.Now;
if (!string.IsNullOrEmpty(config.ReelIDKeyWord))
{
var Reelidstr = GetReelid();
if (key.ContainsKey(config.ReelIDKeyWord))
key[config.ReelIDKeyWord] = Reelidstr;
else
key.Add(config.ReelIDKeyWord, Reelidstr);
}
//第一次刷新界面
for (int i = 0; i < extensions.Count; i++)
{
if (key.ContainsKey(extensions[i].Key))
extensions[i].Control.Text = key[extensions[i].Key];
}
if (!updatereelid(key))
return;
Application.DoEvents();
if (config.AutoPrint && hasMatch)
{
//if (CanPrint())
PrintLabel(null, EventArgs.Empty);
}
}
bool updatereelid(Dictionary<string, string> key) {
if (!string.IsNullOrEmpty(config.HttpReelID))
{
var newid = GetHttpReelID(key);
if (string.IsNullOrEmpty(newid))
return false;
key["ReelID"] = newid;
}
for (int i = 0; i < extensions.Count; i++)
{
if (extensions[i].Key.ToLower() == "reelid")
extensions[i].Control.Text = key["reelid"];
}
SaveRetrospect?.Invoke(key);
return true;
}
public void Update()
{
}
private string GetReelid()
{
LogNet.log.Debug("Enter GetReelid Method");
int idIndex = extensions.FindIndex(match => match.Key == config.ReelIDKeyWord);
if (idIndex == -1) return "";
//读取参数
string matchID = config.ReelIDMatch;
int matchPlace = config.ReelIDPlaces;
bool matchFill = config.ReelIDFillZero;
string matchPrefix = config.ReelIDPrefix;
string matchPostfix = config.ReelIDPostfix;
//把key替换成实际值
string[] keys = ObjConversion.StrGetKey(matchID);
for (int i = 0; i < keys.Length; i++)
{
int index = extensions.FindIndex(match => match.Key == keys[i]);
if (index == -1) continue;
string oldValue = string.Format("[{0}]", keys[i]);
matchID = matchID.Replace(oldValue, extensions[index].Control.Text);
}
SaveAddReelID();
//填充0
string text = matchPrefix;
if (matchFill)
text += string.Format("{0:d" + matchPlace + "}", reelID);
else
text += reelID.ToString();
text += matchPostfix;
return text;
}
private void ReadReelID()
{
try
{
if (System.IO.File.Exists(FilePath.CONFIG_REELID))
{
string text = System.IO.File.ReadAllText(FilePath.CONFIG_REELID);
bool bln = int.TryParse(text, out reelID);
LogNet.log.Info($"ReadReelID {bln} text={text}");
}
else
{
System.IO.FileStream fs = System.IO.File.Create(FilePath.CONFIG_REELID);
fs.Close();
reelID = 0;
LogNet.log.Info("ReadReelID Create");
}
}
catch (Exception ex)
{
LogNet.log.Error("ReadReelID", ex);
System.Windows.Forms.MessageBox.Show(ex.Message, "Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
}
private void SaveAddReelID()
{
try
{
LogNet.log.Debug("打印标签前的ID:" + reelID);
reelID++;
System.IO.File.WriteAllText(FilePath.CONFIG_REELID, reelID.ToString());
LogNet.log.Info($"Save ReelID:{reelID}");
}
catch (Exception ex)
{
LogNet.log.Error("SaveAddReelID error", ex);
}
}
private void GetHttpReelID(object sender, EventArgs e) {
updatereelid(lastkey);
}
private void PrintLabel(object sender, EventArgs e)
{
LogNet.log.Debug("Enter PrintLabel Method");
Dictionary<string, string> key = new();
for (int i = 0; i < extensions.Count; i++)
{
if (extensions[i].Key == "") continue;
if (key.ContainsKey(extensions[i].Key))
key[extensions[i].Key] = extensions[i].Control.Text;
else
key.Add(extensions[i].Key, extensions[i].Control.Text);
}
var keys =new List<string>(key.Keys);
Printing?.Invoke(key);
}
private string GetHttpReelID(Dictionary<string, string> key)
{
//return "test";
string url = config.HttpReelID;
string json = Http.PostJson(url,null, key);
if (json == "") {
new FaceMessageBox("BoxReelIDInfoMaintain", "Api error", System.Windows.Forms.MessageBoxButtons.OK, true).ShowDialog();
return "";
}
JavaScriptSerializer serializer = new();
Dictionary<string, object> dic;
try
{
dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
}
catch {
new FaceMessageBox("BoxReelIDInfoMaintain", "Api parse error:\r\n" + json, System.Windows.Forms.MessageBoxButtons.OK, true).ShowDialog();
return "";
}
if (!dic.TryGetValue("CODE", out object value))
{
new FaceMessageBox("BoxReelIDInfoMaintain", "Api return data error:\r\n" + json, System.Windows.Forms.MessageBoxButtons.OK, true).ShowDialog();
return "";
}
if (Convert.ToInt32(value)!=0)
{
new FaceMessageBox("BoxReelIDInfoMaintain", dic["MSG"].ToString(), System.Windows.Forms.MessageBoxButtons.OK, true).ShowDialog();
return "";
}
if (dic.TryGetValue("ReelID", out value))
return value.ToString();
else
return "";
}
}
}