SnDataProcessor.cs
4.94 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
using System.Collections.Generic;
using System.Data;
namespace Hh.Mes.Common
{
public class SnDataProcessor
{
// 定义产品类别映射规则
private static readonly Dictionary<char, string> CategoryMap = new Dictionary<char, string>
{
['1'] = "管焊",
['2'] = "专机",
['3'] = "管道",
['4'] = "物流",
['5'] = "激光",
['6'] = "减速机",
['7'] = "切割机",
['8'] = "机器人",
['9'] = "软件",
['A'] = "贸易",
['B'] = "其他",
['C'] = "C",
['D'] = "D",
['E'] = "E"
};
public List<Dictionary<string, object>> ProcessAndMergeData(
DataTable dt1, DataTable dt2, DataTable dt3, DataTable dt4)
{
// 初始化合并字典(按产品类别)
var mergedData = InitializeCategoryDictionary();
// 处理每个数据源
ProcessTable(dt1, "SN注册申请数量", mergedData);
ProcessTable(dt2, "SN授权申请数量", mergedData);
ProcessTable(dt3, "接入", mergedData);
ProcessTable(dt4, "上报报警", mergedData);
// 转换为最终格式
return ConvertToResultFormat(mergedData);
}
// 初始化包含所有类别的字典
private Dictionary<string, Dictionary<string, int>> InitializeCategoryDictionary()
{
var dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var category in CategoryMap.Values)
{
dict[category] = new Dictionary<string, int>
{
["SN注册申请数量"] = 0,
["SN授权申请数量"] = 0,
["接入"] = 0,
["上报报警"] = 0
};
}
return dict;
}
private void ProcessTable(DataTable dataTable, string statType, Dictionary<string, Dictionary<string, int>> mergedData)
{
foreach (DataRow row in dataTable.Rows)
{
string sn = row["sn"].ToString().Trim();
//取第一个字符串
char key = sn[0];
if (!CategoryMap.ContainsKey(key)) {
continue;
}
string category = CategoryMap[key];
// 动态累加当前统计类型的数量(每行计数+1)
mergedData[category][statType] += 1;
}
}
// 转换为最终结果格式
private List<Dictionary<string, object>> ConvertToResultFormat( Dictionary<string, Dictionary<string, int>> mergedData)
{
var result = new List<Dictionary<string, object>>();
foreach (var kvp in mergedData)
{
result.Add(new Dictionary<string, object>
{
["sn"] = kvp.Key,
["SN注册申请数量"] = kvp.Value["SN注册申请数量"],
["SN授权申请数量"] = kvp.Value["SN授权申请数量"],
["接入"] = kvp.Value["接入"],
["上报报警"] = kvp.Value["上报报警"]
});
}
return result;
}
public static string GetSNModel(string snCode)
{
if (string.IsNullOrWhiteSpace(snCode))
{
return string.Empty;
}
if (snCode.Length < 2)
{
return string.Empty;
}
var v1 = snCode[..1];
var v2 = snCode.Substring(1, 1);
return v1 switch
{
"1" => "管焊",
"2" => "专机",
"3" => "管道",
"4" => "物流",
"5" => "激光",
"6" => "减速机",
"7" => "切割机",
"8" => "机器人",
//软件部分
"9" => v2 switch
{
"1" => "WMS",
"2" => "WCS",
"3" => "MES",
"4" => "RCS",
"5" => "MTS",
"6" => "ECS",
"7" => "EMS",
"8" => "IOT",
"9" => "DT",
"A" => "参数化编程",
"B" => "离线软件",
//"C" => string.Empty,
//"D" => string.Empty,
//"E" => string.Empty,
//"F" => string.Empty,
_ => "软件",
},
"A" => string.Empty,
"B" => string.Empty,
"C" => string.Empty,
"D" => string.Empty,
"E" => string.Empty,
"F" => string.Empty,
_ => string.Empty,
};
}
}
}