From 2ce6aa8eee18b7436cfe144614074494cb11b551 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 17 二月 2025 16:36:20 +0800
Subject: [PATCH] 巡检时计算巡检距离

---
 pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
index 2e97c7f..6279278 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
@@ -12,6 +12,7 @@
 import com.dy.pipIrrGlobal.pojoOp.OpeInspect;
 import com.dy.pipIrrGlobal.pojoOp.OpeTrack;
 import com.dy.pipIrrGlobal.voOp.VoInspect;
+import com.dy.pipIrrGlobal.voOp.VoTrackPoint;
 import jakarta.validation.Valid;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -23,6 +24,7 @@
 import java.util.Date;
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 
 /**
  * @author ZhuBaoMin
@@ -39,6 +41,9 @@
     private InspectSv inspectSv;
 
     private final IDLongGenerator idLongGenerator;
+
+    // 鍦扮悆鍗婂緞锛屽崟浣嶏細鍗冪背
+    private static final double EARTH_RADIUS = 6371.0;
 
     public InspectCtrl(IDLongGenerator idLongGenerator) {
         this.idLongGenerator = idLongGenerator;
@@ -104,6 +109,11 @@
                     if(rec == null || rec == 0) {
                         return BaseResponseUtils.buildErrorMsg("宸℃杞ㄨ抗娣诲姞澶辫触");
                     }
+
+                    Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0);
+                    if(distance > 0) {
+                        inspectSv.updateInspectDistance(inspectId, distance);
+                    }
                 }else {
                     // 璇ュ贰妫�宸蹭笂浼犺繃璁板綍锛岀画浼�
                     if(stopTime != null) {
@@ -122,6 +132,10 @@
                         if(rec == null || rec == 0) {
                             return BaseResponseUtils.buildErrorMsg("宸℃杞ㄨ抗娣诲姞澶辫触");
                         }
+                    }
+                    Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0);
+                    if(distance > 0) {
+                        inspectSv.updateInspectDistance(inspectId, distance);
                     }
                 }
             }
@@ -150,4 +164,49 @@
             return BaseResponseUtils.buildException(e.getMessage());
         }
     }
+
+    /**
+     * 鏍规嵁宸℃ID鑾峰彇杞ㄨ抗鎬昏窛绂�
+     * @param inspectId
+     * @return
+     */
+    public double getInspectDistance(Long inspectId) {
+        List<VoTrackPoint> points = inspectSv.getTrackPointsById(inspectId);
+        if(points == null || points.size() == 0) {
+            return 0.0;
+        }
+
+        double totalDistance = calculateTotalDistance(points);
+        return totalDistance;
+    }
+
+    // 璁$畻杞ㄨ抗鐨勬�昏窛绂�
+    public static double calculateTotalDistance(List<VoTrackPoint> points) {
+        double totalDistance = 0.0;
+
+        for (int i = 0; i < points.size() - 1; i++) {
+            VoTrackPoint currentPoint = points.get(i);
+            VoTrackPoint nextPoint = points.get(i + 1);
+
+            double distance = haversine( currentPoint.getLat().doubleValue(), currentPoint.getLng().doubleValue(), nextPoint.getLat().doubleValue(), nextPoint.getLng().doubleValue());
+
+            totalDistance += distance;
+        }
+
+        return totalDistance;
+    }
+
+    // Haversine鍏紡璁$畻涓や釜缁忕含搴︾偣涔嬮棿鐨勮窛绂�
+    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
+        double dLat = Math.toRadians(lat2 - lat1);
+        double dLon = Math.toRadians(lon2 - lon1);
+
+        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
+                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
+                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
+
+        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
+
+        return EARTH_RADIUS * c;
+    }
 }

--
Gitblit v1.8.0