puzhibing
2026-04-30 1ee6637e101e539b7e327c779087d81de9d8b129
xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/controller/XpMapController.java
@@ -1,38 +1,47 @@
package com.xiaopeng.map.controller;
import com.alibaba.fastjson2.JSON;
import com.xiaopeng.api.dto.WeatherDTO;
import com.xiaopeng.api.dto.map.HistoryPointDto;
import com.xiaopeng.api.dto.map.OrderRouteAndFareDto;
import com.xiaopeng.api.dto.map.RouteAndFareDto;
import com.xiaopeng.api.dto.operation.LonLatDto;
import com.xiaopeng.api.feignClient.order.XpOrderClient;
import com.xiaopeng.api.model.order.XpOrder;
import com.xiaopeng.api.model.map.XpGeoFencingPolygon;
import com.xiaopeng.api.vo.map.PointByIpVo;
import com.xiaopeng.api.vo.map.XpDistanceAndDurationVo;
import com.xiaopeng.api.vo.operation.RouteVo;
import com.xiaopeng.common.core.constant.CacheConstants;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.map.mangodb.model.GeoPoint;
import com.xiaopeng.map.mangodb.service.XpGeoFencingPolygonService;
import com.xiaopeng.map.utils.AmapWalkingRouteUtil;
import com.xiaopeng.map.utils.GDMapGeocodingUtil;
import com.xiaopeng.map.utils.mangoUtils.MongoGeoFenceUtil;
import com.xiaopeng.unifiedverification.interceptor.XpClientCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
@Slf4j
@RestController
@@ -66,7 +75,7 @@
        List<Document> pointsInFence = xpGeoFencingPolygonService.findPointsByParentId(fenceId, "fenceId", "prohibited_area");
        // 避让区域
        ArrayList<String> pointStrParam = new ArrayList<>();
        ArrayList<JSONObject> pointStrParam = new ArrayList<>();
        try {
            for (Document document : pointsInFence) {
                // 获取points字段(它是GeoJSON对象)
@@ -79,25 +88,31 @@
                    if (coordinates != null && !coordinates.isEmpty()) {
                        // 获取第一个多边形环(通常只有一个)
                        List<List<Double>> polygonPoints = coordinates.get(0);
                        List<String> coordinatesList = new ArrayList<>();
                        // 转换为经纬度字符串
                        for (List<Double> point : polygonPoints) {
                            if (point.size() >= 2) {
                                pointStrParam.add(point.get(0) + "," + point.get(1));
                                coordinatesList.add(point.get(0) + "," + point.get(1));
                            }
                        }
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("coordinates", coordinatesList);
                        pointStrParam.add(jsonObject);
                    }
                }
            }
            List<HistoryPointDto> history = new ArrayList<>();
            if(null != dto.getVehicleId()){
                history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//                history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//                if(history.size() > 10){
//                    history = history.subList(history.size() - 10, history.size());
//                }
//                history.forEach(s->{
//                    s.setDirection(new BigDecimal(s.getDirection()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//                });
//                redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
            }
            if(history.size() > 10){
                history = history.subList(history.size() - 10, history.size());
            }
            redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
            RouteVo routeVo = gdMapGeocodingUtil.getDistanceAndTime(dto.getOrigin(), dto.getDestination(), pointStrParam, fenceId,dto.getRoutePlanRule(), history);
            if(routeVo==null){
                return R.fail("没有找到路线");
@@ -121,30 +136,37 @@
    @PostMapping("/base/v1/map/getOrderDistanceAndDuration")
    public R<RouteVo> getOrderDistanceAndDuration(@Validated @RequestBody OrderRouteAndFareDto dto){
        // 获取电子围栏的禁行区域点位
        GeoPoint startPoint = new GeoPoint(dto.getOrigin().getLongitude(),dto.getOrigin().getLatitude());
        GeoPoint startPoint = new GeoPoint(dto.getOrigin().getLongitude(), dto.getOrigin().getLatitude());
        List<String> operatingArea = xpGeoFencingPolygonService.findFencesContainingPoint(startPoint, "operating_area", "points");
        // 避让区域
        ArrayList<String> pointStrParam = new ArrayList<>();
        ArrayList<JSONObject> pointStrParam = new ArrayList<>();
        String fenceId = null;
        if(!operatingArea.isEmpty()){
            fenceId = operatingArea.get(0);
            List<Document> pointsInFence = xpGeoFencingPolygonService.findPointsByParentId(fenceId, "parentId", "prohibited_area");
            List<Document> pointsInFence = xpGeoFencingPolygonService.findPointsByParentId(fenceId, "fenceId", "prohibited_area");
            for (Document document : pointsInFence) {
                List<GeoPoint> points = document.getList("points", GeoPoint.class);
                List<String> coordinatesList = new ArrayList<>();
                // 一个区域的点位
                for (GeoPoint point : points) {
                    pointStrParam.add(point.getLongitude()+","+point.getLatitude());
                    coordinatesList.add(point.getLongitude()+","+point.getLatitude());
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("coordinates", coordinatesList);
                pointStrParam.add(jsonObject);
            }
        }
        List<HistoryPointDto> history = new ArrayList<>();
        if(null != dto.getVehicleId()){
            history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//            history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//            if(history.size() > 10){
//                history = history.subList(history.size() - 10, history.size());
//            }
//            history.forEach(s->{
//                s.setDirection(new BigDecimal(s.getDirection()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//            });
//            redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
        }
        if(history.size() > 10){
            history = history.subList(history.size() - 10, history.size());
        }
        redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
        RouteVo routeVo = gdMapGeocodingUtil.getDistanceAndTime(dto.getOrigin(), dto.getDestination(), pointStrParam, fenceId,dto.getRoutePlanRule(), history);
        if(routeVo==null){
            return R.fail("没有找到路线");
@@ -175,4 +197,34 @@
        return R.ok(pointByIpVo);
    }
    @Value("${map.urls.weather:https://restapi.amap.com/v3/weather/weatherInfo?key=%s&city=%s}")
    String WEATHER_URL;
    @Autowired
    ObjectMapper om;
    @Value("${map.apikey:66ae577f63898dae3016d599fda57221}")
    String key;
    @ApiOperation(value = "获取天气信息", tags = {"内部-调价"})
    @PostMapping("/logan/inner/weatherInfo")
    public R<WeatherDTO> wether(@RequestParam("cityCode") String cityCode) {
        try{
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(String.format(WEATHER_URL, key, cityCode));
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                log.info("天气预报查询结果:{}", resp);
                return R.ok(om.readValue(resp, WeatherDTO.class));
            }
            return R.fail("获取天气信息失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail("获取天气信息失败");
    }
}