PosIDManger.cs
9.6 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace OnlineStore.Common
{
public static class PosIDManger
{
// Dictionary for storing POS information
public static DRAWERGroup APosList = new DRAWERGroup();
public static DRAWERGroup BPosList = new DRAWERGroup();
// File paths for the JSON data
private static readonly string APosFilePath = "E:\\data1List.json";
private static readonly string BPosFilePath = "E:\\data2List.json";
// Static constructor to initialize APosList and BPosList
static PosIDManger()
{
// Load the data from the JSON files if they exist, otherwise initialize from strings
APosList = LoadFromFile<DRAWERGroup>(APosFilePath) ?? CreatePosListFromString(APosString);
BPosList = LoadFromFile<DRAWERGroup>(BPosFilePath) ?? CreatePosListFromString(BPosString);
//if (BPosList == null)
//{
// BPosList = CreatePosListFromString(BPosString);
// foreach (var x in BPosList.DRAWER.Values)
// {
// x.Reverse();
// }
// LogUtil.error($"反转B面顺序");
// SaveToFile();
//}
// Merge with strings to ensure new POS are added
bool updated = false;
updated |= MergeWithPosString(APosList, APosString);
updated |= MergeWithPosString(BPosList, BPosString);
if (updated)
{
LogUtil.info($"新 POS 信息已添加,保存数据");
SaveToFile();
}
}
// Method to merge new POS from a string into an existing DRAWERGroup
private static bool MergeWithPosString(DRAWERGroup currentGroup, string posString)
{
var newGroup = CreatePosListFromString(posString);
bool updated = false;
foreach (var kvp in newGroup.DRAWER)
{
if (!currentGroup.DRAWER.ContainsKey(kvp.Key))
{
currentGroup.DRAWER[kvp.Key] = kvp.Value;
updated = true;
}
else
{
var existingList = currentGroup.DRAWER[kvp.Key];
foreach (var pos in kvp.Value)
{
if (!existingList.Any(p => p.PosID == pos.PosID))
{
existingList.Add(pos);
updated = true;
}
}
}
}
return updated;
}
// Method to load data from a file and deserialize it
private static T LoadFromFile<T>(string filePath)
{
try
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<T>(json);
}
}
catch (Exception ex)
{
LogUtil.error($"Error loading from file {filePath}: {ex.Message}");
}
return default;
}
// Method to save data to a file (serialize to JSON)
public static void SaveToFile()
{
try
{
string aPosJson = JsonConvert.SerializeObject(APosList, Formatting.Indented);
string bPosJson = JsonConvert.SerializeObject(BPosList, Formatting.Indented);
File.WriteAllText(APosFilePath, aPosJson);
File.WriteAllText(BPosFilePath, bPosJson);
}
catch (Exception ex)
{
LogUtil.error($"Error saving to files: {ex.Message}");
}
}
// Method to clear HasReel and Barcode for all entries and save
public static void EmptyAllPos()
{
try
{
// Iterate through APosList and BPosList to update the properties
UpdatePosList(APosList);
UpdatePosList(BPosList);
// Save the changes back to the files
SaveToFile();
}
catch (Exception ex)
{
LogUtil.error($"Error while emptying all POS: {ex.Message}");
}
}
// Helper method to update the PosInfo objects
private static void UpdatePosList(DRAWERGroup posList)
{
if (posList == null || posList.DRAWER == null) return;
foreach (var drawer in posList.DRAWER.Values)
{
if (drawer == null) continue;
foreach (var pos in drawer)
{
if (pos == null) continue;
pos.HasReel = false;
pos.Barcode = string.Empty;
}
}
}
// Method to create PosInfo list from a string
private static DRAWERGroup CreatePosListFromString(string posString)
{
var drawerGroup = new DRAWERGroup();
int currentKey = 0;
var lines = posString.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (int.TryParse(line.Trim(), out int key))
{
// Update the current key
currentKey = key;
if (!drawerGroup.DRAWER.ContainsKey(currentKey))
{
drawerGroup.DRAWER[currentKey] = new List<PosInfo>();
}
}
else
{
// Add PosInfo to the current key
drawerGroup.DRAWER[currentKey].Add(new PosInfo
{
PosID = line.Trim(),
HasReel = false,
Barcode = string.Empty
});
}
}
return drawerGroup;
}
// New method to retrieve POS lists based on drawer index
public static void GetDarwerPoslist(int darwerindex, out string apos, out string bpos, out string aout, out string bout)
{
apos = string.Empty;
bpos = string.Empty;
aout = string.Empty;
bout = string.Empty;
if (APosList.DRAWER.TryGetValue(darwerindex, out var aDrawer))
{
var pos = aDrawer.FirstOrDefault(p => !p.HasReel);
var outPos = aDrawer.FirstOrDefault(p => p.HasReel);
apos = pos?.PosID ?? string.Empty;
aout = outPos?.PosID ?? string.Empty;
}
if (BPosList.DRAWER.TryGetValue(darwerindex, out var bDrawer))
{
var pos = bDrawer.FirstOrDefault(p => !p.HasReel);
var outPos = bDrawer.FirstOrDefault(p => p.HasReel);
bpos = pos?.PosID ?? string.Empty;
bout = outPos?.PosID ?? string.Empty;
}
}
// Method to update PosInfo based on PosID and HasReel
public static void UpdatePosinfo(string posid, bool hasreel)
{
LogUtil.info($"UpdatePosinfo {posid}={hasreel}");
foreach (var drawer in APosList.DRAWER.Values.Concat(BPosList.DRAWER.Values))
{
var pos = drawer.FirstOrDefault(p => p.PosID == posid);
if (pos != null)
{
pos.HasReel = hasreel;
SaveToFile(); // Save the updated data
return;
}
}
}
public static bool GetPosHasReel(string posid)
{
foreach (var drawer in APosList.DRAWER.Values.Concat(BPosList.DRAWER.Values))
{
var pos = drawer.FirstOrDefault(p => p.PosID == posid);
if (pos != null)
{
return pos.HasReel;
}
}
return true;
}
// APos and BPos string values
static string APosString = @"
1
01AA15060101
01AA15060103
01AA15060105
2
01BB01060220
01BB01060218
01BB01060216
3
01AA15060420
01AA15060418
01AA15060416
4
01BB01020220
01BB01020218
01BB01020216
5
01AA15040101
01AA15040103
01AA15040105
6
01BB15020220
01BB15020218
01BB15020216
7
01AA15020101
01AA15020103
01AA15020105
8
01BB14050401
01BB14050403
01BB14050405
9
01AA01040101
01AA01040103
01AA01040105
10
01BB01040220
01BB01040218
01BB01040216
11
01AA05060101
01AA05060103
01AA05060105
12
01BB15060220
01BB15060218
01BB15060216
13
01AA01060101
01AA01060103
01AA01060105
";
static string BPosString = @"
1
01BB05040220
01BB05040218
01BB05040216
2
01AA01020101
01AA01020103
01AA01020105
3
01BB05020220
01BB05020218
01BB05020216
4
01AA04040101
01AA04040103
01AA04040105
5
01BB05060220
01BB05060218
01BB05060216
6
01AA05040101
01AA05040103
01AA05040105
7
01BB04020220
01BB04020218
01BB04020216
8
01AA05020101
01AA05020103
01AA05020105
9
01BB04060220
01BB04060218
01BB04060216
10
01AA04060101
01AA04060103
01AA04060105
11
01BB15040220
01BB15040218
01BB15040216
12
01AA04020101
01AA04020103
01AA04020105
13
01BB04040220
01BB04040218
01BB04040216
";
}
[Serializable]
public class PosInfo
{
public string PosID { get; set; }
public bool HasReel { get; set; }
public string Barcode { get; set; }
}
[Serializable]
public class DRAWERGroup
{
public Dictionary<int, List<PosInfo>> DRAWER = new Dictionary<int, List<PosInfo>>();
}
}