CSVConfigReader.cs
10.4 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace OnlineStore.LoadCSVLibrary
{
public class CSVConfigReader : CSVReaderBase
{
private static Dictionary<Type, Dictionary<string, string>> allItemProTitleMap = null;
/// <summary>
/// 获取一个类所有的《字段,AttributeName列名》集合
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static Dictionary<string, string> GetProMapByType(Type type)
{
if (allItemProTitleMap == null)
{
allItemProTitleMap = new Dictionary<Type, Dictionary<string, string>>();
}
if (!allItemProTitleMap.ContainsKey(type))
{
allItemProTitleMap.Add(type, getProAttributeMap(type));
}
return allItemProTitleMap[type];
}
public static List<ConfigBase> ReadConfig(string filePath)
{
List<ConfigBase> configList = new List<ConfigBase>();
string[] lines = ReadCSVFile(filePath);
int index = 0;
Dictionary<string, int> allTitleIndex = new Dictionary<string, int>();
int typeIndex = -1;
foreach (var line in lines)
{
if (index == 0)
{
//根据配置表来读取数据
allTitleIndex = GetAllTitleIndex(line);
//必须有列【类型】
if (allTitleIndex.Count < 0 || !allTitleIndex.ContainsKey("类型"))
{
LogUtil.error("未找到必须列:类型,加载数据失败!");
throw new CVSFieldNotMatchingExection("未找到必须列:类型,加载数据失败!");
}
typeIndex = allTitleIndex["类型"];
}
else
{
var array = line.Split(',');
//每一列必须有类型字段
string typeValue = array[typeIndex];
if (typeValue.Equals(""))
{
continue;
}
Type type = typeof(ConfigBase);
if (typeValue.Equals(ConfigItemType.DI) || typeValue.Equals(ConfigItemType.DO))
{
type = typeof(ConfigIO);
}
else if (typeValue.Equals(ConfigItemType.AXIS))
{
type = typeof(ConfigMoveAxis);
}
Dictionary<string, string> proTitleMap = getProAttributeMap(type);
ConfigBase bllIns = (ConfigBase)type.Assembly.CreateInstance(type.FullName);
//取得属性集合
PropertyInfo[] props = type.GetProperties();
int listIndex = 0;
List<string> proNameList = new List<string>(proTitleMap.Keys);
List<string> cvsTitleList = new List<string>(proTitleMap.Values);
List<string> checkFiledList = new List<string>(proNameList);
Dictionary<string, int> proIndexMap = new Dictionary<string, int>();
for (int i = 0; i<cvsTitleList.Count; i++)
{
proIndexMap.Add(cvsTitleList[i], i);
}
foreach (string key in cvsTitleList)
{
try
{
if (allTitleIndex.ContainsKey(key))
{
try
{
int titIndex = allTitleIndex[key];
string value = array[titIndex];
//string proName = proNameList[listIndex];
string proName = proNameList[proIndexMap[key]];
checkFiledList.Remove(proName);
PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
if (prop != null && !value.Equals(""))
{
if (value.Equals("") && prop.PropertyType.Name.ToLower().Contains("int"))
{
prop.SetValue(bllIns, Convert.ChangeType(-1, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
}
else if (value.Equals("") && prop.PropertyType.Name.ToLower().Contains("byte"))
{
prop.SetValue(bllIns, Convert.ChangeType(0, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
}
else
{
prop.SetValue(bllIns, Convert.ChangeType(value, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
}
}
}
catch (Exception ex)
{
LogUtil.error("filepath=" + filePath + ",index=" + index + ",key=" + key + "出错:" + ex.ToString());
}
listIndex++;
}
}
catch (Exception ex)
{
LogUtil.error("filepath=" + filePath + ",index=" + index + ",key=" + key + "出错:" + ex.ToString());
}
}
bllIns.CheckField();
configList.Add(bllIns);
}
index++;
}
return configList;
}
public static AC_BOX_Config LoadBoxConfig(int storeId, string cid, string storeType, string linefilePath)
{
BaseConfig config = null;
config = new AC_BOX_Config(storeId, cid, storeType, linefilePath);
return (AC_BOX_Config)LoadConfig(config);
}
public static Store_Config LoadLineConfig(int storeId, string cid, string storeType, string linefilePath)
{
BaseConfig config = null;
config = new Store_Config(storeId, cid, storeType, linefilePath);
return (Store_Config)LoadConfig(config);
}
public static BaseConfig LoadConfig(int storeId, string cid, string storeType, string filePath)
{
BaseConfig config = null;
if (storeType.Equals(StoreType.RC_AC_SA))
{
config = new AC_BOX_Config(storeId, cid, storeType, filePath);
}
else
{
LogUtil.error("配置的料仓类型=" + storeType + "未找到处理方法!");
}
return LoadConfig(config);
}
public static BaseConfig LoadConfig(BaseConfig config)
{
LogUtil.info("开始读取文件:" + config.ConfigFilePath);
if (config == null || config.ConfigFilePath.Equals(""))
{
return null;
}
List<ConfigBase> configList = ReadConfig(config.ConfigFilePath);
config.LoadConfig(configList);
return config;
}
public static bool SaveBoxPosition<T>(string filePath, T newConfig)
{
Type type = typeof(T);
//取得属性集合
PropertyInfo[] props = type.GetProperties();
string[] lines = ReadCSVFile(filePath);
int index = 0;
Dictionary<string, int> titleIndex = new Dictionary<string, int>();
string[] newLines = new string[lines.Length] ;
// 获取一个类所有的《字段,AttributeName列名》集合
Dictionary<string, string> proMap = GetProMapByType(typeof(ConfigBase));
List<string> proNameList = new List<string>(proMap.Keys);
List<string> csvNameList = new List<string>(proMap.Values);
Dictionary<string, int> allTitleIndex = new Dictionary<string, int>();
foreach (var line in lines)
{
newLines[index] = line;
var array = line.Split(',');
if (index == 0)
{
allTitleIndex = GetAllTitleIndex(line);
newLines[index] = line;
}
else
{
if (array.Length > 0 && array[0].ToString().Equals(ConfigItemType.PRO))
{
string[] newArray = array;
string proName = array[2];
string proValue = array[3];
try
{
PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
if (prop != null)
{
string newValue = prop.GetValue(newConfig, null).ToString();
newArray[3] = newValue;
}
}
catch (Exception e) {
LogUtil.error("出错:" + e.ToString());
}
string newLine = "";
foreach (string s in newArray)
{
newLine = newLine + s + ",";
}
if (newLine.EndsWith(","))
{
newLine = newLine.Substring(0, newLine.Length - 1);
}
newLines[index] = newLine;
}
}
index++;
}
return WriteCSVFile(filePath, newLines);
}
}
}