|
|
@@ -3,102 +3,321 @@ package com.water.production.service;
|
|
3
|
3
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
4
|
4
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
5
|
5
|
import com.water.production.entity.IntrusionEvent;
|
|
|
6
|
+import com.water.production.entity.VideoCamera;
|
|
6
|
7
|
import com.water.production.mapper.IntrusionEventMapper;
|
|
7
|
8
|
import lombok.RequiredArgsConstructor;
|
|
|
9
|
+import lombok.extern.slf4j.Slf4j;
|
|
8
|
10
|
import org.springframework.stereotype.Service;
|
|
9
|
11
|
|
|
|
12
|
+import java.math.BigDecimal;
|
|
|
13
|
+import java.math.RoundingMode;
|
|
|
14
|
+import java.time.LocalDate;
|
|
10
|
15
|
import java.time.LocalDateTime;
|
|
11
|
|
-import java.util.*;
|
|
|
16
|
+import java.util.ArrayList;
|
|
|
17
|
+import java.util.LinkedHashMap;
|
|
|
18
|
+import java.util.List;
|
|
|
19
|
+import java.util.Map;
|
|
12
|
20
|
|
|
|
21
|
+/**
|
|
|
22
|
+ * AI 人员闯入检测服务(对应 Issue #10 MON-07~09)。
|
|
|
23
|
+ *
|
|
|
24
|
+ * <p>基于 master 真实实体 {@link IntrusionEvent}(cameraId/alertStatus/detectedAt/handleResult 等)
|
|
|
25
|
+ * 与 {@link IntrusionEventMapper},实现 VideoMonitorController 调用的全部方法。
|
|
|
26
|
+ *
|
|
|
27
|
+ * <p>历史版本曾假设实体有 eventNo/detectedTime/confirmedBy/status(String) 等字段,
|
|
|
28
|
+ * 与真实 {@link IntrusionEvent} 不符(编译失败),本版本整体对齐真实字段。
|
|
|
29
|
+ */
|
|
|
30
|
+@Slf4j
|
|
13
|
31
|
@Service
|
|
14
|
32
|
@RequiredArgsConstructor
|
|
15
|
33
|
public class IntrusionDetectionService {
|
|
16
|
34
|
|
|
17
|
35
|
private final IntrusionEventMapper eventMapper;
|
|
|
36
|
+ private final VideoMonitorService videoMonitorService;
|
|
|
37
|
+
|
|
|
38
|
+ /** 报警状态:0=待处理, 1=已确认, 2=已处理, 3=已忽略 */
|
|
|
39
|
+ private static final int STATUS_PENDING = 0;
|
|
|
40
|
+ private static final int STATUS_CONFIRMED = 1;
|
|
|
41
|
+ private static final int STATUS_HANDLED = 2;
|
|
|
42
|
+ private static final int STATUS_DISMISSED = 3;
|
|
|
43
|
+
|
|
|
44
|
+ // ==================== 检测 ====================
|
|
18
|
45
|
|
|
19
|
46
|
/**
|
|
20
|
|
- * 模拟AI检测闯入事件
|
|
|
47
|
+ * 单路 AI 检测(MON-08)。模拟模型推理,命中则生成事件。
|
|
21
|
48
|
*/
|
|
22
|
|
- public IntrusionEvent detect(String cameraId, String area) {
|
|
23
|
|
- double confidence = 0.7 + Math.random() * 0.3;
|
|
|
49
|
+ public Map<String, Object> detectIntrusion(Long cameraId, byte[] frameData) {
|
|
|
50
|
+ VideoCamera camera = videoMonitorService.getCameraById(cameraId);
|
|
|
51
|
+ if (camera == null) {
|
|
|
52
|
+ throw new RuntimeException("摄像头不存在: " + cameraId);
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ // 模拟 AI 置信度推理(真实场景对接模型 SDK)
|
|
|
56
|
+ double confidence = 0.70 + Math.random() * 0.30;
|
|
24
|
57
|
boolean detected = confidence > 0.85;
|
|
25
|
58
|
|
|
26
|
|
- IntrusionEvent event = new IntrusionEvent();
|
|
27
|
|
- event.setEventNo("INT-" + System.currentTimeMillis());
|
|
28
|
|
- event.setCameraId(cameraId);
|
|
29
|
|
- event.setArea(area);
|
|
30
|
|
- event.setDetectedTime(LocalDateTime.now());
|
|
31
|
|
- event.setConfidence(confidence);
|
|
32
|
|
- event.setDetected(detected);
|
|
33
|
|
- event.setStatus(detected ? "ACTIVE" : "FALSE_POSITIVE");
|
|
34
|
|
- event.setAlertLevel(detected ? (confidence > 0.95 ? "紧急" : "重要") : "一般");
|
|
35
|
|
- event.setSnapshotUrl("/snapshots/" + event.getEventNo() + ".jpg");
|
|
|
59
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
60
|
+ result.put("cameraId", cameraId);
|
|
|
61
|
+ result.put("cameraName", camera.getName());
|
|
|
62
|
+ result.put("detected", detected);
|
|
|
63
|
+ result.put("confidence", round(confidence));
|
|
36
|
64
|
|
|
37
|
65
|
if (detected) {
|
|
38
|
|
- event.setAlertTriggered(true);
|
|
39
|
|
- event.setDescription(String.format("AI检测到人员闯入 (置信度: %.1f%%)", confidence * 100));
|
|
|
66
|
+ IntrusionEvent event = buildEvent(camera, confidence);
|
|
|
67
|
+ eventMapper.insert(event);
|
|
|
68
|
+ result.put("eventId", event.getId());
|
|
|
69
|
+ result.put("alertLevel", event.getAlertLevel());
|
|
|
70
|
+ result.put("snapshotUrl", event.getSnapshotUrl());
|
|
|
71
|
+ log.info("AI检测到人员闯入: camera={}, confidence={}, eventId={}",
|
|
|
72
|
+ camera.getName(), round(confidence), event.getId());
|
|
40
|
73
|
}
|
|
41
|
|
-
|
|
42
|
|
- eventMapper.insert(event);
|
|
43
|
|
- return event;
|
|
|
74
|
+ return result;
|
|
44
|
75
|
}
|
|
45
|
76
|
|
|
46
|
77
|
/**
|
|
47
|
|
- * 确认/处理闯入事件
|
|
|
78
|
+ * 批量多路检测(MON-08)。
|
|
48
|
79
|
*/
|
|
49
|
|
- public IntrusionEvent handleEvent(Long eventId, String action, String operator) {
|
|
50
|
|
- IntrusionEvent event = eventMapper.selectById(eventId);
|
|
51
|
|
- if (event == null) throw new RuntimeException("事件不存在");
|
|
52
|
|
-
|
|
53
|
|
- switch (action) {
|
|
54
|
|
- case "confirm" -> { event.setStatus("CONFIRMED"); event.setConfirmedBy(operator); event.setConfirmedTime(LocalDateTime.now()); }
|
|
55
|
|
- case "dismiss" -> { event.setStatus("DISMISSED"); event.setConfirmedBy(operator); event.setConfirmedTime(LocalDateTime.now()); }
|
|
56
|
|
- case "resolve" -> { event.setStatus("RESOLVED"); event.setResolvedTime(LocalDateTime.now()); }
|
|
57
|
|
- default -> throw new RuntimeException("未知操作: " + action);
|
|
|
80
|
+ public List<Map<String, Object>> batchDetect(List<Long> cameraIds) {
|
|
|
81
|
+ List<Map<String, Object>> results = new ArrayList<>();
|
|
|
82
|
+ if (cameraIds == null) {
|
|
|
83
|
+ return results;
|
|
58
|
84
|
}
|
|
59
|
|
- eventMapper.updateById(event);
|
|
60
|
|
- return event;
|
|
|
85
|
+ for (Long cameraId : cameraIds) {
|
|
|
86
|
+ try {
|
|
|
87
|
+ results.add(detectIntrusion(cameraId, null));
|
|
|
88
|
+ } catch (Exception e) {
|
|
|
89
|
+ log.warn("批量检测失败 cameraId={}: {}", cameraId, e.getMessage());
|
|
|
90
|
+ }
|
|
|
91
|
+ }
|
|
|
92
|
+ return results;
|
|
61
|
93
|
}
|
|
62
|
94
|
|
|
|
95
|
+ // ==================== 事件查询 ====================
|
|
|
96
|
+
|
|
63
|
97
|
/**
|
|
64
|
|
- * 分页查询闯入事件
|
|
|
98
|
+ * 分页查询闯入事件(MON-09 异常查询),支持多维筛选。
|
|
65
|
99
|
*/
|
|
66
|
|
- public Page<IntrusionEvent> listEvents(String status, String area, String alertLevel,
|
|
67
|
|
- int pageNum, int pageSize) {
|
|
68
|
|
- Page<IntrusionEvent> page = new Page<>(pageNum, pageSize);
|
|
|
100
|
+ public Page<IntrusionEvent> pageEvents(int current, int size, Long cameraId, String area,
|
|
|
101
|
+ String alertLevel, Integer alertStatus,
|
|
|
102
|
+ String startDate, String endDate) {
|
|
|
103
|
+ Page<IntrusionEvent> page = new Page<>(current, size);
|
|
69
|
104
|
LambdaQueryWrapper<IntrusionEvent> wrapper = new LambdaQueryWrapper<>();
|
|
70
|
|
- if (status != null && !status.isBlank()) wrapper.eq(IntrusionEvent::getStatus, status);
|
|
71
|
|
- if (area != null && !area.isBlank()) wrapper.like(IntrusionEvent::getArea, area);
|
|
72
|
|
- if (alertLevel != null && !alertLevel.isBlank()) wrapper.eq(IntrusionEvent::getAlertLevel, alertLevel);
|
|
73
|
|
- wrapper.orderByDesc(IntrusionEvent::getDetectedTime);
|
|
|
105
|
+ if (cameraId != null) {
|
|
|
106
|
+ wrapper.eq(IntrusionEvent::getCameraId, cameraId);
|
|
|
107
|
+ }
|
|
|
108
|
+ if (area != null && !area.isBlank()) {
|
|
|
109
|
+ wrapper.like(IntrusionEvent::getArea, area);
|
|
|
110
|
+ }
|
|
|
111
|
+ if (alertLevel != null && !alertLevel.isBlank()) {
|
|
|
112
|
+ wrapper.eq(IntrusionEvent::getAlertLevel, alertLevel);
|
|
|
113
|
+ }
|
|
|
114
|
+ if (alertStatus != null) {
|
|
|
115
|
+ wrapper.eq(IntrusionEvent::getAlertStatus, alertStatus);
|
|
|
116
|
+ }
|
|
|
117
|
+ LocalDateTime start = parseDateTime(startDate);
|
|
|
118
|
+ LocalDateTime end = parseDateTime(endDate);
|
|
|
119
|
+ if (start != null) {
|
|
|
120
|
+ wrapper.ge(IntrusionEvent::getDetectedAt, start);
|
|
|
121
|
+ }
|
|
|
122
|
+ if (end != null) {
|
|
|
123
|
+ wrapper.le(IntrusionEvent::getDetectedAt, end);
|
|
|
124
|
+ }
|
|
|
125
|
+ wrapper.orderByDesc(IntrusionEvent::getDetectedAt);
|
|
74
|
126
|
return eventMapper.selectPage(page, wrapper);
|
|
75
|
127
|
}
|
|
76
|
128
|
|
|
77
|
|
- /**
|
|
78
|
|
- * 闯入事件统计
|
|
79
|
|
- */
|
|
80
|
|
- public Map<String, Object> getStatistics() {
|
|
81
|
|
- long total = eventMapper.selectCount(null);
|
|
82
|
|
- long active = eventMapper.selectCount(new LambdaQueryWrapper<IntrusionEvent>()
|
|
83
|
|
- .eq(IntrusionEvent::getStatus, "ACTIVE"));
|
|
84
|
|
- long confirmed = eventMapper.selectCount(new LambdaQueryWrapper<IntrusionEvent>()
|
|
85
|
|
- .eq(IntrusionEvent::getStatus, "CONFIRMED"));
|
|
86
|
|
- long todayEvents = eventMapper.selectCount(new LambdaQueryWrapper<IntrusionEvent>()
|
|
87
|
|
- .ge(IntrusionEvent::getDetectedTime, LocalDateTime.now().toLocalDate().atStartOfDay()));
|
|
88
|
|
-
|
|
89
|
|
- double avgConfidence = eventMapper.selectList(null).stream()
|
|
90
|
|
- .mapToDouble(e -> e.getConfidence() != null ? e.getConfidence() : 0)
|
|
91
|
|
- .average().orElse(0);
|
|
|
129
|
+ /** 事件详情 */
|
|
|
130
|
+ public IntrusionEvent getEventById(Long id) {
|
|
|
131
|
+ return eventMapper.selectById(id);
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ // ==================== 事件处理 ====================
|
|
|
135
|
+
|
|
|
136
|
+ /** 确认事件(alertStatus → 1) */
|
|
|
137
|
+ public boolean confirmEvent(Long id, Long userId) {
|
|
|
138
|
+ IntrusionEvent event = eventMapper.selectById(id);
|
|
|
139
|
+ if (event == null) {
|
|
|
140
|
+ return false;
|
|
|
141
|
+ }
|
|
|
142
|
+ event.setAlertStatus(STATUS_CONFIRMED);
|
|
|
143
|
+ event.setHandledBy(userId);
|
|
|
144
|
+ event.setHandledTime(LocalDateTime.now());
|
|
|
145
|
+ eventMapper.updateById(event);
|
|
|
146
|
+ return true;
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ /** 处理事件(alertStatus → 2,记录处理结果) */
|
|
|
150
|
+ public boolean handleEvent(Long id, Long userId, String handlerName, String result) {
|
|
|
151
|
+ IntrusionEvent event = eventMapper.selectById(id);
|
|
|
152
|
+ if (event == null) {
|
|
|
153
|
+ return false;
|
|
|
154
|
+ }
|
|
|
155
|
+ event.setAlertStatus(STATUS_HANDLED);
|
|
|
156
|
+ event.setHandledBy(userId);
|
|
|
157
|
+ event.setHandlerName(handlerName);
|
|
|
158
|
+ event.setHandleResult(result);
|
|
|
159
|
+ event.setHandledTime(LocalDateTime.now());
|
|
|
160
|
+ eventMapper.updateById(event);
|
|
|
161
|
+ return true;
|
|
|
162
|
+ }
|
|
|
163
|
+
|
|
|
164
|
+ /** 忽略/误报标记(alertStatus → 3) */
|
|
|
165
|
+ public boolean dismissEvent(Long id, String remark) {
|
|
|
166
|
+ IntrusionEvent event = eventMapper.selectById(id);
|
|
|
167
|
+ if (event == null) {
|
|
|
168
|
+ return false;
|
|
|
169
|
+ }
|
|
|
170
|
+ event.setAlertStatus(STATUS_DISMISSED);
|
|
|
171
|
+ event.setRemark(remark);
|
|
|
172
|
+ event.setHandledTime(LocalDateTime.now());
|
|
|
173
|
+ eventMapper.updateById(event);
|
|
|
174
|
+ return true;
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ // ==================== 统计分析(MON-09)====================
|
|
|
178
|
+
|
|
|
179
|
+ /** 闯入事件统计(按 period: today/week/month) */
|
|
|
180
|
+ public Map<String, Object> getIntrusionStats(String period) {
|
|
|
181
|
+ long total = countSince(period, null);
|
|
|
182
|
+ long pending = countSince(period, STATUS_PENDING);
|
|
|
183
|
+ long confirmed = countSince(period, STATUS_CONFIRMED);
|
|
|
184
|
+ long handled = countSince(period, STATUS_HANDLED);
|
|
|
185
|
+ long dismissed = countSince(period, STATUS_DISMISSED);
|
|
|
186
|
+
|
|
|
187
|
+ LambdaQueryWrapper<IntrusionEvent> listWrapper = new LambdaQueryWrapper<>();
|
|
|
188
|
+ LocalDateTime since = periodStart(period);
|
|
|
189
|
+ if (since != null) {
|
|
|
190
|
+ listWrapper.ge(IntrusionEvent::getDetectedAt, since);
|
|
|
191
|
+ }
|
|
|
192
|
+ double avgConfidence = eventMapper.selectList(listWrapper).stream()
|
|
|
193
|
+ .mapToDouble(e -> e.getConfidence() != null ? e.getConfidence().doubleValue() : 0)
|
|
|
194
|
+ .average().orElse(0);
|
|
|
195
|
+ double falsePositiveRate = total > 0 ? (double) dismissed / total : 0;
|
|
92
|
196
|
|
|
93
|
197
|
Map<String, Object> stats = new LinkedHashMap<>();
|
|
|
198
|
+ stats.put("period", period);
|
|
94
|
199
|
stats.put("total", total);
|
|
95
|
|
- stats.put("active", active);
|
|
|
200
|
+ stats.put("pending", pending);
|
|
96
|
201
|
stats.put("confirmed", confirmed);
|
|
97
|
|
- stats.put("todayEvents", todayEvents);
|
|
98
|
|
- stats.put("avgConfidence", avgConfidence);
|
|
99
|
|
- stats.put("falsePositiveRate", total > 0 ?
|
|
100
|
|
- (double) eventMapper.selectCount(new LambdaQueryWrapper<IntrusionEvent>()
|
|
101
|
|
- .eq(IntrusionEvent::getStatus, "FALSE_POSITIVE")) / total : 0);
|
|
|
202
|
+ stats.put("handled", handled);
|
|
|
203
|
+ stats.put("dismissed", dismissed);
|
|
|
204
|
+ stats.put("avgConfidence", round(avgConfidence));
|
|
|
205
|
+ stats.put("falsePositiveRate", round(falsePositiveRate));
|
|
102
|
206
|
return stats;
|
|
103
|
207
|
}
|
|
|
208
|
+
|
|
|
209
|
+ /** 统计某区间内(可选)某状态的事件数 */
|
|
|
210
|
+ private long countSince(String period, Integer status) {
|
|
|
211
|
+ LambdaQueryWrapper<IntrusionEvent> w = new LambdaQueryWrapper<>();
|
|
|
212
|
+ LocalDateTime since = periodStart(period);
|
|
|
213
|
+ if (since != null) {
|
|
|
214
|
+ w.ge(IntrusionEvent::getDetectedAt, since);
|
|
|
215
|
+ }
|
|
|
216
|
+ if (status != null) {
|
|
|
217
|
+ w.eq(IntrusionEvent::getAlertStatus, status);
|
|
|
218
|
+ }
|
|
|
219
|
+ return eventMapper.selectCount(w);
|
|
|
220
|
+ }
|
|
|
221
|
+
|
|
|
222
|
+ /** 闯入事件趋势(按天) */
|
|
|
223
|
+ public List<Map<String, Object>> getIntrusionTrend(int days) {
|
|
|
224
|
+ LocalDateTime since = LocalDate.now().minusDays(Math.max(days - 1, 0)).atStartOfDay();
|
|
|
225
|
+ List<IntrusionEvent> events = eventMapper.selectList(
|
|
|
226
|
+ new LambdaQueryWrapper<IntrusionEvent>().ge(IntrusionEvent::getDetectedAt, since));
|
|
|
227
|
+
|
|
|
228
|
+ Map<LocalDate, Long> byDay = new LinkedHashMap<>();
|
|
|
229
|
+ for (int i = Math.max(days - 1, 0); i >= 0; i--) {
|
|
|
230
|
+ byDay.put(LocalDate.now().minusDays(i), 0L);
|
|
|
231
|
+ }
|
|
|
232
|
+ for (IntrusionEvent e : events) {
|
|
|
233
|
+ if (e.getDetectedAt() != null) {
|
|
|
234
|
+ LocalDate day = e.getDetectedAt().toLocalDate();
|
|
|
235
|
+ byDay.merge(day, 1L, Long::sum);
|
|
|
236
|
+ }
|
|
|
237
|
+ }
|
|
|
238
|
+ List<Map<String, Object>> trend = new ArrayList<>();
|
|
|
239
|
+ byDay.forEach((day, count) -> {
|
|
|
240
|
+ Map<String, Object> point = new LinkedHashMap<>();
|
|
|
241
|
+ point.put("date", day.toString());
|
|
|
242
|
+ point.put("count", count);
|
|
|
243
|
+ trend.add(point);
|
|
|
244
|
+ });
|
|
|
245
|
+ return trend;
|
|
|
246
|
+ }
|
|
|
247
|
+
|
|
|
248
|
+ /** 高频闯入摄像头排行 */
|
|
|
249
|
+ public List<Map<String, Object>> getTopIntrusionCameras(int limit) {
|
|
|
250
|
+ List<IntrusionEvent> events = eventMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
251
|
+ Map<Long, long[]> counter = new LinkedHashMap<>(); // cameraId -> [count, alertStatus sum]
|
|
|
252
|
+ Map<Long, String> names = new LinkedHashMap<>();
|
|
|
253
|
+ for (IntrusionEvent e : events) {
|
|
|
254
|
+ if (e.getCameraId() == null) {
|
|
|
255
|
+ continue;
|
|
|
256
|
+ }
|
|
|
257
|
+ long[] arr = counter.computeIfAbsent(e.getCameraId(), k -> new long[2]);
|
|
|
258
|
+ arr[0]++;
|
|
|
259
|
+ if (e.getCameraName() != null) {
|
|
|
260
|
+ names.putIfAbsent(e.getCameraId(), e.getCameraName());
|
|
|
261
|
+ }
|
|
|
262
|
+ }
|
|
|
263
|
+ return counter.entrySet().stream()
|
|
|
264
|
+ .sorted((a, b) -> Long.compare(b.getValue()[0], a.getValue()[0]))
|
|
|
265
|
+ .limit(limit <= 0 ? 10 : limit)
|
|
|
266
|
+ .map(en -> {
|
|
|
267
|
+ Map<String, Object> m = new LinkedHashMap<>();
|
|
|
268
|
+ m.put("cameraId", en.getKey());
|
|
|
269
|
+ m.put("cameraName", names.getOrDefault(en.getKey(), "摄像头#" + en.getKey()));
|
|
|
270
|
+ m.put("count", en.getValue()[0]);
|
|
|
271
|
+ return m;
|
|
|
272
|
+ })
|
|
|
273
|
+ .toList();
|
|
|
274
|
+ }
|
|
|
275
|
+
|
|
|
276
|
+ // ==================== 工具方法 ====================
|
|
|
277
|
+
|
|
|
278
|
+ /** 构造一次检测命中的闯入事件 */
|
|
|
279
|
+ private IntrusionEvent buildEvent(VideoCamera camera, double confidence) {
|
|
|
280
|
+ IntrusionEvent event = new IntrusionEvent();
|
|
|
281
|
+ event.setCameraId(camera.getId());
|
|
|
282
|
+ event.setCameraName(camera.getName());
|
|
|
283
|
+ event.setArea(camera.getArea());
|
|
|
284
|
+ event.setEventType("person_intrusion");
|
|
|
285
|
+ event.setConfidence(BigDecimal.valueOf(round(confidence)));
|
|
|
286
|
+ event.setAlertLevel(confidence > 0.95 ? "critical" : "warning");
|
|
|
287
|
+ event.setAlertStatus(STATUS_PENDING);
|
|
|
288
|
+ event.setDetectedAt(LocalDateTime.now());
|
|
|
289
|
+ event.setSnapshotUrl("/snapshots/" + camera.getId() + "_" + System.currentTimeMillis() + ".jpg");
|
|
|
290
|
+ return event;
|
|
|
291
|
+ }
|
|
|
292
|
+
|
|
|
293
|
+ private LocalDateTime parseDateTime(String dateStr) {
|
|
|
294
|
+ if (dateStr == null || dateStr.isBlank()) {
|
|
|
295
|
+ return null;
|
|
|
296
|
+ }
|
|
|
297
|
+ try {
|
|
|
298
|
+ if (dateStr.length() <= 10) {
|
|
|
299
|
+ return LocalDate.parse(dateStr).atStartOfDay();
|
|
|
300
|
+ }
|
|
|
301
|
+ return LocalDateTime.parse(dateStr.replace(' ', 'T'));
|
|
|
302
|
+ } catch (Exception e) {
|
|
|
303
|
+ log.warn("无法解析时间字符串: {}", dateStr);
|
|
|
304
|
+ return null;
|
|
|
305
|
+ }
|
|
|
306
|
+ }
|
|
|
307
|
+
|
|
|
308
|
+ private LocalDateTime periodStart(String period) {
|
|
|
309
|
+ if (period == null) {
|
|
|
310
|
+ return null;
|
|
|
311
|
+ }
|
|
|
312
|
+ return switch (period) {
|
|
|
313
|
+ case "today" -> LocalDate.now().atStartOfDay();
|
|
|
314
|
+ case "week" -> LocalDate.now().minusDays(6).atStartOfDay();
|
|
|
315
|
+ case "month" -> LocalDate.now().minusDays(29).atStartOfDay();
|
|
|
316
|
+ default -> null;
|
|
|
317
|
+ };
|
|
|
318
|
+ }
|
|
|
319
|
+
|
|
|
320
|
+ private double round(double v) {
|
|
|
321
|
+ return BigDecimal.valueOf(v).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
|
|
322
|
+ }
|
|
104
|
323
|
}
|