SnDataProcessor.cs
3.42 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
}
}
}