wlzboy
2025-10-18 b46065a201c09ce69f111806f2bda4a5f476bc4e
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java
@@ -3,9 +3,13 @@
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.common.config.TencentMapConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@@ -24,6 +28,7 @@
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.http.HttpUtils;
/**
 * 车辆GPS坐标Controller
@@ -51,6 +56,9 @@
    @Autowired
    private ICmsGpsCollectService cmsGpsCollectService;
    @Autowired
    private TencentMapConfig tencentMapConfig;
   /**
     * 查询车辆GPS坐标列表
@@ -139,6 +147,8 @@
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String beginTime= sdf.format(dispatchOrd.getDispatchOrdStartDate());
            String endTime=  sdf.format(new Date());
            logger.info("查询车辆轨迹:车辆号:{}, 开始时间:{}, 结束时间:{}", vehicleNo, beginTime, endTime);
            return this.getAnonymousTracks(vehicleNo,beginTime,endTime);
//
//        vehicleGps.setVehicleNo(tbVehicleOrder.getVehicle());
@@ -231,13 +241,14 @@
            }
            // 处理开始时间
                beginTime = beginTime.replace("T", " ").replace(" ","%20");
                beginTime = beginTime.replace("T", " ");
                if (beginTime.split(":").length == 2) { // 只有小时和分钟
                    beginTime += ":00";
                }
                
                // 处理结束时间
                endTime = endTime.replace("T", " ").replace(" ","%20");
//                endTime = endTime.replace("T", " ").replace(" ","%20");
            endTime = endTime.replace("T", " ");
                if (endTime.split(":").length == 2) { // 只有小时和分钟
                    endTime += ":59";
                }
@@ -312,10 +323,12 @@
                request.setEndtime(endTime);
                request.setTimezone(8); // 中国时区
                logger.info("查询车辆轨迹:车辆号:{}, 设备ID:{}, 开始时间:{}, 结束时间:{}", vehicleNo, vehicleInfo.getDeviceId(), beginTime, endTime);
                // 查询轨迹
                GpsTrackQueryResponse response = gpsCollectService.queryTracks(request);
                if (response.getStatus() != 0) {
                    throw new Error("查询轨迹失败:" + response.getCause());
                    logger.error("查询轨迹失败,状态码:{}, 错误信息:{}", response.getStatus(), response.getCause());
                    throw new Error("查询轨迹失败:" + (response.getCause() != null ? response.getCause() : "未知错误"));
                }
                // 转换GPS51轨迹点为统一格式
@@ -364,4 +377,97 @@
            throw new Error("查询车辆轨迹失败:" + e.getMessage());
        }
    }
}
    /**
     * 腾讯地图地址搜索接口代理
     */
    @Anonymous()
    @GetMapping("/address/search")
    public AjaxResult searchAddress(String keyword, String region) {
        try {
            // 构建腾讯地图搜索API URL
            String url = "https://apis.map.qq.com/ws/place/v1/search";
            String params = "keyword=" + URLEncoder.encode(keyword, StandardCharsets.UTF_8.toString()) +
                           "&boundary=region(" + (region != null ? region : "广州") + ")" +
                           "&key=" + tencentMapConfig.getKey();
            // 发送HTTP请求
            String response = HttpUtils.sendGet(url, params);
            // 返回结果
            return AjaxResult.success("查询成功", response);
        } catch (Exception e) {
            logger.error("地址搜索失败", e);
            return AjaxResult.error("地址搜索失败:" + e.getMessage());
        }
    }
    /**
     * 腾讯地图逆地址解析接口代理
     */
    @Anonymous()
    @GetMapping("/address/geocoder")
    public AjaxResult reverseGeocoder(Double lat, Double lng) {
        try {
            // 检查参数
            logger.info("逆地址解析请求参数: lat={}, lng={}", lat, lng);
            if (lat == null || lng == null) {
                logger.warn("参数不完整,缺少经纬度坐标: lat={}, lng={}", lat, lng);
                return AjaxResult.error("参数不完整,缺少经纬度坐标");
            }
            // 检查参数有效性
            if (Double.isNaN(lat) || Double.isNaN(lng) ||
                Double.isInfinite(lat) || Double.isInfinite(lng)) {
                logger.warn("参数无效,经纬度坐标包含非法值: lat={}, lng={}", lat, lng);
                return AjaxResult.error("参数无效,经纬度坐标格式错误");
            }
            // 构建腾讯地图逆地址解析API URL
            String url = "https://apis.map.qq.com/ws/geocoder/v1/";
            String params = "location=" + lat + "," + lng +
                           "&key=" + tencentMapConfig.getKey() +
                           "&get_poi=1";
            // 发送HTTP请求
            String response = HttpUtils.sendGet(url, params);
            // 返回结果
            return AjaxResult.success("查询成功", response);
        } catch (Exception e) {
            logger.error("逆地址解析失败: lat={}, lng={}", lat, lng, e);
            return AjaxResult.error("逆地址解析失败:" + e.getMessage());
        }
    }
    /**
     * 腾讯地图路线规划接口代理(计算两点间距离)
     */
    @Anonymous()
    @GetMapping("/route/distance")
    public AjaxResult calculateDistance(Double fromLat, Double fromLng, Double toLat, Double toLng) {
        try {
            // 检查参数
            if (fromLat == null || fromLng == null || toLat == null || toLng == null) {
                return AjaxResult.error("参数不完整,缺少起点或终点坐标");
            }
            // 构建腾讯地图路线规划API URL
            String url = "https://apis.map.qq.com/ws/distance/v1/";
            String params = "mode=driving" +
                           "&from=" + fromLat + "," + fromLng +
                           "&to=" + toLat + "," + toLng +
                           "&key=" + tencentMapConfig.getKey();
            // 发送HTTP请求
            String response = HttpUtils.sendGet(url, params);
            // 返回结果
            return AjaxResult.success("计算成功", response);
        } catch (Exception e) {
            logger.error("距离计算失败", e);
            return AjaxResult.error("距离计算失败:" + e.getMessage());
        }
    }
}