|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+package com.water.patrol.service;
|
|
|
2
|
+
|
|
|
3
|
+import lombok.RequiredArgsConstructor;
|
|
|
4
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
5
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
6
|
+import org.springframework.stereotype.Service;
|
|
|
7
|
+
|
|
|
8
|
+import java.time.LocalDate;
|
|
|
9
|
+import java.time.LocalDateTime;
|
|
|
10
|
+import java.util.*;
|
|
|
11
|
+
|
|
|
12
|
+/**
|
|
|
13
|
+ * 巡检管理核心服务 - Issue #86
|
|
|
14
|
+ * PAT-01 总览, PAT-02 轨迹, PAT-03 任务台账, PAT-04 设备台账
|
|
|
15
|
+ */
|
|
|
16
|
+@Slf4j
|
|
|
17
|
+@Service
|
|
|
18
|
+@RequiredArgsConstructor
|
|
|
19
|
+public class PatrolCoreService {
|
|
|
20
|
+
|
|
|
21
|
+ private final JdbcTemplate jdbc;
|
|
|
22
|
+
|
|
|
23
|
+ // ========== PAT-01 巡检总览 ==========
|
|
|
24
|
+
|
|
|
25
|
+ public Map<String, Object> getOverview() {
|
|
|
26
|
+ LocalDate today = LocalDate.now();
|
|
|
27
|
+ LocalDate monthStart = today.withDayOfMonth(1);
|
|
|
28
|
+
|
|
|
29
|
+ long todayTotal = countQuery("SELECT COUNT(*) FROM patrol_task WHERE task_date = ?", today);
|
|
|
30
|
+ long todayCompleted = countQuery("SELECT COUNT(*) FROM patrol_task WHERE task_date = ? AND status = 'completed'", today);
|
|
|
31
|
+ long monthTotal = countQuery("SELECT COUNT(*) FROM patrol_task WHERE task_date >= ?", monthStart);
|
|
|
32
|
+ long monthCompleted = countQuery("SELECT COUNT(*) FROM patrol_task WHERE task_date >= ? AND status = 'completed'", monthStart);
|
|
|
33
|
+
|
|
|
34
|
+ long pendingIssues = countQuery("SELECT COUNT(*) FROM patrol_work_order WHERE status IN ('pending','assigned','processing')");
|
|
|
35
|
+ long resolvedIssues = countQuery("SELECT COUNT(*) FROM patrol_work_order WHERE status = 'resolved'");
|
|
|
36
|
+
|
|
|
37
|
+ long coverageRate = monthTotal > 0 ? Math.round(monthCompleted * 100.0 / monthTotal) : 0;
|
|
|
38
|
+
|
|
|
39
|
+ Map<String, Object> overview = new LinkedHashMap<>();
|
|
|
40
|
+ overview.put("todayTotal", todayTotal);
|
|
|
41
|
+ overview.put("todayCompleted", todayCompleted);
|
|
|
42
|
+ overview.put("monthTotal", monthTotal);
|
|
|
43
|
+ overview.put("monthCompleted", monthCompleted);
|
|
|
44
|
+ overview.put("coverageRate", coverageRate);
|
|
|
45
|
+ overview.put("pendingIssues", pendingIssues);
|
|
|
46
|
+ overview.put("resolvedIssues", resolvedIssues);
|
|
|
47
|
+ return overview;
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ public List<Map<String, Object>> getTodayTasks() {
|
|
|
51
|
+ return jdbc.queryForList(
|
|
|
52
|
+ "SELECT pt.*, pr.route_name, pr.area FROM patrol_task pt " +
|
|
|
53
|
+ "LEFT JOIN patrol_route pr ON pt.route_id = pr.id " +
|
|
|
54
|
+ "WHERE pt.task_date = CURRENT_DATE ORDER BY pt.plan_start");
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ public List<Map<String, Object>> getRecentIssues(int limit) {
|
|
|
58
|
+ return jdbc.queryForList(
|
|
|
59
|
+ "SELECT * FROM patrol_work_order ORDER BY created_at DESC LIMIT ?", limit);
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ // ========== PAT-02 任务轨迹(GPS记录与回放) ==========
|
|
|
63
|
+
|
|
|
64
|
+ public Map<String, Object> recordTrackPoint(Long taskId, Long workerId,
|
|
|
65
|
+ double lng, double lat,
|
|
|
66
|
+ double speed, double accuracy) {
|
|
|
67
|
+ jdbc.update(
|
|
|
68
|
+ "INSERT INTO patrol_track_point (task_id, worker_id, lng, lat, speed, accuracy) " +
|
|
|
69
|
+ "VALUES (?,?,?,?,?,?)",
|
|
|
70
|
+ taskId, workerId, lng, lat, speed, accuracy);
|
|
|
71
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
72
|
+ result.put("taskId", taskId);
|
|
|
73
|
+ result.put("workerId", workerId);
|
|
|
74
|
+ result.put("lng", lng);
|
|
|
75
|
+ result.put("lat", lat);
|
|
|
76
|
+ result.put("recorded", true);
|
|
|
77
|
+ return result;
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ public List<Map<String, Object>> getTrack(Long taskId) {
|
|
|
81
|
+ return jdbc.queryForList(
|
|
|
82
|
+ "SELECT * FROM patrol_track_point WHERE task_id = ? ORDER BY recorded_at", taskId);
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ public Map<String, Object> replayTrack(Long taskId) {
|
|
|
86
|
+ List<Map<String, Object>> points = getTrack(taskId);
|
|
|
87
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
88
|
+ result.put("taskId", taskId);
|
|
|
89
|
+ result.put("points", points);
|
|
|
90
|
+ result.put("totalPoints", points.size());
|
|
|
91
|
+ if (!points.isEmpty()) {
|
|
|
92
|
+ result.put("startTime", points.get(0).get("recorded_at"));
|
|
|
93
|
+ result.put("endTime", points.get(points.size() - 1).get("recorded_at"));
|
|
|
94
|
+ }
|
|
|
95
|
+ return result;
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ public Map<String, Object> getTrackStats(Long taskId) {
|
|
|
99
|
+ List<Map<String, Object>> points = getTrack(taskId);
|
|
|
100
|
+ Map<String, Object> stats = new LinkedHashMap<>();
|
|
|
101
|
+ stats.put("taskId", taskId);
|
|
|
102
|
+ stats.put("totalPoints", points.size());
|
|
|
103
|
+
|
|
|
104
|
+ if (points.size() < 2) {
|
|
|
105
|
+ stats.put("totalDistance", 0.0);
|
|
|
106
|
+ stats.put("duration", 0);
|
|
|
107
|
+ stats.put("avgSpeed", 0.0);
|
|
|
108
|
+ return stats;
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ double totalDistance = 0;
|
|
|
112
|
+ for (int i = 1; i < points.size(); i++) {
|
|
|
113
|
+ double lat1 = ((Number) points.get(i - 1).get("lat")).doubleValue();
|
|
|
114
|
+ double lon1 = ((Number) points.get(i - 1).get("lng")).doubleValue();
|
|
|
115
|
+ double lat2 = ((Number) points.get(i).get("lat")).doubleValue();
|
|
|
116
|
+ double lon2 = ((Number) points.get(i).get("lng")).doubleValue();
|
|
|
117
|
+ totalDistance += haversine(lat1, lon1, lat2, lon2);
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ double avgSpeed = points.stream()
|
|
|
121
|
+ .filter(p -> p.get("speed") != null)
|
|
|
122
|
+ .mapToDouble(p -> ((Number) p.get("speed")).doubleValue())
|
|
|
123
|
+ .average().orElse(0);
|
|
|
124
|
+
|
|
|
125
|
+ stats.put("totalDistance", Math.round(totalDistance * 100.0) / 100.0);
|
|
|
126
|
+ stats.put("avgSpeed", Math.round(avgSpeed * 100.0) / 100.0);
|
|
|
127
|
+ return stats;
|
|
|
128
|
+ }
|
|
|
129
|
+
|
|
|
130
|
+ // ========== PAT-03 任务台账 ==========
|
|
|
131
|
+
|
|
|
132
|
+ public Map<String, Object> getTaskLedger(String status, Long workerId, int page, int size) {
|
|
|
133
|
+ StringBuilder sql = new StringBuilder("SELECT * FROM patrol_task WHERE 1=1");
|
|
|
134
|
+ List<Object> params = new ArrayList<>();
|
|
|
135
|
+ if (status != null && !status.isEmpty()) {
|
|
|
136
|
+ sql.append(" AND status = ?");
|
|
|
137
|
+ params.add(status);
|
|
|
138
|
+ }
|
|
|
139
|
+ if (workerId != null) {
|
|
|
140
|
+ sql.append(" AND assignee_id = ?");
|
|
|
141
|
+ params.add(workerId);
|
|
|
142
|
+ }
|
|
|
143
|
+ sql.append(" ORDER BY task_date DESC LIMIT ? OFFSET ?");
|
|
|
144
|
+ params.add(size);
|
|
|
145
|
+ params.add((page - 1) * size);
|
|
|
146
|
+
|
|
|
147
|
+ List<Map<String, Object>> records = jdbc.queryForList(sql.toString(), params.toArray());
|
|
|
148
|
+
|
|
|
149
|
+ StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM patrol_task WHERE 1=1");
|
|
|
150
|
+ List<Object> countParams = new ArrayList<>();
|
|
|
151
|
+ if (status != null && !status.isEmpty()) {
|
|
|
152
|
+ countSql.append(" AND status = ?");
|
|
|
153
|
+ countParams.add(status);
|
|
|
154
|
+ }
|
|
|
155
|
+ if (workerId != null) {
|
|
|
156
|
+ countSql.append(" AND assignee_id = ?");
|
|
|
157
|
+ countParams.add(workerId);
|
|
|
158
|
+ }
|
|
|
159
|
+ long total = countQuery(countSql.toString(), countParams.toArray());
|
|
|
160
|
+
|
|
|
161
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
162
|
+ result.put("total", total);
|
|
|
163
|
+ result.put("page", page);
|
|
|
164
|
+ result.put("size", size);
|
|
|
165
|
+ result.put("records", records);
|
|
|
166
|
+ return result;
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+ // ========== PAT-04 设备台账 ==========
|
|
|
170
|
+
|
|
|
171
|
+ public Map<String, Object> getDeviceLedger(String deviceType, String status, String area,
|
|
|
172
|
+ int page, int size) {
|
|
|
173
|
+ StringBuilder sql = new StringBuilder("SELECT * FROM patrol_device WHERE 1=1");
|
|
|
174
|
+ List<Object> params = new ArrayList<>();
|
|
|
175
|
+ if (deviceType != null && !deviceType.isEmpty()) {
|
|
|
176
|
+ sql.append(" AND device_type = ?");
|
|
|
177
|
+ params.add(deviceType);
|
|
|
178
|
+ }
|
|
|
179
|
+ if (status != null && !status.isEmpty()) {
|
|
|
180
|
+ sql.append(" AND status = ?");
|
|
|
181
|
+ params.add(status);
|
|
|
182
|
+ }
|
|
|
183
|
+ if (area != null && !area.isEmpty()) {
|
|
|
184
|
+ sql.append(" AND area = ?");
|
|
|
185
|
+ params.add(area);
|
|
|
186
|
+ }
|
|
|
187
|
+ sql.append(" ORDER BY created_at DESC LIMIT ? OFFSET ?");
|
|
|
188
|
+ params.add(size);
|
|
|
189
|
+ params.add((page - 1) * size);
|
|
|
190
|
+
|
|
|
191
|
+ List<Map<String, Object>> records = jdbc.queryForList(sql.toString(), params.toArray());
|
|
|
192
|
+
|
|
|
193
|
+ StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM patrol_device WHERE 1=1");
|
|
|
194
|
+ List<Object> countParams = new ArrayList<>();
|
|
|
195
|
+ if (deviceType != null && !deviceType.isEmpty()) {
|
|
|
196
|
+ countSql.append(" AND device_type = ?");
|
|
|
197
|
+ countParams.add(deviceType);
|
|
|
198
|
+ }
|
|
|
199
|
+ if (status != null && !status.isEmpty()) {
|
|
|
200
|
+ countSql.append(" AND status = ?");
|
|
|
201
|
+ countParams.add(status);
|
|
|
202
|
+ }
|
|
|
203
|
+ if (area != null && !area.isEmpty()) {
|
|
|
204
|
+ countSql.append(" AND area = ?");
|
|
|
205
|
+ countParams.add(area);
|
|
|
206
|
+ }
|
|
|
207
|
+ long total = countQuery(countSql.toString(), countParams.toArray());
|
|
|
208
|
+
|
|
|
209
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
210
|
+ result.put("total", total);
|
|
|
211
|
+ result.put("page", page);
|
|
|
212
|
+ result.put("size", size);
|
|
|
213
|
+ result.put("records", records);
|
|
|
214
|
+ return result;
|
|
|
215
|
+ }
|
|
|
216
|
+
|
|
|
217
|
+ public Map<String, Object> createDevice(String deviceNo, String deviceName, String deviceType,
|
|
|
218
|
+ String location, double lng, double lat, String area) {
|
|
|
219
|
+ jdbc.update(
|
|
|
220
|
+ "INSERT INTO patrol_device (device_no, device_name, device_type, location, lng, lat, area) " +
|
|
|
221
|
+ "VALUES (?,?,?,?,?,?,?)",
|
|
|
222
|
+ deviceNo, deviceName, deviceType, location, lng, lat, area);
|
|
|
223
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
224
|
+ result.put("deviceNo", deviceNo);
|
|
|
225
|
+ result.put("deviceName", deviceName);
|
|
|
226
|
+ result.put("created", true);
|
|
|
227
|
+ return result;
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+ public Map<String, Object> updateDeviceStatus(Long deviceId, String status) {
|
|
|
231
|
+ jdbc.update("UPDATE patrol_device SET status = ?, updated_at = NOW() WHERE id = ?",
|
|
|
232
|
+ status, deviceId);
|
|
|
233
|
+ return Map.of("deviceId", deviceId, "status", status, "updated", true);
|
|
|
234
|
+ }
|
|
|
235
|
+
|
|
|
236
|
+ // ========== 工具方法 ==========
|
|
|
237
|
+
|
|
|
238
|
+ private long countQuery(String sql, Object... args) {
|
|
|
239
|
+ Long count = jdbc.queryForObject(sql, Long.class, args);
|
|
|
240
|
+ return count != null ? count : 0;
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ private double haversine(double lat1, double lon1, double lat2, double lon2) {
|
|
|
244
|
+ double R = 6371;
|
|
|
245
|
+ double dLat = Math.toRadians(lat2 - lat1);
|
|
|
246
|
+ double dLon = Math.toRadians(lon2 - lon1);
|
|
|
247
|
+ double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
|
|
|
248
|
+ + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
|
|
|
249
|
+ * Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
|
250
|
+ return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
|
251
|
+ }
|
|
|
252
|
+}
|