FileCodeController.cs
3.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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 文件条码操作类
/// </summary>
public class FileCodeController
{
public static bool UseFileCode = false;
public static string FilePath = "";
public static List<string> CurrCodeList = new List<string>();
public static int CurrCodeIndex = -1;
static FileCodeController()
{
int isuse = ConfigAppSettings.GetIntValue(Setting_Init.UseFileCode);
if (isuse.Equals(1))
{
FilePath = ConfigAppSettings.GetValue(Setting_Init.CodeFilePath);
if (!FilePath.Equals(""))
{
UseFileCode = true;
}
}
}
/// <summary>
/// 点击开始批量入库,开始读取文件并把条码放入内存
/// </summary>
/// <returns></returns>
public static bool ReloadFileCode()
{
if (!UseFileCode)
{
return false ;
}
try
{
CurrCodeList = new List<string>();
CurrCodeIndex = -1;
List<string> Data = new List<string>();
if (File.Exists(FilePath))
{
string[] allLines = File.ReadAllLines(FilePath);
foreach (string str in allLines)
{
string code = str.Trim();
if (String.IsNullOrEmpty(code))
{
continue;
}
Data.Add(str);
}
//清理文件内容
File.WriteAllLines(FilePath, new string[] { });
}
else
{
LogUtil.error("文件【" + FilePath + "】不存在");
return false;
}
//文件格式,需要拼接成:RI:PN:Q
//PN1
//RI1
//PN2
//RI2
//倒叙拼接二维码
if (Data.Count > 0)
{
string codeMsg = "";
for (int i = Data.Count - 1; i >= 1; i -= 2)
{
string ri = Data[i];
string pn = Data[i - 1];
string code = ri + ";" + pn + ";1";
CurrCodeList.Add(code);
codeMsg += code + "\r\n";
}
LogUtil.info("文件【" + FilePath + "】解析到条码=\r\n" + codeMsg);
}
else
{
LogUtil.error("文件【" + FilePath + "】未读取到条码");
}
}
catch (Exception ex)
{
LogUtil.error("ReloadFileCode出错:" + ex.ToString());
}
return false;
}
public static string NextCode()
{
if (UseFileCode&&CurrCodeList.Count > 0)
{
CurrCodeIndex++;
if (CurrCodeList.Count > CurrCodeIndex)
{
return CurrCodeList[CurrCodeIndex];
}
}
return "";
}
}
}