From 3a154bdb0a5aaa2c0ac3eac95a6ba747068bd454 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 13 一月 2026 10:00:37 +0800
Subject: [PATCH] 优化

---
 server/visits/dmvisit_service/src/main/java/com/doumee/core/tsp/DistanceCalculator.java |  172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 172 insertions(+), 0 deletions(-)

diff --git a/server/visits/dmvisit_service/src/main/java/com/doumee/core/tsp/DistanceCalculator.java b/server/visits/dmvisit_service/src/main/java/com/doumee/core/tsp/DistanceCalculator.java
new file mode 100644
index 0000000..17a6b87
--- /dev/null
+++ b/server/visits/dmvisit_service/src/main/java/com/doumee/core/tsp/DistanceCalculator.java
@@ -0,0 +1,172 @@
+package com.doumee.core.tsp;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.doumee.core.utils.Constants;
+import com.doumee.core.utils.HttpsUtil;
+import com.doumee.dao.business.model.JkCustomer;
+import com.doumee.dao.business.model.JkSketchCustomer;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+
+@Slf4j
+public class DistanceCalculator {
+    private static final double EARTH_RADIUS = 6371.0; // 鍦扮悆鍗婂緞锛堝崟浣嶏細鍏噷锛�
+
+    public static void main(String[] args) {
+        //118.363428,31.326485
+        //118.363312,31.326638
+        JkCustomer start = new JkCustomer();
+        start.setId(-1);
+        start.setLongitude(new BigDecimal(118.39));
+        start.setLatitude(new BigDecimal(31.28));
+
+
+        JkCustomer c = new JkCustomer();
+        c.setId(-2);
+        c.setLongitude(new BigDecimal(118.328593));
+        c.setLatitude(new BigDecimal(30.914289));
+        String url="https://restapi.amap.com/v3/direction/driving?origin=${long1},${lat1}&destination=${long2},${lat2}&extensions=basel&key=248ee35dcdfe3f399ffaf12ac4299eca";
+        DistanceCustomerModel dm = DistanceCalculator.calculateDistanceGaode(url,start,c);
+        System.out.println(dm.getDistance());
+    }
+
+    public static  DistanceCustomerModel calculateDistanceGaode(String urlStr, JkCustomer c1, JkCustomer c2) {
+        DistanceCustomerModel r =new DistanceCustomerModel();
+        r.setStart(c1);
+        r.setEnd(c2);
+        r.setDistance(0);
+        r.setCode(0);
+        r.setLocations( new ArrayList<>());
+        try {
+            String url  =urlStr.replace("${lat1}", Constants.formatBigdecimalScale(c1.getLatitude(),6)+"")
+                    .replace("${lat2}",Constants.formatBigdecimalScale(c2.getLatitude(),6)+"")
+                    .replace("${long1}",Constants.formatBigdecimalScale(c1.getLongitude(),6)+"")
+                    .replace("${long2}",Constants.formatBigdecimalScale(c2.getLongitude(),6)+"");
+            String result = HttpsUtil.get(url ,true);
+            JSONObject json = JSONObject.parseObject(result);
+            if(json!=null
+                    && json.getInteger("status")!=null
+                    && json.getInteger("status") ==1
+                    && json.getJSONObject("route")!=null
+                    && json.getJSONObject("route").getJSONArray("paths") !=null
+                    && json.getJSONObject("route").getJSONArray("paths") .size()>0 ){
+                JSONArray array = json.getJSONObject("route").getJSONArray("paths");
+                JSONObject model = array.getJSONObject(0);//鍙栫涓�涓�
+               Long distance = Long.parseLong(model.getString("distance"));
+               r.setDistance(distance);
+               JSONArray steps = model.getJSONArray("steps");
+               String tl = "";
+               if(steps!=null && steps.size()>0){
+                   for (int i = 0; i < steps.size(); i++) {
+                       if(StringUtils.isBlank(steps.getJSONObject(i).getString("polyline" ))){
+                           continue;
+                       }
+                       if(!tl.equals("") &&!tl.endsWith(";")){
+                           tl += ";";
+                       }
+                       tl+= steps.getJSONObject(i).getString("polyline" );
+                   }
+               }
+               r.setPolyline(tl);
+               r.setLocations(Arrays.asList(tl.split(";")));
+               r.setCode(1);
+               log.error("鑾峰彇浜ら�氳鍒掔嚎璺俊鎭垚鍔�==============");
+            }else{
+                log.error("鑾峰彇浜ら�氳鍒掔嚎璺俊鎭垚鍔�=====澶辫触锛侊紒========="+c1.getName()+"==="+c2.getName()+result);
+            }
+        }catch (Exception e){
+            log.error("鑾峰彇浜ら�氳鍒掔嚎璺俊鎭垚鍔�=====澶辫触=========="+c1.getName()+"==="+c2.getName());
+        }
+
+        return r ;
+    }
+    public static DistanceModel calculateDistanceGaode(String urlStr, JkSketchCustomer c1, JkSketchCustomer c2) {
+        DistanceModel r =new DistanceModel();
+        r.setStart(c1);
+        r.setEnd(c2);
+        r.setDistance(0);
+        r.setCode(0);
+        r.setLocations( new ArrayList<>());
+        try {
+            String url  =urlStr.replace("${lat1}", Constants.formatBigdecimalScale(c1.getLatitude(),6)+"")
+                    .replace("${lat2}",Constants.formatBigdecimalScale(c2.getLatitude(),6)+"")
+                    .replace("${long1}",Constants.formatBigdecimalScale(c1.getLongitude(),6)+"")
+                    .replace("${long2}",Constants.formatBigdecimalScale(c2.getLongitude(),6)+"");
+            String result = HttpsUtil.get(url ,true);
+            JSONObject json = JSONObject.parseObject(result);
+            if(json!=null
+                    && json.getInteger("status")!=null
+                    && json.getInteger("status") ==1
+                    && json.getJSONObject("route")!=null
+                    && json.getJSONObject("route").getJSONArray("paths") !=null
+                    && json.getJSONObject("route").getJSONArray("paths") .size()>0 ){
+                JSONArray array = json.getJSONObject("route").getJSONArray("paths");
+                JSONObject model = array.getJSONObject(0);//鍙栫涓�涓�
+               Long distance = Long.parseLong(model.getString("distance"));
+               r.setDistance(distance);
+               JSONArray steps = model.getJSONArray("steps");
+               String tl = "";
+               if(steps!=null && steps.size()>0){
+                   for (int i = 0; i < steps.size(); i++) {
+                       if(StringUtils.isBlank(steps.getJSONObject(i).getString("polyline" ))){
+                           continue;
+                       }
+                       if(!tl.equals("") &&!tl.endsWith(";")){
+                           tl += ";";
+                       }
+                       tl+= steps.getJSONObject(i).getString("polyline" );
+                   }
+               }
+               r.setPolyline(tl);
+               r.setLocations(Arrays.asList(tl.split(";")));
+               r.setCode(1);
+               log.error("鑾峰彇浜ら�氳鍒掔嚎璺俊鎭垚鍔�==============");
+            }else{
+                log.error("鑾峰彇浜ら�氳鍒掔嚎璺俊鎭垚鍔�=====澶辫触锛侊紒=========");
+            }
+        }catch (Exception e){
+            log.error("鑾峰彇浜ら�氳鍒掔嚎璺俊鎭垚鍔�=====澶辫触==========");
+        }
+
+        return r ;
+    }
+
+    private BigDecimal getDecimalByVal(String val) {
+        try {
+            return new BigDecimal(val);
+        }catch (Exception e){
+
+        }
+        return null;
+    }
+    public static long calculateDistance (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  (long) (EARTH_RADIUS * c * 1000);
+    }
+    public static long calculateDistanceDecinal (BigDecimal lat1, BigDecimal lon1, BigDecimal lat2, BigDecimal lon2) {
+        double dLat = Math.toRadians(Constants.formatBigdecimal(lat2).doubleValue() - Constants.formatBigdecimal(lat1).doubleValue());
+        double dLon = Math.toRadians(Constants.formatBigdecimal(lon2).doubleValue()  - Constants.formatBigdecimal(lon1).doubleValue() );
+
+        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
+                Math.cos(Math.toRadians(Constants.formatBigdecimal(lat1).doubleValue()))
+                        * Math.cos(Math.toRadians(Constants.formatBigdecimal(lat2).doubleValue())) *
+                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
+        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
+
+        return  (long) (EARTH_RADIUS * c * 1000);
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3