|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+package com.water.production.service;
|
|
|
2
|
+
|
|
|
3
|
+import com.water.production.entity.DashboardSummary;
|
|
|
4
|
+import com.water.production.entity.EnergyConsumption;
|
|
|
5
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
6
|
+import org.junit.jupiter.api.Test;
|
|
|
7
|
+
|
|
|
8
|
+import java.math.BigDecimal;
|
|
|
9
|
+import java.math.RoundingMode;
|
|
|
10
|
+import java.time.LocalDate;
|
|
|
11
|
+import java.util.*;
|
|
|
12
|
+
|
|
|
13
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
14
|
+
|
|
|
15
|
+/**
|
|
|
16
|
+ * DashboardService + EnergyService 单元测试
|
|
|
17
|
+ * 测试实体构建、数据聚合逻辑、环比计算等核心逻辑
|
|
|
18
|
+ */
|
|
|
19
|
+class DashboardServiceTest {
|
|
|
20
|
+
|
|
|
21
|
+ @Test
|
|
|
22
|
+ @DisplayName("测试 DashboardSummary 实体字段完整性")
|
|
|
23
|
+ void testDashboardSummaryEntityFields() {
|
|
|
24
|
+ DashboardSummary summary = new DashboardSummary();
|
|
|
25
|
+ summary.setId(1L);
|
|
|
26
|
+ summary.setSummaryDate(LocalDate.of(2026, 6, 14));
|
|
|
27
|
+ summary.setArea("一体化水厂");
|
|
|
28
|
+
|
|
|
29
|
+ // 进出水量
|
|
|
30
|
+ summary.setTodayInflow(new BigDecimal("15000.50"));
|
|
|
31
|
+ summary.setTodayOutflow(new BigDecimal("14200.30"));
|
|
|
32
|
+ summary.setYesterdayInflow(new BigDecimal("14500.00"));
|
|
|
33
|
+ summary.setYesterdayOutflow(new BigDecimal("13800.00"));
|
|
|
34
|
+ summary.setMonthInflow(new BigDecimal("420000.00"));
|
|
|
35
|
+ summary.setMonthOutflow(new BigDecimal("398000.00"));
|
|
|
36
|
+
|
|
|
37
|
+ // 水质
|
|
|
38
|
+ summary.setRawWaterPassRate(new BigDecimal("98.5"));
|
|
|
39
|
+ summary.setFactoryWaterPassRate(new BigDecimal("99.2"));
|
|
|
40
|
+ summary.setTerminalWaterPassRate(new BigDecimal("97.8"));
|
|
|
41
|
+ summary.setOverallPassRate(new BigDecimal("98.5"));
|
|
|
42
|
+
|
|
|
43
|
+ // 设备
|
|
|
44
|
+ summary.setDeviceTotal(120);
|
|
|
45
|
+ summary.setDeviceOnline(105);
|
|
|
46
|
+ summary.setDeviceOffline(10);
|
|
|
47
|
+ summary.setDeviceFault(5);
|
|
|
48
|
+ summary.setDeviceOnlineRate(new BigDecimal("87.5"));
|
|
|
49
|
+
|
|
|
50
|
+ // 报警
|
|
|
51
|
+ summary.setTodayAlertTotal(8);
|
|
|
52
|
+ summary.setActiveAlertCount(3);
|
|
|
53
|
+ summary.setGeneralAlertCount(5);
|
|
|
54
|
+ summary.setImportantAlertCount(2);
|
|
|
55
|
+ summary.setUrgentAlertCount(1);
|
|
|
56
|
+
|
|
|
57
|
+ // 能耗
|
|
|
58
|
+ summary.setTodayPowerKwh(new BigDecimal("1250.50"));
|
|
|
59
|
+ summary.setTodayChemicalKg(new BigDecimal("57.50"));
|
|
|
60
|
+ summary.setUnitEnergyConsumption(new BigDecimal("0.0834"));
|
|
|
61
|
+
|
|
|
62
|
+ // 验证所有字段
|
|
|
63
|
+ assertEquals(1L, summary.getId());
|
|
|
64
|
+ assertEquals(LocalDate.of(2026, 6, 14), summary.getSummaryDate());
|
|
|
65
|
+ assertEquals("一体化水厂", summary.getArea());
|
|
|
66
|
+ assertEquals(new BigDecimal("15000.50"), summary.getTodayInflow());
|
|
|
67
|
+ assertEquals(new BigDecimal("14200.30"), summary.getTodayOutflow());
|
|
|
68
|
+ assertEquals(120, summary.getDeviceTotal());
|
|
|
69
|
+ assertEquals(105, summary.getDeviceOnline());
|
|
|
70
|
+ assertEquals(8, summary.getTodayAlertTotal());
|
|
|
71
|
+ assertEquals(new BigDecimal("1250.50"), summary.getTodayPowerKwh());
|
|
|
72
|
+ assertEquals(new BigDecimal("0.0834"), summary.getUnitEnergyConsumption());
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ @Test
|
|
|
76
|
+ @DisplayName("测试 EnergyConsumption 实体字段及单位能耗计算")
|
|
|
77
|
+ void testEnergyConsumptionEntity() {
|
|
|
78
|
+ EnergyConsumption ec = new EnergyConsumption();
|
|
|
79
|
+ ec.setId(1L);
|
|
|
80
|
+ ec.setRecordDate(LocalDate.of(2026, 6, 14));
|
|
|
81
|
+ ec.setArea("一体化水厂");
|
|
|
82
|
+ ec.setEnergyType("power");
|
|
|
83
|
+ ec.setConsumption(new BigDecimal("1250.50"));
|
|
|
84
|
+ ec.setUnit("kWh");
|
|
|
85
|
+ ec.setProductionVolume(new BigDecimal("15000.00"));
|
|
|
86
|
+
|
|
|
87
|
+ // 计算单位能耗
|
|
|
88
|
+ BigDecimal unitConsumption = ec.getConsumption()
|
|
|
89
|
+ .divide(ec.getProductionVolume(), 4, RoundingMode.HALF_UP);
|
|
|
90
|
+ ec.setUnitConsumption(unitConsumption);
|
|
|
91
|
+
|
|
|
92
|
+ assertEquals("power", ec.getEnergyType());
|
|
|
93
|
+ assertEquals(new BigDecimal("1250.50"), ec.getConsumption());
|
|
|
94
|
+ assertEquals("kWh", ec.getUnit());
|
|
|
95
|
+ assertEquals(new BigDecimal("15000.00"), ec.getProductionVolume());
|
|
|
96
|
+ assertEquals(new BigDecimal("0.0834"), ec.getUnitConsumption());
|
|
|
97
|
+
|
|
|
98
|
+ // 测试药剂类型
|
|
|
99
|
+ EnergyConsumption chemical = new EnergyConsumption();
|
|
|
100
|
+ chemical.setEnergyType("coagulant");
|
|
|
101
|
+ chemical.setConsumption(new BigDecimal("45.00"));
|
|
|
102
|
+ chemical.setUnit("kg");
|
|
|
103
|
+ chemical.setProductionVolume(new BigDecimal("15000.00"));
|
|
|
104
|
+ BigDecimal chemUnit = chemical.getConsumption()
|
|
|
105
|
+ .divide(chemical.getProductionVolume(), 4, RoundingMode.HALF_UP);
|
|
|
106
|
+ chemical.setUnitConsumption(chemUnit);
|
|
|
107
|
+
|
|
|
108
|
+ assertEquals("coagulant", chemical.getEnergyType());
|
|
|
109
|
+ assertEquals(new BigDecimal("0.0030"), chemical.getUnitConsumption());
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ @Test
|
|
|
113
|
+ @DisplayName("测试日环比计算逻辑")
|
|
|
114
|
+ void testDayOverDayCalculation() {
|
|
|
115
|
+ // 模拟进出水量日环比计算
|
|
|
116
|
+ double todayInflow = 15000.0;
|
|
|
117
|
+ double yesterdayInflow = 14000.0;
|
|
|
118
|
+ double expectedRate = ((todayInflow - yesterdayInflow) / yesterdayInflow) * 100;
|
|
|
119
|
+ BigDecimal rate = BigDecimal.valueOf(expectedRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
120
|
+ assertEquals(new BigDecimal("7.1"), rate);
|
|
|
121
|
+
|
|
|
122
|
+ // 测试下降情况
|
|
|
123
|
+ todayInflow = 13000.0;
|
|
|
124
|
+ yesterdayInflow = 14000.0;
|
|
|
125
|
+ expectedRate = ((todayInflow - yesterdayInflow) / yesterdayInflow) * 100;
|
|
|
126
|
+ rate = BigDecimal.valueOf(expectedRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
127
|
+ assertEquals(new BigDecimal("-7.1"), rate);
|
|
|
128
|
+
|
|
|
129
|
+ // 测试昨日为零的情况(除以零保护)
|
|
|
130
|
+ yesterdayInflow = 0;
|
|
|
131
|
+ if (yesterdayInflow > 0) {
|
|
|
132
|
+ fail("Should not reach here");
|
|
|
133
|
+ }
|
|
|
134
|
+ // 应该返回null,不计算
|
|
|
135
|
+ BigDecimal nullRate = null;
|
|
|
136
|
+ assertNull(nullRate);
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ @Test
|
|
|
140
|
+ @DisplayName("测试水质合格率计算逻辑")
|
|
|
141
|
+ void testWaterQualityPassRateCalculation() {
|
|
|
142
|
+ // 模拟原水检测数据
|
|
|
143
|
+ int totalTests = 200;
|
|
|
144
|
+ int passedTests = 195;
|
|
|
145
|
+ double passRate = totalTests > 0 ? (passedTests * 100.0 / totalTests) : 100.0;
|
|
|
146
|
+ BigDecimal rate = BigDecimal.valueOf(passRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
147
|
+ assertEquals(new BigDecimal("97.5"), rate);
|
|
|
148
|
+
|
|
|
149
|
+ // 模拟全部合格
|
|
|
150
|
+ totalTests = 150;
|
|
|
151
|
+ passedTests = 150;
|
|
|
152
|
+ passRate = totalTests > 0 ? (passedTests * 100.0 / totalTests) : 100.0;
|
|
|
153
|
+ rate = BigDecimal.valueOf(passRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
154
|
+ assertEquals(new BigDecimal("100.0"), rate);
|
|
|
155
|
+
|
|
|
156
|
+ // 模拟无检测数据
|
|
|
157
|
+ totalTests = 0;
|
|
|
158
|
+ passedTests = 0;
|
|
|
159
|
+ passRate = totalTests > 0 ? (passedTests * 100.0 / totalTests) : 100.0;
|
|
|
160
|
+ rate = BigDecimal.valueOf(passRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
161
|
+ assertEquals(new BigDecimal("100.0"), rate);
|
|
|
162
|
+
|
|
|
163
|
+ // 模拟综合合格率
|
|
|
164
|
+ int rawTotal = 200, rawPassed = 195;
|
|
|
165
|
+ int factoryTotal = 180, factoryPassed = 178;
|
|
|
166
|
+ int terminalTotal = 160, terminalPassed = 155;
|
|
|
167
|
+ int allTotal = rawTotal + factoryTotal + terminalTotal;
|
|
|
168
|
+ int allPassed = rawPassed + factoryPassed + terminalPassed;
|
|
|
169
|
+ double overallRate = allTotal > 0 ? (allPassed * 100.0 / allTotal) : 100.0;
|
|
|
170
|
+ BigDecimal overallBd = BigDecimal.valueOf(overallRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
171
|
+ assertEquals(new BigDecimal("97.8"), overallBd);
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ @Test
|
|
|
175
|
+ @DisplayName("测试设备在线率计算逻辑")
|
|
|
176
|
+ void testDeviceOnlineRateCalculation() {
|
|
|
177
|
+ int online = 105, offline = 10, fault = 5;
|
|
|
178
|
+ int total = online + offline + fault;
|
|
|
179
|
+ assertEquals(120, total);
|
|
|
180
|
+
|
|
|
181
|
+ double onlineRate = total > 0 ? (online * 100.0 / total) : 0.0;
|
|
|
182
|
+ BigDecimal rate = BigDecimal.valueOf(onlineRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
183
|
+ assertEquals(new BigDecimal("87.5"), rate);
|
|
|
184
|
+
|
|
|
185
|
+ // 测试全部离线
|
|
|
186
|
+ online = 0;
|
|
|
187
|
+ offline = 50;
|
|
|
188
|
+ fault = 10;
|
|
|
189
|
+ total = online + offline + fault;
|
|
|
190
|
+ onlineRate = total > 0 ? (online * 100.0 / total) : 0.0;
|
|
|
191
|
+ rate = BigDecimal.valueOf(onlineRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
192
|
+ assertEquals(new BigDecimal("0.0"), rate);
|
|
|
193
|
+
|
|
|
194
|
+ // 测试无设备
|
|
|
195
|
+ online = 0;
|
|
|
196
|
+ offline = 0;
|
|
|
197
|
+ fault = 0;
|
|
|
198
|
+ total = 0;
|
|
|
199
|
+ onlineRate = total > 0 ? (online * 100.0 / total) : 0.0;
|
|
|
200
|
+ rate = BigDecimal.valueOf(onlineRate).setScale(1, RoundingMode.HALF_UP);
|
|
|
201
|
+ assertEquals(new BigDecimal("0.0"), rate);
|
|
|
202
|
+ }
|
|
|
203
|
+
|
|
|
204
|
+ @Test
|
|
|
205
|
+ @DisplayName("测试报警级别分布统计逻辑")
|
|
|
206
|
+ void testAlertLevelDistribution() {
|
|
|
207
|
+ // 模拟数据库返回的级别统计
|
|
|
208
|
+ List<Map<String, Object>> levelStats = new ArrayList<>();
|
|
|
209
|
+ levelStats.add(Map.of("alert_level", "general", "count", 5L));
|
|
|
210
|
+ levelStats.add(Map.of("alert_level", "important", "count", 2L));
|
|
|
211
|
+ levelStats.add(Map.of("alert_level", "urgent", "count", 1L));
|
|
|
212
|
+
|
|
|
213
|
+ Map<String, Integer> levelMap = new LinkedHashMap<>();
|
|
|
214
|
+ levelMap.put("general", 0);
|
|
|
215
|
+ levelMap.put("important", 0);
|
|
|
216
|
+ levelMap.put("urgent", 0);
|
|
|
217
|
+
|
|
|
218
|
+ for (Map<String, Object> row : levelStats) {
|
|
|
219
|
+ String level = (String) row.get("alert_level");
|
|
|
220
|
+ int count = ((Number) row.get("count")).intValue();
|
|
|
221
|
+ levelMap.put(level, count);
|
|
|
222
|
+ }
|
|
|
223
|
+
|
|
|
224
|
+ assertEquals(5, levelMap.get("general"));
|
|
|
225
|
+ assertEquals(2, levelMap.get("important"));
|
|
|
226
|
+ assertEquals(1, levelMap.get("urgent"));
|
|
|
227
|
+ assertEquals(8, levelMap.values().stream().mapToInt(Integer::intValue).sum());
|
|
|
228
|
+
|
|
|
229
|
+ // 测试缺少某个级别时的默认值
|
|
|
230
|
+ List<Map<String, Object>> partialStats = new ArrayList<>();
|
|
|
231
|
+ partialStats.add(Map.of("alert_level", "urgent", "count", 3L));
|
|
|
232
|
+
|
|
|
233
|
+ Map<String, Integer> partialMap = new LinkedHashMap<>();
|
|
|
234
|
+ partialMap.put("general", 0);
|
|
|
235
|
+ partialMap.put("important", 0);
|
|
|
236
|
+ partialMap.put("urgent", 0);
|
|
|
237
|
+
|
|
|
238
|
+ for (Map<String, Object> row : partialStats) {
|
|
|
239
|
+ String level = (String) row.get("alert_level");
|
|
|
240
|
+ int count = ((Number) row.get("count")).intValue();
|
|
|
241
|
+ partialMap.put(level, count);
|
|
|
242
|
+ }
|
|
|
243
|
+
|
|
|
244
|
+ assertEquals(0, partialMap.get("general"));
|
|
|
245
|
+ assertEquals(0, partialMap.get("important"));
|
|
|
246
|
+ assertEquals(3, partialMap.get("urgent"));
|
|
|
247
|
+ }
|
|
|
248
|
+
|
|
|
249
|
+ @Test
|
|
|
250
|
+ @DisplayName("测试月度进出水量趋势数据结构")
|
|
|
251
|
+ void testMonthlyFlowTrendStructure() {
|
|
|
252
|
+ // 模拟月度趋势数据
|
|
|
253
|
+ List<Map<String, Object>> trend = new ArrayList<>();
|
|
|
254
|
+ for (int i = 0; i < 6; i++) {
|
|
|
255
|
+ Map<String, Object> month = new LinkedHashMap<>();
|
|
|
256
|
+ month.put("month", LocalDate.of(2026, i + 1, 1));
|
|
|
257
|
+ month.put("total_inflow", new BigDecimal(String.valueOf(400000 + i * 10000)));
|
|
|
258
|
+ month.put("total_outflow", new BigDecimal(String.valueOf(380000 + i * 9500)));
|
|
|
259
|
+ trend.add(month);
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
|
262
|
+ assertEquals(6, trend.size());
|
|
|
263
|
+ assertEquals(LocalDate.of(2026, 1, 1), trend.get(0).get("month"));
|
|
|
264
|
+ assertEquals(new BigDecimal("400000"), trend.get(0).get("total_inflow"));
|
|
|
265
|
+ assertEquals(new BigDecimal("450000"), trend.get(5).get("total_inflow"));
|
|
|
266
|
+
|
|
|
267
|
+ // 验证趋势递增
|
|
|
268
|
+ for (int i = 1; i < trend.size(); i++) {
|
|
|
269
|
+ BigDecimal prev = (BigDecimal) trend.get(i - 1).get("total_inflow");
|
|
|
270
|
+ BigDecimal curr = (BigDecimal) trend.get(i).get("total_inflow");
|
|
|
271
|
+ assertTrue(curr.compareTo(prev) > 0);
|
|
|
272
|
+ }
|
|
|
273
|
+ }
|
|
|
274
|
+
|
|
|
275
|
+ @Test
|
|
|
276
|
+ @DisplayName("测试能耗汇总按类型聚合逻辑")
|
|
|
277
|
+ void testEnergySummaryAggregation() {
|
|
|
278
|
+ // 模拟能耗数据
|
|
|
279
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
280
|
+ items.add(Map.of("energy_type", "power", "total", new BigDecimal("1250.50"), "unit", "kWh"));
|
|
|
281
|
+ items.add(Map.of("energy_type", "coagulant", "total", new BigDecimal("45.00"), "unit", "kg"));
|
|
|
282
|
+ items.add(Map.of("energy_type", "disinfectant", "total", new BigDecimal("12.50"), "unit", "kg"));
|
|
|
283
|
+
|
|
|
284
|
+ BigDecimal totalPower = BigDecimal.ZERO;
|
|
|
285
|
+ BigDecimal totalChemical = BigDecimal.ZERO;
|
|
|
286
|
+
|
|
|
287
|
+ for (Map<String, Object> item : items) {
|
|
|
288
|
+ String type = (String) item.get("energy_type");
|
|
|
289
|
+ BigDecimal total = (BigDecimal) item.get("total");
|
|
|
290
|
+ if ("power".equals(type)) {
|
|
|
291
|
+ totalPower = total;
|
|
|
292
|
+ } else {
|
|
|
293
|
+ totalChemical = totalChemical.add(total);
|
|
|
294
|
+ }
|
|
|
295
|
+ }
|
|
|
296
|
+
|
|
|
297
|
+ assertEquals(new BigDecimal("1250.50"), totalPower);
|
|
|
298
|
+ assertEquals(new BigDecimal("57.50"), totalChemical);
|
|
|
299
|
+
|
|
|
300
|
+ // 验证总能耗
|
|
|
301
|
+ BigDecimal grandTotal = totalPower.add(totalChemical);
|
|
|
302
|
+ assertEquals(new BigDecimal("1308.00"), grandTotal);
|
|
|
303
|
+ }
|
|
|
304
|
+}
|